Skip to content

Add an ability with a cooldown ​

Goal ​

Space spends one pulse charge. A second pulse is rejected until its two-second cooldown ends.

Files you will edit ​

  • src/game.ts

Steps ​

  1. Use this isolated definition in the template shell. Only a successful use may perform the ability's effect. Here the effect is a HUD counter.

    ts
    import { createAbilities, defineGame, type GameContext } from 'gameable';
    
    const skills = createAbilities({
      owners: 1,
      abilities: [{ id: 'pulse', cooldown: 2, charges: 1 }],
    });
    let pulses = 0;
    const model = { bars: { pulses: { value: 0, max: 10 }, charge: { value: 1, max: 1 } } };
    function pulse(ctx: GameContext): void {
      skills.step(ctx);
      if (ctx.input.pressed('Space') && skills.use(ctx, 0, 'pulse')) pulses++;
      model.bars.pulses.value = pulses;
      model.bars.charge.value = skills.charges(0, 'pulse');
      ctx.hud.set(model);
    }
    export default defineGame({
      init() {
        skills.reset();
        pulses = 0;
      },
      systems: [{ run: pulse, on: 'authority' }],
      snapshot: () => ({ skills: skills.snapshot(), pulses }),
      restore: (state) => {
        const saved = state as { skills: unknown; pulses: number };
        skills.restore(saved.skills);
        pulses = saved.pulses;
      },
    });
  2. Put a dash, projectile or heal inside the successful-use branch. Owners are seats, not entity ids. On a room authority, use the validated sender seat and publish charges through its HUD/messages. A client may show a cosmetic prediction but must not grant the effect. More charges recharge one at a time; spending during recharge does not restart its clock.

Verify ​

sh
npm run dev

Press Space: pulses becomes 1 and charge becomes 0. Release and press again before two simulated seconds: pulses stays 1. At 60 Hz charge returns after 120 subsequent steps; the next press raises pulses to 2.

See also ​