Starting system

This commit is contained in:
Rendo 2025-11-28 19:39:31 +05:00
commit 57c178716e
10 changed files with 63 additions and 49 deletions

View file

@ -12,13 +12,12 @@ func on_body_entered(body: Node3D):
if body is DroppableWeapon:
if weapon_system.can_add(body.slot) == false:
return
var weapon = weapon_spawner.spawn({
weapon_spawner.spawn({
"ammo": body.weapon.ammo,
"remaining_ammo": body.weapon.remaining_ammo,
"scene_file_path": body.weapon.scene_file_path,
"slot": body.slot
})
weapon_system.on_weapon_added(weapon)
Session.despawn(body.get_path())

View file

@ -0,0 +1,14 @@
extends Node
@export var starting_pistol: StringName
@export var starting_knife: StringName
@export var weapon_spawner: MultiplayerSpawner
func _ready() -> void:
deferred_ready.call_deferred()
func deferred_ready() -> void:
if is_multiplayer_authority():
weapon_spawner.spawn({"scene_file_path": starting_pistol})
weapon_spawner.spawn({"scene_file_path": starting_knife})
queue_free()

View file

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

View file

@ -13,7 +13,7 @@ class_name WeaponSubStateMachine
@export var can_be_previous: bool = true
@export var destroy_when_empty: bool = false
var slot: StringName
@export var slot: StringName
signal request_return

View file

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