mirror of
https://github.com/Steffo99/hella-farm.git
synced 2024-11-25 09:34:24 +00:00
Create SpawnerChoice
selecting a random subspawner to spawn things from
This commit is contained in:
parent
4f18cdaa65
commit
510d14fd4c
2 changed files with 37 additions and 0 deletions
34
behaviours/spawner_choice.gd
Normal file
34
behaviours/spawner_choice.gd
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
extends Node
|
||||||
|
class_name SpawnerChoice
|
||||||
|
|
||||||
|
|
||||||
|
@export var spawners: Array[Spawner] = []
|
||||||
|
@export var weights: Array[int] = []
|
||||||
|
|
||||||
|
|
||||||
|
func compute_total_weight() -> int:
|
||||||
|
var total = 0
|
||||||
|
for weight in weights:
|
||||||
|
if weight < 0:
|
||||||
|
Log.e(self, "Weight with negative value detected.")
|
||||||
|
total += weight
|
||||||
|
return total
|
||||||
|
|
||||||
|
func select_spawner() -> Spawner:
|
||||||
|
var total = compute_total_weight()
|
||||||
|
var sampled = Random.rng.randi_range(0, total - 1)
|
||||||
|
var idx = -1
|
||||||
|
while sampled >= 0:
|
||||||
|
idx += 1
|
||||||
|
sampled -= weights[idx]
|
||||||
|
return spawners[idx]
|
||||||
|
|
||||||
|
func spawn():
|
||||||
|
select_spawner().spawn()
|
||||||
|
|
||||||
|
|
||||||
|
func _ready():
|
||||||
|
if len(spawners) != len(weights):
|
||||||
|
Log.e(self, "Spawners and weights are different lengths.")
|
||||||
|
if compute_total_weight() <= 0:
|
||||||
|
Log.e(self, "Weight total is less or equal to 0.")
|
3
behaviours/spawner_choice.tscn
Normal file
3
behaviours/spawner_choice.tscn
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
[gd_scene format=3 uid="uid://cokdfsk6rgeup"]
|
||||||
|
|
||||||
|
[node name="SpawnerChoice" type="Node"]
|
Loading…
Reference in a new issue