Line of sight for enemy

This commit is contained in:
2024-09-04 17:49:40 -07:00
parent 3dffa12eb8
commit d24b28acd5
5 changed files with 32 additions and 3 deletions

View File

@@ -17,6 +17,8 @@ public interface IEnemy : ICharacterBody3D
public EnemyStatInfo EnemyStatInfo { get; set; } public EnemyStatInfo EnemyStatInfo { get; set; }
public NavigationAgent3D NavAgent { get; set; } public NavigationAgent3D NavAgent { get; set; }
public Area3D LineOfSight { get; set; }
} }
[Meta(typeof(IAutoNode))] [Meta(typeof(IAutoNode))]
@@ -43,6 +45,8 @@ public partial class Enemy : CharacterBody3D, IEnemy, IProvide<IEnemyLogic>
[Node] public NavigationAgent3D NavAgent { get; set; } = default!; [Node] public NavigationAgent3D NavAgent { get; set; } = default!;
[Node] public Area3D LineOfSight { get; set; } = default!;
public void Setup() public void Setup()
{ {
EnemyLogic = new EnemyLogic(); EnemyLogic = new EnemyLogic();
@@ -55,6 +59,12 @@ public partial class Enemy : CharacterBody3D, IEnemy, IProvide<IEnemyLogic>
{ {
CurrentHP = new AutoProp<double>(EnemyStatInfo.MaximumHP); CurrentHP = new AutoProp<double>(EnemyStatInfo.MaximumHP);
CurrentHP.Sync += OnHPChanged; CurrentHP.Sync += OnHPChanged;
LineOfSight.BodyEntered += LineOfSight_BodyEntered;
}
private void LineOfSight_BodyEntered(Node3D body)
{
EnemyLogic.Input(new EnemyLogic.Input.Alerted());
} }
public void OnResolved() public void OnResolved()

View File

@@ -1,4 +1,4 @@
[gd_scene load_steps=9 format=4 uid="uid://dcgj5i52i76gj"] [gd_scene load_steps=10 format=4 uid="uid://dcgj5i52i76gj"]
[ext_resource type="Script" path="res://src/enemy/Enemy.cs" id="1_jw471"] [ext_resource type="Script" path="res://src/enemy/Enemy.cs" id="1_jw471"]
[ext_resource type="Resource" uid="uid://c8h26ip4ly18r" path="res://src/enemy/enemy_types/floating_enemy/FloatingEnemy.tres" id="2_ewaf6"] [ext_resource type="Resource" uid="uid://c8h26ip4ly18r" path="res://src/enemy/enemy_types/floating_enemy/FloatingEnemy.tres" id="2_ewaf6"]
@@ -104,6 +104,10 @@ shadow_mesh = SubResource("ArrayMesh_2wch8")
radius = 0.2 radius = 0.2
height = 1.0 height = 1.0
[sub_resource type="CylinderShape3D" id="CylinderShape3D_jbgmx"]
height = 3.0
radius = 1.0
[node name="EnemyGuy" type="CharacterBody3D"] [node name="EnemyGuy" type="CharacterBody3D"]
collision_layer = 10 collision_layer = 10
collision_mask = 9 collision_mask = 9
@@ -126,3 +130,12 @@ debug_path_custom_color = Color(1, 0, 0, 1)
[node name="CollisionShape3D" type="CollisionShape3D" parent="."] [node name="CollisionShape3D" type="CollisionShape3D" parent="."]
shape = SubResource("CapsuleShape3D_cwfph") shape = SubResource("CapsuleShape3D_cwfph")
[node name="LineOfSight" type="Area3D" parent="."]
unique_name_in_owner = true
collision_layer = 2
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")

View File

@@ -21,6 +21,12 @@ namespace GameJamDungeon
enemy.LookAt(new Vector3(gameRepo.PlayerGlobalPosition.Value.X, enemy.GlobalPosition.Y, gameRepo.PlayerGlobalPosition.Value.Z), Vector3.Up); enemy.LookAt(new Vector3(gameRepo.PlayerGlobalPosition.Value.X, enemy.GlobalPosition.Y, gameRepo.PlayerGlobalPosition.Value.Z), Vector3.Up);
return ToSelf(); return ToSelf();
} }
if (enemy.GlobalPosition.DistanceTo(gameRepo.PlayerGlobalPosition.Value) > 20f)
{
return To<Idle>();
}
enemy.NavAgent.TargetPosition = gameRepo.PlayerGlobalPosition.Value; enemy.NavAgent.TargetPosition = gameRepo.PlayerGlobalPosition.Value;
var nextPosition = enemy.NavAgent.GetNextPathPosition(); var nextPosition = enemy.NavAgent.GetNextPathPosition();
var lookAtPos = enemy.NavAgent.GetNextPathPosition(); var lookAtPos = enemy.NavAgent.GetNextPathPosition();

View File

@@ -13,7 +13,7 @@ namespace GameJamDungeon
public InventoryOpened() public InventoryOpened()
{ {
this.OnEnter(() => { Get<IGameRepo>().Pause(); Output(new Output.SetInventoryMode(Get<IGameRepo>().InventoryItems.Value)); }); this.OnEnter(() => { Get<IGameRepo>().Pause(); Output(new Output.SetInventoryMode(Get<IGameRepo>().InventoryItems.Value)); });
this.OnExit(() => { Output(new Output.HideInventory()); }); this.OnExit(() => { Get<IGameRepo>().Resume(); Output(new Output.HideInventory()); });
} }
public Transition On(in Input.InventoryMenuButtonPressed input) => To<Playing>(); public Transition On(in Input.InventoryMenuButtonPressed input) => To<Playing>();
} }

View File

@@ -13,7 +13,7 @@ namespace GameJamDungeon
public MinimapOpen() public MinimapOpen()
{ {
this.OnEnter(() => { Get<IGameRepo>().Pause(); Output(new Output.ShowMiniMap()); }); this.OnEnter(() => { Get<IGameRepo>().Pause(); Output(new Output.ShowMiniMap()); });
this.OnExit(() => { Output(new Output.HideMiniMap()); }); this.OnExit(() => { Get<IGameRepo>().Resume(); Output(new Output.HideMiniMap()); });
} }
public Transition On(in Input.MiniMapButtonReleased input) => To<Playing>(); public Transition On(in Input.MiniMapButtonReleased input) => To<Playing>();