Task #19: UFO model built from primitives

UFOs no longer reuse the crow's bird mesh. EnemySystem.build_ufo_mesh()
merges the mesh arrays of two CylinderMesh hull halves, a TorusMesh rim, a
hemisphere SphereMesh dome and eight small sphere lights into one ArrayMesh
(about 1.65 m across, -0.13..0.45 m about its origin), with per-part vertex
colors and (glow, metalness) in UV.

A second HordeRenderer (ufo_renderer) draws it with the new
game/resources/ufo.gdshader: albedo from COLOR, no flap, a slow spin and bob
with a per-enemy phase, chasing rim lights, and the bird shader's hit flash
(INSTANCE_CUSTOM.g) and night_glow. Both batches mirror the dense slots
(write_new, move_slot, clear), and commit_render scales each slot to nothing
in the batch that doesn't draw its type. DayNight now takes a list of enemy
materials and sets night_glow on both.

UFOs get their own arrival cue (kenney/digital/powerUp2.ogg, previously
unused) instead of the crow's caw. HordeRenderer.is_drawn() backs the new
test_roster checks: a UFO is drawn by the UFO batch only, a crow by the
bird batch only, a thief by neither, also after a swap-removal; and the
saucer's size and centering.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Forrest 2026-10-06 12:55:14 -04:00
parent 26b735d3e6
commit 9cb01f1ede
8 changed files with 264 additions and 35 deletions

View file

