Proper firing logic handling

This commit is contained in:
Alexey 2025-07-26 18:10:44 +03:00
commit 6a317d1913
12 changed files with 44 additions and 20 deletions

View file

@ -8,6 +8,7 @@ enum Command {
NONE,
TAKE_WEAPON,
FIRE,
STOP_FIRING,
RELOAD,
HOLSTER_WEAPON,
TAKE_ZAZA,
@ -30,7 +31,6 @@ var command_queue: Dictionary = {}
func _init() -> void:
for side in Side.values():
print(side)
var arr: Array[Command] = []
command_queue[side] = arr

View file

@ -36,12 +36,11 @@ func _process(_delta: float) -> void:
var weapon_sides_are_free = not queue.sides_are_busy(weapon_sides)
if weapon_sides_are_free:
# Fire logic
var fire_action = Input.is_action_just_pressed('shoot') if \
current_weapon.fire_mode is SingleFireMode else \
Input.is_action_pressed('shoot')
var fire_action = Input.is_action_just_pressed('shoot')
if fire_action:
push_copied_command(CommandQueue.Command.FIRE, weapon_sides)
# Reload logic
var reload_action = Input.is_action_just_pressed('reload')
if reload_action:
@ -57,6 +56,13 @@ func _process(_delta: float) -> void:
slot_id = slot
if slot_action:
weapons.select_slot(slot_id)
# Stop firing logic
if not current_weapon.fire_mode is SingleFireMode:
var stop_firing_action = Input.is_action_just_released('shoot')
if stop_firing_action:
push_copied_command(CommandQueue.Command.STOP_FIRING, weapon_sides)
for side in CommandQueue.Side.values():
var command = queue.current_command(side)
@ -92,6 +98,7 @@ func on_weapon_slot_selected():
if current_weapon != null:
current_weapon.fired.disconnect(on_weapon_fired)
current_weapon.fire_failed.disconnect(finish_task)
current_weapon.fire_allowed.disconnect(finish_task)
weapon_player.remove_animation_library("current")
@ -99,6 +106,7 @@ func on_weapon_slot_selected():
current_weapon.fired.connect(on_weapon_fired)
current_weapon.fire_failed.connect(finish_task)
current_weapon.fire_allowed.connect(finish_task)
weapon_player.add_animation_library("current", current_weapon.animation_library)
weapon_player.play("current/static")
@ -150,6 +158,7 @@ func update_ammo_label():
})
func on_weapon_fired():
weapon_player.stop()
weapon_player.play("current/fire")
update_ammo_label()
@ -167,6 +176,9 @@ func handle_new_right_command(command: CommandQueue.Command):
match command:
CommandQueue.Command.RELOAD:
weapon_player.play('current/reload')
CommandQueue.Command.STOP_FIRING:
if queue.current_command(CommandQueue.Side.RIGHT) == CommandQueue.Command.FIRE:
current_weapon.end_fire()
func handle_ended_left_command(command: CommandQueue.Command):
match command:
@ -189,6 +201,8 @@ func handle_started_right_command(command: CommandQueue.Command):
match command:
CommandQueue.Command.FIRE:
current_weapon.request_fire()
CommandQueue.Command.STOP_FIRING:
queue.pop()
func handle_current_left_command(command: CommandQueue.Command):
match command:

View file

@ -26,5 +26,4 @@ func select_slot(index: int):
if slots[index].has_weapon:
current_slot = slots[index]
current_weapon = current_slot.weapon
print('here we go')
slot_selected.emit()

View file

@ -24,6 +24,8 @@ func _process() -> void:
if _fire():
on_fire()
func _on_fire_end() -> void:
fire_allowed.emit()
## Invoked when cooldown has ended
func on_cooldown_timeout() -> void:

View file

@ -7,6 +7,9 @@ class_name BaseFireMode
var barrel : Barrel
var tree : SceneTree
## Emits when it's possible to request fire from outside
signal fire_allowed
func _init() -> void:
resource_local_to_scene = true

View file

@ -26,3 +26,4 @@ func _on_fire_begin() -> void:
## Invoked when cooldown has ended
func on_cooldown_timeout() -> void:
cooldown = false
fire_allowed.emit()

View file

@ -4,12 +4,13 @@ class_name Weapon
signal fired()
signal fire_failed()
signal fire_allowed()
@onready var barrel = $"Barrel"
@export var uses_hands: Array[CommandQueue.Side]
@export var max_ammo: int = 7
var ammo: int = max_ammo
var ammo: int
@export var ammo_consumption: int = 1
@export var fire_mode: BaseFireMode
@ -20,15 +21,19 @@ var ammo: int = max_ammo
var is_firing: bool = false
func _ready() -> void:
ammo = max_ammo
fire_mode.barrel = barrel
fire_mode.tree = get_tree()
fire_mode.fire_allowed.connect(on_fire_allowed)
barrel.fired.connect(on_barrel_fired)
## Begin to fire
func request_fire() -> void:
if not is_firing and ammo >= ammo_consumption:
is_firing = true
fire_mode._on_fire_begin()
elif ammo < ammo_consumption:
end_fire()
fire_failed.emit()
func _process(_dt) -> void:
@ -45,6 +50,12 @@ func on_barrel_fired() -> void:
is_firing = true
ammo -= ammo_consumption
fired.emit()
if ammo < ammo_consumption:
end_fire()
func on_fire_allowed() -> void:
fire_allowed.emit()
is_firing = false
func reload() -> void:
ammo = max_ammo