A Load scene destroyed every object of the old scene. A player, a HUD or a music object had to be built again in each scene, with its state pushed through shared variables. A top-level object with the new switch Keep on scene load now travels into the next scene as the same entity, with everything below it. - The switch is a common property right under Enabled, off by default, on the asset and on a placed object. The inspector shows the row on top-level objects of a scene only, as answered in the issue. - runtime/keptObjects.ts holds the rules as pure functions. keptEntities picks what travels, objectsToBring copies the stored objects as the run parents them, and bringObjects puts them into the next scene. - World.instantiate takes a handed entity as it is instead of making one. The next syncToModel then writes the run's values into the scene. - BehaviorSystem.release takes the running instances out before the old world is destroyed, and the next system adopts them. They hear no On destroy and no second On start, and their sounds play on. - PhysicsSystem.resume gives a dynamic body the velocity and spin it had. - Duplicates: the object brought along wins. The next scene does not build its own object with the same id, which is the way back into the scene the object came from. - Decided here: the next scene does not build a top-level object of the same asset that is kept itself either. The issue only names the id. With the id alone a level that places its own player would hold two after a swap. - Decided here: kept objects land behind the scene's own, the switch is no animation row, and collisions are found anew. A finding, fixed along with it: Load scene did not build a scene as it is stored, but as the run had left it. A moved object stayed moved and a destroyed one stayed gone. The way back into a scene needs a clean build. - GameRun copies a scene's objects and groups before it first builds it and puts them back before it builds it again. - GameRun.end puts every such scene back. The player calls it on stop, so spawned objects no longer stay for "Click to play again". - The consequence: an edit made in a scene during Play is gone the next time that scene is loaded. Tests: - Gate: 199 files, 2944 tests (2929 + 15), exit 0 - e2e, full run: 1625 checks passed (1619 + 6), exit 0 - Smokes: 24 / 19 / 21, exit 0 - 15 unit tests in tests/core/keep-on-scene-load.test.ts - Six e2e checks in module 35: the switch is set with a real mouse, the child has no row, the object is in the new scene, its counter still reads 1, its sound plays on, and the stored scenes are unchanged after Stop - Eleven tests with fixed row lists only count the new schema row - 16 unit counter-probes all fail tests, run on stein2. One did not fail at first (same id not left out), it has its own test now. - The browser counter-probe fails 4 of the 6 new checks - Not looked at in the browser: the switch as a port of the Properties node, and a released game with a kept object Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
189 lines
7.2 KiB
TypeScript
189 lines
7.2 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
||
import {
|
||
CreateAssetCommand,
|
||
SetAssetPropertyCommand,
|
||
SetMachineScopeCommand,
|
||
} from '../../src/core/commands/assetCommands';
|
||
import { SetOverrideCommand } from '../../src/core/commands/sceneObjectCommands';
|
||
import { AddVariableCommand } from '../../src/core/commands/variableCommands';
|
||
import { pluginsOf } from '../../src/core/store/projectPlugins';
|
||
import { deserializeProject, serializeProject } from '../../src/core/store/serialize';
|
||
import { makeBehavior, makeSpriteScene, place, roundTrip } from '../helpers';
|
||
|
||
describe('serialize', () => {
|
||
it('round-trips a project', () => {
|
||
const f = makeSpriteScene();
|
||
const box = place(f, f.spriteId, { x: 7, y: 8 });
|
||
f.stack.execute(new SetOverrideCommand(f.sceneId, box, 'width', 33));
|
||
expect(roundTrip(f.project)).toEqual(f.project);
|
||
});
|
||
|
||
/**
|
||
* Which plugins the project switches on (#119). The editor reads them off the raw file before it
|
||
* deserializes anything, so `pluginsOf` is what both sides use and both have to agree.
|
||
*/
|
||
it('reads the plugins of a project the same way before and after deserializing', () => {
|
||
const raw = { id: 'p', name: 'P', plugins: ['b.js', 'a.js', 'a.js', '', 7] };
|
||
expect(pluginsOf(raw)).toEqual(['a.js', 'b.js']);
|
||
expect(deserializeProject(raw).plugins).toEqual(['a.js', 'b.js']);
|
||
// A project written before there was such a field, and anything that is not a list of names.
|
||
expect(pluginsOf({ id: 'p' })).toEqual([]);
|
||
expect(pluginsOf({ plugins: 'demo.js' })).toEqual([]);
|
||
expect(pluginsOf(null)).toEqual([]);
|
||
expect(deserializeProject({ id: 'p' }).plugins).toEqual([]);
|
||
});
|
||
|
||
/** A behavior's own rows are not the common ones: coercing against those alone dropped the value. */
|
||
it('keeps a behavior running in the editor across a save', () => {
|
||
const f = makeSpriteScene();
|
||
const behavior = makeBehavior(f, 'B');
|
||
f.stack.execute(new SetAssetPropertyCommand(behavior.id, 'runInEditor', true));
|
||
const loaded = roundTrip(f.project).assets.find((a) => a.id === behavior.id)!;
|
||
expect(loaded.props.runInEditor).toBe(true);
|
||
});
|
||
|
||
/** The declarations of a Variables asset and their starting values are the whole asset. */
|
||
it('round-trips a Variables asset and the scope of a state machine', () => {
|
||
const f = makeSpriteScene();
|
||
const shared = new CreateAssetCommand('variables', 'Score');
|
||
f.stack.execute(shared);
|
||
f.stack.execute(new AddVariableCommand(shared.asset.id, 'score', 'number'));
|
||
f.stack.execute(new SetAssetPropertyCommand(shared.asset.id, 'score', 7));
|
||
const machine = new CreateAssetCommand('statemachine', 'M');
|
||
f.stack.execute(machine);
|
||
f.stack.execute(new SetMachineScopeCommand(machine.asset.id, 'global'));
|
||
expect(roundTrip(f.project)).toEqual(f.project);
|
||
});
|
||
|
||
/** `local` is the absence of the field, so a project made before scopes existed saves back byte-alike. */
|
||
it('writes no scope for a local machine and reads an unknown one as local', () => {
|
||
const f = makeSpriteScene();
|
||
makeBehavior(f, 'B', ['score']);
|
||
f.stack.execute(new CreateAssetCommand('statemachine', 'M'));
|
||
expect(JSON.stringify(serializeProject(f.project))).not.toContain('scope');
|
||
|
||
const loaded = deserializeProject({
|
||
assets: [{ id: 'm1', type: 'statemachine', name: 'M', scope: 'planet' }],
|
||
});
|
||
const machine = loaded.assets[0]!;
|
||
if (machine.type !== 'statemachine') throw new Error('wrong types');
|
||
expect(machine.scope).toBeUndefined();
|
||
});
|
||
|
||
/** #41: the borders are part of the frame, and a frame nobody sliced still saves without them. */
|
||
it('round-trips the nine-slice of a frame and repairs an impossible one', () => {
|
||
const file = `${'ab'.repeat(32)}.png`;
|
||
const loaded = deserializeProject({
|
||
assets: [
|
||
{
|
||
id: 't1',
|
||
type: 'texture',
|
||
name: 'Sheet',
|
||
file,
|
||
width: 64,
|
||
height: 64,
|
||
frames: [
|
||
{
|
||
id: 'f1',
|
||
name: 'Button',
|
||
x: 0,
|
||
y: 0,
|
||
w: 24,
|
||
h: 24,
|
||
border: { left: 8, right: 8 },
|
||
fill: 'stretch',
|
||
},
|
||
{ id: 'f2', name: 'Plain', x: 24, y: 0, w: 24, h: 24 },
|
||
{
|
||
id: 'f3',
|
||
name: 'Greedy',
|
||
x: 0,
|
||
y: 24,
|
||
w: 8,
|
||
h: 8,
|
||
border: { top: 99, bottom: 99 },
|
||
fill: 'wat',
|
||
},
|
||
],
|
||
},
|
||
],
|
||
});
|
||
const texture = loaded.assets[0]!;
|
||
if (texture.type !== 'texture') throw new Error('wrong types');
|
||
const [sliced, plain, greedy] = texture.frames;
|
||
expect(sliced?.border).toEqual({ left: 8, top: 0, right: 8, bottom: 0 });
|
||
// Only the fill that is not the default is written; an unknown one reads as the default.
|
||
expect(sliced?.fill).toBe('stretch');
|
||
expect(plain?.border).toBeUndefined();
|
||
expect(plain?.fill).toBeUndefined();
|
||
// Measured against the frame, not the image, and never leaving it without a middle.
|
||
expect(greedy?.border).toEqual({ left: 0, top: 4, right: 0, bottom: 3 });
|
||
expect(greedy?.fill).toBeUndefined();
|
||
expect(roundTrip(loaded)).toEqual(loaded);
|
||
});
|
||
|
||
it('repairs broken documents', () => {
|
||
const project = deserializeProject(
|
||
{
|
||
name: 'Broken',
|
||
activeSceneId: 'missing',
|
||
assets: [
|
||
{
|
||
id: 's1',
|
||
type: 'scene',
|
||
name: 'Scene',
|
||
props: { backgroundColor: '#000000', junk: 1 },
|
||
objects: [
|
||
{
|
||
id: 'o1',
|
||
assetId: 'sp1',
|
||
position: { x: '5', y: null },
|
||
overrides: { color: '#00ff00', width: -3, foo: 1 },
|
||
},
|
||
{ id: 'o2', assetId: 'nope', position: {} },
|
||
{ id: 'o3', assetId: 's1', position: {} },
|
||
],
|
||
},
|
||
{ id: 'sp1', type: 'sprite', props: { width: 10 } },
|
||
// An asset type this build does not know (a newer document, a renamed type). It used to
|
||
// be `sound`, which this build does know since #45 – hence a type nothing has claimed.
|
||
{ id: 'x', type: 'video' },
|
||
{ type: 'camera' },
|
||
],
|
||
},
|
||
'fallback-id',
|
||
);
|
||
expect(project.id).toBe('fallback-id');
|
||
expect(project.activeSceneId).toBe('s1');
|
||
expect(project.assets.map((a) => a.id)).toEqual(['s1', 'sp1']);
|
||
const scene = project.assets[0]!;
|
||
expect(scene.type === 'scene' && scene.props).toEqual({ backgroundColor: 0, gravityX: 0, gravityY: 980 });
|
||
const objects = scene.type === 'scene' ? scene.objects : [];
|
||
expect(objects).toHaveLength(1);
|
||
expect(objects[0]).toEqual({
|
||
id: 'o1',
|
||
assetId: 'sp1',
|
||
name: '',
|
||
position: { x: 5, y: 0, z: 0 },
|
||
editorOffset: { x: 0, y: 0 },
|
||
overrides: { color: 0x00ff00, width: 1 },
|
||
parentId: '',
|
||
instanceEdits: {},
|
||
});
|
||
expect(project.assets[1]!.name).toBe('sprite');
|
||
expect(project.assets[1]!.props).toEqual({
|
||
enabled: true,
|
||
keepOnSceneLoad: false,
|
||
scale: 1,
|
||
rotation: 0,
|
||
mirrorX: false,
|
||
mirrorY: false,
|
||
width: 10,
|
||
height: 100,
|
||
color: 0xffffff,
|
||
texture: '',
|
||
shader: '',
|
||
});
|
||
});
|
||
});
|