Refactor Part II

This commit is contained in:
Rendo 2025-12-09 21:02:29 +05:00
commit 3f99f1b8dd
56 changed files with 193 additions and 168 deletions

View file

@ -27,7 +27,6 @@ signal slots_updated(slots: Dictionary[StringName,WeaponSubStateMachine])
signal ammo_updated(ammo: int, remaining_ammo: int)
func _ready() -> void:
$WeaponSpawner.spawn_function = pick_up_weapon
player_input.drop.connect(drop_current)
player_input.fire_begin.connect(use_begin)
player_input.fire_end.connect(use_end)
@ -43,33 +42,32 @@ func get_speed_modifier() -> float:
func can_add(slot: StringName) -> bool:
return slots.has(slot) and slots[slot] == null
@rpc("call_local","reliable")
func add(state: WeaponSubStateMachine, slot: StringName,ignore_parent: bool = false) -> void:
func add(state: WeaponSubStateMachine, slot: StringName) -> void:
if not multiplayer.is_server():
return
if can_add(slot) == false:
return
if ignore_parent == false:
if state.get_parent() == null:
add_child(state, true)
if state.get_parent() != self:
state.get_parent().remove_child(state)
add_child(state,true)
state.ready.emit()
add_child(state, true)
slots[slot] = state
state.system = self
state.animation_player = animation_player
state.player_camera = camera
state.player = player
state.request_return.connect(return_to_previous)
state.ammo_depleted.connect(check_for_empty)
state.ammo_updated.connect(on_ammo_updated)
slots_updated.emit(slots)
if current_state == null:
current_state = state
ammo_updated.emit(current_state.ammo,current_state.remaining_ammo)
state.enter.call_deferred()
func process_spawned_weapon(weapon_node: Node):
var weapon = weapon_node as WeaponSubStateMachine
slots[weapon.slot] = weapon
slots_updated.emit(slots)
if current_state == null:
current_state = weapon
ammo_updated.emit(current_state.ammo,current_state.remaining_ammo)
func get_empty_ability_slot() -> StringName:
if slots["ability_first"] == null:
@ -133,32 +131,6 @@ func drop_slot(slot: StringName):
return
drop(slots[slot])
# Spawn function
# Data should be a dictionary with these keys:
# ammo
# remaining_ammo
# scene_file_path
func pick_up_weapon(data: Variant) -> Node:
if data.has("scene_file_path") == false:
return Node.new()
if data.has_all(["ammo","remaining_ammo"]):
var scene: WeaponSubStateMachine = load(data["scene_file_path"]).instantiate()
scene.ammo = data["ammo"]
scene.remaining_ammo = data["remaining_ammo"]
scene.slot = data["slot"]
scene.set_multiplayer_authority(get_multiplayer_authority())
add(scene,scene.slot,true)
return scene
else:
var scene: WeaponSubStateMachine = load(data["scene_file_path"]).instantiate()
scene.set_multiplayer_authority(get_multiplayer_authority())
add(scene,scene.slot,true)
return scene
func check_for_empty() -> void:
if is_multiplayer_authority() == false:
return
@ -167,7 +139,8 @@ func check_for_empty() -> void:
child.queue_free()
func on_ammo_updated() -> void:
ammo_updated.emit(current_state.ammo,current_state.remaining_ammo)
if current_state != null:
ammo_updated.emit(current_state.ammo,current_state.remaining_ammo)
func disable() -> void:
disabled = true