added project with nerabochie cows
This commit is contained in:
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