Godot Version
4.7.stable
Question
I have code written to where the orb shots incoming orcs. The code always leaves one orc on screen tho, when it should’ve been shot and taken out. Screenshot below:
Below is how I have some of the nodes arrainged:
Code for MobSpawn below:
extends Node2D
@export var mob_scenes: Array[PackedScene] = []
@onready var spawn_points = [$Spawn1, $Spawn2, $Spawn3]
@onready var mob_timer: Timer = $"../Mob_timer"
@onready var wave: Label = $"../Main_CanvasLayer/VBoxContainer/HBoxContainer/Wave"
var mob_counter: int = 0
var max_mob_index: int = 0
func _on_mob_timer_timeout():
# Create a new instance of the Mob scene.
var mob_scene = mob_scenes[randi_range(0, max_mob_index)]
var mob = mob_scene.instantiate()
#Stores spawned mobs in Globals array
Globals.mobs_array.append(mob)
# Set the mob's position to the random location.
var spawn = spawn_points.pick_random()
mob.position = spawn.position
# Spawn the mob by adding it to the Main scene.
add_child(mob)
#Tracks how many mobs have arrived
mob_counter += 1
#Ends the wave of mobs
if mob_counter == Globals.mob_wave_capacity:
mob_timer.stop()
Globals.mob_wave_capacity += 10
mob_counter = 0
wave_tracker()
func wave_tracker():
Globals.wave_tracker += 1
wave.text = str(Globals.wave_tracker)
match Globals.wave_tracker:
5: max_mob_index += 1
10: max_mob_index += 1
15: mob_timer.wave_time = 0.9
20: mob_timer.wave_time = 0.8
25: mob_timer.wave_time = 0.7
30: mob_timer.wave_time = 0.6
35: mob_timer.wave_time = 0.5
40: mob_timer.wave_time = 0.4
func _on_next_wave_pressed():
mob_timer.start()
Code for Line2D below:
extends Line2D
var firing_status: bool = false
var shot_duration: float = 0.05
var shot_delay: float = 0.3
@onready var shot_sound: AudioStreamPlayer = $"../../../Shot_sound"
@onready var mob_spawn: Node2D = $"../.."
@onready var balance: Label = $"../../../Main_CanvasLayer/VBoxContainer/HBoxContainer/Balance"
var duplicate_array = []
func _ready():
Globals.authorize_shots.connect(update_trajectory.bind())
func _process(_delta: float):
#Updates the balance in the label
balance.text = str(Globals.balance)
func update_trajectory():
#Ensures the PackedVector2Array is cleared beforehand
clear_points()
duplicate_array = Globals.mobs_array.duplicate(true)
for mob in duplicate_array:
if Globals.shoot_counter > 0:
add_point(Vector2(0,0))
add_point(to_local(mob.position))
await get_tree().create_timer(shot_duration).timeout
clear_points()
await get_tree().create_timer(shot_delay).timeout
Globals.shoot_counter -= 1
mob.when_attacked()
Globals.mobs_array.erase(mob)
shot_sound.play()
func _on_orb_area_shape_entered(_area_rid: RID, _area: Area2D, _area_shape_index: int, _local_shape_index: int):
Globals.roll_dice.emit()
Code for the orc scene below:
extends CharacterBody2D
@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D
var SPEED: int
const DIRECTION = -1
func _ready():
$AnimatedSprite2D.play()
SPEED = Globals.mob_speed
func _physics_process(_delta: float):
velocity.x = SPEED * DIRECTION
if velocity.length() > 0:
$AnimatedSprite2D.play("run")
move_and_slide()
func _on_area_2d_body_entered(_body: Node2D):
SPEED = 0
attacking_mode()
func attacking_mode():
while Globals.wizard_tower_health > 0:
$AnimatedSprite2D.play("attack")
await $AnimatedSprite2D.animation_finished
Globals.attacked_signal.emit()
func when_attacked():
SPEED = 0
$AnimatedSprite2D.play("death")
Globals.balance += 1
await $AnimatedSprite2D.animation_finished
queue_free()
The nodes for the orc scene is below:





