blank weapon fire system
This commit is contained in:
parent
8aa37bd929
commit
16b1d0f701
11 changed files with 92 additions and 51 deletions
|
@ -7,7 +7,7 @@ class_name CommandQueue
|
|||
enum Command {
|
||||
NONE,
|
||||
TAKE_WEAPON,
|
||||
SHOOT,
|
||||
FIRE,
|
||||
RELOAD,
|
||||
HOLSTER_WEAPON,
|
||||
TAKE_ZAZA,
|
||||
|
|
|
@ -11,6 +11,9 @@ var queue: CommandQueue = CommandQueue.new()
|
|||
@onready var weapon_player: AnimationPlayer = $"HUD/Weapon/AnimationPlayer"
|
||||
@onready var weapons: Node3D = $"WeaponContainer"
|
||||
|
||||
const DEFAULT_SIDES: Array[CommandQueue.Side] = [CommandQueue.Side.LEFT, CommandQueue.Side.RIGHT]
|
||||
const ONEHANDED_FIRE_COMMAND: Array[CommandQueue.Command] = [CommandQueue.Command.NONE, CommandQueue.Command.FIRE]
|
||||
|
||||
var current_weapon: Weapon
|
||||
|
||||
func _ready() -> void:
|
||||
|
@ -18,6 +21,7 @@ func _ready() -> void:
|
|||
queue.command_popped.connect(on_queue_command_popped)
|
||||
|
||||
current_weapon = weapons.get_child(0) as Weapon
|
||||
current_weapon.fired.connect(on_weapon_fired)
|
||||
|
||||
weapon_player.add_animation_library("current", current_weapon.animation_library)
|
||||
weapon_player.play("current/static")
|
||||
|
@ -25,16 +29,13 @@ func _ready() -> void:
|
|||
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
|
||||
|
||||
func _process(_delta: float) -> void:
|
||||
if Input.is_action_pressed("shoot") \
|
||||
and queue.current_command(CommandQueue.Side.RIGHT) \
|
||||
if queue.current_command(CommandQueue.Side.RIGHT) \
|
||||
== CommandQueue.DEFAULT_COMMAND:
|
||||
queue.push([
|
||||
CommandQueue.Command.NONE,
|
||||
CommandQueue.Command.SHOOT
|
||||
], [
|
||||
CommandQueue.Side.LEFT,
|
||||
CommandQueue.Side.RIGHT
|
||||
])
|
||||
var fire_action = Input.is_action_just_pressed('shoot') if \
|
||||
current_weapon.fire_mode is SingleFireMode else \
|
||||
Input.is_action_pressed('shoot')
|
||||
if fire_action:
|
||||
queue.push(ONEHANDED_FIRE_COMMAND, DEFAULT_SIDES)
|
||||
|
||||
for side in CommandQueue.Side.values():
|
||||
var command = queue.current_command(side)
|
||||
|
@ -74,7 +75,7 @@ func on_queue_command_pushed(sides, commands):
|
|||
CommandQueue.Side.RIGHT:
|
||||
handle_new_right_command(commands[i])
|
||||
|
||||
func shoot_animation_ended():
|
||||
func fire_task_finish():
|
||||
queue.pop()
|
||||
|
||||
func on_queue_command_popped(commands):
|
||||
|
@ -85,6 +86,12 @@ func on_queue_command_popped(commands):
|
|||
CommandQueue.Side.RIGHT:
|
||||
handle_ended_right_command(commands[i])
|
||||
|
||||
func on_weapon_fired():
|
||||
weapon_player.play("current/fire")
|
||||
|
||||
func on_fire_animation_end():
|
||||
weapon_player.play("current/static")
|
||||
|
||||
func handle_new_left_command(command: CommandQueue.Command):
|
||||
match command:
|
||||
CommandQueue.Command.NONE:
|
||||
|
@ -94,7 +101,7 @@ func handle_new_left_command(command: CommandQueue.Command):
|
|||
|
||||
func handle_new_right_command(command: CommandQueue.Command):
|
||||
match command:
|
||||
CommandQueue.Command.NONE | CommandQueue.Command.SHOOT:
|
||||
CommandQueue.Command.NONE | CommandQueue.Command.FIRE:
|
||||
pass
|
||||
_:
|
||||
print('New command %s is not implemented for right hand.' % command)
|
||||
|
@ -108,10 +115,8 @@ func handle_ended_left_command(command: CommandQueue.Command):
|
|||
|
||||
func handle_ended_right_command(command: CommandQueue.Command):
|
||||
match command:
|
||||
CommandQueue.Command.NONE:
|
||||
CommandQueue.Command.NONE | CommandQueue.Command.FIRE:
|
||||
pass
|
||||
CommandQueue.Command.SHOOT:
|
||||
weapon_player.play('current/static')
|
||||
_:
|
||||
print('Ended command %s is not implemented for right hand.' % command)
|
||||
|
||||
|
@ -126,5 +131,7 @@ func handle_current_right_command(command: CommandQueue.Command):
|
|||
match command:
|
||||
CommandQueue.Command.NONE:
|
||||
pass
|
||||
CommandQueue.Command.FIRE:
|
||||
current_weapon.request_fire()
|
||||
_:
|
||||
print('Current command %s is not implemented for right hand.' % command)
|
||||
|
|
|
@ -2,5 +2,10 @@ extends Node3D
|
|||
|
||||
class_name Barrel
|
||||
|
||||
signal fired()
|
||||
|
||||
func can_fire() -> bool:
|
||||
return true
|
||||
|
||||
func fire() -> void:
|
||||
fired.emit()
|
||||
|
|
|
@ -14,15 +14,15 @@ var cooldown : bool = false
|
|||
## Reference to cooldown scene timer
|
||||
var cooldown_timer : SceneTreeTimer = null
|
||||
|
||||
func _on_fire_begin(_tree : SceneTree) -> void:
|
||||
func _on_fire_begin() -> void:
|
||||
cooldown = false
|
||||
check_unfinished_timer(cooldown_timer,on_cooldown_timeout)
|
||||
|
||||
func _process(tree : SceneTree) -> void:
|
||||
func _process() -> void:
|
||||
if can_fire() :
|
||||
return
|
||||
if _fire():
|
||||
on_fire(tree)
|
||||
on_fire()
|
||||
|
||||
|
||||
## Invoked when cooldown has ended
|
||||
|
@ -42,7 +42,7 @@ func can_fire() -> bool:
|
|||
return cooldown
|
||||
|
||||
## Invoked when fired
|
||||
func on_fire(tree : SceneTree) -> void:
|
||||
func on_fire() -> void:
|
||||
cooldown = true
|
||||
cooldown_timer = tree.create_timer(fire_delay)
|
||||
cooldown_timer.timeout.connect(on_cooldown_timeout)
|
||||
|
|
|
@ -5,7 +5,7 @@ class_name BaseFireMode
|
|||
|
||||
## Assigned barrel to shoot out of
|
||||
var barrel : Barrel
|
||||
var tree : Tree
|
||||
var tree : SceneTree
|
||||
|
||||
func _init() -> void:
|
||||
resource_local_to_scene = true
|
||||
|
@ -14,6 +14,7 @@ func _init() -> void:
|
|||
## Returns [color=green]true[/color] if barrel can shoot,[br]
|
||||
## Returns [color=red]false[/color] if barrel cannot shoot
|
||||
func _fire() -> bool:
|
||||
barrel.fire()
|
||||
return true
|
||||
|
||||
|
||||
|
|
|
@ -20,15 +20,15 @@ var reloading : bool = false
|
|||
## Reference to reload scene timer
|
||||
var reload_timer : SceneTreeTimer = null
|
||||
|
||||
func _on_fire_begin(_tree : SceneTree) -> void:
|
||||
super._on_fire_begin(_tree)
|
||||
func _on_fire_begin() -> void:
|
||||
super._on_fire_begin()
|
||||
current_amount = 0
|
||||
reloading = false
|
||||
check_unfinished_timer(reload_timer,on_reload_timeout)
|
||||
|
||||
|
||||
func on_fire(tree : SceneTree) -> void:
|
||||
super.on_fire(tree)
|
||||
func on_fire() -> void:
|
||||
super.on_fire()
|
||||
current_amount += 1
|
||||
if current_amount >= burst_amount:
|
||||
reloading = true
|
||||
|
|
|
@ -13,7 +13,7 @@ class_name SingleFireMode
|
|||
var cooldown : bool = false
|
||||
|
||||
|
||||
func _on_fire_begin(tree) -> void:
|
||||
func _on_fire_begin() -> void:
|
||||
if cooldown:
|
||||
return
|
||||
|
||||
|
|
|
@ -2,6 +2,8 @@ extends Node3D
|
|||
|
||||
class_name Weapon
|
||||
|
||||
signal fired()
|
||||
|
||||
@onready var barrel = $"Barrel"
|
||||
@export var uses_hands: Array[CommandQueue.Side]
|
||||
|
||||
|
@ -10,6 +12,7 @@ var ammo: int = max_ammo
|
|||
@export var ammo_consumption: int = 1
|
||||
|
||||
@export var fire_mode: BaseFireMode
|
||||
|
||||
## Weapon animation library. Should contain "static", "fire", "reload" animations
|
||||
@export var animation_library: AnimationLibrary
|
||||
|
||||
|
@ -17,17 +20,23 @@ var is_firing: bool = false
|
|||
|
||||
func _ready() -> void:
|
||||
fire_mode.barrel = barrel
|
||||
fire_mode.tree = get_tree()
|
||||
barrel.fired.connect(on_barrel_fired)
|
||||
|
||||
## Begin to fire
|
||||
func request_fire() -> void:
|
||||
fire_mode.on_fire_begin(get_tree())
|
||||
is_firing = true
|
||||
fire_mode._on_fire_begin()
|
||||
|
||||
func _process(_dt) -> void:
|
||||
if is_firing:
|
||||
fire_mode._process(get_tree())
|
||||
fire_mode._process()
|
||||
|
||||
## End fire
|
||||
func end_fire() -> void:
|
||||
fire_mode.on_fire_end(get_tree())
|
||||
is_firing = false
|
||||
if is_firing:
|
||||
fire_mode._on_fire_end()
|
||||
is_firing = false
|
||||
|
||||
func on_barrel_fired() -> void:
|
||||
is_firing = true
|
||||
fired.emit()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue