ai refactoring

This commit is contained in:
2026-05-22 00:06:23 +02:00
parent b22f219a5e
commit 0c237430ea
10 changed files with 67 additions and 68 deletions

View File

@@ -2,7 +2,7 @@ extends CharacterBody3D
class_name AIBase
@export_group("Movement")
@export_group("Settings")
@export var speed: float = 4.0
var _enable_state_machine: bool = true
@export var enable_state_machine: bool:
@@ -11,13 +11,8 @@ var _enable_state_machine: bool = true
toggle_enable_state_machine()
get:
return _enable_state_machine
@export var anim_player: AnimationPlayer
@onready var patrol_radius_shape: CollisionShape3D = $%PatrolRadiusShape
@onready var nav_agent: NavigationAgent3D = $%NavigationAgent3D
@onready var state_machine: StateMachine = $%StateMachine
@export_group("Entities")
enum EntityType { HUMANOID, ANIMAL }
@export var entity_type: EntityType = EntityType.HUMANOID
@@ -25,6 +20,9 @@ enum EntityType { HUMANOID, ANIMAL }
@export var allowed_biomes: Array[String] = []
@export_range(0.0, 100.0, 0.1) var spawn_uniqueness: float = 1.0
@onready var patrol_radius_shape: CollisionShape3D = $%PatrolRadiusShape
@onready var nav_agent: NavigationAgent3D = $%NavigationAgent3D
@onready var state_machine: StateMachine = $%StateMachine
func _ready() -> void:
randomize()
@@ -63,15 +61,3 @@ func navigate_to_random_point() -> void:
var target_point_in_space = global_position + Vector3(randf_range(-1, 1), 0, randf_range(-1, 1)).normalized() * randf_range(0, patrol_radius)
var closest_valid_point = NavigationServer3D.map_get_closest_point(nav_map_rid, target_point_in_space)
nav_agent.target_position = closest_valid_point
func run_walking_animation() -> void:
if anim_player and anim_player.has_animation("custom/walking"):
var anim = anim_player.get_animation("custom/walking")
anim.loop_mode = Animation.LOOP_LINEAR
anim_player.play("custom/walking")
func run_idle_animation() -> void:
if anim_player and anim_player.has_animation("custom/idle"):
var anim = anim_player.get_animation("custom/idle")
anim.loop_mode = Animation.LOOP_LINEAR
anim_player.play("custom/idle")

View File

@@ -2,8 +2,6 @@
[ext_resource type="Script" uid="uid://b30p1yqojbbbk" path="res://core/ai/agents/base/ai_base.gd" id="1_4d1nn"]
[ext_resource type="Script" uid="uid://ps3vlu2qvmop" path="res://core/ai/framework/state_machine.gd" id="2_q1hg3"]
[ext_resource type="Script" uid="uid://nga8qx56iwgu" path="res://core/ai/agents/base/idle_state.gd" id="3_26faq"]
[ext_resource type="Script" uid="uid://bngfthvt04ivv" path="res://core/ai/agents/base/patrol_state.gd" id="4_lim44"]
[sub_resource type="CapsuleMesh" id="CapsuleMesh_mh3lg"]
@@ -25,18 +23,9 @@ shape = SubResource("CapsuleShape3D_mh3lg")
[node name="NavigationAgent3D" type="NavigationAgent3D" parent="." unique_id=1370024449]
unique_name_in_owner = true
[node name="StateMachine" type="Node" parent="." unique_id=1286404264 node_paths=PackedStringArray("initial_state")]
[node name="StateMachine" type="Node" parent="." unique_id=1286404264]
unique_name_in_owner = true
script = ExtResource("2_q1hg3")
initial_state = NodePath("IdleState")
[node name="IdleState" type="Node" parent="StateMachine" unique_id=763668255]
script = ExtResource("3_26faq")
state_id = &"idle"
[node name="PatrolState" type="Node" parent="StateMachine" unique_id=2054606629]
script = ExtResource("4_lim44")
state_id = &"patrol"
[node name="PatrolRadiusShape" type="CollisionShape3D" parent="." unique_id=1379515938]
unique_name_in_owner = true

View File

@@ -1,14 +1,11 @@
extends State
const PATROL_STATE_ID: StringName = &"patrol"
@onready var agent: AIBase = owner
@export var wait_time: float = 3.0
var runtime_timer: Timer
func enter() -> void:
agent.run_idle_animation()
run_animation()
runtime_timer = Timer.new()
runtime_timer.name = "IdleTimer"
add_child(runtime_timer)
@@ -25,4 +22,4 @@ func exit() -> void:
runtime_timer = null
func _on_timer_timeout() -> void:
transitioned.emit(self, PATROL_STATE_ID)
transitioned.emit(self, next_state_id)

View File

@@ -1,13 +1,9 @@
extends State
const IDLE_STATE_ID: StringName = &"idle"
@onready var agent: AIBase = owner
func enter() -> void:
run_animation()
agent.navigate_to_random_point()
agent.run_walking_animation()
func physics_update(_delta) -> void:
if agent.nav_agent.is_navigation_finished():
transitioned.emit(self, IDLE_STATE_ID)
transitioned.emit(self, next_state_id)