added player Finite State Machine

This commit is contained in:
2025-07-19 22:08:07 +10:00
parent 4a26cf0ddb
commit d2dc74d533
39 changed files with 496 additions and 376 deletions

30
PCs/FSM/building_state.gd Normal file
View File

@@ -0,0 +1,30 @@
class_name BuildingState
extends HeroState
@export var swap_state: HeroState
func enter_state() -> void:
hero.edit_tool.enabled = true
hero.game_manager.level.enable_non_path_tower_frames()
func exit_state() -> void:
hero.edit_tool.interact_key_held = false
hero.edit_tool.enabled = false
hero.game_manager.level.disable_all_tower_frames()
func process_state(_delta: float) -> void:
hero.check_world_button()
if Input.is_action_just_pressed("Primary Fire"):
hero.edit_tool.interact_key_held = true
if Input.is_action_just_released("Primary Fire"):
hero.edit_tool.interact_key_held = false
if Input.is_action_just_pressed("Swap Weapons"):
state_changed.emit(swap_state)
if Input.is_action_pressed("Ready"):
if hero.ready_state:
hero.unready_self()
else:
hero.ready_self()

View File

@@ -0,0 +1 @@
uid://b5h7jfwkyokes

66
PCs/FSM/carding_state.gd Normal file
View File

@@ -0,0 +1,66 @@
class_name CardingState
extends HeroState
@export var swap_state: HeroState
func enter_state() -> void:
hero.set_card_elements_visibility(true)
hero.left_hand.visible = true
hero.carding_tool.enabled = true
func exit_state() -> void:
hero.set_card_elements_visibility(false)
hero.left_hand.visible = false
hero.carding_tool.enabled = false
func process_state(_delta: float) -> void:
hero.check_world_button()
if Input.is_action_just_pressed("Interact"):
hero.carding_tool.interact()
if Input.is_action_just_pressed("Primary Fire"):
hero.equip_weapon(0)
if Input.is_action_just_pressed("Secondary Fire"):
hero.equip_weapon(1)
if Input.is_action_just_pressed("Select Next Card") and hero.hand.size > 1:
hero.increment_selected()
hero.swap_card_audio.play()
if Input.is_action_just_pressed("Select Previous Card") and hero.hand.size > 1:
hero.decrement_selected()
hero.swap_card_audio.play()
if Input.is_action_just_pressed("Equip 1"):
swap_to_slot(1)
if Input.is_action_just_pressed("Equip 2"):
swap_to_slot(2)
if Input.is_action_just_pressed("Equip 3"):
swap_to_slot(3)
if Input.is_action_just_pressed("Equip 4"):
swap_to_slot(4)
if Input.is_action_just_pressed("Equip 5"):
swap_to_slot(5)
if Input.is_action_just_pressed("Equip 6"):
swap_to_slot(6)
if Input.is_action_just_pressed("Equip 7"):
swap_to_slot(7)
if Input.is_action_just_pressed("Equip 8"):
swap_to_slot(8)
if Input.is_action_just_pressed("Equip 9"):
swap_to_slot(9)
if Input.is_action_just_pressed("Equip 10"):
swap_to_slot(10)
if Input.is_action_just_pressed("Swap Weapons"):
state_changed.emit(swap_state)
if Input.is_action_pressed("Ready"):
if hero.ready_state:
hero.unready_self()
else:
hero.ready_self()
func swap_to_slot(num: int) -> void:
if hero.unique_cards.size() >= num:
hero.hand_selected_index = num - 1
hero.swap_card_audio.play()
hero.update_selected_box()

View File

@@ -0,0 +1 @@
uid://cjdv1onyfej0m

57
PCs/FSM/fighting_state.gd Normal file
View File

@@ -0,0 +1,57 @@
class_name FightingState
extends HeroState
func enter_state() -> void:
if hero.weapons[hero.equipped_weapon]:
hero.hud.set_energy_visible(true)
var offhand_weapon: Weapon = hero.weapons[0] if hero.equipped_weapon == 1 else hero.weapons[1]
if offhand_weapon:
offhand_weapon.current_energy = offhand_weapon.max_energy
if (!hero.weapons[hero.equipped_weapon] and offhand_weapon) or (hero.weapons[0] and hero.equipped_weapon == 1):
hero.swap_weapons()
if hero.weapons[hero.equipped_weapon]:
hero.weapons[hero.equipped_weapon].current_energy = hero.weapons[hero.equipped_weapon].max_energy
#this had to be commented out coz the new energy bar thinks "energy changed" is "energy used"
#weapons[equipped_weapon].energy_changed.emit(weapons[equipped_weapon].current_energy)
hero.weapon_swap_timer.start()
hero.hud.primary_duration.visible = false
hero.hud.secondary_duration.visible = false
hero.hud.energy_label.visible = false
func exit_state() -> void:
if hero.weapons[hero.equipped_weapon]:
hero.weapons[hero.equipped_weapon].release_trigger()
hero.weapons[hero.equipped_weapon].release_second_trigger()
hero.weapons[hero.equipped_weapon].visible = false
hero.hud.set_energy_visible(false)
hero.hud.grow_wave_start_label()
hero.hud.primary_duration.visible = true
hero.hud.secondary_duration.visible = true
hero.hud.energy_label.visible = true
func process_state(_delta: float) -> void:
if hero.weapons[hero.equipped_weapon] and hero.weapons_active:
if Input.is_action_just_pressed("Primary Fire"):
hero.weapons[hero.equipped_weapon].hold_trigger()
if Input.is_action_just_released("Primary Fire"):
hero.weapons[hero.equipped_weapon].release_trigger()
if Input.is_action_pressed("Secondary Fire"):
hero.weapons[hero.equipped_weapon].hold_second_trigger()
if Input.is_action_just_released("Secondary Fire"):
hero.weapons[hero.equipped_weapon].release_second_trigger()
if Input.is_action_pressed("Primary Fire"):
hero.movement.can_sprint = false
if Input.is_action_pressed("Secondary Fire"):
hero.movement.can_sprint = false
if Input.is_action_just_pressed("Equip Primary Weapon"):
if hero.equipped_weapon == 1 and hero.weapons[0]:
hero.swap_weapons()
if Input.is_action_just_pressed("Equip Secondary Weapon"):
if hero.equipped_weapon == 0 and hero.weapons[1]:
hero.swap_weapons()
if Input.is_action_just_pressed("Swap Weapons"):
if hero.weapons[0] and hero.weapons[1]:
hero.swap_weapons()

View File

@@ -0,0 +1 @@
uid://cf7m4yyiqhhru

20
PCs/FSM/hero_state.gd Normal file
View File

@@ -0,0 +1,20 @@
class_name HeroState
extends Node
@warning_ignore("unused_signal")
signal state_changed(new_state: HeroState)
@export var hero: Hero
func enter_state() -> void:
pass
func exit_state() -> void:
pass
@warning_ignore("unused_parameter")
func process_state(delta: float) -> void:
pass

View File

@@ -0,0 +1 @@
uid://cl8ovljfiagxv