Basic enemy attack pattern

This commit is contained in:
2024-09-04 23:05:49 -07:00
parent 6fc9568137
commit 54d9dcf9fa
14 changed files with 282 additions and 54 deletions

View File

@@ -19,6 +19,10 @@ public interface IEnemy : ICharacterBody3D
public NavigationAgent3D NavAgent { get; set; }
public Area3D LineOfSight { get; set; }
public AnimationPlayer AnimationPlayer { get; set; }
public Timer AttackTimer { get; set; }
}
[Meta(typeof(IAutoNode))]
@@ -49,6 +53,10 @@ public partial class Enemy : CharacterBody3D, IEnemy, IProvide<IEnemyLogic>
[Node] public Timer PatrolTimer { get; set; } = default!;
[Node] public Timer AttackTimer { get; set; } = default!;
[Node] public AnimationPlayer AnimationPlayer { get; set; } = default!;
public void Setup()
{
EnemyLogic = new EnemyLogic();
@@ -63,6 +71,7 @@ public partial class Enemy : CharacterBody3D, IEnemy, IProvide<IEnemyLogic>
CurrentHP.Sync += OnHPChanged;
LineOfSight.BodyEntered += LineOfSight_BodyEntered;
PatrolTimer.Timeout += OnPatrolTimeout;
AttackTimer.Timeout += OnAttackTimeout;
var rng = new RandomNumberGenerator();
rng.Randomize();
PatrolTimer.WaitTime = rng.RandfRange(7.0f, 15.0f);
@@ -77,6 +86,17 @@ public partial class Enemy : CharacterBody3D, IEnemy, IProvide<IEnemyLogic>
PatrolTimer.WaitTime = rng.RandfRange(7.0f, 15.0f);
}
private void OnAttackTimeout()
{
if (GlobalPosition.DistanceTo(GameRepo.PlayerGlobalPosition.Value) > 2.5f)
return;
var rng = new RandomNumberGenerator();
rng.Randomize();
EnemyLogic.Input(new EnemyLogic.Input.AttackTimer());
AttackTimer.WaitTime = rng.RandfRange(2f, 3.0f);
}
private void LineOfSight_BodyEntered(Node3D body)
{
EnemyLogic.Input(new EnemyLogic.Input.Alerted());