godot-plateformer/_player/states/character_state_machine.gd

119 lines
3.9 KiB
GDScript3
Raw Permalink Normal View History

2025-12-29 11:54:31 +08:00
class_name Normal extends LimboHSM
@onready var ground_state : LimboHSM = %Grounded
@onready var idle_state: LimboState = %Idle
@onready var move_state: LimboState = %Move
@onready var airbone_state : LimboHSM = %Airbone
@onready var fall_state: LimboState = %Fall
@onready var jump_state: LimboState = %Jump
@onready var wall_jump_state: LimboState = %WallJump
@onready var on_wall_state: LimboHSM = %OnWall
@onready var climb_state: LimboState = %Climb
@onready var climb_jump_state: LimboState = %ClimbJump
@onready var dash_state: LimboState = %Dash
@onready var grap_hook_state: LimboHSM = %GrapHook
@onready var hook_shooting_state: LimboState = %HookShooting
2025-12-31 13:07:31 +08:00
@onready var grapping_state: LimboState = %Grapping
2025-12-29 11:54:31 +08:00
2026-01-10 17:36:16 +08:00
var _climb_cd_timer: float = 0
2025-12-29 11:54:31 +08:00
func _setup() -> void:
self._init_trans()
self._init_handler()
func _init_handler() -> void:
self.add_event_handler(&"trigger_dash",_handler_trigger_dash) ##处理Dash输入
2026-01-15 13:01:31 +08:00
self.add_event_handler(&"trigger_external_dash",_handler_trigger_external_dash) ##处理外部触发的Dash
2025-12-29 11:54:31 +08:00
self.add_event_handler(&"trigger_climb",_handler_trigger_climb) ##处理瞬时的climb
self.add_event_handler(&"trigger_grap_hook",_handler_grap_hook) ##处理grap hook 的輸入
func _init_trans() -> void:
self.add_transition(ground_state,airbone_state,ground_state.EVENT_FINISHED)
self.add_transition(airbone_state,ground_state,airbone_state.EVENT_FINISHED)
self.add_transition(ANYSTATE,dash_state,&"want_to_dash")
self.add_transition(dash_state,ground_state,&"exit_on_ground")
self.add_transition(dash_state,airbone_state,&"exit_on_air")
##用於處理貼在墻面的State流轉
2026-01-10 16:53:21 +08:00
self.add_transition(ANYSTATE,on_wall_state,&"want_to_on_wall",_can_on_wall)
2025-12-29 11:54:31 +08:00
self.add_transition(on_wall_state,airbone_state,&"exit_on_ground")
self.add_transition(on_wall_state,ground_state,&"exit_on_air")
##用於處理鈎爪的State流轉
2025-12-31 13:07:31 +08:00
self.add_transition(ANYSTATE,grap_hook_state,&"want_to_grap_hook",_has_move_input)
2025-12-29 11:54:31 +08:00
self.add_transition(grap_hook_state,airbone_state,&"exit_on_ground")
self.add_transition(grap_hook_state,ground_state,&"exit_on_air")
2026-01-05 15:28:43 +08:00
self.add_transition(grap_hook_state,airbone_state,&"hook_to_jump")
2025-12-31 13:07:31 +08:00
2025-12-29 11:54:31 +08:00
func _update(delta: float) -> void:
process_direction()
2026-01-10 17:36:16 +08:00
## climb状态的cd处理
if _climb_cd_timer > 0:
_climb_cd_timer -= delta
2025-12-29 11:54:31 +08:00
##如果玩家当前在墙上并且玩家按下了climb
if agent.wall_detector.is_on_wall and agent.m_climb_press:
self.dispatch(&"want_to_on_wall")
##处理Dash输入
func _handler_trigger_dash() -> bool:
#如果当前无法Dash则返回
if not agent.locomotion_comp.get_can_dash():
return false
2026-01-15 13:01:31 +08:00
2025-12-29 11:54:31 +08:00
if get_root().blackboard.get_var(&"is_dashing",false):
return false
2026-01-15 13:01:31 +08:00
self.dispatch(&"want_to_dash")
return true
##处理外部触发的Dash如道具触发
func _handler_trigger_external_dash() -> bool:
if get_root().blackboard.get_var(&"is_dashing",false):
return false
2025-12-29 11:54:31 +08:00
self.dispatch(&"want_to_dash")
return true
func _handler_trigger_climb() -> bool:
if not agent.is_on_wall():
return false
return true
func _handler_grap_hook() -> bool:
self.dispatch(&"want_to_grap_hook")
return true
func process_direction() -> void:
var b = get_root().blackboard.get_var(&"lock_direction_changed",false) as bool
if b: return
##切换方向的逻辑
var move_direction = Input.get_axis(&"move_left",&"move_right") as float
if move_direction * Player.get_direction_vector(agent.direction).x <= 0 and move_direction != 0:
agent.direction = Player.Direction.RIGHT if move_direction> 0 else Player.Direction.LEFT
func _get_dash_exit_enter_state()->LimboState:
if agent.is_on_floor():
return ground_state
else:
return airbone_state
2025-12-31 13:07:31 +08:00
func _has_move_input() -> bool:
return agent.get_move_input() != Vector2.ZERO
2026-01-10 16:53:21 +08:00
2026-01-10 17:36:16 +08:00
func _trigger_climb_cd(duration: float) -> void:
if duration > 0:
_climb_cd_timer = duration
2026-01-10 16:53:21 +08:00
func _can_on_wall() -> bool:
2026-01-10 17:36:16 +08:00
return not grap_hook_state.is_active() and _climb_cd_timer <= 0