Basic weapon slots system

This commit is contained in:
Alexey 2025-07-26 13:56:13 +03:00
commit 22a72e572e
7 changed files with 115 additions and 15 deletions

View file

@ -9,7 +9,7 @@ var queue: CommandQueue = CommandQueue.new()
@onready var camera: Camera3D = $"Camera"
@onready var weapon_player: AnimationPlayer = $"HUD/Weapon/AnimationPlayer"
@onready var weapons: Node3D = $"WeaponContainer"
@onready var weapons: WeaponContainer = $"WeaponContainer"
# Placeholder UI
@onready var ammo_label: Label = $"HUD/Ammo"
@ -20,19 +20,15 @@ func _ready() -> void:
queue.command_pushed.connect(on_queue_command_pushed)
queue.command_popped.connect(on_queue_command_popped)
current_weapon = weapons.get_child(0) as Weapon
current_weapon.fired.connect(on_weapon_fired)
current_weapon.fire_failed.connect(finish_task)
weapon_player.add_animation_library("current", current_weapon.animation_library)
weapon_player.play("current/static")
weapons.slot_selected.connect(on_weapon_slot_selected)
get_tree().create_timer(0.02).timeout.connect(on_weapon_slot_selected)
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
update_ammo_label()
func _process(_delta: float) -> void:
if current_weapon == null:
return
var weapon_sides = current_weapon.uses_hands
var weapon_sides_are_free = not queue.sides_are_busy(weapon_sides)
if weapon_sides_are_free:
@ -77,6 +73,25 @@ func _input(event):
camera.rotation.x = new_rotation
rotation.y -= event.relative.x * horizontal_sensivity
func on_weapon_slot_selected():
if current_weapon != null:
current_weapon.fired.disconnect(on_weapon_fired)
current_weapon.fire_failed.disconnect(finish_task)
weapon_player.remove_animation_library("current")
current_weapon = weapons.current_weapon
print('im here')
current_weapon.fired.connect(on_weapon_fired)
current_weapon.fire_failed.connect(finish_task)
weapon_player.add_animation_library("current", current_weapon.animation_library)
weapon_player.play("current/static")
update_ammo_label()
func on_queue_command_pushed(commands: Dictionary):
for side in commands:
match side:
@ -91,7 +106,6 @@ func push_copied_command(command: CommandQueue.Command, sides: Array[CommandQueu
commands[side] = command
queue.push(commands)
func finish_task():
queue.pop()

View file

@ -0,0 +1,30 @@
extends Node3D
class_name WeaponContainer
var slots: Array[WeaponSlot]
var current_slot: WeaponSlot = null
var current_weapon: Weapon
signal slot_selected()
func _ready():
for slot in get_children():
assert(slot is WeaponSlot)
slots.push_back(slot as WeaponSlot)
for slot in range(slots.size()):
if current_slot == null:
select_slot(slot)
else:
break
assert(current_slot != null)
func select_slot(index: int):
assert(index < slots.size())
if slots[index].has_weapon:
current_slot = slots[index]
current_weapon = current_slot.weapon
print('here we go')
slot_selected.emit()

View file

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

View file

@ -0,0 +1,14 @@
extends Node3D
class_name WeaponSlot
var has_weapon = false
var weapon: Weapon
func _ready():
has_weapon = get_child_count() > 0
if has_weapon:
var child = get_child(0)
assert(child is Weapon)
weapon = child as Weapon

View file

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