added project with nerabochie cows
This commit is contained in:
25
scripts/state_machine/node_state.gd
Normal file
25
scripts/state_machine/node_state.gd
Normal file
@ -0,0 +1,25 @@
|
||||
class_name NodeState
|
||||
extends Node
|
||||
|
||||
@warning_ignore("unused_signal")
|
||||
signal transition
|
||||
|
||||
|
||||
func _on_process(_delta : float) -> void:
|
||||
pass
|
||||
|
||||
|
||||
func _on_physics_process(_delta : float) -> void:
|
||||
pass
|
||||
|
||||
|
||||
func _on_next_transitions() -> void:
|
||||
pass
|
||||
|
||||
|
||||
func _on_enter() -> void:
|
||||
pass
|
||||
|
||||
|
||||
func _on_exit() -> void:
|
||||
pass
|
1
scripts/state_machine/node_state.gd.uid
Normal file
1
scripts/state_machine/node_state.gd.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://cbtljbbmwh4pu
|
48
scripts/state_machine/node_state_machine.gd
Normal file
48
scripts/state_machine/node_state_machine.gd
Normal file
@ -0,0 +1,48 @@
|
||||
class_name NodeStateMachine
|
||||
extends Node
|
||||
|
||||
@export var initial_node_state : NodeState
|
||||
|
||||
var node_states : Dictionary = {}
|
||||
var current_node_state : NodeState
|
||||
var current_node_state_name : String
|
||||
|
||||
func _ready() -> void:
|
||||
for child in get_children():
|
||||
if child is NodeState:
|
||||
node_states[child.name.to_lower()] = child
|
||||
child.transition.connect(transition_to)
|
||||
|
||||
if initial_node_state:
|
||||
initial_node_state._on_enter()
|
||||
current_node_state = initial_node_state
|
||||
current_node_state_name = current_node_state.name.to_lower()
|
||||
|
||||
func _process(delta : float) -> void:
|
||||
if current_node_state:
|
||||
current_node_state._on_process(delta)
|
||||
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
if current_node_state:
|
||||
current_node_state._on_physics_process(delta)
|
||||
current_node_state._on_next_transitions()
|
||||
|
||||
|
||||
func transition_to(node_state_name : String) -> void:
|
||||
if node_state_name == current_node_state.name.to_lower():
|
||||
return
|
||||
|
||||
var new_node_state = node_states.get(node_state_name.to_lower())
|
||||
|
||||
if !new_node_state:
|
||||
return
|
||||
|
||||
if current_node_state:
|
||||
current_node_state._on_exit()
|
||||
|
||||
new_node_state._on_enter()
|
||||
|
||||
current_node_state = new_node_state
|
||||
current_node_state_name = current_node_state.name.to_lower()
|
||||
print("Current State: ", current_node_state_name)
|
1
scripts/state_machine/node_state_machine.gd.uid
Normal file
1
scripts/state_machine/node_state_machine.gd.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://c47padj61m0xm
|
41
scripts/state_machine/npc_states/idle_state.gd
Normal file
41
scripts/state_machine/npc_states/idle_state.gd
Normal file
@ -0,0 +1,41 @@
|
||||
extends NodeState
|
||||
|
||||
@export var character: CharacterBody2D
|
||||
@export var animated_sprite_2d: AnimatedSprite2D
|
||||
@export var idle_state_time_interval: float = 5.0
|
||||
|
||||
@onready var idle_state_timer: Timer = Timer.new()
|
||||
|
||||
var idle_state_timeout: bool = false
|
||||
|
||||
func _ready() -> void:
|
||||
idle_state_timer.wait_time = idle_state_time_interval
|
||||
idle_state_timer.timeout.connect(on_idle_state_timeout)
|
||||
add_child(idle_state_timer)
|
||||
|
||||
func _on_process(_delta : float) -> void:
|
||||
pass
|
||||
|
||||
|
||||
func _on_physics_process(_delta : float) -> void:
|
||||
pass
|
||||
|
||||
|
||||
func _on_next_transitions() -> void:
|
||||
if idle_state_timeout:
|
||||
transition.emit("Walk")
|
||||
|
||||
|
||||
func _on_enter() -> void:
|
||||
animated_sprite_2d.play("idle")
|
||||
|
||||
idle_state_timeout = false
|
||||
idle_state_timer.start()
|
||||
|
||||
|
||||
func _on_exit() -> void:
|
||||
animated_sprite_2d.stop()
|
||||
idle_state_timer.stop()
|
||||
|
||||
func on_idle_state_timeout() -> void:
|
||||
idle_state_timeout = true
|
1
scripts/state_machine/npc_states/idle_state.gd.uid
Normal file
1
scripts/state_machine/npc_states/idle_state.gd.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://b0y0fxeo52kqi
|
68
scripts/state_machine/npc_states/walk_state.gd
Normal file
68
scripts/state_machine/npc_states/walk_state.gd
Normal file
@ -0,0 +1,68 @@
|
||||
extends NodeState
|
||||
|
||||
@export var character: NonPlayableCharacter
|
||||
@export var animated_sprite_2d: AnimatedSprite2D
|
||||
@export var navigation_agent_2d: NavigationAgent2D
|
||||
@export var min_speed: float = 5.0
|
||||
@export var max_speed: float = 10.0
|
||||
|
||||
var speed: float
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
navigation_agent_2d.velocity_computed.connect(on_safe_velocity_computed)
|
||||
call_deferred("character_setup")
|
||||
|
||||
func on_safe_velocity_computed(safe_velocity: Vector2) -> void:
|
||||
animated_sprite_2d.flip_h = safe_velocity.x < 0
|
||||
character.velocity = safe_velocity
|
||||
character.move_and_slide()
|
||||
|
||||
|
||||
func character_setup() -> void:
|
||||
await get_tree().physics_frame
|
||||
|
||||
set_movement_target()
|
||||
|
||||
func set_movement_target() -> void:
|
||||
var target_position: Vector2 = NavigationServer2D.map_get_random_point(navigation_agent_2d.get_navigation_map(), navigation_agent_2d.navigation_layers, false)
|
||||
navigation_agent_2d.target_position = target_position
|
||||
speed = randf_range(min_speed, max_speed)
|
||||
|
||||
|
||||
func _on_process(_delta : float) -> void:
|
||||
pass
|
||||
|
||||
|
||||
func _on_physics_process(_delta : float) -> void:
|
||||
if navigation_agent_2d.is_navigation_finished():
|
||||
character.current_walk_cycle += 1
|
||||
set_movement_target()
|
||||
return
|
||||
|
||||
var target_position: Vector2 = navigation_agent_2d.get_next_path_position()
|
||||
var target_direction: Vector2 = character.global_position.direction_to(target_position)
|
||||
|
||||
var velocity: Vector2 = target_direction * speed
|
||||
if navigation_agent_2d.avoidance_enabled:
|
||||
animated_sprite_2d.flip_h = velocity.x < 0
|
||||
navigation_agent_2d.velocity = velocity
|
||||
else:
|
||||
character.velocity = velocity
|
||||
character.move_and_slide()
|
||||
|
||||
|
||||
|
||||
func _on_next_transitions() -> void:
|
||||
if character.current_walk_cycle == character.walk_cycles:
|
||||
character.velocity = Vector2.ZERO
|
||||
transition.emit("Idle")
|
||||
|
||||
|
||||
func _on_enter() -> void:
|
||||
animated_sprite_2d.play("walk")
|
||||
character.current_walk_cycle = 0
|
||||
|
||||
|
||||
func _on_exit() -> void:
|
||||
animated_sprite_2d.stop()
|
1
scripts/state_machine/npc_states/walk_state.gd.uid
Normal file
1
scripts/state_machine/npc_states/walk_state.gd.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://ciwbskg1hsree
|
Reference in New Issue
Block a user