@ -2,8 +2,10 @@ class_name EnemySystem
extends Node3D
## Pooled enemy simulation for the farm, forked from the prototype's HordeSystem: dense slots
## with stable ids and generation counters (handles that go stale when a slot is reused),
## swap-removal, removals deferred to the end of the tick, and one shared MultiMesh batch
## (HordeRenderer) for the flyers. Adds per-slot type, behavior state, target and carried loot,
## swap-removal, removals deferred to the end of the tick, and MultiMesh batches (HordeRenderer)
## for the flyers: one for crows (the bird mesh) and one for UFOs (a saucer built from primitives,
## build_ufo_mesh). Both mirror the dense slots, and each draws only its own type (the other
## slots scaled to nothing). Adds per-slot type, behavior state, target and carried loot,
## and a state timer. Each type's stats come from GameConfig.enemy_types (cached per type here).
## Ground enemies collide with the farm through the FarmNavigation grid: an enemy's center stays
## in walkable cells (free of static obstacles and of player-built structures, both already
@ -46,7 +48,9 @@ var behavior: ThiefBehavior
var structures: StructureSystem
var characters: EnemyCharacters
var grid := SpatialGrid.new()
## Draws the crows (the bird mesh); ufo_renderer draws the UFOs.
var renderer: HordeRenderer
var ufo_renderer: HordeRenderer
# Per type (index = type id), from GameConfig.enemy_types.
var t_speed := PackedFloat32Array()
@ -123,8 +127,9 @@ var attacks_blocked_by_cover: int = 0
var hit_t: float = 0.0
## mesh / material draw the crows, ufo_mesh / ufo_material the UFOs.
func setup(cfg: GameConfig, p_nav: FarmNavigation, p_behavior: ThiefBehavior, mesh: Mesh,
material: Material, area: Rect2) -> void:
material: Material, area: Rect2, ufo_mesh: Mesh, ufo_material: Material) -> void:
config = cfg
nav = p_nav
behavior = p_behavior
@ -142,11 +147,15 @@ func setup(cfg: GameConfig, p_nav: FarmNavigation, p_behavior: ThiefBehavior, me
a.fill(0)
id_dense.fill(-1)
grid.setup(area, 1.0, capacity)
var aabb := AABB(Vector3(area.position.x, 0.0, area.position.y), Vector3(area.size.x, 4.0, area.size.y))
renderer = HordeRenderer.new()
renderer.name = "EnemyRenderer"
add_child(renderer)
var aabb := AABB(Vector3(area.position.x, 0.0, area.position.y), Vector3(area.size.x, 4.0, area.size.y))
renderer.setup(mesh, material, capacity, cfg.combat_height, aabb)
ufo_renderer = HordeRenderer.new()
ufo_renderer.name = "UfoRenderer"
add_child(ufo_renderer)
ufo_renderer.setup(ufo_mesh, ufo_material, capacity, cfg.combat_height, aabb)
clear()
@ -203,9 +212,10 @@ func clear() -> void:
if id_dense[id] >= 0:
id_generation[id] += 1
id_dense[id] = -1
if renderer:
renderer.clear()
renderer.commit(0)
for r: HordeRenderer in [renderer, ufo_renderer]:
if r:
r.clear()
r.commit(0)
if characters:
characters.clear()
@ -255,6 +265,7 @@ func spawn(enemy_type: int, x: float, z: float, heading: Vector2, p_want: int =
dense_id[d] = id
id_dense[id] = d
renderer.write_new(d, x, z, h.x, h.y, flap_phase[d])
ufo_renderer.write_new(d, x, z, h.x, h.y, flap_phase[d])
return handle_of(d)
@ -560,26 +571,44 @@ func commit_removals() -> void:
_release_slot(d)
## Uploads the flyers' poses (with their altitude) to the MultiMesh; ground enemies are drawn by
## their characters, so their instances are scaled to nothing.
## Uploads the flyers' poses (with their altitude) to the MultiMeshes: each slot is written to
## both batches, scaled to nothing in the one that doesn't draw its type (crows in renderer, UFOs
## in ufo_renderer). Ground enemies are drawn by their characters, so they're hidden in both.
func commit_render() -> void:
var b := renderer.buffer
var u := ufo_renderer.buffer
var o := 0
for i in active_count:
var on := 1.0 if t_flying[type[i]] == 1 else 0.0
var c := -head_z[i] * on
var s := -head_x[i] * on
b[o] = c
b[o + 2] = s
b[o + 3] = pos_x[i]
b[o + 5] = on
b[o + 7] = alt[i]
b[o + 8] = -s
b[o + 10] = c
b[o + 11] = pos_z[i]
b[o + 13] = hurt[i]
var t := type[i]
var saucer := 1.0 if t == TYPE_UFO else 0.0
var bird := 1.0 if t_flying[t] == 1 and t != TYPE_UFO else 0.0
var c := -head_z[i]
var s := -head_x[i]
var x := pos_x[i]
var y := alt[i]
var z := pos_z[i]
var flash := hurt[i]
b[o] = c * bird
b[o + 2] = s * bird
b[o + 3] = x
b[o + 5] = bird
b[o + 7] = y
b[o + 8] = -s * bird
b[o + 10] = c * bird
b[o + 11] = z
b[o + 13] = flash
u[o] = c * saucer
u[o + 2] = s * saucer
u[o + 3] = x
u[o + 5] = saucer
u[o + 7] = y
u[o + 8] = -s * saucer
u[o + 10] = c * saucer
u[o + 11] = z
u[o + 13] = flash
o += HordeRenderer.STRIDE
renderer.commit(active_count)
ufo_renderer.commit(active_count)
func _release_slot(d: int) -> void:
@ -597,6 +626,7 @@ func _release_slot(d: int) -> void:
dense_id[d] = moved
id_dense[moved] = d
renderer.move_slot(last, d)
ufo_renderer.move_slot(last, d)
alive[last] = 0
id_dense[id] = -1
id_generation[id] += 1
@ -623,3 +653,85 @@ static func build_bird_mesh(scale: float) -> ArrayMesh:
arrays[Mesh.ARRAY_TANGENT] = null
mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arrays)
return mesh
## The UFO: a flattened saucer merged from primitives into one surface (no imported asset),
## centered on its origin with the hull's widest edge at y = 0. About 1.65 m across and 0.6 m tall
## (-0.13 .. 0.45) at scale 1, a little wider than a crow's 1.33 m wingspan. A metal hull, light
## grey above and darker below; a dark rim ring around the edge; a tinted glass dome on top; and
## eight lights on the rim. Per part, the vertex COLOR holds its color and UV its (glow,
## metalness) for ufo.gdshader, which spins it slowly and pulses the lights (glow 1). It's round,
## so forward (-Z) only matters to the instance's yaw.
static func build_ufo_mesh(scale: float = 1.0) -> ArrayMesh:
var upper := CylinderMesh.new()
upper.top_radius = 0.38
upper.bottom_radius = 0.74
upper.height = 0.16
upper.radial_segments = 24
upper.rings = 0
upper.cap_bottom = false
var lower := CylinderMesh.new()
lower.top_radius = 0.74
lower.bottom_radius = 0.3
lower.height = 0.13
lower.radial_segments = 24
lower.rings = 0
lower.cap_top = false
var rim := TorusMesh.new()
rim.inner_radius = 0.68
rim.outer_radius = 0.82
rim.rings = 28
rim.ring_segments = 8
var dome := SphereMesh.new() # a hemisphere: flat base at its origin, the top at its height
dome.radius = 0.36
dome.height = 0.3
dome.is_hemisphere = true
dome.radial_segments = 18
dome.rings = 8
var bulb := SphereMesh.new()
bulb.radius = 0.065
bulb.height = 0.13
bulb.radial_segments = 6
bulb.rings = 3
# [mesh, offset, color (sRGB), (glow, metalness)]
var parts: Array = [
[upper, Vector3(0.0, 0.08, 0.0), Color(0.68, 0.71, 0.76), Vector2(0.0, 0.6)],
[lower, Vector3(0.0, -0.065, 0.0), Color(0.38, 0.4, 0.46), Vector2(0.0, 0.6)],
[rim, Vector3.ZERO, Color(0.26, 0.28, 0.33), Vector2(0.0, 0.4)],
[dome, Vector3(0.0, 0.15, 0.0), Color(0.42, 0.95, 0.82), Vector2(0.3, 0.0)],
]
const LIGHTS := 8
for k in LIGHTS:
var a := TAU * k / LIGHTS
var color := Color(1.0, 0.72, 0.2) if k % 2 == 0 else Color(0.4, 1.0, 0.35)
parts.append([bulb, Vector3(cos(a) * 0.76, 0.05, sin(a) * 0.76), color, Vector2(1.0, 0.0)])
var verts := PackedVector3Array()
var normals := PackedVector3Array()
var colors := PackedColorArray()
var uvs := PackedVector2Array()
var indices := PackedInt32Array()
for part: Array in parts:
var src := (part[0] as PrimitiveMesh).get_mesh_arrays()
var pv: PackedVector3Array = src[Mesh.ARRAY_VERTEX]
var pn: PackedVector3Array = src[Mesh.ARRAY_NORMAL]
var offset: Vector3 = part[1]
var color := (part[2] as Color).srgb_to_linear()
var uv: Vector2 = part[3]
var base := verts.size()
for k in pv.size():
verts.append((pv[k] + offset) * scale)
normals.append(pn[k])
colors.append(color)
uvs.append(uv)
for k: int in src[Mesh.ARRAY_INDEX]:
indices.append(base + k)
var arrays := []
arrays.resize(Mesh.ARRAY_MAX)
arrays[Mesh.ARRAY_VERTEX] = verts
arrays[Mesh.ARRAY_NORMAL] = normals
arrays[Mesh.ARRAY_COLOR] = colors
arrays[Mesh.ARRAY_TEX_UV] = uvs
arrays[Mesh.ARRAY_INDEX] = indices
var mesh := ArrayMesh.new()
mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arrays)
return mesh