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

@ -7,8 +7,8 @@ extends Node
## night -> sunset to night in the first NIGHTFALL seconds (the sun becomes the moon);
## dawn -> night to sunrise over the dawn grace (brightening early: by the square root).
## The scarecrow carries a lantern that lights up as it gets dark, and enemies glow faintly at
## night (thief.gdshader night_glow) so they stay readable. The current look eases toward the
## target, so phase changes never pop.
## night (night_glow in thief.gdshader and ufo.gdshader) so they stay readable. The current
## look eases toward the target, so phase changes never pop.
const NIGHTFALL := 8.0
const EASE := 2.5
@ -25,7 +25,8 @@ const SUNRISE := {"elev": 10.0, "yaw": 150.0, "color": Color(1.0, 0.72, 0.58), "
var campaign: CampaignController
var sun: DirectionalLight3D
var env: Environment
var enemy_material: ShaderMaterial
## The flyers' materials (crows, UFOs), given night_glow.
var enemy_materials: Array[ShaderMaterial] = []
## Set by FarmSim: the ground enemies' characters glow too.
var characters: EnemyCharacters
var lantern: OmniLight3D
@ -35,11 +36,11 @@ var current := DAY.duplicate()
func setup(p_campaign: CampaignController, p_sun: DirectionalLight3D, p_env: Environment, player: Node3D,
p_enemy_material: ShaderMaterial) -> void:
p_enemy_materials: Array[ShaderMaterial]) -> void:
campaign = p_campaign
sun = p_sun
env = p_env
enemy_material = p_enemy_material
enemy_materials = p_enemy_materials
env.adjustment_enabled = true
lantern = add_lantern(player)
snap()
@ -108,8 +109,9 @@ func _apply() -> void:
for l in lanterns:
if is_instance_valid(l):
l.light_energy = current["lantern"]
if enemy_material != null:
enemy_material.set_shader_parameter("night_glow", current["glow"])
for m in enemy_materials:
if m != null:
m.set_shader_parameter("night_glow", current["glow"])
if characters != null:
characters.set_night_glow(current["glow"])

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

View file

@ -5,8 +5,8 @@ extends Node3D
## creates it and setup() connects it to the farm's systems:
## * scarecrow: footsteps, its weapons (a pitchfork's whoosh and what it strikes, corn shots,
## a pumpkin bomb's toss and burst), hurt, fall, fear and fear ready, patching and patched
## * enemies: crow caws and thieves' / fence breakers' arrivals (from where they come in), hits
## and deaths by type
## * enemies: crow caws and thieves' / fence breakers' / UFOs' arrivals (from where they come
## in), hits and deaths by type
## * theft: grabbed, dropped, recovered, carried off (with an alarm tone), animals following
## * building: placed, refused, dismantled, hit, broken (with a breach alarm), repaired,
## automated defenses firing, traps springing, the repair hammering
@ -57,6 +57,7 @@ const CUES := {
"crow_die": {"files": ["synth/crow_squawk.wav"], "db": -2.0, "pitch": 0.1, "gap": 0.05, "max": 3},
"thief_arrive": {"files": ["kenney/rpg/creak*.ogg"], "db": 0.0, "pitch": 0.1, "gap": 0.8},
"breaker_arrive": {"files": ["kenney/impact/impactWood_heavy_000.ogg"], "db": 4.0, "pitch": 0.0, "gap": 0.8},
"ufo_arrive": {"files": ["kenney/digital/powerUp2.ogg"], "db": -3.0, "pitch": 0.06, "gap": 0.7, "max": 2},
"enemy_die": {"files": ["kenney/impact/impactPunch_heavy_00*.ogg"], "db": -3.0, "pitch": 0.1, "gap": 0.05, "max": 3},
# Theft and alerts
"grab": {"files": ["kenney/rpg/handleSmallLeather.ogg"], "db": -4.0, "pitch": 0.1, "gap": 0.1},
@ -218,8 +219,8 @@ func setup(p_sim: FarmSim) -> void:
sim.waves.arrived.connect(func(h: int, at: Vector2) -> void:
var d := sim.enemies.resolve_handle(h)
var type := sim.enemies.type[d] if d >= 0 else EnemySystem.TYPE_THIEF
# thief, crow, breaker, and a UFO (no cue of its own yet: it flies in like a crow).
var arrive_cues := ["thief_arrive", "crow_arrive", "breaker_arrive", "crow_arrive"]
# thief, crow, breaker, UFO
var arrive_cues := ["thief_arrive", "crow_arrive", "breaker_arrive", "ufo_arrive"]
play(arrive_cues[type], Vector3(at.x, 0.0, at.y)))
var kit := sim.scarecrow
kit.damaged.connect(func(hp: int) -> void: play("fall" if hp <= 0 else "hurt"))

View file

@ -176,7 +176,10 @@ func _ready() -> void:
add_child(characters)
var mat := ShaderMaterial.new()
mat.shader = load("res://game/resources/thief.gdshader") as Shader
enemies.setup(config, nav, behavior, EnemySystem.build_bird_mesh(0.0055), mat, zones.playable.grow(12.0))
var ufo_mat := ShaderMaterial.new()
ufo_mat.shader = load("res://game/resources/ufo.gdshader") as Shader
enemies.setup(config, nav, behavior, EnemySystem.build_bird_mesh(0.0055), mat, zones.playable.grow(12.0),
EnemySystem.build_ufo_mesh(), ufo_mat)
characters.setup(enemies, config)
enemies.characters = characters
loot.animals = animals
@ -251,7 +254,7 @@ func _ready() -> void:
day_night.name = "DayNight"
add_child(day_night)
day_night.characters = characters
day_night.setup(campaign, sun, world_environment.environment, player, mat)
day_night.setup(campaign, sun, world_environment.environment, player, [mat, ufo_mat])
build = BuildMode.new()
build.name = "Build"
add_child(build)