Finding out what a game does in its first frames, or right at one spot of a flow, needed a quick hand on the pause button. Now the editor can start a play paused, step it frame by frame and let a behavior pause it. - The pause button can be pressed in edit mode. Then Play starts paused, and every further Play does too until it is released. A reload of the page clears it. - A third button, Next frame, right of Pause, or Ctrl+Shift+Enter. It works only while paused and runs exactly one frame of 1/60 s, whatever the frame took on screen. Hierarchy and inspector follow. - The Break node pauses the play like the button, at the end of the frame it ran in. The status line names the behavior's object. It is the editor's pause, not Pause game. - Break reaches the editor through a service only the editor hands to a play. In play.html and Run in editor the node does nothing. - Restart game keeps the running play's state. Start paused only counts when Play is pressed. - Wiki Concepts, Behaviors, Keyboard: the buttons, the node and the shortcut. Not tried: sound, free-running videos and a running recording while stepping. Sound and videos stay held during the pause. Tests: - tsc, eslint and Prettier are clean. - Unit pause, node-catalog, tooltip: 465 green. svg-icons now expects the step icon too. - e2e 18 and 46 on stein2 (melektaus-atx was off): ALL PASSED, 63 checks, 7 new in 18j. A step moves the box by 1.667 px at 100 px/s, Break stops the play with "Break in Breaker". - Counter-checks: a session that ignores the step turns 18j red. A Break that does not report its object turns the unit test red. - Gate on stein2: tsc, eslint and Prettier clean, 305 files, 4722 tests, 6 more than before. One was red at first, the icon list of the top bar did not know step yet. Fixed in svg-icons.test.ts and run again. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
225 lines
8.1 KiB
TypeScript
225 lines
8.1 KiB
TypeScript
/**
|
||
* The game's own pause and time scale (#167): Pause game holds the world from the next frame and
|
||
* leaves only the behaviors that run while paused ticking; Set time scale turns the clock a played
|
||
* game hands the behaviors, and nothing else.
|
||
*/
|
||
import { beforeAll, describe, expect, it } from 'vitest';
|
||
import { AddColliderCommand } from '../../src/core/commands/colliderCommands';
|
||
import { SetAssetPropertyCommand } from '../../src/core/commands/assetCommands';
|
||
import type { BehaviorAsset } from '../../src/core/model/Asset';
|
||
import { GameClock } from '../../src/core/time';
|
||
import { loadPhysics } from '../../src/runtime/physics/engine';
|
||
import {
|
||
COLLISION_SYSTEMS,
|
||
graphEditor,
|
||
makeBehavior,
|
||
makeSpriteScene,
|
||
place,
|
||
startWorld,
|
||
type SceneFixture,
|
||
} from '../helpers';
|
||
|
||
/** Wires `<event>(params) → Log(message)`. */
|
||
function logOn(f: SceneFixture, behavior: BehaviorAsset, event: string, message: string, params = {}): void {
|
||
const edit = graphEditor(f, behavior);
|
||
edit.connect(edit.addNode(event, params), 'exec', edit.addNode('Log', { message }), 'exec');
|
||
}
|
||
|
||
/**
|
||
* A box with two behaviors: "Game" logs every frame, "Menu" runs while paused, logs every frame and
|
||
* toggles the pause on P – the one-key toggle a pause menu is built from.
|
||
*/
|
||
function pauseMenu() {
|
||
const f = makeSpriteScene('Box');
|
||
const box = place(f, f.spriteId, { x: 0, y: 0 });
|
||
const game = makeBehavior(f, 'Game');
|
||
logOn(f, game, 'OnUpdate', 'game');
|
||
const menu = makeBehavior(f, 'Menu');
|
||
f.stack.execute(new SetAssetPropertyCommand(menu.id, 'runWhilePaused', true));
|
||
logOn(f, menu, 'OnUpdate', 'menu');
|
||
const edit = graphEditor(f, menu);
|
||
const key = edit.addNode('OnKeyDown', { key: 'p' });
|
||
const paused = edit.addNode('IsGamePaused');
|
||
const branch = edit.addNode('Branch');
|
||
const resume = edit.addNode('ResumeGame');
|
||
const pause = edit.addNode('PauseGame');
|
||
edit.wire([
|
||
[key, 'exec', branch, 'exec'],
|
||
[paused, 'paused', branch, 'condition'],
|
||
[branch, 'then', resume, 'exec'],
|
||
[branch, 'else', pause, 'exec'],
|
||
]);
|
||
place(f, game.id, { x: 0, y: 0 }, box);
|
||
place(f, menu.id, { x: 0, y: 0 }, box);
|
||
return startWorld(f, { behaviors: true });
|
||
}
|
||
|
||
describe('Pause game', () => {
|
||
it('holds the world from the next frame, and only a behavior that runs while paused is ticked', () => {
|
||
const { world, input, frame } = pauseMenu();
|
||
frame(0.1);
|
||
expect(world.log).toEqual(['game', 'menu']);
|
||
|
||
input.press('p');
|
||
frame(0.1);
|
||
// The frame that asked still runs whole: the pause starts with the next one.
|
||
expect(world.log.slice(2)).toEqual(['game', 'menu']);
|
||
expect(world.paused).toBe(true);
|
||
const heldAt = world.time;
|
||
|
||
world.log.length = 0;
|
||
frame(0.1);
|
||
frame(0.1);
|
||
expect(world.log).toEqual(['menu', 'menu']);
|
||
expect(world.time).toBe(heldAt);
|
||
|
||
input.release('p');
|
||
input.press('p');
|
||
frame(0.1);
|
||
expect(world.paused).toBe(false);
|
||
world.log.length = 0;
|
||
frame(0.1);
|
||
expect(world.log).toEqual(['game', 'menu']);
|
||
expect(world.time).toBeCloseTo(heldAt + 0.1);
|
||
});
|
||
|
||
it('switches nothing off: a paused behavior runs on from where it stood, without an On disable', () => {
|
||
const f = makeSpriteScene('Box');
|
||
const box = place(f, f.spriteId, { x: 0, y: 0 });
|
||
const game = makeBehavior(f, 'Game');
|
||
logOn(f, game, 'OnDisable', 'disabled');
|
||
logOn(f, game, 'OnEnable', 'enabled');
|
||
logOn(f, game, 'OnTimer', 'timer', { interval: 0.25 });
|
||
place(f, game.id, { x: 0, y: 0 }, box);
|
||
const { world, frame } = startWorld(f, { behaviors: true });
|
||
frame(0.1);
|
||
frame(0.1);
|
||
world.paused = true;
|
||
for (let i = 0; i < 10; i++) frame(0.1);
|
||
world.paused = false;
|
||
frame(0.1);
|
||
// 0.3 s of unpaused time: the timer fired once, and the paused second did not count.
|
||
expect(world.log).toEqual(['enabled', 'timer']);
|
||
});
|
||
});
|
||
|
||
describe.each(COLLISION_SYSTEMS)('$system', ({ physics }) => {
|
||
beforeAll(async () => {
|
||
await loadPhysics();
|
||
});
|
||
|
||
it('tells a behavior running while paused no collision again: nothing collides in a held world', () => {
|
||
const f = makeSpriteScene('Crate');
|
||
const a = place(f, f.spriteId, { x: 0, y: 0 });
|
||
const b = place(f, f.spriteId, { x: 50, y: 0 });
|
||
f.stack.execute(new AddColliderCommand(f.sceneId, a));
|
||
f.stack.execute(new AddColliderCommand(f.sceneId, b));
|
||
const hit = makeBehavior(f, 'Hit');
|
||
f.stack.execute(new SetAssetPropertyCommand(hit.id, 'runWhilePaused', true));
|
||
logOn(f, hit, 'OnCollisionEnter', 'enter');
|
||
logOn(f, hit, 'OnCollisionStay', 'stay');
|
||
place(f, hit.id, { x: 0, y: 0 }, a);
|
||
const { world, frame } = startWorld(f, { physics, collisions: true, behaviors: true });
|
||
|
||
frame(0.1);
|
||
frame(0.1);
|
||
expect(world.log).toEqual(['enter', 'stay']);
|
||
world.paused = true;
|
||
frame(0.1);
|
||
frame(0.1);
|
||
expect(world.log).toEqual(['enter', 'stay']);
|
||
world.paused = false;
|
||
frame(0.1);
|
||
expect(world.log).toEqual(['enter', 'stay', 'stay']);
|
||
});
|
||
});
|
||
|
||
describe('Break', () => {
|
||
/** `On key down(b) → Break → Log(after)`, and `On update → Log(frame)`. */
|
||
function breaker() {
|
||
const f = makeSpriteScene('Box');
|
||
const box = place(f, f.spriteId, { x: 0, y: 0 });
|
||
const behavior = makeBehavior(f, 'Breaker');
|
||
logOn(f, behavior, 'OnUpdate', 'frame');
|
||
const edit = graphEditor(f, behavior);
|
||
const key = edit.addNode('OnKeyDown', { key: 'b' });
|
||
const halt = edit.addNode('Break');
|
||
const after = edit.addNode('Log', { message: 'after' });
|
||
edit.wire([
|
||
[key, 'exec', halt, 'exec'],
|
||
[halt, 'exec', after, 'exec'],
|
||
]);
|
||
place(f, behavior.id, { x: 0, y: 0 }, box);
|
||
return f;
|
||
}
|
||
|
||
it("tells the editor's Play which behavior object broke, and the frame and flow run to their end", () => {
|
||
const breaks: string[] = [];
|
||
const { world, input, frame } = startWorld(breaker(), {
|
||
behaviors: true,
|
||
breakpoint: (where) => breaks.push(where),
|
||
});
|
||
frame(0.1);
|
||
expect(breaks).toEqual([]);
|
||
input.press('b');
|
||
frame(0.1);
|
||
expect(breaks).toEqual(['Breaker']);
|
||
expect(world.log).toEqual(['frame', 'after', 'frame']);
|
||
// It is the editor's pause, not the game's own.
|
||
expect(world.paused).toBe(false);
|
||
});
|
||
|
||
it('goes past without a breakpoint, as a released game and the editor run do', () => {
|
||
const { world, input, frame } = startWorld(breaker(), { behaviors: true });
|
||
input.press('b');
|
||
frame(0.1);
|
||
frame(0.1);
|
||
expect(world.log).toEqual(['after', 'frame', 'frame']);
|
||
expect(world.paused).toBe(false);
|
||
});
|
||
});
|
||
|
||
describe('Set time scale', () => {
|
||
/** `On start → Set time scale(scale)`, and `On update → Log(Time scale)`. */
|
||
function scaler(scale: number) {
|
||
const f = makeSpriteScene('Box');
|
||
const box = place(f, f.spriteId, { x: 0, y: 0 });
|
||
const behavior = makeBehavior(f, 'Scaler');
|
||
const edit = graphEditor(f, behavior);
|
||
const start = edit.addNode('OnStart');
|
||
const set = edit.addNode('SetTimeScale', { scale });
|
||
const update = edit.addNode('OnUpdate');
|
||
const read = edit.addNode('TimeScale');
|
||
const log = edit.addNode('Log');
|
||
edit.wire([
|
||
[start, 'exec', set, 'exec'],
|
||
[update, 'exec', log, 'exec'],
|
||
[read, 'scale', log, 'message'],
|
||
]);
|
||
place(f, behavior.id, { x: 0, y: 0 }, box);
|
||
return f;
|
||
}
|
||
|
||
it('turns the clock a played game hands over, and Time scale reads it back', () => {
|
||
const clock = new GameClock();
|
||
const { world, frame } = startWorld(scaler(0.5), { behaviors: true, clock });
|
||
frame();
|
||
expect(clock.scale).toBe(0.5);
|
||
expect(world.log).toEqual(['0.5']);
|
||
});
|
||
|
||
it('is ignored without a clock – the editor run, which shares its clock with the previews', () => {
|
||
const { world, frame } = startWorld(scaler(0.5), { behaviors: true });
|
||
frame();
|
||
expect(world.log).toEqual(['Set time scale is ignored in the editor run (0.5)', '1']);
|
||
});
|
||
|
||
it('ends with the run: resetting the clock puts it back to real time', () => {
|
||
const clock = new GameClock();
|
||
clock.scale = 0.25;
|
||
clock.advance(0.05);
|
||
clock.reset();
|
||
expect({ now: clock.now, scale: clock.scale }).toEqual({ now: 0, scale: 1 });
|
||
});
|
||
});
|