61 lines
1.3 KiB
GDScript
61 lines
1.3 KiB
GDScript
extends Node3D
|
|
|
|
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
|
|
@export var ammo_consumption: int = 1
|
|
|
|
@export var fire_mode: BaseFireMode
|
|
|
|
## Weapon animation library. Should contain "static", "fire", "reload", "holster", "draw" animations
|
|
@export var animation_library: AnimationLibrary
|
|
|
|
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:
|
|
if is_firing:
|
|
fire_mode._process()
|
|
|
|
## End fire
|
|
func end_fire() -> void:
|
|
if is_firing:
|
|
fire_mode._on_fire_end()
|
|
is_firing = false
|
|
|
|
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
|