From 5f7193e216b6d2fa91e6d5fc67f580acf674a883 Mon Sep 17 00:00:00 2001 From: Rendo Date: Sun, 27 Jul 2025 20:48:59 +0500 Subject: [PATCH] basic enemy movement --- base/scenes/enemies/test_enemy.tscn | 41 ++++++++++- base/scenes/game.tscn | 1 + base/scenes/rooms/navigation_test_scene.tscn | 71 ++++++++++++++++++++ base/scripts/enemies/floor_mover.gd | 20 ++++++ base/scripts/enemies/floor_mover.gd.uid | 1 + project.godot | 1 + 6 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 base/scenes/rooms/navigation_test_scene.tscn create mode 100644 base/scripts/enemies/floor_mover.gd create mode 100644 base/scripts/enemies/floor_mover.gd.uid diff --git a/base/scenes/enemies/test_enemy.tscn b/base/scenes/enemies/test_enemy.tscn index fd018d5..e000e91 100644 --- a/base/scenes/enemies/test_enemy.tscn +++ b/base/scenes/enemies/test_enemy.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=10 format=3 uid="uid://twqkevpn0q1a"] +[gd_scene load_steps=13 format=3 uid="uid://twqkevpn0q1a"] [ext_resource type="Texture2D" uid="uid://b0gyyb6xwfjex" path="res://base/assets/sprites/test_enemy/0000.png" id="1_7qny8"] [ext_resource type="Script" uid="uid://j0w37fjl13ox" path="res://base/scripts/enemies/directional_sprite3d.gd" id="2_hmq60"] @@ -9,13 +9,29 @@ [ext_resource type="Texture2D" uid="uid://lbo6ygri22or" path="res://base/assets/sprites/test_enemy/0005.png" id="7_yln7d"] [ext_resource type="Texture2D" uid="uid://bu54h8g5r50qp" path="res://base/assets/sprites/test_enemy/0006.png" id="8_snoby"] [ext_resource type="Texture2D" uid="uid://c57drlhnuicu5" path="res://base/assets/sprites/test_enemy/0007.png" id="9_83myg"] +[ext_resource type="Script" uid="uid://biqn05eyrhdya" path="res://base/scripts/enemies/floor_mover.gd" id="10_rvsun"] + +[sub_resource type="CylinderShape3D" id="CylinderShape3D_hmq60"] +height = 1.52902 +radius = 0.543945 + +[sub_resource type="GDScript" id="GDScript_rvsun"] +resource_name = "temp" +script/source = "extends Node + + +# Called every frame. 'delta' is the elapsed time since the previous frame. +func _process(delta: float) -> void: + $\"../../NavigationAgent3D\".target_position = get_tree().current_scene.get_node(\"%Player\").global_position +" [node name="TestEnemy" type="Node3D" groups=["enemy"]] [node name="RotationalSprite" type="Sprite3D" parent="."] +transform = Transform3D(3, 0, 0, 0, 3, 0, 0, 0, 3, 0, 0.97973, 0) billboard = 2 texture_filter = 0 -texture = ExtResource("1_7qny8") +texture = ExtResource("3_rvsun") script = ExtResource("2_hmq60") rotations = Dictionary[float, Texture2D]({ 0.0: ExtResource("6_d1vpv"), @@ -28,3 +44,24 @@ rotations = Dictionary[float, Texture2D]({ 5.49779: ExtResource("5_xto1v"), 6.28319: ExtResource("6_d1vpv") }) + +[node name="Hitbox" type="Area3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.181836, 0) +collision_layer = 4 +collision_mask = 0 + +[node name="CollisionShape3D" type="CollisionShape3D" parent="Hitbox"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.584702, 0) +shape = SubResource("CylinderShape3D_hmq60") + +[node name="NavigationAgent3D" type="NavigationAgent3D" parent="."] + +[node name="Mover" type="Node" parent="." node_paths=PackedStringArray("navigation_agent", "move_target")] +script = ExtResource("10_rvsun") +navigation_agent = NodePath("../NavigationAgent3D") +move_target = NodePath("..") +max_speed = 10.0 +rotation_speed = 0.0872665 + +[node name="Node" type="Node" parent="Mover"] +script = SubResource("GDScript_rvsun") diff --git a/base/scenes/game.tscn b/base/scenes/game.tscn index a49fcbc..ebae183 100644 --- a/base/scenes/game.tscn +++ b/base/scenes/game.tscn @@ -9,6 +9,7 @@ [node name="Test Room" parent="." instance=ExtResource("1_sjeh3")] [node name="Player" parent="." instance=ExtResource("2_uxs03")] +unique_name_in_owner = true transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.208048, 1.61008) [node name="TestEnemy" parent="." instance=ExtResource("3_tk4w0")] diff --git a/base/scenes/rooms/navigation_test_scene.tscn b/base/scenes/rooms/navigation_test_scene.tscn new file mode 100644 index 0000000..fe8fe4f --- /dev/null +++ b/base/scenes/rooms/navigation_test_scene.tscn @@ -0,0 +1,71 @@ +[gd_scene load_steps=5 format=3 uid="uid://bjeawqo2kppqk"] + +[ext_resource type="PackedScene" uid="uid://twqkevpn0q1a" path="res://base/scenes/enemies/test_enemy.tscn" id="1_cmbee"] +[ext_resource type="PackedScene" uid="uid://dwx5tcatj35gu" path="res://base/scenes/player.tscn" id="2_2xi8s"] + +[sub_resource type="NavigationMesh" id="NavigationMesh_cmbee"] +vertices = PackedVector3Array(-12.7732, 1, 10.2876, -2.77324, 1, 10.2876, -2.77324, 1, 8.2876, -0.523237, 1, 8.0376, -0.523237, 1, -4.4624, -12.7732, 1, -4.4624, 11.2268, 1, 8.0376, 11.4768, 1, 10.0376, 12.7268, 1, 9.7876, 12.7268, 1, -4.4624, -1.77324, 4.75, 9.0376, -1.77324, 4.75, 17.0376, -1.27324, 4.75, 17.0376, -1.27324, 4.75, 9.0376, 9.47676, 1, 17.0376, 9.22676, 1, 17.0376, 8.97676, 1, 18.0376, 9.72676, 1, 18.7876, 12.7268, 1, 29.0376, 11.4768, 1, 18.5376, -0.523237, 1, 18.0376, -0.523237, 1, 29.0376, 8.97676, 1, 16.0376, 9.47676, 1, 10.0376, -0.523237, 1, 16.0376, 2.22676, 1, 16.0376, 2.22676, 1, 12.2876, -0.523237, 1, 10.0376, 4.22676, 1, 12.0376, 4.47676, 1, 16.0376, -12.7732, 1, 15.7876, -2.77324, 1, 15.7876, 2.97676, 4.75, 16.7876, 3.72676, 4.75, 17.0376, 3.47676, 4.75, 13.0376, -2.77324, 1, 17.7876, -12.7732, 1, 29.0376) +polygons = [PackedInt32Array(2, 1, 0), PackedInt32Array(4, 3, 2), PackedInt32Array(0, 5, 2), PackedInt32Array(2, 5, 4), PackedInt32Array(7, 6, 8), PackedInt32Array(8, 6, 9), PackedInt32Array(3, 4, 6), PackedInt32Array(6, 4, 9), PackedInt32Array(13, 12, 10), PackedInt32Array(10, 12, 11), PackedInt32Array(15, 14, 16), PackedInt32Array(16, 14, 17), PackedInt32Array(17, 19, 18), PackedInt32Array(7, 8, 19), PackedInt32Array(19, 8, 18), PackedInt32Array(16, 17, 20), PackedInt32Array(20, 17, 21), PackedInt32Array(21, 17, 18), PackedInt32Array(15, 22, 14), PackedInt32Array(14, 22, 23), PackedInt32Array(25, 24, 26), PackedInt32Array(26, 24, 27), PackedInt32Array(29, 28, 22), PackedInt32Array(22, 28, 23), PackedInt32Array(26, 27, 28), PackedInt32Array(28, 27, 23), PackedInt32Array(0, 1, 30), PackedInt32Array(30, 1, 31), PackedInt32Array(32, 34, 33), PackedInt32Array(30, 31, 35), PackedInt32Array(35, 20, 21), PackedInt32Array(30, 35, 36), PackedInt32Array(36, 35, 21)] + +[sub_resource type="ORMMaterial3D" id="ORMMaterial3D_yvrvx"] +albedo_color = Color(1, 0, 0, 1) + +[node name="NavigationTestScene" type="Node3D"] + +[node name="NavigationRegion3D" type="NavigationRegion3D" parent="."] +navigation_mesh = SubResource("NavigationMesh_cmbee") + +[node name="CSGCombiner3D" type="CSGCombiner3D" parent="NavigationRegion3D"] +use_collision = true + +[node name="CSGBox3D" type="CSGBox3D" parent="NavigationRegion3D/CSGCombiner3D"] +size = Vector3(26.4385, 1, 9.9248) + +[node name="CSGBox3D2" type="CSGBox3D" parent="NavigationRegion3D/CSGCombiner3D"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 24.6131) +size = Vector3(26.4385, 1, 9.9248) + +[node name="CSGBox3D3" type="CSGBox3D" parent="NavigationRegion3D/CSGCombiner3D"] +transform = Transform3D(-4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, -8.31083, 9.53674e-07, 11.2665) +size = Vector3(26.4385, 1, 9.9248) + +[node name="CSGBox3D4" type="CSGBox3D" parent="NavigationRegion3D/CSGCombiner3D"] +transform = Transform3D(-4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, 4.78142, 9.53674e-07, 12.2382) +size = Vector3(15.1416, 1, 16.73) +material = SubResource("ORMMaterial3D_yvrvx") + +[node name="CSGBox3D5" type="CSGBox3D" parent="NavigationRegion3D/CSGCombiner3D"] +transform = Transform3D(-4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, 4.35131, 1.95319, 9.02124) +size = Vector3(0.87915, 4.90637, 12.7579) +material = SubResource("ORMMaterial3D_yvrvx") + +[node name="CSGBox3D6" type="CSGBox3D" parent="NavigationRegion3D/CSGCombiner3D"] +transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, -1.61005, 1.95319, 13.0554) +size = Vector3(0.87915, 4.90637, 8.94635) +material = SubResource("ORMMaterial3D_yvrvx") + +[node name="CSGBox3D8" type="CSGBox3D" parent="NavigationRegion3D/CSGCombiner3D"] +transform = Transform3D(1.31134e-07, 0, -1, 0, 1, 0, 1, 0, 1.31134e-07, 3.21596, 1.95319, 17.0787) +size = Vector3(0.87915, 4.90637, 10.5567) +material = SubResource("ORMMaterial3D_yvrvx") + +[node name="CSGBox3D9" type="CSGBox3D" parent="NavigationRegion3D/CSGCombiner3D"] +transform = Transform3D(1, 0, 1.74846e-07, 0, 1, 0, -1.74846e-07, 0, 1, 3.4037, 1.95319, 15.0201) +size = Vector3(0.87915, 4.90637, 4.93153) +material = SubResource("ORMMaterial3D_yvrvx") + +[node name="CSGBox3D7" type="CSGBox3D" parent="NavigationRegion3D/CSGCombiner3D"] +transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, 10.495, 1.95319, 13.3886) +size = Vector3(0.87915, 4.90637, 9.60895) +material = SubResource("ORMMaterial3D_yvrvx") + +[node name="TestEnemy" parent="." instance=ExtResource("1_cmbee")] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -5.16028, 0.549035, 11.0707) + +[node name="Player" parent="." instance=ExtResource("2_2xi8s")] +unique_name_in_owner = true +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -7.24002, 1.52635, 15.011) +speed = 1000.0 + +[node name="DirectionalLight3D" type="DirectionalLight3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 0.113359, 0.993554, 0, -0.993554, 0.113359, 0, 5.273, 0) diff --git a/base/scripts/enemies/floor_mover.gd b/base/scripts/enemies/floor_mover.gd new file mode 100644 index 0000000..84157ae --- /dev/null +++ b/base/scripts/enemies/floor_mover.gd @@ -0,0 +1,20 @@ +extends Node + +class_name FloorMover + +@export var navigation_agent : NavigationAgent3D +@export var move_target : Node3D + +@export_category("Velocity Settings") +@export var max_speed : float +@export_range(0,720,0.1,"or_greater","radians_as_degrees","exp") var rotation_speed : float + +func _physics_process(delta: float) -> void: + if navigation_agent.is_navigation_finished(): return + + if navigation_agent.is_target_reachable(): + var direction_to_next = (navigation_agent.get_next_path_position() - move_target.global_position).normalized() + var delta_angle = clamp(move_target.basis.z.signed_angle_to(direction_to_next,Vector3.UP),-rotation_speed,rotation_speed) + + move_target.rotation.y += delta_angle + move_target.global_position += move_target.basis.z * max_speed * delta diff --git a/base/scripts/enemies/floor_mover.gd.uid b/base/scripts/enemies/floor_mover.gd.uid new file mode 100644 index 0000000..0e5043a --- /dev/null +++ b/base/scripts/enemies/floor_mover.gd.uid @@ -0,0 +1 @@ +uid://biqn05eyrhdya diff --git a/project.godot b/project.godot index d1135e0..a766ae8 100644 --- a/project.godot +++ b/project.godot @@ -88,6 +88,7 @@ slot_previous={ [layer_names] 3d_physics/layer_1="Static Environment" +3d_navigation/layer_1="Floor enemies" 3d_physics/layer_2="Friendly entities" 3d_physics/layer_3="Enemy entities" 3d_physics/layer_4="Projectile"