2023-04-08 15:00:54 +00:00
|
|
|
extends Node
|
2023-04-09 10:01:03 +00:00
|
|
|
class_name BunnyGenerator
|
2023-04-08 15:00:54 +00:00
|
|
|
|
|
|
|
@export var bunny_prefab : Resource
|
2023-04-09 08:36:03 +00:00
|
|
|
@export var player : CharacterBody2D
|
2023-04-09 20:45:00 +00:00
|
|
|
@export var player_save_distance : float = 400
|
2023-04-08 15:00:54 +00:00
|
|
|
@onready var BunnyPrefab : PackedScene = load(bunny_prefab.resource_path)
|
|
|
|
|
2023-04-23 13:00:28 +00:00
|
|
|
func spawn_bunny(pos : Vector2, team : int, health : int, damage : int, attack_speed : float) -> Bunny:
|
2023-04-08 15:00:54 +00:00
|
|
|
var bunny = BunnyPrefab.instantiate()
|
|
|
|
self.add_child(bunny)
|
2023-04-09 08:36:03 +00:00
|
|
|
bunny.global_position = pos
|
2023-04-08 15:00:54 +00:00
|
|
|
bunny.health = health
|
2023-04-23 12:50:34 +00:00
|
|
|
bunny.damage = damage
|
2023-04-23 13:00:28 +00:00
|
|
|
bunny.attack_speed = attack_speed
|
2023-04-08 15:00:54 +00:00
|
|
|
bunny.team = team
|
|
|
|
return bunny
|
2023-04-09 08:36:03 +00:00
|
|
|
|
2023-04-23 13:00:28 +00:00
|
|
|
func spawn_wave(free_tiles : Array, team: int, amount : int, health : int, damage : int, attack_speed : float) -> Array:
|
2023-04-09 10:01:03 +00:00
|
|
|
var bunnys = []
|
2023-04-09 08:36:03 +00:00
|
|
|
|
|
|
|
# Make Sure that no possitions to near to the player are insdie of the list
|
2023-04-09 10:01:03 +00:00
|
|
|
free_tiles = free_tiles.filter(func(val): return val.distance_to(player.global_position) > player_save_distance)
|
2023-04-09 08:36:03 +00:00
|
|
|
# Make sure amount isnt bigger then available positions
|
|
|
|
if free_tiles.size() < amount: amount = free_tiles.size()
|
2023-04-08 15:00:54 +00:00
|
|
|
|
2023-04-09 10:01:03 +00:00
|
|
|
# Spawn at random positions of the list
|
2023-04-09 08:36:03 +00:00
|
|
|
for i in amount:
|
|
|
|
var pos = free_tiles.pick_random()
|
|
|
|
free_tiles.erase(pos)
|
2023-04-23 13:00:28 +00:00
|
|
|
var bunny = spawn_bunny(pos, team, health, damage, attack_speed)
|
2023-04-09 10:01:03 +00:00
|
|
|
bunnys.push_back(bunny)
|
|
|
|
return bunnys
|
2023-04-09 20:45:00 +00:00
|
|
|
|
2023-04-23 13:00:28 +00:00
|
|
|
func spawn_batched_wave(batch_size : float, batch_delay: float, free_tiles : Array, team: int, amount : int, health : int, damage : int, attack_speed : float) -> Array:
|
2023-04-09 20:45:00 +00:00
|
|
|
var bunnys = []
|
|
|
|
var batch_count = ceil(amount / batch_size)
|
|
|
|
var actual_batch_size = floor(amount / batch_count)
|
|
|
|
var last_batch_size = amount - batch_size * batch_count
|
|
|
|
|
|
|
|
for s in batch_count:
|
2023-04-23 13:00:28 +00:00
|
|
|
bunnys += spawn_wave(free_tiles.duplicate(), team, actual_batch_size, health, damage, attack_speed)
|
2023-04-09 20:45:00 +00:00
|
|
|
await get_tree().create_timer(batch_delay).timeout
|
2023-04-23 13:00:28 +00:00
|
|
|
bunnys += spawn_wave(free_tiles.duplicate(), team, last_batch_size, health, damage, attack_speed)
|
2023-04-09 20:45:00 +00:00
|
|
|
await get_tree().create_timer(batch_delay).timeout
|
|
|
|
return bunnys
|