(wip) Player food
This commit is contained in:
50
evolve-die-repeat/molecular/food/food_manager.gd
Normal file
50
evolve-die-repeat/molecular/food/food_manager.gd
Normal file
@@ -0,0 +1,50 @@
|
||||
extends Node
|
||||
class_name FoodManager2D
|
||||
|
||||
var minCount: int = 20
|
||||
var _currentCount: int = 0
|
||||
|
||||
var rng = RandomNumberGenerator.new()
|
||||
|
||||
@export var foodTypes: Array[PackedScene] = []
|
||||
|
||||
## The probabilities have to add up to 1.0
|
||||
@export var foodProbs: Array[float] = []
|
||||
|
||||
@export var spawnRange: Rect2 = Rect2(0, 0, 0, 0)
|
||||
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
spawnRange = GameManager.extent
|
||||
call_deferred("_spawn_minimum")
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta: float) -> void:
|
||||
pass
|
||||
|
||||
func _spawn_minimum() -> void:
|
||||
while _currentCount < minCount:
|
||||
_spawn_random()
|
||||
|
||||
func _random_pos() -> Vector2:
|
||||
return Vector2(randf_range(spawnRange.position[0], spawnRange.size[0]), randf_range(spawnRange.position[1], spawnRange.size[1]))
|
||||
|
||||
func _spawn_random() -> void:
|
||||
var pos = _random_pos()
|
||||
_spawn_food(pos)
|
||||
_currentCount += 1
|
||||
|
||||
func _spawn_food(position: Vector2) -> void:
|
||||
var instance = foodTypes[rng.rand_weighted(foodProbs)].instantiate()
|
||||
instance.position = position
|
||||
add_child(instance)
|
||||
if instance.has_signal("consumed"):
|
||||
instance.consumed.connect(_on_entity_consumed)
|
||||
|
||||
|
||||
func _on_entity_consumed() -> void:
|
||||
_currentCount = max(0, _currentCount-1)
|
||||
call_deferred("_spawn_minimum")
|
||||
|
||||
# TODO: Spawn food when an entity (any) dies
|
||||
Reference in New Issue
Block a user