add weights and cost to enemy spawner

This commit is contained in:
Michael Campbell 2026-03-19 02:19:14 -04:00
parent 829b383a2a
commit 9e47e27cbf
4 changed files with 61 additions and 13 deletions

5
levels/enemy_entry.gd Normal file
View file

@ -0,0 +1,5 @@
class_name EnemyEntry
extends Resource
@export var scene: PackedScene
@export var cost: float

View file

@ -0,0 +1 @@
uid://cx7xhojlh0s0f

View file

@ -6,7 +6,12 @@ var points: Array[Vector3]
const MAX_CACHED_POINTS := 100 const MAX_CACHED_POINTS := 100
var doomed_point_idx := 0 var doomed_point_idx := 0
@export var enemy_scenes: Array[PackedScene] @export var enemy_scenes: Array[EnemyEntry]
var weights: Array[float]
var total_cost := 0.
var money := 5.
var rng := RandomNumberGenerator.new()
func _get_point() -> Vector3: func _get_point() -> Vector3:
assert(not boxes.is_empty()) assert(not boxes.is_empty())
@ -26,6 +31,11 @@ func _ready() -> void:
if child is CSGBox3D: if child is CSGBox3D:
boxes.push_back(child) boxes.push_back(child)
for entry in enemy_scenes:
total_cost += entry.cost
for entry in enemy_scenes:
weights.push_back(total_cost - entry.cost + 1.)
func _process(_delta: float) -> void: func _process(_delta: float) -> void:
var v := _get_point() var v := _get_point()
v.y = 50. v.y = 50.
@ -49,15 +59,26 @@ func _process(_delta: float) -> void:
func _on_timer_timeout() -> void: func _on_timer_timeout() -> void:
if not Level.is_active(): return if not Level.is_active(): return
money += 2.
if randf() > .1: return
if points.is_empty(): if points.is_empty():
push_warning("tried to spawn enemy, but can't find a point!") push_warning("tried to spawn enemy, but can't find a point!")
return return
while money > 3.:
var entry: EnemyEntry = enemy_scenes.pick_random()
if entry.cost > money:
return
var scene := entry.scene
var pos: Vector3 = points.pick_random() var pos: Vector3 = points.pick_random()
pos += Vector3.UP * 0.02 pos += Vector3.UP * 0.02
var scene: PackedScene = enemy_scenes.pick_random()
var car: Node3D = scene.instantiate() var car: Node3D = scene.instantiate()
car.position = pos car.position = pos
get_tree().current_scene.add_child(car) get_tree().current_scene.add_child(car)
money -= entry.cost

File diff suppressed because one or more lines are too long