From 6a317d19132d60603e3fddf25f22c14a6547fd1c Mon Sep 17 00:00:00 2001 From: 2ndbeam <2ndbeam@disroot.org> Date: Sat, 26 Jul 2025 18:10:44 +0300 Subject: [PATCH] Proper firing logic handling --- .../guns/gangstaholder/gangstaholder.tres | 7 ++----- .../sprites/guns/placeholder/placeholder.tres | 7 ++----- base/scenes/player.tscn | 4 ++-- base/scenes/weapons/auto_weapon_test.tscn | 2 +- base/scenes/weapons/weapon_base.tscn | 2 +- base/scripts/player/command_queue.gd | 2 +- base/scripts/player/player.gd | 20 ++++++++++++++++--- base/scripts/player/weapon_container.gd | 1 - .../weapons/fire_mode/auto_fire_mode.gd | 2 ++ .../weapons/fire_mode/base_fire_mode.gd | 3 +++ .../weapons/fire_mode/single_fire_mode.gd | 1 + base/scripts/weapons/weapon_base.gd | 13 +++++++++++- 12 files changed, 44 insertions(+), 20 deletions(-) diff --git a/base/assets/sprites/guns/gangstaholder/gangstaholder.tres b/base/assets/sprites/guns/gangstaholder/gangstaholder.tres index 47b6217..2af4c99 100644 --- a/base/assets/sprites/guns/gangstaholder/gangstaholder.tres +++ b/base/assets/sprites/guns/gangstaholder/gangstaholder.tres @@ -50,13 +50,10 @@ tracks/1/path = NodePath("../..") tracks/1/interp = 1 tracks/1/loop_wrap = true tracks/1/keys = { -"times": PackedFloat32Array(0.2, 0.3), -"transitions": PackedFloat32Array(1, 1), +"times": PackedFloat32Array(0.3), +"transitions": PackedFloat32Array(1), "values": [{ "args": [], -"method": &"finish_task" -}, { -"args": [], "method": &"reset_animation" }] } diff --git a/base/assets/sprites/guns/placeholder/placeholder.tres b/base/assets/sprites/guns/placeholder/placeholder.tres index c377e3a..8bb08dc 100644 --- a/base/assets/sprites/guns/placeholder/placeholder.tres +++ b/base/assets/sprites/guns/placeholder/placeholder.tres @@ -50,13 +50,10 @@ tracks/1/path = NodePath("../..") tracks/1/interp = 1 tracks/1/loop_wrap = true tracks/1/keys = { -"times": PackedFloat32Array(0.2, 0.3), -"transitions": PackedFloat32Array(1, 1), +"times": PackedFloat32Array(0.3), +"transitions": PackedFloat32Array(1), "values": [{ "args": [], -"method": &"finish_task" -}, { -"args": [], "method": &"reset_animation" }] } diff --git a/base/scenes/player.tscn b/base/scenes/player.tscn index 710f31c..ade45d6 100644 --- a/base/scenes/player.tscn +++ b/base/scenes/player.tscn @@ -1,7 +1,7 @@ [gd_scene load_steps=8 format=3 uid="uid://dwx5tcatj35gu"] [ext_resource type="Script" uid="uid://dts8lbivkgsmj" path="res://base/scripts/player/player.gd" id="1_1w3ab"] -[ext_resource type="Texture2D" uid="uid://dftqlo1lltx5f" path="res://base/assets/sprites/guns/gangstaholder/static.png" id="2_1w3ab"] +[ext_resource type="Texture2D" uid="uid://cy3ija8ratxjq" path="res://base/assets/sprites/guns/gangstaholder/shoot1.png" id="2_gt0rj"] [ext_resource type="PackedScene" uid="uid://bb6ovrbusyxpi" path="res://base/scenes/weapons/weapon_base.tscn" id="2_ma1q3"] [ext_resource type="Script" uid="uid://byhp4pklbjltn" path="res://base/scripts/player/weapon_container.gd" id="3_1w3ab"] [ext_resource type="Script" uid="uid://diu6eno2ag6ga" path="res://base/scripts/player/weapon_slot.gd" id="4_gt0rj"] @@ -32,7 +32,7 @@ grow_horizontal = 2 grow_vertical = 2 size_flags_horizontal = 3 size_flags_vertical = 3 -texture = ExtResource("2_1w3ab") +texture = ExtResource("2_gt0rj") stretch_mode = 6 [node name="AnimationPlayer" type="AnimationPlayer" parent="HUD/Weapon"] diff --git a/base/scenes/weapons/auto_weapon_test.tscn b/base/scenes/weapons/auto_weapon_test.tscn index 6b832e4..a4fa702 100644 --- a/base/scenes/weapons/auto_weapon_test.tscn +++ b/base/scenes/weapons/auto_weapon_test.tscn @@ -6,7 +6,7 @@ [sub_resource type="Resource" id="Resource_s6ep3"] script = ExtResource("2_wftdl") -fire_delay = 0.3 +fire_delay = 0.35 metadata/_custom_type_script = "uid://dsvgbyeerw1ld" [node name="Weapon" instance=ExtResource("1_dnsf3")] diff --git a/base/scenes/weapons/weapon_base.tscn b/base/scenes/weapons/weapon_base.tscn index c73b281..60ede34 100644 --- a/base/scenes/weapons/weapon_base.tscn +++ b/base/scenes/weapons/weapon_base.tscn @@ -7,7 +7,7 @@ [sub_resource type="Resource" id="Resource_ij06d"] script = ExtResource("2_bt0tf") -fire_delay = 0.5 +fire_delay = 0.3 metadata/_custom_type_script = "uid://ckexgw71dd5jc" [node name="Weapon" type="Node3D"] diff --git a/base/scripts/player/command_queue.gd b/base/scripts/player/command_queue.gd index 37d6673..ef12870 100644 --- a/base/scripts/player/command_queue.gd +++ b/base/scripts/player/command_queue.gd @@ -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 diff --git a/base/scripts/player/player.gd b/base/scripts/player/player.gd index 1207c73..beeac61 100644 --- a/base/scripts/player/player.gd +++ b/base/scripts/player/player.gd @@ -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: diff --git a/base/scripts/player/weapon_container.gd b/base/scripts/player/weapon_container.gd index 3838ab0..92c5cd8 100644 --- a/base/scripts/player/weapon_container.gd +++ b/base/scripts/player/weapon_container.gd @@ -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() diff --git a/base/scripts/weapons/fire_mode/auto_fire_mode.gd b/base/scripts/weapons/fire_mode/auto_fire_mode.gd index 8b710ef..1fd87c2 100644 --- a/base/scripts/weapons/fire_mode/auto_fire_mode.gd +++ b/base/scripts/weapons/fire_mode/auto_fire_mode.gd @@ -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: diff --git a/base/scripts/weapons/fire_mode/base_fire_mode.gd b/base/scripts/weapons/fire_mode/base_fire_mode.gd index 2150db3..570973d 100644 --- a/base/scripts/weapons/fire_mode/base_fire_mode.gd +++ b/base/scripts/weapons/fire_mode/base_fire_mode.gd @@ -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 diff --git a/base/scripts/weapons/fire_mode/single_fire_mode.gd b/base/scripts/weapons/fire_mode/single_fire_mode.gd index c74afe2..c74ce45 100644 --- a/base/scripts/weapons/fire_mode/single_fire_mode.gd +++ b/base/scripts/weapons/fire_mode/single_fire_mode.gd @@ -26,3 +26,4 @@ func _on_fire_begin() -> void: ## Invoked when cooldown has ended func on_cooldown_timeout() -> void: cooldown = false + fire_allowed.emit() diff --git a/base/scripts/weapons/weapon_base.gd b/base/scripts/weapons/weapon_base.gd index 9fb804f..263e23e 100644 --- a/base/scripts/weapons/weapon_base.gd +++ b/base/scripts/weapons/weapon_base.gd @@ -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