Starting system
This commit is contained in:
parent
aa1c1c8d64
commit
57c178716e
10 changed files with 63 additions and 49 deletions
|
|
@ -2,9 +2,6 @@ extends Node
|
|||
|
||||
class_name WeaponSystem
|
||||
|
||||
@export var default_pistol: WeaponSubStateMachine
|
||||
@export var default_knife: WeaponSubStateMachine
|
||||
|
||||
@export var animation_player: AnimationPlayer
|
||||
@export var camera: PlayerCamera
|
||||
@export var player: Player
|
||||
|
|
@ -26,27 +23,23 @@ var slots: Dictionary[StringName,WeaponSubStateMachine] = {
|
|||
signal switched_to(state: WeaponSubStateMachine)
|
||||
|
||||
func _ready() -> void:
|
||||
current_state = default_pistol
|
||||
add(default_pistol,"secondary")
|
||||
add(default_knife,"knife")
|
||||
current_state.enter()
|
||||
$WeaponSpawner.spawn_function = pick_up_weapon
|
||||
$WeaponSpawner.spawned.connect(on_weapon_added)
|
||||
|
||||
func can_add(slot: StringName) -> bool:
|
||||
return slots.has(slot) and slots[slot] == null
|
||||
|
||||
@rpc("call_local","reliable")
|
||||
func add(state: WeaponSubStateMachine, slot: StringName) -> void:
|
||||
func add(state: WeaponSubStateMachine, slot: StringName,ignore_parent: bool = false) -> void:
|
||||
if can_add(slot) == false:
|
||||
return
|
||||
|
||||
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()
|
||||
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()
|
||||
|
||||
slots[slot] = state
|
||||
state.system = self
|
||||
|
|
@ -54,6 +47,10 @@ func add(state: WeaponSubStateMachine, slot: StringName) -> void:
|
|||
state.player_camera = camera
|
||||
state.player = player
|
||||
state.request_return.connect(return_to_previous)
|
||||
|
||||
if current_state == null:
|
||||
current_state = state
|
||||
state.enter.call_deferred()
|
||||
|
||||
func switch(to: StringName, exit: bool = true):
|
||||
if slots.has(to) == false or slots[to] == null or slots[to] == current_state or is_multiplayer_authority() == false:
|
||||
|
|
@ -86,6 +83,7 @@ func drop():
|
|||
|
||||
$"../PickupRange".start_temp_ignore()
|
||||
|
||||
slots[slots.find_key(current_state)] = null
|
||||
current_state.queue_free()
|
||||
return_to_previous(false)
|
||||
|
||||
|
|
@ -95,16 +93,23 @@ func drop():
|
|||
# remaining_ammo
|
||||
# scene_file_path
|
||||
func pick_up_weapon(data: Variant) -> Node:
|
||||
if data.has("ammo") == false or data.has("remaining_ammo") == false or data.has("scene_file_path") == false or data.has("slot") == false:
|
||||
return null
|
||||
var scene: WeaponSubStateMachine = load(data["scene_file_path"]).instantiate()
|
||||
scene.ammo = data["ammo"]
|
||||
scene.remaining_ammo = data["remaining_ammo"]
|
||||
scene.slot = data["slot"]
|
||||
return scene
|
||||
|
||||
func on_weapon_added(weapon: Node):
|
||||
add(weapon,weapon.slot)
|
||||
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"]
|
||||
|
||||
add(scene,scene.slot,true)
|
||||
|
||||
return scene
|
||||
else:
|
||||
var scene: WeaponSubStateMachine = load(data["scene_file_path"]).instantiate()
|
||||
|
||||
add(scene,scene.slot,true)
|
||||
|
||||
return scene
|
||||
|
||||
func return_to_previous(exit: bool = true):
|
||||
if last_slot != "":
|
||||
|
|
@ -123,13 +128,11 @@ func update_remotes(to: StringName):
|
|||
|
||||
func _process(delta: float) -> void:
|
||||
if current_state == null:
|
||||
push_error("State is not set")
|
||||
return
|
||||
current_state.update(delta)
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
if current_state == null:
|
||||
push_error("State is not set")
|
||||
return
|
||||
current_state.physics_update(delta)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue