41 lines
928 B
GDScript
41 lines
928 B
GDScript
class_name Weapon
|
|
extends Node2D
|
|
|
|
# constants
|
|
@export var max_ammo = 1
|
|
## Number of seconds until the weapon can be used again.
|
|
@export var cooldown = 1
|
|
@export var shot: Projectile
|
|
@export var shot_spawn: Node2D
|
|
@export var audio: AudioStreamRandomizer
|
|
|
|
# gameplay variables
|
|
var ammo
|
|
var timer = 0.0
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
func _ready() -> void:
|
|
ammo = max_ammo
|
|
|
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
func _process(delta: float) -> void:
|
|
timer += delta
|
|
|
|
# Creates and returns an instance of the weapon's projectile.
|
|
func shoot() -> Projectile:
|
|
if timer > cooldown && ammo > 0:
|
|
timer = 0
|
|
var new_shot = shot.instantiate()
|
|
owner.add_child(new_shot)
|
|
new_shot.transform = shot_spawn.transform
|
|
ammo -= 1
|
|
audio.play()
|
|
return new_shot
|
|
else:
|
|
return null
|
|
|
|
func add_ammo(amount):
|
|
ammo += amount
|
|
if ammo > max_ammo:
|
|
ammo = max_ammo
|