Make an effect of my own
A new kind of ability effect is one function (defineEffect) listed in your rules' effects. Declare its fields in EffectTypes so abilities that use it are type-checked.
ts
// in src/rules.ts
import { defineComponent, defineEffect, defineIds, type Rules } from '@v12-engine/engine'
// the game's own kind of effect, with what it reads
declare module '@v12-engine/engine' {
interface EffectTypes {
pull: { distance: number }
}
}
export const UNIT = defineIds('warden')
export const ABILITY = defineIds('pull')
/** How often a unit was pulled: kept on the unit, saved with it */
const pulled = defineComponent<{ times: number }>({ id: 'mygame.pulled', default: () => ({ times: 0 }) })
/** Drags the target toward the caster by so many cells, and counts it */
const pull = defineEffect({ type: 'pull', run: (context, effect) => {
const { world, caster, target } = context
if (!target) return
const dx = caster.pos.x - target.pos.x
const dz = caster.pos.z - target.pos.z
const away = Math.hypot(dx, dz) || 1
const step = Math.min(effect.distance, Math.max(away - 1, 0))
// put down where it lands: the world keeps it on the ground and tells the screen
world.place(target, { x: target.pos.x + (dx / away) * step, z: target.pos.z + (dz / away) * step })
const state = target.get(pulled) ?? { times: 0 }
target.set(pulled, { times: state.times + 1 })
} })
export const rules: Rules = {
id: 'my-game',
name: 'My game',
components: [pulled],
effects: [pull],
units: [
{
id: UNIT.warden,
name: 'Warden',
kind: 'unit',
radius: 0.4,
sight: 9,
stats: { health: 120, moveSpeed: 4, mana: 80 },
abilities: [ABILITY.pull]
}
],
abilities: [
{
id: ABILITY.pull,
name: 'Pull',
target: 'unit',
range: 7,
manaCost: 25,
cooldown: 8,
// the game's own kind of effect, with what it needs
effects: [{ type: 'pull', distance: 3 }]
}
],
victory: null
}Done: the warden's Pull drags its target 3 cells closer and counts it on the target.
Keep it deterministic
- Draw random numbers from
world.rng, neverMath.random. - Don't read the clock or anything outside the world.
- Keep state between casts in a component listed in
components: it is saved with the entity.
That way a replay and a server play the same game.
Reference
The function gets the cast's context and the effect's own fields:
| Context field | What it is |
|---|---|
world | The world. |
caster | The unit casting. |
target | The unit aimed at, when the ability has one. |
point | The point aimed at, when it has one. |
level | The ability's level. |
See EffectContext in the API reference, Abilities for the engine's own effects, and Saves, mods, data and components for components. The editor's Effects panel is for how an effect looks, not what it does.