State machine rework

This commit is contained in:
Rendo 2025-12-09 22:34:17 +05:00
commit 87919ed890
25 changed files with 102 additions and 76 deletions

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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)

View file

@ -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:

View file

@ -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:

View file

@ -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:

View file

@ -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()

View file

@ -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()

View file

@ -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)

View file

@ -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")

View file

@ -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):

View file

@ -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

View file

@ -2,18 +2,19 @@ extends WeaponState
@export var emptyable: bool
func enter() -> void:
func _enter() -> void:
machine.animation_player.play(with_morphems("idle"))
if is_multiplayer_authority():
machine.player.get_node("PlayerInput").reload.connect(init_reload)
func exit() -> void:
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

View file

@ -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):

View file

@ -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):

View file

@ -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()

View file

@ -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

View file

@ -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:

View file

@ -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")

View file

@ -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):

View file

@ -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")

View file

@ -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):

View file

@ -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):