Random patrol

This commit is contained in:
2024-09-04 18:31:35 -07:00
parent d24b28acd5
commit 6fc9568137
4 changed files with 43 additions and 2 deletions

View File

@@ -47,6 +47,8 @@ public partial class Enemy : CharacterBody3D, IEnemy, IProvide<IEnemyLogic>
[Node] public Area3D LineOfSight { get; set; } = default!;
[Node] public Timer PatrolTimer { get; set; } = default!;
public void Setup()
{
EnemyLogic = new EnemyLogic();
@@ -60,6 +62,19 @@ public partial class Enemy : CharacterBody3D, IEnemy, IProvide<IEnemyLogic>
CurrentHP = new AutoProp<double>(EnemyStatInfo.MaximumHP);
CurrentHP.Sync += OnHPChanged;
LineOfSight.BodyEntered += LineOfSight_BodyEntered;
PatrolTimer.Timeout += OnPatrolTimeout;
var rng = new RandomNumberGenerator();
rng.Randomize();
PatrolTimer.WaitTime = rng.RandfRange(7.0f, 15.0f);
}
private void OnPatrolTimeout()
{
var rng = new RandomNumberGenerator();
rng.Randomize();
var randomizedSpot = new Vector3(rng.RandfRange(-3.0f, 3.0f), 0, rng.RandfRange(-3.0f, 3.0f));
EnemyLogic.Input(new EnemyLogic.Input.PatrolToRandomSpot(GlobalPosition + randomizedSpot));
PatrolTimer.WaitTime = rng.RandfRange(7.0f, 15.0f);
}
private void LineOfSight_BodyEntered(Node3D body)

View File

@@ -139,3 +139,8 @@ collision_mask = 2
[node name="CollisionShape3D" type="CollisionShape3D" parent="LineOfSight"]
transform = Transform3D(1, 0, 0, 0, 0.0745088, 0.99722, 0, -0.99722, 0.0745088, 0, 0, -1.46944)
shape = SubResource("CylinderShape3D_jbgmx")
[node name="PatrolTimer" type="Timer" parent="."]
unique_name_in_owner = true
wait_time = 10.0
autostart = true

View File

@@ -1,4 +1,7 @@
namespace GameJamDungeon

using Godot;
namespace GameJamDungeon
{
public partial class EnemyLogic
{
@@ -13,6 +16,8 @@
public readonly record struct HitByPlayer(double Damage);
public readonly record struct Killed();
public readonly record struct PatrolToRandomSpot(Vector3 PatrolTarget);
}
}
}

View File

@@ -8,7 +8,7 @@ public partial class EnemyLogic
public partial record State
{
[Meta, Id("enemy_logic_state_idle")]
public partial record Idle : Alive, IGet<Input.Alerted>, IGet<Input.PhysicsTick>
public partial record Idle : Alive, IGet<Input.Alerted>, IGet<Input.PhysicsTick>, IGet<Input.PatrolToRandomSpot>
{
public Transition On(in Input.Alerted _)
{
@@ -20,6 +20,22 @@ public partial class EnemyLogic
var delta = input.Delta;
var gameRepo = Get<IGameRepo>();
var enemy = Get<IEnemy>();
var nextPosition = enemy.NavAgent.GetNextPathPosition();
var lookAtPos = enemy.NavAgent.GetNextPathPosition();
var direction = enemy.NavAgent.GetNextPathPosition() - enemy.GlobalPosition;
enemy.Velocity = enemy.Velocity.MoveToward(direction.Normalized() * 0.01f, (float)delta);
Output(new Output.MovementComputed(enemy.Velocity));
return ToSelf();
}
public Transition On(in Input.PatrolToRandomSpot input)
{
var enemy = Get<IEnemy>();
enemy.NavAgent.TargetPosition = input.PatrolTarget;
enemy.LookAt(new Vector3(input.PatrolTarget.X, enemy.GlobalPosition.Y, input.PatrolTarget.Z), Vector3.Up);
return ToSelf();
}
}