Added in editor warnings when weapons are missing components (Audioplayer, Shot spawn location, projectile PackedScene)
Added functionality for weapons to fire projectiles.
This commit is contained in:
parent
9621323983
commit
40ffbb84cd
8 changed files with 135 additions and 17 deletions
|
@ -1,13 +1,17 @@
|
|||
@tool
|
||||
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 cooldown = 0.1
|
||||
@export var shot: PackedScene:
|
||||
set(new_shot):
|
||||
shot = new_shot
|
||||
update_configuration_warnings()
|
||||
@export var shot_spawn: Node2D
|
||||
@export var audio: AudioStreamRandomizer
|
||||
@export var audio: AudioStreamPlayer2D
|
||||
|
||||
# gameplay variables
|
||||
var ammo
|
||||
|
@ -15,20 +19,52 @@ var timer = 0.0
|
|||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
ammo = max_ammo
|
||||
if !Engine.is_editor_hint():
|
||||
ammo = max_ammo
|
||||
else:
|
||||
child_entered_tree.connect(_on_child_entered_tree)
|
||||
child_exiting_tree.connect(_on_child_exited_tree)
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta: float) -> void:
|
||||
timer += delta
|
||||
if !Engine.is_editor_hint():
|
||||
timer += delta
|
||||
|
||||
func _get_configuration_warnings():
|
||||
var warnings = []
|
||||
|
||||
if shot == null:
|
||||
warnings.append("Please select a projectile scene for the shot variable.")
|
||||
if shot_spawn == null:
|
||||
warnings.append("Please add a Node2D as the muzzle/projectile spawning location for the shot_spawn variable.")
|
||||
if audio == null:
|
||||
warnings.append("Please add an AudioStreamPlayer2D which plays the weapon's audio.")
|
||||
return warnings
|
||||
|
||||
func _on_child_entered_tree(node):
|
||||
print_debug("saw a child entering tree: ", node)
|
||||
if node.get_class() == "Node2D":
|
||||
shot_spawn = node
|
||||
if node is AudioStreamPlayer2D:
|
||||
audio = node
|
||||
update_configuration_warnings()
|
||||
|
||||
func _on_child_exited_tree(node):
|
||||
if node == shot_spawn:
|
||||
shot_spawn = null
|
||||
if node == audio:
|
||||
audio = null
|
||||
update_configuration_warnings()
|
||||
|
||||
# Creates and returns an instance of the weapon's projectile.
|
||||
func shoot() -> Projectile:
|
||||
print_debug("trying to shoot, timer at ", timer, " out of ", cooldown, " ammo at ", ammo, " out of ", max_ammo)
|
||||
if timer > cooldown && ammo > 0:
|
||||
timer = 0
|
||||
var new_shot = shot.instantiate()
|
||||
owner.add_child(new_shot)
|
||||
new_shot.transform = shot_spawn.transform
|
||||
new_shot.transform = shot_spawn.global_transform
|
||||
ammo -= 1
|
||||
audio.play()
|
||||
return new_shot
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue