From 3681614196da15d27fb72191530d89a4df19cb29 Mon Sep 17 00:00:00 2001 From: Marco Date: Fri, 24 Jan 2025 15:24:37 +0100 Subject: [PATCH] Disabled navigation --- Scenes/Enemy.tscn | 2 + Scenes/test.tscn | 77 ++++++++++++++++++++++++++++++++++++- Scripts/Enemy.cs | 36 ++++++++++++++++- Scripts/GameManager.cs | 8 +++- Scripts/InventoryManager.cs | 1 - Scripts/PlayerMovement.cs | 4 ++ project.godot | 2 + 7 files changed, 126 insertions(+), 4 deletions(-) diff --git a/Scenes/Enemy.tscn b/Scenes/Enemy.tscn index 1850f881..4cae04fe 100644 --- a/Scenes/Enemy.tscn +++ b/Scenes/Enemy.tscn @@ -56,6 +56,8 @@ BulletScene = ExtResource("2_ogldd") BulletCapacity = 4 BulletSpeed = 50.0 +[node name="NavigationAgent2D" type="NavigationAgent2D" parent="."] + [connection signal="area_entered" from="." to="." method="_on_area_entered"] [connection signal="area_entered" from="PlayerDetection" to="." method="_on_player_detection_area_entered"] [connection signal="area_exited" from="PlayerDetection" to="." method="_on_player_detection_area_exited"] diff --git a/Scenes/test.tscn b/Scenes/test.tscn index beb9cd9b..af9f66ab 100644 --- a/Scenes/test.tscn +++ b/Scenes/test.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=28 format=4 uid="uid://bv451a8wgty4u"] +[gd_scene load_steps=38 format=4 uid="uid://bv451a8wgty4u"] [ext_resource type="Script" path="res://Scripts/GameManager.cs" id="1_8tmoj"] [ext_resource type="PackedScene" uid="uid://bghghp5ep4w2j" path="res://Scenes/player.tscn" id="2_8mh54"] @@ -25,6 +25,66 @@ [ext_resource type="PackedScene" uid="uid://dkwi1hu1bixoe" path="res://Scenes/HUD/HUD.tscn" id="22_krk0o"] [ext_resource type="Script" path="res://Scenes/CameraTarget.gd" id="24_0c2yt"] +[sub_resource type="NavigationPolygon" id="NavigationPolygon_lnwgu"] +vertices = PackedVector2Array(8, 8, -8, 8, -8, -8, 8, -8) +polygons = Array[PackedInt32Array]([PackedInt32Array(0, 1, 2, 3)]) +outlines = Array[PackedVector2Array]([PackedVector2Array(-8, -8, 8, -8, 8, 8, -8, 8)]) +agent_radius = 0.0 + +[sub_resource type="NavigationPolygon" id="NavigationPolygon_ml1qs"] +vertices = PackedVector2Array(8, 8, -8, 8, -8, -8, 8, -8) +polygons = Array[PackedInt32Array]([PackedInt32Array(0, 1, 2, 3)]) +outlines = Array[PackedVector2Array]([PackedVector2Array(-8, -8, 8, -8, 8, 8, -8, 8)]) +agent_radius = 0.0 + +[sub_resource type="NavigationPolygon" id="NavigationPolygon_h4p0b"] +vertices = PackedVector2Array(8, 8, -8, 8, -8, -8, 8, -8) +polygons = Array[PackedInt32Array]([PackedInt32Array(0, 1, 2, 3)]) +outlines = Array[PackedVector2Array]([PackedVector2Array(-8, -8, 8, -8, 8, 8, -8, 8)]) +agent_radius = 0.0 + +[sub_resource type="NavigationPolygon" id="NavigationPolygon_1miy6"] +vertices = PackedVector2Array(8, 8, -8, 8, -8, -8, 8, -8) +polygons = Array[PackedInt32Array]([PackedInt32Array(0, 1, 2, 3)]) +outlines = Array[PackedVector2Array]([PackedVector2Array(-8, -8, 8, -8, 8, 8, -8, 8)]) +agent_radius = 0.0 + +[sub_resource type="NavigationPolygon" id="NavigationPolygon_uanwu"] +vertices = PackedVector2Array(8, 8, -8, 8, -8, -8, 8, -8) +polygons = Array[PackedInt32Array]([PackedInt32Array(0, 1, 2, 3)]) +outlines = Array[PackedVector2Array]([PackedVector2Array(-8, -8, 8, -8, 8, 8, -8, 8)]) +agent_radius = 0.0 + +[sub_resource type="NavigationPolygon" id="NavigationPolygon_6t4b1"] +vertices = PackedVector2Array(8, 8, -8, 8, -8, -8, 8, -8) +polygons = Array[PackedInt32Array]([PackedInt32Array(0, 1, 2, 3)]) +outlines = Array[PackedVector2Array]([PackedVector2Array(-8, -8, 8, -8, 8, 8, -8, 8)]) +agent_radius = 0.0 + +[sub_resource type="NavigationPolygon" id="NavigationPolygon_ajuv4"] +vertices = PackedVector2Array(8, 8, -8, 8, -8, -8, 8, -8) +polygons = Array[PackedInt32Array]([PackedInt32Array(0, 1, 2, 3)]) +outlines = Array[PackedVector2Array]([PackedVector2Array(-8, -8, 8, -8, 8, 8, -8, 8)]) +agent_radius = 0.0 + +[sub_resource type="NavigationPolygon" id="NavigationPolygon_3ass8"] +vertices = PackedVector2Array(8, 8, -8, 8, -8, -8, 8, -8) +polygons = Array[PackedInt32Array]([PackedInt32Array(0, 1, 2, 3)]) +outlines = Array[PackedVector2Array]([PackedVector2Array(-8, -8, 8, -8, 8, 8, -8, 8)]) +agent_radius = 0.0 + +[sub_resource type="NavigationPolygon" id="NavigationPolygon_2t186"] +vertices = PackedVector2Array(8, 8, -8, 8, -8, -8, 8, -8) +polygons = Array[PackedInt32Array]([PackedInt32Array(0, 1, 2, 3)]) +outlines = Array[PackedVector2Array]([PackedVector2Array(-8, -8, 8, -8, 8, 8, -8, 8)]) +agent_radius = 0.0 + +[sub_resource type="NavigationPolygon" id="NavigationPolygon_63d1d"] +vertices = PackedVector2Array(8, 8, -8, 8, -8, -8, 8, -8) +polygons = Array[PackedInt32Array]([PackedInt32Array(0, 1, 2, 3)]) +outlines = Array[PackedVector2Array]([PackedVector2Array(-8, -8, 8, -8, 8, 8, -8, 8)]) +agent_radius = 0.0 + [sub_resource type="TileSetAtlasSource" id="TileSetAtlasSource_jwf4b"] resource_name = "Factory" texture = ExtResource("8_0km4o") @@ -50,6 +110,7 @@ texture = ExtResource("8_0km4o") 5:1/0 = 0 5:1/0/physics_layer_0/polygon_0/points = PackedVector2Array(-8, -8, 8, -8, 8, 8, -8, 8) 6:1/0 = 0 +6:1/0/navigation_layer_0/polygon = SubResource("NavigationPolygon_uanwu") 7:1/0 = 0 8:1/0 = 0 9:1/0 = 0 @@ -60,13 +121,18 @@ texture = ExtResource("8_0km4o") 4:2/0 = 0 5:2/0 = 0 6:2/0 = 0 +6:2/0/navigation_layer_0/polygon = SubResource("NavigationPolygon_6t4b1") 7:2/0 = 0 8:2/0 = 0 9:2/0 = 0 0:3/0 = 0 +0:3/0/navigation_layer_0/polygon = SubResource("NavigationPolygon_lnwgu") 1:3/0 = 0 +1:3/0/navigation_layer_0/polygon = SubResource("NavigationPolygon_ml1qs") 2:3/0 = 0 +2:3/0/navigation_layer_0/polygon = SubResource("NavigationPolygon_h4p0b") 3:3/0 = 0 +3:3/0/navigation_layer_0/polygon = SubResource("NavigationPolygon_1miy6") 4:3/0 = 0 5:3/0 = 0 7:3/0 = 0 @@ -99,8 +165,11 @@ texture = ExtResource("8_0km4o") 5:5/0 = 0 5:5/0/physics_layer_0/polygon_0/points = PackedVector2Array(-8, -8, 8, -8, 8, 8, -8, 8) 6:5/0 = 0 +6:5/0/navigation_layer_0/polygon = SubResource("NavigationPolygon_ajuv4") 7:5/0 = 0 +7:5/0/navigation_layer_0/polygon = SubResource("NavigationPolygon_ajuv4") 8:5/0 = 0 +8:5/0/navigation_layer_0/polygon = SubResource("NavigationPolygon_ajuv4") 9:5/0 = 0 0:6/0 = 0 0:6/0/physics_layer_0/polygon_0/points = PackedVector2Array(-8, -8, 8, -8, 8, 8, -8, 8) @@ -113,8 +182,10 @@ texture = ExtResource("8_0km4o") 5:6/0 = 0 5:6/0/physics_layer_0/polygon_0/points = PackedVector2Array(-8, -8, 8, -8, 8, 8, -8, 8) 6:6/0 = 0 +6:6/0/navigation_layer_0/polygon = SubResource("NavigationPolygon_3ass8") 7:6/0 = 0 8:6/0 = 0 +8:6/0/navigation_layer_0/polygon = SubResource("NavigationPolygon_63d1d") 9:6/0 = 0 0:7/0 = 0 0:7/0/physics_layer_0/polygon_0/points = PackedVector2Array(-8, -8, 8, -8, 8, 8, -8, 8) @@ -127,8 +198,11 @@ texture = ExtResource("8_0km4o") 5:7/0 = 0 5:7/0/physics_layer_0/polygon_0/points = PackedVector2Array(-8, -8, 8, -8, 8, 8, -8, 8) 6:7/0 = 0 +6:7/0/navigation_layer_0/polygon = SubResource("NavigationPolygon_2t186") 7:7/0 = 0 +7:7/0/navigation_layer_0/polygon = SubResource("NavigationPolygon_2t186") 8:7/0 = 0 +8:7/0/navigation_layer_0/polygon = SubResource("NavigationPolygon_63d1d") 9:7/0 = 0 0:8/0 = 0 1:8/0 = 0 @@ -220,6 +294,7 @@ physics_layer_0/collision_layer = 1 physics_layer_0/collision_mask = 30 physics_layer_1/collision_layer = 32 physics_layer_1/collision_mask = 18 +navigation_layer_0/layers = 1 sources/0 = SubResource("TileSetAtlasSource_jwf4b") sources/1 = SubResource("TileSetScenesCollectionSource_qg3vu") diff --git a/Scripts/Enemy.cs b/Scripts/Enemy.cs index aa940ed7..07433715 100644 --- a/Scripts/Enemy.cs +++ b/Scripts/Enemy.cs @@ -10,19 +10,33 @@ public partial class Enemy : Area2D, IDestructible [Export] public float Health = 4f; + [Export] public float WalkSpeed = 2500f; + [Export] public Weapon EquippedWeapon; private float _currentHealth = 0f; private bool _isDestroyed = false; + private NavigationAgent2D _navigationAgent; + + [Export] public bool NavigationEnabled { get; set; } = false; // Called when the node enters the scene tree for the first time. public override void _Ready() { _currentHealth = Health; + + _navigationAgent = GetNode("NavigationAgent2D"); + + CallDeferred("Setup"); } + private void Setup() + { + + } + // Called every frame. 'delta' is the elapsed time since the previous frame. public override void _Process(double delta) { @@ -48,6 +62,18 @@ public partial class Enemy : Area2D, IDestructible public override void _PhysicsProcess(double delta) { HandlePlayerDetection(); + if (NavigationEnabled) + { + var moveLocation = _navigationAgent.GetNextPathPosition(); + + if (_currentState is EnemyState.Primed) + { + this.Position = moveLocation; + } + } + + + } private void HandlePlayerDetection() @@ -59,7 +85,14 @@ public partial class Enemy : Area2D, IDestructible if (IsPlayerInSight()) { + // Update player position only if player is in sight + if (NavigationEnabled) + { + _navigationAgent.SetTargetPosition(_cachedPlayer.GlobalPosition); + } Shoot(); + + } } @@ -161,6 +194,7 @@ public partial class Enemy : Area2D, IDestructible private enum EnemyState { Idle, - Primed + Primed, + Patrolling } } diff --git a/Scripts/GameManager.cs b/Scripts/GameManager.cs index b25d5cb3..77be729e 100644 --- a/Scripts/GameManager.cs +++ b/Scripts/GameManager.cs @@ -16,13 +16,19 @@ public partial class GameManager : Node2D [Export] public Marker2D PlayerSpawnMarker { get; set; } - + private InventoryManager _inventoryManager { get; set; } + + public InventoryManager Inventory => _inventoryManager; + + // Called when the node enters the scene tree for the first time. public override void _Ready() { _hud = GetNode("HUD"); _cameraTarget = GetNode("CameraTarget"); + _inventoryManager = GetNode("InventoryManager"); + if (PlayerSpawnMarker != null) { SpawnPlayer(); diff --git a/Scripts/InventoryManager.cs b/Scripts/InventoryManager.cs index a287799b..ee0a7023 100644 --- a/Scripts/InventoryManager.cs +++ b/Scripts/InventoryManager.cs @@ -3,7 +3,6 @@ using System; public partial class InventoryManager : Node2D { - public bool RedKeycard { get; set; } // Called when the node enters the scene tree for the first time. diff --git a/Scripts/PlayerMovement.cs b/Scripts/PlayerMovement.cs index 8a7f82da..bcce0634 100644 --- a/Scripts/PlayerMovement.cs +++ b/Scripts/PlayerMovement.cs @@ -40,6 +40,8 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible private bool _isDestroyed = false; + private GameManager _gameManager; + //private InventoryManager _inventoryManager; public override void _Ready() @@ -52,6 +54,8 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible _movementDirection = Vector2.Zero; _facingDirection = Vector2.Zero; + + _gameManager = GetNode("/root/GameScene"); if (SelectorScene != null) { diff --git a/project.godot b/project.godot index ae4ed2ee..18a61e44 100644 --- a/project.godot +++ b/project.godot @@ -92,7 +92,9 @@ Use={ [layer_names] 2d_physics/layer_1="world" +2d_navigation/layer_1="Walkable" 2d_physics/layer_2="player" +2d_navigation/layer_2="Solid" 2d_physics/layer_3="items" 2d_physics/layer_4="bullets" 2d_physics/layer_5="enemies"