This repository has been archived on 2024-07-02. You can view files and clone it, but cannot push or open issues or pull requests.
HoppyEaster/Scripts/EntitySystem/BunnyGenerator.gd

33 lines
1.1 KiB
GDScript3
Raw Normal View History

extends Node
2023-04-09 10:01:03 +00:00
class_name BunnyGenerator
@export var bunny_prefab : Resource
2023-04-09 08:36:03 +00:00
@export var player : CharacterBody2D
@export var player_save_distance : float = 32.0
@onready var BunnyPrefab : PackedScene = load(bunny_prefab.resource_path)
2023-04-09 08:36:03 +00:00
func spawn_bunny(pos : Vector2, team : int, health : int) -> Bunny:
var bunny = BunnyPrefab.instantiate()
self.add_child(bunny)
2023-04-09 08:36:03 +00:00
bunny.global_position = pos
bunny.health = health
bunny.team = team
2023-04-09 14:39:45 +00:00
bunny.player = player
return bunny
2023-04-09 08:36:03 +00:00
2023-04-09 10:01:03 +00:00
func spawn_wave(free_tiles : Array, team: int, amount : int, health : int) -> Array:
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-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-09 10:01:03 +00:00
var bunny = spawn_bunny(pos, team, 3)
bunnys.push_back(bunny)
return bunnys