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>
92 lines
3.7 KiB
TypeScript
92 lines
3.7 KiB
TypeScript
/**
|
||
* Objects that travel through a `Load scene` (#165): which entities of the running world are kept,
|
||
* and how their objects are put into the scene loaded next.
|
||
*/
|
||
import type { SceneAsset } from '../core/model/Asset';
|
||
import { removeSubtrees } from '../core/model/hierarchy';
|
||
import { findAsset, type Project } from '../core/model/Project';
|
||
import type { SceneObject } from '../core/model/SceneObject';
|
||
import { KEEP_ON_SCENE_LOAD_KEY } from '../core/properties/PropertySchema';
|
||
import { resolveSceneObjectProps } from '../core/properties/resolve';
|
||
import type { Entity } from './Entity';
|
||
|
||
/** Whether the switch is on for an object as the scene stores it. */
|
||
function isKept(project: Project, object: SceneObject): boolean {
|
||
const asset = findAsset(project, object.assetId);
|
||
return (
|
||
asset !== undefined && resolveSceneObjectProps(project, asset, object)[KEEP_ON_SCENE_LOAD_KEY] === true
|
||
);
|
||
}
|
||
|
||
/**
|
||
* The entities a scene swap keeps: every top-level one with the switch on, and all below it. Only a
|
||
* top-level object can be kept – one further down would arrive without the parent it is placed in.
|
||
*/
|
||
export function keptEntities(entities: readonly Entity[]): Entity[] {
|
||
const kept = new Set<Entity>();
|
||
// A parent may come after its child in the list, so the chain is walked rather than the order trusted.
|
||
const travels = (entity: Entity): boolean => {
|
||
let top = entity;
|
||
while (top.parent) top = top.parent;
|
||
return top.props[KEEP_ON_SCENE_LOAD_KEY] === true;
|
||
};
|
||
for (const entity of entities) if (travels(entity)) kept.add(entity);
|
||
return entities.filter((e) => kept.has(e));
|
||
}
|
||
|
||
/**
|
||
* The stored objects of the kept entities, as copies ready for another scene: parented as the run
|
||
* has them now, and in no hierarchy folder, which belongs to the scene they leave. An entity the
|
||
* scene stores no object for cannot travel and is left out of `entities`.
|
||
*/
|
||
export function objectsToBring(
|
||
scene: SceneAsset,
|
||
entities: readonly Entity[],
|
||
): { entities: Entity[]; objects: SceneObject[] } {
|
||
const stored = new Map(scene.objects.map((o) => [o.id, o]));
|
||
const brought: Entity[] = [];
|
||
const objects: SceneObject[] = [];
|
||
for (const entity of entities) {
|
||
const object = stored.get(entity.objectId);
|
||
if (!object) continue;
|
||
const copy = structuredClone(object);
|
||
copy.parentId = entity.parent?.objectId ?? '';
|
||
delete copy.groupId;
|
||
brought.push(entity);
|
||
objects.push(copy);
|
||
}
|
||
return { entities: brought, objects };
|
||
}
|
||
|
||
/**
|
||
* Puts the brought objects into the scene, after its own. What the scene holds of the same thing is
|
||
* not built, the one brought along wins: an object of the same id – the scene the object came from,
|
||
* loaded again – and a top-level object of the same asset that is kept itself, which is how each
|
||
* level can place its own player and still end up with one. Answers the ids of the objects left out
|
||
* that way, each with the id of the object standing in for it.
|
||
*/
|
||
export function bringObjects(
|
||
project: Project,
|
||
scene: SceneAsset,
|
||
brought: readonly SceneObject[],
|
||
): Map<string, string> {
|
||
const ids = new Set(brought.map((o) => o.id));
|
||
const tops = brought.filter((o) => o.parentId === '');
|
||
const standIns = new Map<string, string>();
|
||
const leftOut: string[] = [];
|
||
for (const object of scene.objects) {
|
||
if (ids.has(object.id)) {
|
||
leftOut.push(object.id);
|
||
continue;
|
||
}
|
||
if (object.parentId !== '') continue;
|
||
const standIn = tops.find((top) => top.assetId === object.assetId);
|
||
if (!standIn || !isKept(project, object)) continue;
|
||
leftOut.push(object.id);
|
||
standIns.set(object.id, standIn.id);
|
||
}
|
||
removeSubtrees(scene, leftOut);
|
||
scene.objects.push(...brought);
|
||
return standIns;
|
||
}
|