From ed3c47e9cf2500d73d28f4fca9b4d29785864bd1 Mon Sep 17 00:00:00 2001 From: derek Date: Mon, 12 May 2025 16:43:25 -0500 Subject: [PATCH] working on rat --- assets/LevelBlockouts/hub_1.tscn | 28 +++++++++++++++- assets/jump_platform.tres | 2 +- assets/rat.tscn | 34 ++++++++++++------- assets/rat_hole.tscn | 7 ++-- scripts/Rat_Control.gd | 56 +++++++++++++++++--------------- scripts/rat.gd | 46 +++++++++----------------- scripts/rat_hole.gd | 21 +++--------- scripts/rat_walk.gd | 12 +++++++ scripts/rat_walk.gd.uid | 1 + scripts/spider.gd | 4 +-- 10 files changed, 119 insertions(+), 92 deletions(-) create mode 100644 scripts/rat_walk.gd create mode 100644 scripts/rat_walk.gd.uid diff --git a/assets/LevelBlockouts/hub_1.tscn b/assets/LevelBlockouts/hub_1.tscn index e871660..e0dd41f 100644 --- a/assets/LevelBlockouts/hub_1.tscn +++ b/assets/LevelBlockouts/hub_1.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=33 format=3 uid="uid://otkecr0hcyon"] +[gd_scene load_steps=35 format=3 uid="uid://otkecr0hcyon"] [ext_resource type="PackedScene" uid="uid://bj1y0fbjtul4a" path="res://post_processing.tscn" id="1_48lr2"] [ext_resource type="PackedScene" uid="uid://drwae3loscbw7" path="res://assets/player.tscn" id="1_ibypk"] @@ -23,6 +23,8 @@ [ext_resource type="PackedScene" uid="uid://bvjrsc86n2ak0" path="res://assets/deposit_money.tscn" id="21_v6g1i"] [ext_resource type="PackedScene" uid="uid://bessq6hl7qsh8" path="res://assets/stats.tscn" id="22_v6g1i"] [ext_resource type="PackedScene" uid="uid://c10cvl6w5rn61" path="res://deposit_bot.tscn" id="23_7kmox"] +[ext_resource type="Script" uid="uid://17ejs4cam07p" path="res://scripts/Rat_Control.gd" id="24_4o8us"] +[ext_resource type="PackedScene" uid="uid://6rhnl7vbvvr4" path="res://assets/rat_hole.tscn" id="24_dblmv"] [sub_resource type="Gradient" id="Gradient_vr1m7"] offsets = PackedFloat32Array(0.2, 1) @@ -446,3 +448,27 @@ transform = Transform3D(-0.993502, 0, -0.11381, 0, 1, 0, 0.11381, 0, -0.993502, [node name="Stats" parent="." instance=ExtResource("22_v6g1i")] transform = Transform3D(-0.992703, 0, -0.120587, 0, 1, 0, 0.120587, 0, -0.992703, 18.3029, 3.10808, -2.49889) + +[node name="RATS" type="Node3D" parent="."] +script = ExtResource("24_4o8us") + +[node name="RatHole2" parent="RATS" instance=ExtResource("24_dblmv")] +transform = Transform3D(1.19249e-08, 0, -1, 0, 1, 0, 1, 0, 1.19249e-08, -13.0478, 0.410853, -9.74827) + +[node name="RatHole3" parent="RATS" instance=ExtResource("24_dblmv")] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -24.8821, 0.410853, -0.663009) + +[node name="RatHole6" parent="RATS" instance=ExtResource("24_dblmv")] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -24.8821, 13.2573, -1.41909) + +[node name="RatHole7" parent="RATS" instance=ExtResource("24_dblmv")] +transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, -24.8821, 13.0097, -39.7559) + +[node name="RatHole4" parent="RATS" instance=ExtResource("24_dblmv")] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 19.7407, 0.410853, -0.663009) + +[node name="RatHole" parent="RATS" instance=ExtResource("24_dblmv")] +transform = Transform3D(-4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, 12.9703, 0.410853, -9.91221) + +[node name="RatHole5" parent="RATS" instance=ExtResource("24_dblmv")] +transform = Transform3D(-4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, 12.9703, 0.410853, -42.5574) diff --git a/assets/jump_platform.tres b/assets/jump_platform.tres index 844db72..e40c203 100644 --- a/assets/jump_platform.tres +++ b/assets/jump_platform.tres @@ -1,4 +1,4 @@ -[gd_resource type="VisualShader" load_steps=31 format=3 uid="uid://bif4rjooo7q2n"] +[gd_resource type="VisualShader" load_steps=31 format=3 uid="uid://ddf8pgbvo0glp"] [sub_resource type="VisualShaderNodeFresnel" id="VisualShaderNodeFresnel_jqdis"] default_input_values = [2, false, 3, 1.5] diff --git a/assets/rat.tscn b/assets/rat.tscn index f740d48..8e92834 100644 --- a/assets/rat.tscn +++ b/assets/rat.tscn @@ -1,10 +1,12 @@ -[gd_scene load_steps=26 format=4 uid="uid://0xs247hfvkml"] +[gd_scene load_steps=28 format=4 uid="uid://0xs247hfvkml"] [ext_resource type="Texture2D" uid="uid://cy4cwv7ppdclm" path="res://assets/Models/rat1.albedo.png" id="1_57px3"] [ext_resource type="Script" uid="uid://b5rs1lkgq027t" path="res://scripts/rat.gd" id="1_m8jnv"] -[ext_resource type="Texture2D" uid="uid://caboww85wl3r3" path="res://assets/Models/rat1.roughness.png" id="2_i87w4"] +[ext_resource type="Texture2D" uid="uid://cuiwcyxkgx0ki" path="res://assets/Models/rat1.roughness.png" id="2_i87w4"] [ext_resource type="PackedScene" uid="uid://b7rlijqhcmkjp" path="res://assets/rat_dead.tscn" id="2_it4rd"] [ext_resource type="Texture2D" uid="uid://ch863c8gv6ec5" path="res://assets/Models/rat1.normal.png" id="3_e5v6c"] +[ext_resource type="Script" uid="uid://csju024nerln6" path="res://scripts/StateMachine.gd" id="3_j2fki"] +[ext_resource type="Script" uid="uid://3bd2b0b8dpmx" path="res://scripts/rat_walk.gd" id="4_7v38v"] [ext_resource type="Texture2D" uid="uid://4hcotl7lxuph" path="res://assets/Models/rat1.skin.albedo.png" id="4_umlul"] [sub_resource type="StandardMaterial3D" id="StandardMaterial3D_o45vq"] @@ -337,35 +339,43 @@ _data = { &"run": SubResource("Animation_rf35q") } -[node name="rat" type="CharacterBody3D" groups=["breakable", "npc", "rat", "spawned"]] -transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.117073, 0) +[node name="rat" type="CharacterBody3D" node_paths=PackedStringArray("state_machine") groups=["breakable", "npc", "rat", "spawned"]] collision_layer = 64 collision_mask = 45 script = ExtResource("1_m8jnv") dead_rat = ExtResource("2_it4rd") +state_machine = NodePath("StateMachine") + +[node name="StateMachine" type="Node" parent="." node_paths=PackedStringArray("initial_state")] +script = ExtResource("3_j2fki") +initial_state = NodePath("RatWalk") + +[node name="RatWalk" type="Node" parent="StateMachine" node_paths=PackedStringArray("character")] +script = ExtResource("4_7v38v") +character = NodePath("../..") [node name="rat1" type="MeshInstance3D" parent="."] -transform = Transform3D(-4.37114e-08, 0, -1, 0, 1, 0, 1, 0, -4.37114e-08, 0, 0.144121, 0) +transform = Transform3D(-4.37114e-08, 0, -1, 0, 1, 0, 1, 0, -4.37114e-08, 0, 0.079, 0) mesh = SubResource("ArrayMesh_bpli1") skeleton = NodePath("") [node name="rat1_backL" type="MeshInstance3D" parent="rat1"] -transform = Transform3D(0.960509, -0.27825, 0, 0.27825, 0.960509, 0, 0, 0, 1, 0.332101, -0.139938, 0.0851064) +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.332101, -0.139938, 0.0851064) mesh = SubResource("ArrayMesh_0q1vk") skeleton = NodePath("") [node name="rat1_backR" type="MeshInstance3D" parent="rat1"] -transform = Transform3D(0.908279, -0.418365, 0, 0.418365, 0.908279, 0, 0, 0, 1, 0.33924, -0.139938, -0.0693291) +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.33924, -0.139938, -0.0693291) mesh = SubResource("ArrayMesh_ssw2y") skeleton = NodePath("") [node name="rat1_frontL" type="MeshInstance3D" parent="rat1"] -transform = Transform3D(0.997319, -0.0731729, 0, 0.0731729, 0.997319, 0, 0, 0, 1, -0.0784779, -0.139885, 0.0819205) +transform = Transform3D(0.761312, -0.648385, 0, 0.648385, 0.761312, 0, 0, 0, 1, -0.0784779, -0.139885, 0.0819205) mesh = SubResource("ArrayMesh_xme6u") skeleton = NodePath("") [node name="rat1_frontR" type="MeshInstance3D" parent="rat1"] -transform = Transform3D(0.949505, -0.313755, 7.79043e-09, 0.313755, 0.949505, -7.31993e-09, -5.10039e-09, 9.39459e-09, 1, -0.0759167, -0.139885, -0.083779) +transform = Transform3D(-0.476865, -0.878978, 4.64309e-08, 0.878978, -0.476865, -4.36267e-08, 6.04881e-08, 2.00077e-08, 1, -0.0759167, -0.139885, -0.083779) mesh = SubResource("ArrayMesh_q1dp2") skeleton = NodePath("") @@ -380,12 +390,12 @@ shape = SubResource("CapsuleShape3D_q7xl1") [node name="AnimationPlayer" type="AnimationPlayer" parent="."] libraries = { -"": SubResource("AnimationLibrary_0gijd") +&"": SubResource("AnimationLibrary_0gijd") } autoplay = "run" [node name="NavigationAgent3D" type="NavigationAgent3D" parent="."] -path_height_offset = 1.1 +path_height_offset = 1.0 avoidance_enabled = true [node name="RayCast3D" type="RayCast3D" parent="."] @@ -399,3 +409,5 @@ target_position = Vector3(0, 0, -0.2) [node name="RayCast3D3" type="RayCast3D" parent="."] transform = Transform3D(0.707107, 0, 0.707107, 0, 1, 0, -0.707107, 0, 0.707107, 0, -0.0699478, -0.342128) target_position = Vector3(0, 0, -0.2) + +[connection signal="velocity_computed" from="NavigationAgent3D" to="." method="_on_navigation_agent_3d_velocity_computed"] diff --git a/assets/rat_hole.tscn b/assets/rat_hole.tscn index 5b0e11f..61e3611 100644 --- a/assets/rat_hole.tscn +++ b/assets/rat_hole.tscn @@ -20,10 +20,6 @@ rat = ExtResource("2_dih3n") [node name="MeshInstance3D" type="MeshInstance3D" parent="."] mesh = SubResource("BoxMesh_7h30q") -[node name="RayCast3D" type="RayCast3D" parent="."] -transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -0.356676) -target_position = Vector3(0, 0, -1) - [node name="Area3D" type="Area3D" parent="."] collision_layer = 2 collision_mask = 2 @@ -32,4 +28,7 @@ collision_mask = 2 transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.0113525, -0.302734) shape = SubResource("BoxShape3D_elkdi") +[node name="spawnpos" type="Marker3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -1.258) + [connection signal="body_entered" from="Area3D" to="." method="_on_area_3d_body_entered"] diff --git a/scripts/Rat_Control.gd b/scripts/Rat_Control.gd index e063b7e..353409a 100644 --- a/scripts/Rat_Control.gd +++ b/scripts/Rat_Control.gd @@ -1,34 +1,38 @@ extends Node3D @export var rat : Resource -@export var spawn_amount = 10 #max amount in level at any given time +@export var spawn_time_max = 5 -var rng = RandomNumberGenerator.new() +var spawn_timer var holes = [] -var start_hole_id -var start_hole -var end_hole_id -var end_hole -var hole_length_id -var control_node = self # Called when the node enters the scene tree for the first time. -func _ready(): - holes = get_children() - hole_length_id = holes.size() - 1 +func _ready() -> void: + #get holes + for node in self.get_children(): + if node is RatHole: + holes.append(node) + print("HOLES : ",holes) + spawn_rat_at_random_hole() + reset_spawn_timer() -# Called every frame. 'delta' is the elapsed time since the previous frame. -func _process(delta): - - if spawn_amount > 0: - start_hole_id = rng.randi_range(0,hole_length_id) - start_hole = holes[start_hole_id] - end_hole_id = rng.randi_range(0,hole_length_id) - while start_hole_id == end_hole_id: - end_hole_id = rng.randi_range(0,hole_length_id) - - end_hole = holes[end_hole_id] - - #spawn rat at first hole and pass position of next hole - start_hole.spawn_rat(end_hole,control_node) - spawn_amount -= 1 +func _process(delta: float) -> void: + print("TIME UNTIL NEXT SPAWN : ",spawn_timer) + if spawn_timer > 0: + spawn_timer -= delta + else: + reset_spawn_timer() + spawn_rat_at_random_hole() + +func spawn_rat_at_random_hole(): + if holes.size() >= 2: + var hole_options = holes.duplicate() + hole_options.shuffle() + var spawn_hole = hole_options.pop_front() + var destination_hole = hole_options.pop_front() + print("SPAWN HOLE : ", spawn_hole) + print("END_HOLE : ",destination_hole) + spawn_hole.spawn_rat(destination_hole) + +func reset_spawn_timer(): + spawn_timer = randf_range(1,spawn_time_max) diff --git a/scripts/rat.gd b/scripts/rat.gd index d6e6a72..dfbd9f1 100644 --- a/scripts/rat.gd +++ b/scripts/rat.gd @@ -1,47 +1,27 @@ extends CharacterBody3D -var number_of_drops = 1 -const MAX_LV = 20 -const MAX_AV = 10 - @export var dead_rat : Resource -@export var SPEED = 7 -var end_hole -var control_node -var rng = RandomNumberGenerator.new() +@export var SPEED = 5 +@export var state_machine : StateMachine @onready var level_control = get_tree().current_scene @onready var anim_player = $AnimationPlayer @onready var nav_agent = $NavigationAgent3D -@onready var ray = $RayCast3D -@onready var ray_2 = $RayCast3D2 -@onready var ray_3 = $RayCast3D3 +var end_hole -# Called when the node enters the scene tree for the first time. -func _ready(): - pass # Replace with function body. - +const number_of_drops = 1 +const MAX_LV = 20 +const MAX_AV = 10 # Called every frame. 'delta' is the elapsed time since the previous frame. -func _physics_process(delta): - - - # Navigation - if end_hole != null: #REMOVE WHEN FIXED LEVEL RELOAD - nav_agent.set_target_position(end_hole.global_transform.origin) - var next_nav_point = nav_agent.get_next_path_position() - velocity = (next_nav_point - global_transform.origin).normalized() * SPEED - - look_at(end_hole.global_transform.origin, Vector3.UP) - - - move_and_slide() +func _process(delta): + move_and_slide() func breaking(bullet_velocity): - + var drops_left = number_of_drops #pickup drop - while number_of_drops > 0: + while drops_left > 0: var pickup_spawn = level_control.ITEM_PICKUP.instantiate() var item_stats = level_control.pickup_spawn(false) @@ -64,7 +44,7 @@ func breaking(bullet_velocity): pickup_spawn.linear_velocity += self.global_transform.basis * Vector3(lv_x,lv_y,lv_z) pickup_spawn.angular_velocity += self.global_transform.basis * Vector3(av_x,av_y,av_z) get_tree().get_root().add_child(pickup_spawn) - number_of_drops -= 1 + drops_left -= 1 #animate dead var spawn_broken = dead_rat.instantiate() @@ -80,3 +60,7 @@ func breaking(bullet_velocity): var pieces = spawn_broken.get_children() get_tree().get_root().add_child(spawn_broken) queue_free() + + +func _on_navigation_agent_3d_velocity_computed(safe_velocity: Vector3) -> void: + state_machine.current_state.velocity_computed(safe_velocity) diff --git a/scripts/rat_hole.gd b/scripts/rat_hole.gd index 84846c8..b20f6b1 100644 --- a/scripts/rat_hole.gd +++ b/scripts/rat_hole.gd @@ -1,26 +1,15 @@ extends Node3D +class_name RatHole @export var rat : Resource -@onready var ray = $RayCast3D +@onready var spawnpos: Marker3D = $spawnpos @onready var area_3d = $Area3D - -# Called when the node enters the scene tree for the first time. -func _ready(): - pass # Replace with function body. - - -# Called every frame. 'delta' is the elapsed time since the previous frame. -func _process(delta): - pass - -func spawn_rat(end_hole,control_node): +func spawn_rat(end_hole): var spawn_rat = rat.instantiate() - spawn_rat.position = ray.global_position - spawn_rat.transform.basis = ray.global_transform.basis - spawn_rat.look_at_from_position(end_hole.position,Vector3.UP) + spawn_rat.position = spawnpos.global_position spawn_rat.end_hole = end_hole - get_tree().get_root().add_child(spawn_rat) + get_parent().add_child(spawn_rat) func _on_area_3d_body_entered(body): if body.is_in_group("rat"): diff --git a/scripts/rat_walk.gd b/scripts/rat_walk.gd new file mode 100644 index 0000000..5c69c2c --- /dev/null +++ b/scripts/rat_walk.gd @@ -0,0 +1,12 @@ +extends CharacterState +class_name RatWalk + + +func Update(delta): + get_new_waypoint() + move_to_nav_point(delta) + +func get_new_waypoint(): + if character.end_hole: + move_target = character.end_hole.global_position + character.nav_agent.set_target_position(move_target) diff --git a/scripts/rat_walk.gd.uid b/scripts/rat_walk.gd.uid new file mode 100644 index 0000000..1f7da08 --- /dev/null +++ b/scripts/rat_walk.gd.uid @@ -0,0 +1 @@ +uid://3bd2b0b8dpmx diff --git a/scripts/spider.gd b/scripts/spider.gd index 654dc19..983fdba 100644 --- a/scripts/spider.gd +++ b/scripts/spider.gd @@ -100,9 +100,9 @@ func _ready(): func _process(delta): line_of_sight.global_position = global_position + Vector3(0,1.5,0) - move_and_slide() - look_at_player() + + move_and_slide() func stun(): change_state_to("stunned")