add ai, photo_mode and radio
This commit was merged in pull request #11.
This commit is contained in:
51
core/ai/agents/base/ai_base.gd
Normal file
51
core/ai/agents/base/ai_base.gd
Normal file
@@ -0,0 +1,51 @@
|
||||
extends CharacterBody3D
|
||||
|
||||
class_name AIBase
|
||||
|
||||
@export var speed: float = 4.0
|
||||
var _enable_state_machine: bool = true
|
||||
@export var enable_state_machine: bool:
|
||||
set(value):
|
||||
_enable_state_machine = value
|
||||
toggle_enable_state_machine()
|
||||
get:
|
||||
return _enable_state_machine
|
||||
|
||||
|
||||
@onready var patrol_radius_shape: CollisionShape3D = $%PatrolRadiusShape
|
||||
@onready var nav_agent: NavigationAgent3D = $%NavigationAgent3D
|
||||
@onready var state_machine: StateMachine = $%StateMachine
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
randomize()
|
||||
toggle_enable_state_machine()
|
||||
|
||||
func toggle_enable_state_machine() -> void:
|
||||
state_machine.enable = _enable_state_machine
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
if !_enable_state_machine:
|
||||
return
|
||||
|
||||
state_machine.physics_process(delta)
|
||||
|
||||
if nav_agent.is_navigation_finished():
|
||||
velocity = Vector3.ZERO
|
||||
move_and_slide()
|
||||
return
|
||||
|
||||
var next_point = nav_agent.get_next_path_position()
|
||||
var direction = global_position.direction_to(next_point)
|
||||
|
||||
velocity.x = direction.x * speed
|
||||
velocity.z = direction.z * speed
|
||||
|
||||
move_and_slide()
|
||||
|
||||
func navigate_to_random_point() -> void:
|
||||
var nav_map_rid = get_world_3d().get_navigation_map()
|
||||
var patrol_radius = patrol_radius_shape.shape.radius
|
||||
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
|
||||
1
core/ai/agents/base/ai_base.gd.uid
Normal file
1
core/ai/agents/base/ai_base.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://b30p1yqojbbbk
|
||||
44
core/ai/agents/base/ai_base.tscn
Normal file
44
core/ai/agents/base/ai_base.tscn
Normal file
@@ -0,0 +1,44 @@
|
||||
[gd_scene format=3 uid="uid://clx701xdwelgx"]
|
||||
|
||||
[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"]
|
||||
|
||||
[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_mh3lg"]
|
||||
|
||||
[sub_resource type="SphereShape3D" id="SphereShape3D_lim44"]
|
||||
radius = 20.0
|
||||
|
||||
[node name="AiBase" type="CharacterBody3D" unique_id=1228675528]
|
||||
script = ExtResource("1_4d1nn")
|
||||
|
||||
[node name="MeshInstance3D" type="MeshInstance3D" parent="." unique_id=214482161]
|
||||
mesh = SubResource("CapsuleMesh_mh3lg")
|
||||
|
||||
[node name="CollisionShape3D" type="CollisionShape3D" parent="." unique_id=92811823]
|
||||
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")]
|
||||
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
|
||||
shape = SubResource("SphereShape3D_lim44")
|
||||
disabled = true
|
||||
debug_color = Color(1, 1, 0, 1)
|
||||
26
core/ai/agents/base/idle_state.gd
Normal file
26
core/ai/agents/base/idle_state.gd
Normal file
@@ -0,0 +1,26 @@
|
||||
extends State
|
||||
|
||||
const PATROL_STATE_ID: StringName = &"patrol"
|
||||
|
||||
@export var wait_time: float = 3.0
|
||||
|
||||
var runtime_timer: Timer
|
||||
|
||||
func enter() -> void:
|
||||
runtime_timer = Timer.new()
|
||||
runtime_timer.name = "IdleTimer"
|
||||
add_child(runtime_timer)
|
||||
runtime_timer.one_shot = true
|
||||
runtime_timer.timeout.connect(_on_timer_timeout)
|
||||
runtime_timer.start(wait_time)
|
||||
|
||||
func exit() -> void:
|
||||
if runtime_timer:
|
||||
if runtime_timer.is_connected("timeout", _on_timer_timeout):
|
||||
runtime_timer.timeout.disconnect(_on_timer_timeout)
|
||||
|
||||
runtime_timer.queue_free()
|
||||
runtime_timer = null
|
||||
|
||||
func _on_timer_timeout() -> void:
|
||||
transitioned.emit(self, PATROL_STATE_ID)
|
||||
1
core/ai/agents/base/idle_state.gd.uid
Normal file
1
core/ai/agents/base/idle_state.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://nga8qx56iwgu
|
||||
12
core/ai/agents/base/patrol_state.gd
Normal file
12
core/ai/agents/base/patrol_state.gd
Normal file
@@ -0,0 +1,12 @@
|
||||
extends State
|
||||
|
||||
const IDLE_STATE_ID: StringName = &"idle"
|
||||
|
||||
@onready var agent: AIBase = owner
|
||||
|
||||
func enter() -> void:
|
||||
agent.navigate_to_random_point()
|
||||
|
||||
func physics_update(_delta) -> void:
|
||||
if agent.nav_agent.is_navigation_finished():
|
||||
transitioned.emit(self, IDLE_STATE_ID)
|
||||
1
core/ai/agents/base/patrol_state.gd.uid
Normal file
1
core/ai/agents/base/patrol_state.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://bngfthvt04ivv
|
||||
Reference in New Issue
Block a user