Global refactor
This commit is contained in:
parent
3868af29e3
commit
0589ca4e23
180 changed files with 249 additions and 401 deletions
9
systems/weapon_system/dropped_weapon.gd
Normal file
9
systems/weapon_system/dropped_weapon.gd
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
extends RigidBody3D
|
||||
|
||||
class_name DroppableWeapon
|
||||
|
||||
const IMPULSE = 10
|
||||
|
||||
@export var slot: StringName
|
||||
@export var weapon: WeaponSubStateMachine
|
||||
@export var team: Session.TEAMS
|
||||
1
systems/weapon_system/dropped_weapon.gd.uid
Normal file
1
systems/weapon_system/dropped_weapon.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://cskgqgkr7pmb0
|
||||
14
systems/weapon_system/starting_weapon_spawner.gd
Normal file
14
systems/weapon_system/starting_weapon_spawner.gd
Normal 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()
|
||||
1
systems/weapon_system/starting_weapon_spawner.gd.uid
Normal file
1
systems/weapon_system/starting_weapon_spawner.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://doevvmbvhlig8
|
||||
9
systems/weapon_system/weapon_resource.gd
Normal file
9
systems/weapon_system/weapon_resource.gd
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
extends Resource
|
||||
|
||||
class_name WeaponResource
|
||||
|
||||
@export var cost: int
|
||||
@export var preview: Texture2D
|
||||
@export var dropped_scene: PackedScene
|
||||
@export var weapon_system_scene: PackedScene
|
||||
@export var slot: StringName
|
||||
1
systems/weapon_system/weapon_resource.gd.uid
Normal file
1
systems/weapon_system/weapon_resource.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://bvnn2eiwqbu7t
|
||||
20
systems/weapon_system/weapon_state.gd
Normal file
20
systems/weapon_system/weapon_state.gd
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
@abstract
|
||||
extends State
|
||||
|
||||
class_name WeaponState
|
||||
|
||||
@warning_ignore("unused_signal")
|
||||
signal return_to_previous
|
||||
|
||||
var machine: WeaponSubStateMachine
|
||||
|
||||
func use_begin() -> void:
|
||||
pass
|
||||
func use_end() -> void:
|
||||
pass
|
||||
func alternate_state() -> void:
|
||||
pass
|
||||
# Need to clarify naming; Switch mode like firemode. For different states use
|
||||
# alternate_state
|
||||
func switch_mode() -> void:
|
||||
pass
|
||||
1
systems/weapon_system/weapon_state.gd.uid
Normal file
1
systems/weapon_system/weapon_state.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://fd4tm5wexdk
|
||||
80
systems/weapon_system/weapon_substate_machine.gd
Normal file
80
systems/weapon_system/weapon_substate_machine.gd
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
extends SubStateMachine
|
||||
|
||||
class_name WeaponSubStateMachine
|
||||
|
||||
@export var animation_prefix: StringName
|
||||
@export var registry_entry: StringName
|
||||
@export var visibility_target: StringName
|
||||
|
||||
@export var max_ammo: int
|
||||
@export var ammo: int = -1:
|
||||
set(value):
|
||||
if value < 0:
|
||||
ammo = 0
|
||||
else:
|
||||
ammo = value
|
||||
ammo_updated.emit()
|
||||
if ammo <= 0 and remaining_ammo <= 0:
|
||||
ammo_depleted.emit()
|
||||
get:
|
||||
return ammo
|
||||
@export var ammo_mags: int = 3
|
||||
@export var remaining_ammo: int = -1:
|
||||
set(value):
|
||||
if value < 0:
|
||||
remaining_ammo = 0
|
||||
else:
|
||||
remaining_ammo = value
|
||||
|
||||
@export var speed_modifier: float = 1.0
|
||||
@export var can_be_previous: bool = true
|
||||
@export var destroy_when_empty: bool = false
|
||||
|
||||
@export var slot: StringName
|
||||
|
||||
signal request_return
|
||||
signal ammo_updated
|
||||
signal ammo_depleted
|
||||
|
||||
var system: WeaponSystem
|
||||
var animation_player: AnimationPlayer
|
||||
var player_camera: PlayerCamera
|
||||
var player: Player
|
||||
|
||||
func _ready() -> void:
|
||||
if remaining_ammo == -1:
|
||||
remaining_ammo = max_ammo * ammo_mags
|
||||
if ammo == -1:
|
||||
ammo = max_ammo
|
||||
for child in get_children():
|
||||
if child is WeaponState:
|
||||
states[child.name] = child
|
||||
child.machine = self
|
||||
child.transition.connect(on_transition_required)
|
||||
child.return_to_previous.connect(request_return.emit)
|
||||
|
||||
func enter() -> void:
|
||||
super()
|
||||
player.weapon_models[visibility_target].show()
|
||||
|
||||
func exit() -> void:
|
||||
super()
|
||||
player.weapon_models[visibility_target].hide()
|
||||
|
||||
@rpc("authority","call_local","reliable")
|
||||
func use_begin() -> void:
|
||||
if current_state != null:
|
||||
current_state.use_begin()
|
||||
|
||||
@rpc("authority","call_local","reliable")
|
||||
func use_end() -> void:
|
||||
if current_state != null:
|
||||
current_state.use_end()
|
||||
func alternate_state() -> void:
|
||||
if current_state != null:
|
||||
current_state.alternate_state()
|
||||
# Need to clarify naming; Switch mode like firemode. For different states use
|
||||
# alternate_state
|
||||
func switch_mode() -> void:
|
||||
if current_state != null:
|
||||
current_state.switch_mode()
|
||||
1
systems/weapon_system/weapon_substate_machine.gd.uid
Normal file
1
systems/weapon_system/weapon_substate_machine.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://e6lqknfl4ngt
|
||||
186
systems/weapon_system/weapon_system.gd
Normal file
186
systems/weapon_system/weapon_system.gd
Normal file
|
|
@ -0,0 +1,186 @@
|
|||
extends Node
|
||||
|
||||
class_name WeaponSystem
|
||||
|
||||
@export var animation_player: AnimationPlayer
|
||||
@export var camera: PlayerCamera
|
||||
@export var player: Player
|
||||
@export var player_input: PlayerInput
|
||||
|
||||
var current_state: WeaponSubStateMachine
|
||||
var last_slot: StringName
|
||||
var disabled: bool
|
||||
|
||||
var slots: Dictionary[StringName,WeaponSubStateMachine] = {
|
||||
"primary": null,
|
||||
"secondary": null,
|
||||
"knife": null,
|
||||
"bomb": null,
|
||||
"ability_first": null,
|
||||
"ability_second": null,
|
||||
"ability_third": null,
|
||||
"ultimate": null
|
||||
}
|
||||
|
||||
signal switched_to(state: WeaponSubStateMachine)
|
||||
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)
|
||||
player_input.switch_weapon.connect(switch)
|
||||
player_input.alternate_state.connect(alternate_state)
|
||||
player_input.switch_firemode.connect(switch_mode)
|
||||
|
||||
func get_speed_modifier() -> float:
|
||||
if current_state == null:
|
||||
return 1
|
||||
return current_state.speed_modifier
|
||||
|
||||
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:
|
||||
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()
|
||||
|
||||
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()
|
||||
|
||||
@rpc("authority","call_remote","reliable")
|
||||
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()
|
||||
if current_state.can_be_previous:
|
||||
last_slot = slots.find_key(current_state)
|
||||
else:
|
||||
last_slot = ""
|
||||
current_state = slots[to]
|
||||
current_state.enter()
|
||||
|
||||
ammo_updated.emit(current_state.ammo,current_state.remaining_ammo)
|
||||
switched_to.emit(current_state)
|
||||
if is_multiplayer_authority():
|
||||
switch.rpc(to,exit)
|
||||
|
||||
func return_to_previous(exit: bool = true):
|
||||
if last_slot != "":
|
||||
switch(last_slot, exit)
|
||||
else:
|
||||
switch("knife", exit)
|
||||
|
||||
func drop_current():
|
||||
drop(current_state)
|
||||
|
||||
func drop(weapon: WeaponSubStateMachine) -> void:
|
||||
if not is_multiplayer_authority():
|
||||
return
|
||||
if slots.find_key(weapon) == "knife":
|
||||
return
|
||||
|
||||
var dropped_weapon: DroppableWeapon = Registry.weapons[weapon.registry_entry].dropped_scene.instantiate()
|
||||
dropped_weapon.weapon.ammo = weapon.ammo
|
||||
dropped_weapon.weapon.remaining_ammo = weapon.remaining_ammo
|
||||
dropped_weapon.weapon.slot = weapon.slot
|
||||
Session.dynamic_objects_parent.add_child(dropped_weapon)
|
||||
dropped_weapon.global_position = camera.global_position
|
||||
dropped_weapon.apply_central_impulse(-camera.global_basis.z * 10 + player.velocity)
|
||||
|
||||
$"../PickupRange".start_temp_ignore()
|
||||
|
||||
slots[slots.find_key(weapon)] = null
|
||||
slots_updated.emit(slots)
|
||||
weapon.queue_free()
|
||||
return_to_previous(false)
|
||||
|
||||
func drop_slot(slot: StringName):
|
||||
if slots.has(slot) == false or slots[slot] == null:
|
||||
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
|
||||
for child in get_children():
|
||||
if child is WeaponSubStateMachine and child.ammo == 0 and child.remaining_ammo == 0 and child.destroy_when_empty:
|
||||
child.queue_free()
|
||||
|
||||
func on_ammo_updated() -> void:
|
||||
ammo_updated.emit(current_state.ammo,current_state.remaining_ammo)
|
||||
|
||||
func disable() -> void:
|
||||
disabled = true
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
if current_state == null or disabled:
|
||||
return
|
||||
current_state.update(delta)
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
if current_state == null or disabled:
|
||||
return
|
||||
current_state.physics_update(delta)
|
||||
|
||||
func use_begin() -> void:
|
||||
current_state.use_begin.rpc()
|
||||
|
||||
func use_end() -> void:
|
||||
current_state.use_end.rpc()
|
||||
|
||||
func alternate_state() -> void:
|
||||
current_state.alternate_state()
|
||||
|
||||
func switch_mode() -> void:
|
||||
current_state.switch_mode()
|
||||
1
systems/weapon_system/weapon_system.gd.uid
Normal file
1
systems/weapon_system/weapon_system.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://bmecgup3kcua7
|
||||
Loading…
Add table
Add a link
Reference in a new issue