xonecas.ioDon't click! Sean Caetano Martin Sign in
tiny-adventure / web / js / levelup.js 44 lines1.4 kB Raw
1import { flash } from './minis.js';
2import { bonusText } from './stats.js';
3import { T } from './theme.js';
4
5const HOP = 0.7;
6const LINE = 17;
7const HUSH = 1400;
8
9export function createLevelUp(party, cam, ui, sound) {
10  const hops = [];
11
12  function play(before, after) {
13    const prev = new Map(before.party.units.map((u) => [u.id, u]));
14    for (const u of after.party.units) {
15      const old = prev.get(u.id);
16      const fig = party.figures.get(u.id);
17      if (!old || !fig || u.level <= old.level) continue;
18      const gain = Object.fromEntries(Object.keys(u.stats).filter((k) => k !== 'hp').map((k) => [k, u.stats[k] - old.stats[k]]));
19      const gains = bonusText(gain).split('  ');
20      sound.play('level');
21      flash(fig, T.accent, 700);
22      hops.push({ fig, t: 0 });
23      ui.logLine(`${u.name} reaches level ${u.level}: ${gains.join(', ')}`, 'reward');
24      const at = ui.cardTop(u.id);
25      if (!at) continue;
26      const [x, y, k] = at;
27      ui.floater(x, y - LINE * k * (gains.length + 1), `Level ${u.level}`, 'level');
28      ui.hush(HUSH);
29      gains.forEach((g, i) => ui.floater(x, y - LINE * k * (gains.length - i), g, 'gain'));
30    }
31  }
32
33  function update(dt) {
34    for (let i = hops.length - 1; i >= 0; i--) {
35      const h = hops[i];
36      h.t += dt;
37      const p = Math.min(1, h.t / HOP);
38      h.fig.userData.body.position.y = 0.06 + Math.sin(p * Math.PI) * 0.35;
39      if (p >= 1) hops.splice(i, 1);
40    }
41  }
42
43  return { play, update };
44}