From 87919ed890126b311374340f49f2e3b694267290 Mon Sep 17 00:00:00 2001 From: Rendo Date: Tue, 9 Dec 2025 22:34:17 +0500 Subject: [PATCH] State machine rework --- players/player/states/crouching.gd | 4 +-- players/player/states/death.gd | 4 +-- players/player/states/falling.gd | 4 +-- players/player/states/standing.gd | 4 +-- players/player/states/walk.gd | 4 +-- systems/state_machine/machine.gd | 14 --------- systems/state_machine/state.gd | 17 ++++++++-- systems/state_machine/substate_machine.gd | 7 ++--- systems/weapon_system/weapon_state.gd | 31 +++++++++++++++++-- .../weapon_system/weapon_substate_machine.gd | 4 +-- systems/weapon_system/weapon_system.gd | 6 ++-- weapons/bomb/bomb_idle_state.gd | 6 ++-- weapons/bomb/bomb_intro_state.gd | 4 +-- weapons/bomb/bomb_main_state.gd | 4 +-- weapons/gun/idle_state.gd | 13 ++++---- weapons/gun/intro_state.gd | 4 +-- weapons/gun/reload_state.gd | 4 +-- weapons/gun/semi_auto_shoot_state.gd | 6 ++-- weapons/knife/knife_attack.gd | 8 ++--- weapons/knife/knife_attack_heavy.gd | 4 +-- weapons/knife/knife_idle.gd | 8 ++--- weapons/knife/knife_intro.gd | 4 +-- weapons/molikman/molik/idle_state.gd | 6 ++-- weapons/molikman/molik/intro_state.gd | 4 +-- weapons/molikman/molik/throw.gd | 4 +-- 25 files changed, 102 insertions(+), 76 deletions(-) diff --git a/players/player/states/crouching.gd b/players/player/states/crouching.gd index 5783cfd..b161a81 100644 --- a/players/player/states/crouching.gd +++ b/players/player/states/crouching.gd @@ -13,11 +13,11 @@ extends State @export var crouch_time: float = 0.1 @export var weapon_system: WeaponSystem -func enter() -> void: +func _enter() -> void: animation_player.play("crouch",-1,1/crouch_time) player_input.crouch_end.connect(try_end_crouch) -func exit() -> void: +func _exit() -> void: animation_player.play("crouch",-1,-1/crouch_time,true) player_input.crouch_end.disconnect(try_end_crouch) diff --git a/players/player/states/death.gd b/players/player/states/death.gd index 68a4a57..61f1a64 100644 --- a/players/player/states/death.gd +++ b/players/player/states/death.gd @@ -5,8 +5,8 @@ extends State func on_death() -> void: transition.emit("Death") -func enter() -> void: +func _enter() -> void: animation_player.play("die") -func exit() -> void: +func _exit() -> void: pass diff --git a/players/player/states/falling.gd b/players/player/states/falling.gd index 2b7750a..5adb1c2 100644 --- a/players/player/states/falling.gd +++ b/players/player/states/falling.gd @@ -8,10 +8,10 @@ extends State @export var weapon_system: WeaponSystem @export var land_sound: MultiplayerAudio3D -func enter() -> void: +func _enter() -> void: pass -func exit() -> void: +func _exit() -> void: pass diff --git a/players/player/states/standing.gd b/players/player/states/standing.gd index 31151a0..ef7a53f 100644 --- a/players/player/states/standing.gd +++ b/players/player/states/standing.gd @@ -13,12 +13,12 @@ extends State var step_time: float -func enter() -> void: +func _enter() -> void: player_input.jumped.connect(on_jumped) player_input.crouch_begin.connect(begin_crouch) player_input.walk_begin.connect(begin_walk) -func exit() -> void: +func _exit() -> void: player_input.jumped.disconnect(on_jumped) player_input.crouch_begin.disconnect(begin_crouch) player_input.walk_begin.disconnect(begin_walk) diff --git a/players/player/states/walk.gd b/players/player/states/walk.gd index 8d31be5..4396220 100644 --- a/players/player/states/walk.gd +++ b/players/player/states/walk.gd @@ -9,12 +9,12 @@ extends State @export var player_input: PlayerInput @export var weapon_system: WeaponSystem -func enter() -> void: +func _enter() -> void: player_input.crouch_begin.connect(begin_crouch) player_input.walk_end.connect(end_walk) player_input.jumped.connect(on_jumped) -func exit() -> void: +func _exit() -> void: player_input.crouch_begin.disconnect(begin_crouch) player_input.walk_end.disconnect(end_walk) player_input.jumped.disconnect(on_jumped) diff --git a/systems/state_machine/machine.gd b/systems/state_machine/machine.gd index bfcd0dd..eb4a8d1 100644 --- a/systems/state_machine/machine.gd +++ b/systems/state_machine/machine.gd @@ -23,7 +23,6 @@ func on_transition_required(to: StringName): return change_state(states[to]) - change_state_to_name.rpc(to) func change_state(to_state: State) -> void: if current_state != null: @@ -31,19 +30,6 @@ func change_state(to_state: State) -> void: current_state = to_state current_state.enter() -@rpc("authority","call_local","reliable") -func change_state_to_name(to_name: StringName): - if current_state != null: - current_state.exit() - current_state = states[to_name] - current_state.enter() - -@rpc("authority","call_local","unreliable") -func clear_state(): - if current_state == null: - return - current_state.exit() - current_state = null func _process(delta: float) -> void: if current_state == null: diff --git a/systems/state_machine/state.gd b/systems/state_machine/state.gd index 7e293a5..2b60dba 100644 --- a/systems/state_machine/state.gd +++ b/systems/state_machine/state.gd @@ -7,8 +7,21 @@ class_name State signal transition(to: StringName) -@abstract func enter() -> void -@abstract func exit() -> void +@abstract func _enter() -> void +@abstract func _exit() -> void + +@rpc("authority","call_remote","reliable") +func enter(): + _enter() + if is_multiplayer_authority(): + enter.rpc() + +@rpc("authority","call_remote","reliable") +func exit(): + _exit() + if is_multiplayer_authority(): + exit.rpc() + func update(delta: float) -> void: pass func physics_update(delta: float) -> void: diff --git a/systems/state_machine/substate_machine.gd b/systems/state_machine/substate_machine.gd index 5e29246..cc293ec 100644 --- a/systems/state_machine/substate_machine.gd +++ b/systems/state_machine/substate_machine.gd @@ -4,12 +4,11 @@ class_name SubStateMachine @export var enter_state: State -func enter() -> void: +func _enter() -> void: change_state(enter_state) -func exit() -> void: - if is_multiplayer_authority(): - clear_state.rpc() +func _exit() -> void: + current_state.exit() func update(delta: float) -> void: if current_state == null: diff --git a/systems/weapon_system/weapon_state.gd b/systems/weapon_system/weapon_state.gd index b00dc16..ce25367 100644 --- a/systems/weapon_system/weapon_state.gd +++ b/systems/weapon_system/weapon_state.gd @@ -8,13 +8,40 @@ signal return_to_previous var machine: WeaponSubStateMachine +func _use_begin() -> void: + pass + +@rpc("authority","call_remote","reliable") func use_begin() -> void: + _use_begin() + if is_multiplayer_authority(): + use_begin.rpc() + +func _use_end(): pass + +@rpc("authority","call_remote","reliable") func use_end() -> void: + _use_end() + if is_multiplayer_authority(): + use_end.rpc() + +func _alternate_state() -> void: pass + +@rpc("authority","call_remote","reliable") func alternate_state() -> void: - pass + _alternate_state() + if is_multiplayer_authority(): + alternate_state.rpc() # Need to clarify naming; Switch mode like firemode. For different states use # alternate_state -func switch_mode() -> void: + +func _switch_mode(): pass + +@rpc("authority","call_remote","reliable") +func switch_mode() -> void: + _switch_mode() + if is_multiplayer_authority(): + switch_mode.rpc() diff --git a/systems/weapon_system/weapon_substate_machine.gd b/systems/weapon_system/weapon_substate_machine.gd index d143526..ea22a54 100644 --- a/systems/weapon_system/weapon_substate_machine.gd +++ b/systems/weapon_system/weapon_substate_machine.gd @@ -64,11 +64,11 @@ func _ready() -> void: ammo_depleted.connect(system.check_for_empty) ammo_updated.connect(system.on_ammo_updated) -func enter() -> void: +func _enter() -> void: super() player.weapon_models[visibility_target].show() -func exit() -> void: +func _exit() -> void: super() player.weapon_models[visibility_target].hide() diff --git a/systems/weapon_system/weapon_system.gd b/systems/weapon_system/weapon_system.gd index 850acb8..4837fa1 100644 --- a/systems/weapon_system/weapon_system.gd +++ b/systems/weapon_system/weapon_system.gd @@ -56,7 +56,7 @@ func add(state: WeaponSubStateMachine, slot: StringName) -> void: if current_state == null: current_state = state ammo_updated.emit(current_state.ammo,current_state.remaining_ammo) - state.enter.call_deferred() + state._enter.call_deferred() func process_spawned_weapon(weapon_node: Node): @@ -83,13 +83,13 @@ func switch(to: StringName, exit: bool = true): if slots.has(to) == false or slots[to] == null or slots[to] == current_state or (multiplayer.get_remote_sender_id() != 1 and is_multiplayer_authority() == false): return if current_state != null and exit: - current_state.exit() + current_state._exit() if current_state.can_be_previous: last_slot = slots.find_key(current_state) else: last_slot = "" current_state = slots[to] - current_state.enter() + current_state._enter() ammo_updated.emit(current_state.ammo,current_state.remaining_ammo) switched_to.emit(current_state) diff --git a/weapons/bomb/bomb_idle_state.gd b/weapons/bomb/bomb_idle_state.gd index 2761471..08a3ed9 100644 --- a/weapons/bomb/bomb_idle_state.gd +++ b/weapons/bomb/bomb_idle_state.gd @@ -1,12 +1,12 @@ extends WeaponState -func enter() -> void: +func _enter() -> void: machine.animation_player.play(machine.animation_prefix+"idle") -func exit() -> void: +func _exit() -> void: pass -func use_begin() -> void: +func _use_begin() -> void: if Session.is_on_site(machine.player.player_id): transition.emit("Plant") diff --git a/weapons/bomb/bomb_intro_state.gd b/weapons/bomb/bomb_intro_state.gd index 4e48af7..66f4813 100644 --- a/weapons/bomb/bomb_intro_state.gd +++ b/weapons/bomb/bomb_intro_state.gd @@ -1,10 +1,10 @@ extends WeaponState -func enter() -> void: +func _enter() -> void: machine.animation_player.play(machine.animation_prefix+"intro") machine.animation_player.animation_finished.connect(on_animation_finished) -func exit() -> void: +func _exit() -> void: machine.animation_player.animation_finished.disconnect(on_animation_finished) func on_animation_finished(animation: StringName): diff --git a/weapons/bomb/bomb_main_state.gd b/weapons/bomb/bomb_main_state.gd index f851dd8..86e6a2a 100644 --- a/weapons/bomb/bomb_main_state.gd +++ b/weapons/bomb/bomb_main_state.gd @@ -2,14 +2,14 @@ extends WeaponState @export var bomb_scene: PackedScene -func enter(): +func _enter(): machine.animation_player.play(machine.animation_prefix+"plant") machine.animation_player.animation_finished.connect(on_animation_finished) if is_multiplayer_authority(): machine.speed_modifier = 0.0 machine.player.get_node("PlantAudio").multiplayer_play() -func exit(): +func _exit(): machine.animation_player.animation_finished.disconnect(on_animation_finished) machine.speed_modifier = 1.0 diff --git a/weapons/gun/idle_state.gd b/weapons/gun/idle_state.gd index 821a3dd..1ff2028 100644 --- a/weapons/gun/idle_state.gd +++ b/weapons/gun/idle_state.gd @@ -2,18 +2,19 @@ extends WeaponState @export var emptyable: bool -func enter() -> void: +func _enter() -> void: machine.animation_player.play(with_morphems("idle")) - machine.player.get_node("PlayerInput").reload.connect(init_reload) + if is_multiplayer_authority(): + machine.player.get_node("PlayerInput").reload.connect(init_reload) -func exit() -> void: - machine.player.get_node("PlayerInput").reload.disconnect(init_reload) +func _exit() -> void: + if is_multiplayer_authority(): + machine.player.get_node("PlayerInput").reload.disconnect(init_reload) -func use_begin() -> void: +func _use_begin() -> void: if machine.ammo > 0: transition.emit("Shoot") -@rpc("authority","call_local","reliable") func init_reload(): if machine.ammo == machine.max_ammo or machine.remaining_ammo <= 0: return diff --git a/weapons/gun/intro_state.gd b/weapons/gun/intro_state.gd index 2d41843..26f7768 100644 --- a/weapons/gun/intro_state.gd +++ b/weapons/gun/intro_state.gd @@ -2,11 +2,11 @@ extends WeaponState @export var emptyable: bool -func enter() -> void: +func _enter() -> void: machine.animation_player.play(with_morphems("intro")) machine.animation_player.animation_finished.connect(on_animation_finished) -func exit() -> void: +func _exit() -> void: machine.animation_player.animation_finished.disconnect(on_animation_finished) func on_animation_finished(animation): diff --git a/weapons/gun/reload_state.gd b/weapons/gun/reload_state.gd index b20de2f..36fcb99 100644 --- a/weapons/gun/reload_state.gd +++ b/weapons/gun/reload_state.gd @@ -2,11 +2,11 @@ extends WeaponState @export var emptyable: bool -func enter() -> void: +func _enter() -> void: machine.animation_player.play(with_morphems("reload")) machine.animation_player.animation_finished.connect(on_animation_finished) -func exit() -> void: +func _exit() -> void: machine.animation_player.animation_finished.disconnect(on_animation_finished) func on_animation_finished(animation): diff --git a/weapons/gun/semi_auto_shoot_state.gd b/weapons/gun/semi_auto_shoot_state.gd index f603f64..0ad62af 100644 --- a/weapons/gun/semi_auto_shoot_state.gd +++ b/weapons/gun/semi_auto_shoot_state.gd @@ -11,11 +11,11 @@ extends WeaponState var bullets_shot: int = 0 -func enter() -> void: +func _enter() -> void: fire() machine.animation_player.animation_finished.connect(on_animation_finished) -func exit() -> void: +func _exit() -> void: bullets_shot = 0 machine.animation_player.animation_finished.disconnect(on_animation_finished) @@ -23,7 +23,7 @@ func on_animation_finished(animation): if animation == with_morphems("shoot"): transition.emit("Idle") -func use_begin() -> void: +func _use_begin() -> void: if fire_timer.time_left > 0: return fire() diff --git a/weapons/knife/knife_attack.gd b/weapons/knife/knife_attack.gd index 57285f6..1c3df29 100644 --- a/weapons/knife/knife_attack.gd +++ b/weapons/knife/knife_attack.gd @@ -3,12 +3,12 @@ extends WeaponState @export var damage: int var end_it: bool = true -func enter() -> void: +func _enter() -> void: machine.animation_player.play(machine.animation_prefix + "attack") attack() machine.animation_player.animation_finished.connect(on_animation_finished) -func exit() -> void: +func _exit() -> void: machine.animation_player.animation_finished.disconnect(on_animation_finished) end_it = false @@ -24,8 +24,8 @@ func on_animation_finished(animation): attack() machine.animation_player.play(machine.animation_prefix + "attack") -func use_begin() -> void: +func _use_begin() -> void: end_it = false -func use_end() -> void: +func _use_end() -> void: end_it = true diff --git a/weapons/knife/knife_attack_heavy.gd b/weapons/knife/knife_attack_heavy.gd index 2b5a557..94def1b 100644 --- a/weapons/knife/knife_attack_heavy.gd +++ b/weapons/knife/knife_attack_heavy.gd @@ -2,12 +2,12 @@ extends WeaponState @export var damage: int -func enter() -> void: +func _enter() -> void: machine.animation_player.play(machine.animation_prefix + "heavy_attack") machine.animation_player.animation_finished.connect(on_animation_finished) attack() -func exit() -> void: +func _exit() -> void: machine.animation_player.animation_finished.disconnect(on_animation_finished) func attack() -> void: diff --git a/weapons/knife/knife_idle.gd b/weapons/knife/knife_idle.gd index f33893d..16d1b2c 100644 --- a/weapons/knife/knife_idle.gd +++ b/weapons/knife/knife_idle.gd @@ -1,13 +1,13 @@ extends WeaponState -func enter() -> void: +func _enter() -> void: machine.animation_player.play(machine.animation_prefix + "idle") -func exit() -> void: +func _exit() -> void: pass -func use_begin() -> void: +func _use_begin() -> void: transition.emit("Attack") -func alternate_state() -> void: +func _alternate_state() -> void: transition.emit("HeavyAttack") diff --git a/weapons/knife/knife_intro.gd b/weapons/knife/knife_intro.gd index aa9f7d8..7579ea0 100644 --- a/weapons/knife/knife_intro.gd +++ b/weapons/knife/knife_intro.gd @@ -1,10 +1,10 @@ extends WeaponState -func enter() -> void: +func _enter() -> void: machine.animation_player.play(machine.animation_prefix + "intro") machine.animation_player.animation_finished.connect(on_animation_finished) -func exit() -> void: +func _exit() -> void: machine.animation_player.animation_finished.disconnect(on_animation_finished) func on_animation_finished(animation): diff --git a/weapons/molikman/molik/idle_state.gd b/weapons/molikman/molik/idle_state.gd index 19fae68..3caf4a5 100644 --- a/weapons/molikman/molik/idle_state.gd +++ b/weapons/molikman/molik/idle_state.gd @@ -1,11 +1,11 @@ extends WeaponState -func enter() -> void: +func _enter() -> void: machine.animation_player.play(machine.animation_prefix +"idle") -func exit() -> void: +func _exit() -> void: pass -func use_begin() -> void: +func _use_begin() -> void: if machine.ammo > 0: transition.emit("Throw") diff --git a/weapons/molikman/molik/intro_state.gd b/weapons/molikman/molik/intro_state.gd index 7853ec8..80177fb 100644 --- a/weapons/molikman/molik/intro_state.gd +++ b/weapons/molikman/molik/intro_state.gd @@ -2,11 +2,11 @@ extends WeaponState @export var emptyable: bool -func enter() -> void: +func _enter() -> void: machine.animation_player.play(machine.animation_prefix + "intro") machine.animation_player.animation_finished.connect(on_animation_finished) -func exit() -> void: +func _exit() -> void: machine.animation_player.animation_finished.disconnect(on_animation_finished) func on_animation_finished(animation): diff --git a/weapons/molikman/molik/throw.gd b/weapons/molikman/molik/throw.gd index 71cca99..b959d78 100644 --- a/weapons/molikman/molik/throw.gd +++ b/weapons/molikman/molik/throw.gd @@ -2,11 +2,11 @@ extends WeaponState const molik: PackedScene = preload("uid://b6qahd6q60js7") -func enter() -> void: +func _enter() -> void: fire() machine.animation_player.animation_finished.connect(on_animation_finished) -func exit() -> void: +func _exit() -> void: machine.animation_player.animation_finished.disconnect(on_animation_finished) func on_animation_finished(animation):