characters.test.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import { describe, expect, it } from 'vitest';
  2. import { loadCharacterStats, loadMap } from '../src/server/assets';
  3. import { Game } from '../src/server/game';
  4. import { Anim, CHARACTER_IDS } from '../src/shared/characters';
  5. import { MATERIAL_WOOL, TICK_RATE } from '../src/shared/constants';
  6. import { Button } from '../src/shared/input';
  7. import type { FxEvent } from '../src/shared/protocol';
  8. import { ASSETS, realMaterials } from './helpers';
  9. const materials = realMaterials();
  10. function newGame(): Game {
  11. const { definition, terrain } = loadMap(ASSETS, 'gateau-etages', materials);
  12. return new Game({ map: definition, terrain, materials, seed: 5, loadCharacter: (id) => loadCharacterStats(ASSETS, id) });
  13. }
  14. function run(game: Game, ticks: number): FxEvent[] {
  15. const fx: FxEvent[] = [];
  16. for (let i = 0; i < ticks; i++) fx.push(...game.step().fx);
  17. return fx;
  18. }
  19. const me = (game: Game, id: number) => game.snapshot().find((p) => p.id === id)!;
  20. const countWool = (game: Game): number => game.terrain.data.reduce((n, m) => n + (m === MATERIAL_WOOL ? 1 : 0), 0);
  21. /** Presses `buttons` aimed at (x, y) for one tick, then releases. */
  22. function press(game: Game, id: number, buttons: number, x: number, y: number): FxEvent[] {
  23. game.setInput(id, { buttons, aimX: Math.round(x), aimY: Math.round(y) });
  24. const fx = run(game, 1);
  25. game.setInput(id, { buttons: 0, aimX: Math.round(x), aimY: Math.round(y) });
  26. return fx;
  27. }
  28. describe('roster', () => {
  29. it('every character has data that loads', () => {
  30. expect(CHARACTER_IDS).toEqual(['castor', 'mamie', 'pyromanchot', 'generic']);
  31. for (const id of CHARACTER_IDS) expect(loadCharacterStats(ASSETS, id).id).toBe(id);
  32. });
  33. });
  34. describe('Mamie Tricot', () => {
  35. it('her yarn ball lays a taut wool bridge that holds: nothing falls, nobody is hurt', () => {
  36. const game = newGame();
  37. const { id } = game.addPlayer('Mamie', 'mamie');
  38. game.teleport(id, 640, 440); // middle tier, left of the top tier wall (x ≈ 780)
  39. run(game, 60);
  40. const m = me(game, id);
  41. let detached = 0;
  42. let crushed = 0;
  43. game.setInput(id, { buttons: Button.Special, aimX: 800, aimY: Math.round(m.y - 40) });
  44. for (let i = 0; i < 4 * TICK_RATE; i++) {
  45. const r = game.step();
  46. if (i === 0) game.setInput(id, { buttons: 0, aimX: 800, aimY: Math.round(m.y - 40) });
  47. detached += r.terrain.filter((e) => e.ev.kind === 'detach').length;
  48. crushed += r.crushed.length;
  49. }
  50. expect(countWool(game)).toBeGreaterThan(300);
  51. expect(detached).toBe(0);
  52. expect(crushed).toBe(0);
  53. expect(me(game, id).anim & Anim.Dead).toBe(0);
  54. });
  55. it('the thread follows the ball in flight, tied under her feet', () => {
  56. const game = newGame();
  57. const { id } = game.addPlayer('Mamie', 'mamie');
  58. game.teleport(id, 640, 440);
  59. run(game, 60);
  60. const m = me(game, id);
  61. press(game, id, Button.Special, 800, m.y - 40);
  62. const ball = game.itemStates().find((it) => it.kind === 'yarn')!;
  63. expect(ball.thread).toBeDefined();
  64. const [ax, ay] = ball.thread!;
  65. expect(Math.abs(ax - m.x)).toBeLessThan(20);
  66. expect(ay).toBeGreaterThan(m.y); // on the ground under her
  67. expect(game.terrain.isSolid(Math.round(ax), Math.round(ay))).toBe(true);
  68. });
  69. it('a yarn ball thrown at her own feet never buries or kills her', () => {
  70. const game = newGame();
  71. const { id } = game.addPlayer('Mamie', 'mamie');
  72. game.teleport(id, 640, 440);
  73. run(game, 60);
  74. for (const [ax, ay] of [[0, 60], [0, -200], [40, 20], [-40, 20]]) {
  75. const m = me(game, id);
  76. press(game, id, Button.Special, m.x + ax, m.y + ay);
  77. run(game, 3 * TICK_RATE);
  78. const after = me(game, id);
  79. expect(after.anim & Anim.Dead).toBe(0);
  80. // Not stuck inside wool: no solid pixel at her centre.
  81. expect(game.terrain.isSolid(Math.round(after.x), Math.round(after.y))).toBe(false);
  82. }
  83. });
  84. it('her needles barely push but stun for a long time', () => {
  85. const game = newGame();
  86. const mamie = game.addPlayer('Mamie', 'mamie');
  87. const victim = game.addPlayer('Victime', 'generic');
  88. run(game, 90);
  89. const m = me(game, mamie.id);
  90. game.teleport(victim.id, m.x + 40, m.y);
  91. press(game, mamie.id, Button.Attack, m.x + 40, m.y);
  92. const v = me(game, victim.id);
  93. expect(v.anim & Anim.Stunned).toBeTruthy();
  94. expect(Math.hypot(v.vx, v.vy)).toBeLessThan(400);
  95. run(game, Math.round(0.6 * TICK_RATE));
  96. expect(me(game, victim.id).anim & Anim.Stunned).toBeTruthy();
  97. });
  98. it('her wool burns: an explosion nearby consumes it', () => {
  99. const game = newGame();
  100. const mamie = game.addPlayer('Mamie', 'mamie');
  101. const pyro = game.addPlayer('Pyro', 'pyromanchot');
  102. game.teleport(mamie.id, 640, 440);
  103. run(game, 60);
  104. const m = me(game, mamie.id);
  105. press(game, mamie.id, Button.Special, 800, m.y - 40);
  106. run(game, 2 * TICK_RATE);
  107. const before = countWool(game);
  108. expect(before).toBeGreaterThan(0);
  109. // A firecracker thrown into the wool.
  110. game.teleport(pyro.id, 700, 300);
  111. run(game, 2);
  112. const fx = press(game, pyro.id, Button.Attack, 760, m.y - 30).concat(run(game, TICK_RATE));
  113. expect(fx.some((e) => e.kind === 'explosion')).toBe(true);
  114. run(game, 2);
  115. expect(countWool(game)).toBeLessThan(before);
  116. });
  117. });
  118. describe('Pyromanchot', () => {
  119. it('throws firecrackers that explode away from him, and every third one backfires', () => {
  120. const game = newGame();
  121. const { id } = game.addPlayer('Pyro', 'pyromanchot');
  122. run(game, 90);
  123. const explosions: { x: number; y: number }[] = [];
  124. for (let shot = 0; shot < 3; shot++) {
  125. const p = me(game, id);
  126. const fx = press(game, id, Button.Attack, p.x + 300, p.y - 100).concat(run(game, TICK_RATE));
  127. for (const e of fx) if (e.kind === 'explosion') explosions.push({ x: e.x - p.x, y: e.y - p.y });
  128. run(game, Math.round(0.5 * TICK_RATE));
  129. }
  130. expect(explosions).toHaveLength(3);
  131. // The first two flew off; the third went off right next to him.
  132. expect(Math.hypot(explosions[0].x, explosions[0].y)).toBeGreaterThan(60);
  133. expect(Math.hypot(explosions[1].x, explosions[1].y)).toBeGreaterThan(60);
  134. expect(Math.hypot(explosions[2].x, explosions[2].y)).toBeLessThan(40);
  135. });
  136. it('a backfire stuns him', () => {
  137. const game = newGame();
  138. const { id } = game.addPlayer('Pyro', 'pyromanchot');
  139. run(game, 90);
  140. for (let shot = 0; shot < 3; shot++) {
  141. const p = me(game, id);
  142. press(game, id, Button.Attack, p.x + 300, p.y - 100);
  143. if (shot < 2) run(game, TICK_RATE);
  144. }
  145. expect(me(game, id).anim & Anim.Stunned).toBeTruthy();
  146. });
  147. it('belly slide: fast, then an explosion at the end that spares him', () => {
  148. const game = newGame();
  149. const pyro = game.addPlayer('Pyro', 'pyromanchot');
  150. const victim = game.addPlayer('Victime', 'generic');
  151. game.teleport(pyro.id, 900, 680); // bottom tier, lots of room to the right
  152. game.teleport(victim.id, 1500, 680);
  153. run(game, 60);
  154. const start = me(game, pyro.id);
  155. press(game, pyro.id, Button.Special, start.x + 500, start.y);
  156. expect(me(game, pyro.id).anim & Anim.Special).toBeTruthy();
  157. const fx = run(game, TICK_RATE);
  158. const end = me(game, pyro.id);
  159. expect(end.x - start.x).toBeGreaterThan(200);
  160. expect(fx.some((e) => e.kind === 'explosion')).toBe(true);
  161. expect(end.anim & Anim.Stunned).toBe(0);
  162. });
  163. });