ft: improved npc spawning logic
This commit is contained in:
@@ -4,6 +4,8 @@ class_name NPC2D
|
||||
@export var maxHealth: int = 0
|
||||
var health: int = maxHealth
|
||||
|
||||
signal died
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
spawn()
|
||||
@@ -30,6 +32,7 @@ func feed(source ) -> void:
|
||||
push_error("Function feed() not implemented.")
|
||||
|
||||
func die() -> void:
|
||||
died.emit()
|
||||
queue_free()
|
||||
# TODO: should associate this class with a loot table (or equivalent), and can then implement logic for dying in parent class here.
|
||||
|
||||
|
||||
6
evolve-die-repeat/shared/npc/spawnManager2D.tscn
Normal file
6
evolve-die-repeat/shared/npc/spawnManager2D.tscn
Normal file
@@ -0,0 +1,6 @@
|
||||
[gd_scene load_steps=2 format=3 uid="uid://dje58m1cj34gn"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://coetidfssb80w" path="res://shared/npc/spawn_manager_2d.gd" id="1_624qc"]
|
||||
|
||||
[node name="SpawnManager2d" type="Node"]
|
||||
script = ExtResource("1_624qc")
|
||||
41
evolve-die-repeat/shared/npc/spawn_manager_2d.gd
Normal file
41
evolve-die-repeat/shared/npc/spawn_manager_2d.gd
Normal file
@@ -0,0 +1,41 @@
|
||||
extends Node
|
||||
class_name SpawnManager2D
|
||||
|
||||
#var scene = preload("res://molecular/nucleotide_prey.tscn")
|
||||
@export var scene: PackedScene
|
||||
@export var minCount = 1
|
||||
@export var maxCount = 2
|
||||
@export var spawnRange: Rect2
|
||||
var _currentCount = 0
|
||||
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
_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 and _currentCount < maxCount:
|
||||
_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_creature(pos)
|
||||
_currentCount += 1
|
||||
|
||||
func _spawn_creature(position: Vector2) -> void:
|
||||
var instance = scene.instantiate()
|
||||
instance.position = position
|
||||
add_child(instance)
|
||||
if instance.has_signal("died"):
|
||||
instance.died.connect(_on_entity_died)
|
||||
|
||||
func _on_entity_died() -> void:
|
||||
_currentCount = max(0, _currentCount-1)
|
||||
_spawn_minimum()
|
||||
1
evolve-die-repeat/shared/npc/spawn_manager_2d.gd.uid
Normal file
1
evolve-die-repeat/shared/npc/spawn_manager_2d.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://coetidfssb80w
|
||||
Reference in New Issue
Block a user