Float a number over a unit
Floating words are set by kind in the view's hud.floats: damage, heal, xp, mana, levelUp, or one of your game's events by id. client.floats.show floats any words you like.
ts
// in src/game.ts
import { presetFloats, type GameView } from '@v12-engine/client'
import { createGame } from '@v12-engine/engine'
import { rules } from './rules'
export const view: GameView = {
units: {
looks: {}
},
hud: {
floats: {
// the ready-made damage, healing and experience, the damage in gold
...presetFloats(['damage', 'heal', 'xp'], { damage: { style: { color: '#ffd23c' } } }),
// damage taken by the player's own units: red, drifting left, fast hits one number counting up
damageTaken: {
text: '-{amount}',
style: { color: '#ff5a5a', size: 18, weight: 700 },
motion: [{ drift: { x: -26, up: 1 }, ease: 'outQuad' }, { pop: { from: 1.4, seconds: 0.15, ease: 'outBack' } }],
merge: { window: 0.8, countUp: 0.25, pulse: { pop: { from: 1.3, seconds: 0.15 } } },
seconds: 1.1,
most: 60,
within: 60
},
// mana spent, only for spells that cost much (a function of what is floated: a def, or nothing)
mana: (input) => ((input.amount ?? 0) < -50 ? { text: '{+amount}', style: { color: '#5aa8ff' }, at: 'chest' } : null),
// a game event of its own (`defineEvent({ id: 'mygame.combo', replicate: 'everyone' })`) by its id: its payload's
// `entity` (or `id`) is where it floats, its `amount` the number
'mygame.combo': {
text: 'Combo ×{amount}',
style: { color: '#ff8aff', background: { color: 'rgba(20, 0, 20, 0.7)', padding: 4, radius: 6 } },
motion: [{ bounce: { height: 0.6, times: 2, seconds: 0.8 } }]
},
// nothing for a miss
miss: null
}
}
}
export const game = createGame(rules).view(view)Done: damage floats in gold, damage to your own units in red with fast hits merged into one counting number, big mana costs in blue, combos bounce, and misses show nothing.
Float words in code
ts
client.floats.show('Rally!', { on: heroId })
// a number told more as it goes: a channel's ticking heal, counted up
const channel = client.floats.show({ text: '+{amount}', style: { color: '#5cff7a' }, merge: { window: 2, countUp: 0.3 } }, { on: heroId, amount: 10 })
channel?.update(40)
channel?.end()
// what the damage floats from now on, and back to the view's
client.floats.set('damage', { text: '{amount}', style: { color: '#fff' } })
client.floats.set('damage', undefined)Float a resource of your own
Emit it from the rules and it floats as resource.<kind>:
ts
world.events.emit(resourceChanged, { entity, kind: 'rage', amount: 12 })In the editor
- Open the Words panel (the speech box on the right rail).
- Fire a kind over a unit: Hit, ×10 fast, Crit, Heal, XP, Level up.
- Tune its look, motions, life and merging.
- It writes
floats.json, laid over the view withlayFiles(FLOATS_FILE).
Reference
| Field | What it does |
|---|---|
text | Words; {amount} and {+amount} are the number. |
style | color, size, weight, background. |
motion | drift, pop, bounce… with an ease. |
merge | Join hits within window seconds into one number (countUp, pulse). |
seconds, most, within | Life, cap, and distance from the camera. |
at | Where on the unit: chest… |
render | Overlay canvas (default, cheapest), dom (class vg-float), gui, or world (hidden behind things). |
A function in place of a def decides per float, or returns null for none. See Pings, floating words and plates.