diff --git a/game/resources/ufo.gdshader b/game/resources/ufo.gdshader new file mode 100644 index 0000000..3bc39c8 --- /dev/null +++ b/game/resources/ufo.gdshader @@ -0,0 +1,48 @@ +shader_type spatial; +// UFO material (its own MultiMesh batch, EnemySystem.ufo_renderer) for the saucer from +// EnemySystem.build_ufo_mesh(): each part's color comes from the vertex COLOR, and its glow and +// metalness from UV (x, y). No flap: the saucer turns slowly about its axis and bobs a little, +// with a per-enemy phase from INSTANCE_CUSTOM.r, and the rim lights (glow 1) pulse in a chase +// around the ring. As in thief.gdshader, a hit flashes it white through INSTANCE_CUSTOM.g (1 on +// the hit, fading), a rim light keeps it readable, and night_glow (set by DayNight) adds a faint +// purple glow at night; the lights and the dome glow brighter in the dark. + +uniform float spin_speed = 1.2; +uniform float bob_amount = 0.05; +uniform float bob_speed = 2.2; +uniform float chase_speed = 6.0; +uniform float glow_energy = 1.4; +uniform float night_glow : hint_range(0.0, 1.0) = 0.0; + +varying float flash; +varying float glow; +varying float metal; + +void vertex() { + float phase = INSTANCE_CUSTOM.r * 6.2831853; + // The lights chase around the ring (two pulses per turn), riding along with the spin. + float around = atan(VERTEX.z, VERTEX.x); + float pulse = 0.45 + 0.55 * max(sin(around * 2.0 - TIME * chase_speed + phase), 0.0); + glow = UV.x * mix(1.0, pulse, step(0.99, UV.x)); + metal = UV.y; + float a = TIME * spin_speed + phase; + mat2 spin = mat2(vec2(cos(a), sin(a)), vec2(-sin(a), cos(a))); + VERTEX.xz = spin * VERTEX.xz; + NORMAL.xz = spin * NORMAL.xz; + VERTEX.y += sin(TIME * bob_speed + phase) * bob_amount; + flash = INSTANCE_CUSTOM.g; +} + +void fragment() { + vec3 base = COLOR.rgb; + float f = clamp(flash, 0.0, 1.0); + ALBEDO = mix(base, vec3(1.0), f); + METALLIC = metal; + ROUGHNESS = mix(0.12, 0.4, metal); + SPECULAR = 0.5; + // The night's purple sits on the hull; the glowing parts keep their own color. + EMISSION = base * glow * glow_energy * (1.0 + night_glow) + vec3(1.0, 0.9, 0.8) * f * 0.6 + + vec3(0.5, 0.28, 0.75) * night_glow * 0.35 * (1.0 - glow); + RIM = 0.6; + RIM_TINT = 0.4; +} diff --git a/game/resources/ufo.gdshader.uid b/game/resources/ufo.gdshader.uid new file mode 100644 index 0000000..0ddfcbc --- /dev/null +++ b/game/resources/ufo.gdshader.uid @@ -0,0 +1 @@ +uid://ml3kjt32ru27 diff --git a/game/scripts/day_night.gd b/game/scripts/day_night.gd index a89ff1f..e6acc43 100644 --- a/game/scripts/day_night.gd +++ b/game/scripts/day_night.gd @@ -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"]) diff --git a/game/scripts/enemy_system.gd b/game/scripts/enemy_system.gd index 168fa3c..d3cd8d1 100644 --- a/game/scripts/enemy_system.gd +++ b/game/scripts/enemy_system.gd @@ -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 diff --git a/game/scripts/farm_audio.gd b/game/scripts/farm_audio.gd index 66ebd56..b89abf6 100644 --- a/game/scripts/farm_audio.gd +++ b/game/scripts/farm_audio.gd @@ -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")) diff --git a/game/scripts/farm_sim.gd b/game/scripts/farm_sim.gd index 461bef7..9f34f18 100644 --- a/game/scripts/farm_sim.gd +++ b/game/scripts/farm_sim.gd @@ -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) diff --git a/game/tests/test_roster.gd b/game/tests/test_roster.gd index fd3181d..27ebeba 100644 --- a/game/tests/test_roster.gd +++ b/game/tests/test_roster.gd @@ -35,6 +35,7 @@ func run(r: GameTestRunner) -> void: _thieves(r, sim, player) _breakers(r, sim, player) _ufos(r, sim, player) + _batches(r, sim) _characters(r, sim) _alerts(r, farm, sim, player) _scenarios(r, farm, sim) @@ -280,6 +281,55 @@ func _ufos(r: GameTestRunner, sim: FarmSim, player: PlayerCharacter) -> void: probe.free() +## Flyers are drawn by two MultiMesh batches: crows by the bird one, UFOs by their own saucer +## (built from primitives). Each batch draws only its type, also after a swap-removal moves a UFO +## into a crow's slot; ground enemies are in neither. +func _batches(r: GameTestRunner, sim: FarmSim) -> void: + var es := sim.enemies + var birds := es.renderer + var saucers := es.ufo_renderer + _step(sim, 1) + var base_birds := _drawn_count(birds, es.active_count) + var base_saucers := _drawn_count(saucers, es.active_count) + var crow := es.spawn(EnemySystem.TYPE_CROW, 20.0, -30.0, Vector2.UP) + var thief := es.spawn(EnemySystem.TYPE_THIEF, 22.0, -30.0, Vector2.UP) + var ufo := es.spawn(EnemySystem.TYPE_UFO, 24.0, -30.0, Vector2.UP) + _step(sim, 1) + var d_crow := es.resolve_handle(crow) + var d_thief := es.resolve_handle(thief) + var d_ufo := es.resolve_handle(ufo) + r.check(d_ufo >= 0 and saucers.is_drawn(d_ufo) and not birds.is_drawn(d_ufo), + "a UFO is drawn by the UFO batch, not the bird batch") + r.check(d_crow >= 0 and birds.is_drawn(d_crow) and not saucers.is_drawn(d_crow), "a crow is still drawn by the bird batch, and only there") + r.check(d_thief >= 0 and not birds.is_drawn(d_thief) and not saucers.is_drawn(d_thief), "a thief is in neither batch (its character draws it)") + es.apply_hit_handle(crow, 99) + _step(sim, 1) + d_ufo = es.resolve_handle(ufo) + r.check(d_ufo == d_crow and saucers.is_drawn(d_ufo) and not birds.is_drawn(d_ufo) + and _drawn_count(birds, es.active_count) == base_birds and _drawn_count(saucers, es.active_count) == base_saucers + 1, + "the UFO swapped into the dead crow's slot is still drawn as a saucer only") + var mesh := EnemySystem.build_ufo_mesh() + var box := mesh.get_aabb() + var center := box.get_center() + r.check(box.size.x > 1.4 and box.size.x < 1.8 and absf(box.size.x - box.size.z) < 0.03 and absf(center.x) < 0.01 + and absf(center.z) < 0.01 and box.position.y > -1.5 and box.end.y < 1.0 + and (mesh.surface_get_format(0) & Mesh.ARRAY_FORMAT_COLOR) != 0, + "the saucer is round, %.2f m across, centered, from %.2f to %.2f m about its origin, and colored per part" + % [box.size.x, box.position.y, box.end.y]) + for hh in [thief, ufo]: + es.apply_hit_handle(hh, 99) + _step(sim, 1) + sim.characters._process(EnemyCharacters.CORPSE_TIME + 0.1) + + +func _drawn_count(rend: HordeRenderer, n: int) -> int: + var drawn := 0 + for slot in n: + if rend.is_drawn(slot): + drawn += 1 + return drawn + + ## Ground enemies are drawn by pooled characters; flyers aren't. func _characters(r: GameTestRunner, sim: FarmSim) -> void: var es := sim.enemies diff --git a/prototype/scripts/horde_renderer.gd b/prototype/scripts/horde_renderer.gd index ed93888..38e4cbd 100644 --- a/prototype/scripts/horde_renderer.gd +++ b/prototype/scripts/horde_renderer.gd @@ -1,6 +1,6 @@ class_name HordeRenderer extends MultiMeshInstance3D -## Shared-mesh instanced rendering for pooled entities (birds, and projectiles via the same +## Shared-mesh instanced rendering for pooled entities (birds, UFOs, and projectiles via the same ## class). Active entities occupy the dense prefix [0, count); visible_instance_count is set to ## count, so released entities stop rendering immediately (no parking at a remote coordinate or ## zero scale). The buffer is written each physics tick and handed to the RenderingServer with @@ -107,6 +107,18 @@ func commit(count: int) -> void: _reset_count = 0 +## Whether a slot is drawn: inside the visible prefix and not scaled to nothing (a batch that +## shares its slots with entities it doesn't draw, like EnemySystem's, hides them that way). +func is_drawn(slot: int) -> bool: + if slot < 0 or slot >= multimesh.visible_instance_count: + return false + var o := slot * STRIDE + var sq := 0.0 + for k: int in [0, 1, 2, 4, 5, 6, 8, 9, 10]: + sq += buffer[o + k] * buffer[o + k] + return sq > 1e-6 + + ## Restart: drop everything and make the next commit snap instead of interpolate. func clear() -> void: _reset_count = 0