Files
nodewars/scripts/Pip.cs
2026-05-14 13:17:23 +02:00

50 lines
918 B
C#

using System;
using Godot;
using NodeWar.scripts;
namespace NodeWar.Scripts;
public partial class Pip : Area2D
{
[Export]
public Faction Faction;
[Export]
public Vector2 Target;
[Export]
public float Speed { get; set; } = 100;
private Sprite2D _gfx;
public override void _Ready()
{
_gfx = GetNode<Sprite2D>("Gfx");
_gfx.Modulate = Faction switch
{
Faction.Neutral => Colors.Gray,
Faction.Player => Colors.DarkGreen,
Faction.Computer => Colors.DarkRed,
_ => throw new ArgumentOutOfRangeException(),
};
AreaEntered += OnAreaEntered;
}
public override void _Process(double delta)
{
var direction = Target - GlobalPosition;
Position += direction.Normalized() * Speed * delta.ToFloat();
}
private void OnAreaEntered(Node2D body)
{
if (body is Base enteredBase && enteredBase.Faction != Faction)
{
enteredBase.HealthComponent.TakeDamage(1);
QueueFree();
}
}
}