This commit is contained in:
2026-05-14 13:17:23 +02:00
parent 37f4ea3b4e
commit 70bba45b8a
18 changed files with 293 additions and 71 deletions

View File

@@ -1,27 +1,33 @@
using System;
using Godot;
using NodeWar;
using NodeWar.Scripts;
using NodeWar.scripts.components;
namespace NodeWar.scripts;
public partial class Base : Area2D
{
private ProgressBar _progressBar;
public HealthComponent HealthComponent;
private Node2D _selection;
private Node2D _gfx;
public Vector2 TargetPosition { get; set; }
[Export]
public Node2D Target;
[Export]
public HealthComponent HealthComponent;
[Export]
public SpawnerComponent SpawnerComponent;
[Export]
public bool Selected = false;
[Export]
public Faction Faction = Faction.Neutral;
private float _buildSpeed = 28;
private float _buildProgress = 0;
private float _maxBuildProgress = 100;
private bool _mouseInside = false;
private bool _selected = false;
private PackedScene _pipScene = GD.Load<PackedScene>("res://pip.tscn");
@@ -30,44 +36,46 @@ public partial class Base : Area2D
MouseEntered += OnMouseEntered;
MouseExited += OnMouseExited;
HealthComponent = GetNode<HealthComponent>("HealthComponent");
_progressBar = GetNode<ProgressBar>("ProgressBar");
_selection = GetNode<Node2D>("Selection");
_gfx = GetNode<Node2D>("Gfx");
_selection.Visible = false;
_progressBar.MaxValue = _maxBuildProgress;
HealthComponent.MaxHealth = 100;
HealthComponent.Health = 100;
SpawnerComponent.Faction = Faction;
UpdateColor();
}
public override void _Process(double delta)
{
if (Faction == Faction.Neutral)
return;
_buildProgress += _buildSpeed * delta.ToFloat();
if (_buildProgress >= _maxBuildProgress)
{
_buildProgress = 0;
var pip = _pipScene.Instantiate<Pip>();
GetParent().AddChild(pip);
pip.Faction = Faction;
pip.GlobalPosition = GlobalPosition;
pip.Target = GetTree().GetFirstNodeInGroup("enemyBase") as Base;
}
UpdateProgressBars();
}
public override void _UnhandledInput(InputEvent @event)
{
if (@event is InputEventMouseButton { Pressed: true, ButtonIndex: MouseButton.Left })
{
_selection.Visible = _mouseInside;
_selected = _mouseInside;
if (_selected)
{
_selection.Visible = true;
// TODO: super ugly
EventBus.Instance.EmitSignal(
EventBus.SignalName.SelectionChanged,
"This is a Base"
);
}
else
{
_selection.Visible = false;
}
}
if (
@event is InputEventMouseButton { Pressed: true, ButtonIndex: MouseButton.Right }
&& _selected
)
{
// TODO: 3 Stellen eine Sache zu speichern?
TargetPosition = GetGlobalMousePosition();
Target.GlobalPosition = TargetPosition;
SpawnerComponent.Target = TargetPosition;
}
}
@@ -82,11 +90,6 @@ public partial class Base : Area2D
};
}
private void UpdateProgressBars()
{
_progressBar.Value = _buildProgress;
}
private void OnMouseExited()
{
_mouseInside = false;