26 lines
784 B
C#
26 lines
784 B
C#
using Godot;
|
|
using System.Linq;
|
|
|
|
public partial class MeleeEnemy : BasicEnemy
|
|
{
|
|
[Export]
|
|
private float _speed = 0.4f;
|
|
private bool _targetingPlayer = false;
|
|
[Export]
|
|
private double _distanceToPlayer = 3;
|
|
|
|
public override void _PhysicsProcess(double delta)
|
|
{
|
|
var players = GetTree().GetNodesInGroup("Player");
|
|
if (players.Any())
|
|
{
|
|
var convertedPlayers = players.Select(x => (Node3D)x);
|
|
var target = convertedPlayers.OrderBy(x => Position.DistanceTo(x.Position)).FirstOrDefault();
|
|
if (_targetingPlayer || Position.DistanceTo(target.Position) < _distanceToPlayer)
|
|
{
|
|
Position = Position.MoveToward(target.Position, _speed * (float)delta);
|
|
_targetingPlayer = true;
|
|
}
|
|
}
|
|
}
|
|
} |