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;

23
scripts/EventBus.cs Normal file
View File

@@ -0,0 +1,23 @@
using Godot;
namespace NodeWar.scripts;
public partial class EventBus : Node
{
[Signal]
public delegate void GameStartedEventHandler();
[Signal]
public delegate void GameEndedEventHandler(bool win);
[Signal]
public delegate void SelectionChangedEventHandler(string infoText);
public static EventBus Instance { get; private set; }
public override void _EnterTree()
{
Instance ??= this;
}
}

1
scripts/EventBus.cs.uid Normal file
View File

@@ -0,0 +1 @@
uid://bibq3e4nkwiwv

View File

@@ -1,8 +1,8 @@
namespace NodeWar;
namespace NodeWar.scripts;
public enum Faction
{
Neutral,
Player,
Computer
}
Computer,
}

View File

@@ -1,5 +1,6 @@
using System;
using Godot;
using NodeWar.scripts;
namespace NodeWar.Scripts;
@@ -9,7 +10,7 @@ public partial class Pip : Area2D
public Faction Faction;
[Export]
public Base Target;
public Vector2 Target;
[Export]
public float Speed { get; set; } = 100;
@@ -33,16 +34,13 @@ public partial class Pip : Area2D
public override void _Process(double delta)
{
if (Target == null)
return;
var direction = Target.Position - GlobalPosition;
var direction = Target - GlobalPosition;
Position += direction.Normalized() * Speed * delta.ToFloat();
}
private void OnAreaEntered(Node2D body)
{
if (body is Base { Faction: Faction.Computer } enteredBase)
if (body is Base enteredBase && enteredBase.Faction != Faction)
{
enteredBase.HealthComponent.TakeDamage(1);
QueueFree();

10
scripts/TODO.md Normal file
View File

@@ -0,0 +1,10 @@
# TODO
- [ ] dummy
- [ ] dummy
# DONE
14/05/26
- [x] Base spawns only Pips of its faction
- [x] Ability to set a waypoint for pips (a la Buggos or Flow Fields?)

View File

@@ -1,3 +1,4 @@
using System;
using System.Threading.Tasks;
using Godot;
@@ -5,11 +6,25 @@ namespace NodeWar.scripts.components;
public partial class HealthComponent : ProgressBar
{
private float _health = 100;
[Export]
public Sprite2D Gfx { get; set; }
[Export]
public float Health { get; set; } = 100;
public float Health
{
get => _health;
set
{
if (value >= MaxHealth)
{
_health = MaxHealth;
}
_health = value;
}
}
[Export]
public float MaxHealth { get; set; } = 100;
@@ -55,6 +70,8 @@ public partial class HealthComponent : ProgressBar
public override void _Process(double delta)
{
Visible = Math.Abs(Health - MaxHealth) > 0.001f;
Value = Health;
}
}

View File

@@ -0,0 +1,42 @@
using Godot;
using NodeWar.Scripts;
namespace NodeWar.scripts.components;
public partial class SpawnerComponent : Node
{
[Export]
public PackedScene Spawnee;
[Export]
public Faction Faction;
[Export]
public float BuildSpeed = 28;
[Export]
public float BuildProgress = 0;
[Export]
public float MaxBuildProgress = 100;
public Vector2 Target { get; set; }
public override void _Ready() { }
public override void _Process(double delta)
{
BuildProgress += BuildSpeed * delta.ToFloat();
if (BuildProgress >= MaxBuildProgress)
{
BuildProgress = 0;
var pip = Spawnee.Instantiate<Pip>();
pip.Faction = Faction;
pip.Target = Target;
GetParent().AddChild(pip);
pip.GlobalPosition = GetParent<Node2D>().GlobalPosition;
}
}
}

View File

@@ -0,0 +1 @@
uid://bkjuvw4kmtnq4

View File

@@ -0,0 +1,8 @@
[gd_scene format=3 uid="uid://c2blpf56yvidh"]
[ext_resource type="Script" uid="uid://bkjuvw4kmtnq4" path="res://scripts/components/SpawnerComponent.cs" id="1_6hmx4"]
[ext_resource type="PackedScene" uid="uid://4b8osd3h3xbm" path="res://pip.tscn" id="2_x4vwu"]
[node name="SpawnerComponent" type="Node" unique_id=1304757964]
script = ExtResource("1_6hmx4")
Spawnee = ExtResource("2_x4vwu")