Break a building to pieces when it is destroyed
Give the building's look a death with a collapse of crumble: when it dies, its own model breaks into pieces that slump where it stood, in a cloud of dust.
// in src/rules.ts, in place of the starter's: raiders of yours, and a hut of an enemy's to knock down
import { createGame, createMap, defineIds, type GameMap, type IdOf } from '@v12-engine/engine'
export const UNIT = defineIds('raider', 'hut')
export type Unit = IdOf<typeof UNIT>
export const game = createGame({ id: 'my-game', name: 'My game', checkView: 'throw', victory: null })
game.unit({
id: UNIT.raider,
name: 'Raider',
kind: 'unit',
radius: 0.4,
sight: 10,
// an attack makes it fight: 40 damage a second, from close by
stats: { health: 200, moveSpeed: 5, attackDamage: 40, attackSpeed: 1, attackRange: 1 },
attacks: [{ acquisitionRange: 6, damagePoint: 0.3, damageType: 'physical' }]
})
game.unit({
id: UNIT.hut,
name: 'Hut',
kind: 'building',
radius: 1,
// a building sits on whole cells: this one on 2 by 2
footprint: { w: 2, d: 2 },
sight: 4,
stats: { health: 600 }
})
// a flat field with two players: you are player 1, the hut is player 2's (an enemy)
function field(): GameMap {
const map = createMap({ name: 'Field', width: 32, depth: 32, players: 2, game: 'my-game' })
map.players[0].start = { x: 10, z: 16 }
map.players[1].start = { x: 22, z: 16 }
return map
}
game.maps({ field: field() })
game.onStart((world) => {
// three raiders for you, and the hut across the field
for (let i = 0; i < 3; i++) world.spawn(UNIT.raider, { at: { x: 10, z: 15 + i }, owner: 1 })
world.spawn(UNIT.hut, { at: { x: 22, z: 16 }, owner: 2 })
})// in src/view.ts, in place of the starter's
import { type GameView, presetView } from '@v12-engine/client'
// the shared art (the starter's assets option); or a .glb of your own in public/models/
import { rts } from '@v12-engine/assets'
import type { Unit } from './rules'
export const view: GameView<Unit> = presetView({
units: {
looks: {
raider: {
height: 1.4,
shapes: [{ shape: 'cylinder', size: [0.6, 1, 0.6], offset: [0, 0.5, 0], color: 'team' }]
},
hut: {
// where its health bar goes
height: 2,
// drawn while the model loads
shapes: [{ shape: 'box', size: [2, 1.6, 2], offset: [0, 0.8, 0], color: [0.5, 0.4, 0.3] }],
// the model, made 2 across (it is 2.6 across in its file); a point over its roof, in the file's units
model: { src: rts.models.hut, scale: 0.75, points: { roof: { offset: [0, 1.9, 0] } } },
death: {
// it comes down: crumbling into about 20 pieces of itself, with dust where it stood
collapse: { look: 'crumble', pieces: 20, dust: true }
}
}
}
}
})Done: select your raiders and right-click the hut. When its health runs out, the hut breaks into about 20 pieces of its own model. They slump where it stood in a cloud of dust, lie a few seconds, then sink into the ground.
The pieces fly with physics, which is loaded the first time something breaks. Where there is none (the Low quality, or Debris set to none), the hut sinks into the ground in one piece instead.
Explode outward from the killer
explode blows the model apart: 32 pieces when you say nothing, flung away from where the killer stood. Add an effect and a sound as it goes.
import type { DeathDef } from '@v12-engine/client'
// as the hut's `death`
export const death: DeathDef = {
collapse: { look: 'explode', pieces: 32, dust: true },
// how hard the pieces fly: 1 a blow, 2 a blast (an explosion is 2.5 when you say nothing)
strength: 3,
// the ready-made explosion, at its roof, and smoke half a second later
fx: [{ fx: 'explosion', point: 'roof', params: { size: 1.5 } }, { fx: 'smoke', delay: 0.5 }],
// a sound of your game's (sounds/blast.mp3)
sound: 'blast'
}Topple away from the attacker
topple tips the whole building over, away from the killer, then sinks it. It needs no physics.
import type { DeathDef } from '@v12-engine/client'
export const death: DeathDef = {
// it falls over in the first 70% of its time, then sinks for the rest
collapse: { look: 'topple', time: 2, dust: true },
fx: 'rubble-fall'
}With no killer (a building the rules removed), it falls a way of its own.
Sink when sold, crumble when destroyed
The rules give each death a reason. Name your own when you remove a building: world.kill(building, undefined, false, 'sold'). Then turn the reason into a how in the death hook, and give the look an entry for it.
import { createGame } from '@v12-engine/engine'
export const game = createGame({ id: 'my-game', name: 'My game', victory: null })
// after the engine's own handlers, so this word is the last
game.after('death', (_world, death) => {
if (death.reason === 'sold') death.how = 'sold'
})import type { DeathDef } from '@v12-engine/client'
export const death: DeathDef = {
collapse: { look: 'crumble', dust: true },
// a sold one sinks quietly in one piece instead: these fields go over the ones above
how: { sold: { collapse: { look: 'sink', time: 2 }, fx: 'dust' } }
}The reasons the engine gives are damage, killed (world.kill), cancelled, expired, depleted, felled and removed.
Throw pieces of your own
debris throws pieces you describe: solids of a size and a colour, as many of each as count. The building is gone at once in their place. With shatter too, you get both: pieces of itself and yours.
import type { DeathDef } from '@v12-engine/client'
export const death: DeathDef = {
debris: [
// planks, stones and straw
{ shape: 'box', size: [0.9, 0.12, 0.25], color: [0.45, 0.3, 0.18], count: 10 },
{ shape: 'sphere', size: [0.35, 0.3, 0.35], color: [0.5, 0.5, 0.48], count: 8 },
{ shape: 'box', size: [0.3, 0.05, 0.3], color: [0.85, 0.75, 0.4], count: 6 }
],
strength: 1.5,
fx: 'rubble-fall'
}A piece is a solid (box, cylinder, cone, sphere, pyramid), not a model of your own. For broken walls or beams made in a modelling tool, leave them as remains (below), or play an effect with a model part.
Trail smoke from flying pieces, and raise dust where they land
pieces puts an effect on some of the pieces as they fly, and another where each comes to rest. It works for crumble, explode, shatter and debris.
import type { DeathDef } from '@v12-engine/client'
export const death: DeathDef = {
collapse: { look: 'explode', dust: true },
pieces: {
// a smoke trail on 30% of them (a third when you say nothing)
fx: 'smoke-trail',
share: 0.3,
// a puff of dust where each lands
landed: 'dust',
// knobs for both effects
params: { size: 0.4 }
}
}Leave ruins for a while, then let them sink
remains puts a model where the building stood. It lies there time seconds (60), then sinks into the ground over fade seconds (3).
import type { DeathDef } from '@v12-engine/client'
export const death: DeathDef = {
collapse: { look: 'sink', time: 1.5, dust: true },
// a model of yours under models/; it appears as the building is most of the way down
remains: { model: 'ruins/hut_ruins.glb', scale: 0.75, time: 45, fade: 4 }
}Ruins are only drawn. Nothing walks around them or clears them. Rubble that blocks the way, that workers clear or that gives stone is a unit: spawn one where the building stood in a death hook.
End differently by what killed it
The look's how holds a death for each word: burnt, crushed, blasted. Its fields go over the rest of death.
import type { DeathDef } from '@v12-engine/client'
export const death: DeathDef = {
collapse: { look: 'crumble', dust: true },
how: {
// burnt: fire, and it sinks in one piece
burnt: { fx: 'fire', collapse: { look: 'sink', time: 3 } },
// crushed: many more pieces, thrown harder
crushed: { collapse: { look: 'crumble', pieces: 40 }, strength: 1.5 },
// blasted: blown apart
blasted: { collapse: { look: 'explode' }, fx: 'explosion' }
}
}The rules say the word. Set it on an attack, an ability or a damage effect, or pass it when you deal damage in code. The death hook has the last say.
import { createGame, type AbilityDef, type AttackDef } from '@v12-engine/engine'
// a catapult's attack: what it kills is crushed, or blasted on a critical strike
export const boulder: AttackDef = {
acquisitionRange: 10,
damagePoint: 0.5,
projectileSpeed: 12,
how: (hit) => (hit.crit ? 'blasted' : 'crushed')
}
// a spell: what it kills is burnt
export const firestorm: AbilityDef = {
id: 'firestorm',
name: 'Firestorm',
target: 'point',
range: 9,
how: 'burnt',
// a damage effect can say its own, over the ability's
effects: [{ type: 'damage', amount: 300, radius: 3, how: 'burnt' }]
}
export const game = createGame({ id: 'my-game', name: 'My game', victory: null })
// the last word, in the hook: a building killed by a blow tagged 'fire' is burnt, whatever the blow said
game.after('death', (_world, death) => {
if (death.entity.def.kind === 'building' && death.cause?.tags?.includes('fire')) death.how = 'burnt'
})In code, pass it with the damage: world.damage(target, 80, source, { how: 'crushed', tags: ['siege'] }).
A tree or a building that burns down in a game with the burning pack (burningPack) dies burnt. See Let trees burn.
Smoke under half health, fire under a quarter
damaged turns looks on while health is under a share. Each turns off again as the building is repaired.
import type { DamagedStage } from '@v12-engine/client'
// as the hut's `damaged`
export const damaged: DamagedStage[] = [
// smoke from its roof under half health
{ below: 0.5, fx: 'smoke', at: 'roof', params: { size: 0.8 } },
// fire under a quarter, with a sound as it catches
{ below: 0.25, fx: 'fire', at: 'roof', sound: 'whoosh' },
// under a tenth, drawn as another model: the broken hut
{ below: 0.1, model: { src: 'ruins/hut_broken.glb', scale: 0.75 } }
]To decide in code, give a function. It runs again whenever health changes, and gives the stages that are on now: damaged: (entity, health) => (health < 0.3 ? [{ id: 'smoke', below: 1, fx: 'smoke' }] : []).
Flash and shake on each hit
onHit plays on every blow: an effect where it is struck, a flash of colour, a shake of the view, a sound.
import type { UnitLook } from '@v12-engine/client'
// as the hut's `onHit`: sparks and a white flash on every hit; a shake only on a big one
export const onHit: UnitLook['onHit'] = (_entity, hit) => ({
fx: 'spark-burst',
params: { count: 6 },
flash: [1, 1, 1],
...(hit.amount > 100 && { shake: 0.4 })
})As plain data in code: onHit: { fx: 'spark-burst', flash: { color: [1, 0.8, 0.6], time: 0.15 } }.
Decide each death in code
onDeath on a look decides for each death of that type. It gets the death as the player's screen hears of it: who killed (killerType), with what (ability, damageType, tags), how, why (reason), from where (from). Its fields go over death.
import type { UnitLook } from '@v12-engine/client'
// as the hut's `onDeath`: blown apart only when a catapult killed it; otherwise as `death` says
export const onDeath: UnitLook['onDeath'] = (_entity, death) =>
death.killerType === 'catapult' ? { collapse: { look: 'explode' }, fx: 'explosion' } : undefinedGameView.units.deathFor decides for every type of the game, before the type has its say:
import type { GameView } from '@v12-engine/client'
export const view: GameView = {
units: {
looks: {},
// every building that burns down sinks in smoke, whatever its look says
deathFor: (entity, death) =>
entity.def.kind === 'building' && death.how === 'burnt' ? { collapse: { look: 'sink', time: 3 }, fx: 'smoke' } : undefined
}
}Without physics
Pieces fly with physics (Havok). The client loads it the first time something breaks, not before. Until then, or where it cannot load, nothing breaks:
| Death | With physics | Without |
|---|---|---|
collapse: 'crumble', 'explode' | Pieces of itself | Sinks into the ground in one piece |
collapse: 'sink', 'topple' | As it says | As it says |
shatter, debris | Pieces | A building sinks; a unit falls over |
fx, sound, dust, remains | Played | Played |
The player's Debris setting (Settings) caps the pieces: none on Low, 80 on Medium, 240 on High. At none, nothing breaks apart.
Many buildings at once
Breaking a model into pieces takes a few milliseconds. Each break waits its turn under the frame's budget (GameView.world.frameBudget: debris may take 4 ms a frame), the nearest the camera first, so a whole town falling does not freeze a frame. All pieces together are capped by the Debris setting: past it, the oldest go first. Pieces lie 3.5 seconds, sink for 1 more, and are gone.
So a big death costs little when you keep pieces modest (20 to 30 a building). A dozen buildings crumbling at once on High is 240 pieces at most.
What is data and what is code
| Part | Data (looks.json, the editor) | Code only |
|---|---|---|
death: collapse, shatter, debris, pieces, fx, sound, remains, how | yes | |
damaged as a list of stages | yes | |
damaged as a function | yes | |
onHit, onDeath, GameView.units.deathFor | yes | |
The rules' how as a word on an attack or ability | yes (data.json) | |
The rules' how as a function, the death hook | yes |
A death set in looks.json replaces the code's death and onDeath for that type.
In the editor
- In Content, right-click the building and pick Look into it. The Model view opens.
- Open Death, damage, icon, portrait, sounds, then the Death tab.
- Under How it comes down, pick Comes down: Sinks in one piece, Crumbles where it stood, Is blown apart or Topples over. Set Over (s) and turn on Dust.
- Press An effect as it dies to add an effect, with the point it plays at and a delay. Set Breaks in pieces and How hard for
shatterandstrength. - Under What is left, type or drop a Model, and set Lies (s).
- Press Play it to watch it die on the model. Drag Hit from to see it topple or fly from another side. Pick a how (Killed: any how) to play that death.
- Open the Damage tab. Drag Health to see each stage come on. Press A look under …% to add one, then set Under, Effect, Clip and Model.
- Every change is written into
looks.jsonat once. As the code's takes it out again.
pieces, debris and the entries of how have no field in the tab yet: write them in looks.json or in code.
Reference
DeathDef
| Field | Default | What it does |
|---|---|---|
collapse | sink | A building: sink, crumble, explode, topple; or { look, time, dust, pieces }: seconds it comes down over (1.2), dust (true for the ready-made, or an effect), pieces of itself (24 crumbling, 32 exploding) |
strength | 0.4 crumble, 2.5 explode | How hard pieces fly: 1 a blow, 2 a blast |
shatter | none | Breaks its model into about this many pieces (any unit, not only buildings) |
debris | none | Pieces you describe (DebrisPiece: shape, size, color, count) |
pieces | none | { fx, share, landed, params }: an effect on a share of flying pieces, one where each lands |
fx | none | Effects as it dies: names, or DeathFx (fx, point, delay, params, offset) |
sound | none | A sound of the game's from where it died |
remains | none | { model, time: 60, fade: 3, scale, decal }: what is left, then sinks |
how | none | A DeathDef for each word the rules give |
keep, none | no | Left as it is for your code (client.renderer.kit.letGo(id) lets it go), or gone at once |
Also
| Name | What it does |
|---|---|
UnitLook.damaged | DamagedStages: below, fx, at, params, model, clip, sound |
UnitLook.onHit | HitLook: fx, at, params, flash, shake, sound; or a function |
UnitLook.onDeath | (entity, death) => DeathLook, over death |
GameView.units.deathFor | The same for every type, first |
AttackDef.how, AbilityDef.how, EffectFields.how | The word for what the blow kills |
world.kill(entity, killer, attack, reason) | A death with a reason of your own |
death hook | how and reason of every death, to change |
See Destruction and debris for bodies, burning and the rest of how things die.