From 5e053243d5b2e90e61442abffa752008fec898ad Mon Sep 17 00:00:00 2001 From: "m.cirafisi" Date: Thu, 25 Jun 2026 21:45:18 +0200 Subject: [PATCH] add sit state and colors to cat --- core/ai/agents/cat/ai_cat.gd | 38 ++++++++++++++++++++++++++++ core/ai/agents/cat/ai_cat.gd.uid | 1 + core/ai/agents/cat/ai_cat.tscn | 21 +++++++++++++--- core/ai/framework/sit_state.gd | 40 ++++++++++++++++++++++++++++++ core/ai/framework/sit_state.gd.uid | 1 + 5 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 core/ai/agents/cat/ai_cat.gd create mode 100644 core/ai/agents/cat/ai_cat.gd.uid create mode 100644 core/ai/framework/sit_state.gd create mode 100644 core/ai/framework/sit_state.gd.uid diff --git a/core/ai/agents/cat/ai_cat.gd b/core/ai/agents/cat/ai_cat.gd new file mode 100644 index 0000000..752d5b5 --- /dev/null +++ b/core/ai/agents/cat/ai_cat.gd @@ -0,0 +1,38 @@ +extends AIBase + +class_name AICat + +@export_group("Cat Colors") +@export var mesh: MeshInstance3D +@export var fur_colors: Array[Color] = [] +@export var eye_colors: Array[Color] = [] + +@export var fur_material_index: int = 0 +@export var eye_material_index: int = 1 + +func _ready() -> void: + super._ready() + _apply_random_color() + +func _apply_random_color() -> void: + if fur_colors.is_empty() or eye_colors.is_empty(): + return + + if not mesh or not mesh.mesh: + return + + var max_index = min(fur_colors.size(), eye_colors.size()) + var random_idx = randi() % max_index + + _override_material_color(mesh, fur_material_index, fur_colors[random_idx]) + _override_material_color(mesh, eye_material_index, eye_colors[random_idx]) + +func _override_material_color(mesh_node: MeshInstance3D, surface_idx: int, color: Color) -> void: + if surface_idx < 0 or surface_idx >= mesh_node.mesh.get_surface_count(): + return + + var mat = mesh_node.mesh.surface_get_material(surface_idx) + if mat and mat is StandardMaterial3D: + var new_mat = mat.duplicate() as StandardMaterial3D + new_mat.albedo_color = color + mesh_node.set_surface_override_material(surface_idx, new_mat) diff --git a/core/ai/agents/cat/ai_cat.gd.uid b/core/ai/agents/cat/ai_cat.gd.uid new file mode 100644 index 0000000..c01a1aa --- /dev/null +++ b/core/ai/agents/cat/ai_cat.gd.uid @@ -0,0 +1 @@ +uid://cx67xas1hxv3x diff --git a/core/ai/agents/cat/ai_cat.tscn b/core/ai/agents/cat/ai_cat.tscn index 7f39749..78032e8 100644 --- a/core/ai/agents/cat/ai_cat.tscn +++ b/core/ai/agents/cat/ai_cat.tscn @@ -2,10 +2,18 @@ [ext_resource type="PackedScene" uid="uid://clx701xdwelgx" path="res://core/ai/agents/base/ai_base.tscn" id="1_85o24"] [ext_resource type="Script" uid="uid://d3hy70ec8vqo5" path="res://core/ai/framework/run_animation_state.gd" id="2_6mu8r"] +[ext_resource type="Script" uid="uid://cx67xas1hxv3x" path="res://core/ai/agents/cat/ai_cat.gd" id="2_cat_script"] [ext_resource type="PackedScene" uid="uid://c5rccx22gs6jt" path="res://core/ai/agents/cat/Cat_rig.fbx" id="2_rohxe"] [ext_resource type="Script" uid="uid://bngfthvt04ivv" path="res://core/ai/framework/patrol_state.gd" id="3_0jm3k"] +[ext_resource type="Script" uid="uid://dty1p02kqviwf" path="res://core/ai/framework/sit_state.gd" id="3_scd7t"] -[node name="AiCat" unique_id=1228675528 node_paths=PackedStringArray("anim_player") instance=ExtResource("1_85o24")] +[node name="AiCat" unique_id=1228675528 node_paths=PackedStringArray("mesh", "anim_player") instance=ExtResource("1_85o24")] +script = ExtResource("2_cat_script") +mesh = NodePath("Cat_rig/Rig_Cat/Skeleton3D/Cat") +fur_colors = Array[Color]([]) +eye_colors = Array[Color]([]) +fur_material_index = 0 +eye_material_index = 1 anim_player = NodePath("Cat_rig/AnimationPlayer") entity_type = 3 entity_name = "Cat" @@ -17,9 +25,16 @@ initial_state = NodePath("IdleState") script = ExtResource("2_6mu8r") state_id = &"idle" next_state_id = &"patrol" +random_next_state_ids = Array[StringName]([&"patrol", &"sit"]) animation_name = "Cat_Idle" -[node name="PatrolState" type="Node" parent="StateMachine" index="1" unique_id=1861645340] +[node name="SitState" type="Node" parent="StateMachine" index="1" unique_id=860354787] +script = ExtResource("3_scd7t") +state_id = &"sit" +next_state_id = &"idle" +animation_name = "Cat_Sit" + +[node name="PatrolState" type="Node" parent="StateMachine" index="2" unique_id=1861645340] script = ExtResource("3_0jm3k") state_id = &"patrol" next_state_id = &"idle" @@ -28,7 +43,7 @@ exit_on_action_finished = true [node name="Cat_rig" parent="." index="3" unique_id=1332997618 instance=ExtResource("2_rohxe")] -[node name="Skeleton3D" parent="Cat_rig/Rig_Cat" parent_id_path=PackedInt32Array(1332997618, 1292226211) index="0" unique_id=807490174] +[node name="Skeleton3D" parent="Cat_rig/Rig_Cat" parent_id_path=PackedInt32Array(1332997618, 656824407) index="0" unique_id=1952223098] bones/1/position = Vector3(1.6719584e-08, 0.23547882, 0.34218493) bones/1/rotation = Quaternion(0.12769094, -0.6954821, -0.12769029, 0.69548184) bones/2/rotation = Quaternion(-1.5819447e-07, -1.9079575e-07, -0.5904159, 0.8070992) diff --git a/core/ai/framework/sit_state.gd b/core/ai/framework/sit_state.gd new file mode 100644 index 0000000..1ddb2f4 --- /dev/null +++ b/core/ai/framework/sit_state.gd @@ -0,0 +1,40 @@ +extends State + +class_name SitState + +var reverse: bool = false + +func enter() -> void: + reverse = false + super.enter() + + if agent.anim_player and not agent.anim_player.animation_finished.is_connected(_on_animation_finished): + agent.anim_player.animation_finished.connect(_on_animation_finished) + + if runtime_timer and not runtime_timer.is_stopped(): + runtime_timer.stop() + + run_animation() + +func exit() -> void: + super.exit() + if agent.anim_player and agent.anim_player.animation_finished.is_connected(_on_animation_finished): + agent.anim_player.animation_finished.disconnect(_on_animation_finished) + +func _on_animation_finished(anim_name: StringName) -> void: + if animation_name == anim_name: + if not reverse: + if min_wait_time > 0: + var wait_time := randf_range(min_wait_time, max_wait_time) + runtime_timer.start(wait_time) + else: + _on_timer_timeout() + else: + transitioned.emit(self, get_next_state()) + +func _on_timer_timeout() -> void: + reverse = true + if agent.anim_player and agent.anim_player.has_animation(animation_name): + agent.anim_player.play_backwards(animation_name, blend_time) + else: + transitioned.emit(self, get_next_state()) diff --git a/core/ai/framework/sit_state.gd.uid b/core/ai/framework/sit_state.gd.uid new file mode 100644 index 0000000..b1863d3 --- /dev/null +++ b/core/ai/framework/sit_state.gd.uid @@ -0,0 +1 @@ +uid://dty1p02kqviwf