diff --git a/packages/engine/src/scenes/map.scene.ts b/packages/engine/src/scenes/map.scene.ts index d2711293..1e339ddb 100644 --- a/packages/engine/src/scenes/map.scene.ts +++ b/packages/engine/src/scenes/map.scene.ts @@ -8,6 +8,7 @@ import type { MapResource } from '../resource/MapResource.ts' import type { SpriteSetResource } from '../resource/SpriteSetResource.ts' import { areObjectsVisible } from '../services/editor-view.ts' import { setCollisionActive } from '../services/collision-mode.ts' +import { isRuntimeModeActive } from '../services/runtime-mode.ts' import type { GameSystemSpec } from '../game-systems/game-system-spec.ts' import { CameraControlSystem, @@ -169,7 +170,9 @@ export class MapScene extends Scene { const def = resolvePlacementDefinition(placement, this.entityLibrary) if (!def) return const layersById = new Map(mapData.layers.map((l) => [l.id, l])) - const entity = buildPlacementEntity(placement, def, this.mapResource, layersById, this.componentRegistry) + const entity = buildPlacementEntity(placement, def, this.mapResource, layersById, this.componentRegistry, { + runtime: isRuntimeModeActive(this), + }) // Respect the global objects toggle for live spawns (place / undo). if (entity instanceof Actor && !areObjectsVisible(this)) entity.graphics.isVisible = false this.add(entity) diff --git a/packages/engine/src/services/actor-depth.ts b/packages/engine/src/services/actor-depth.ts index aa212be8..a9915d36 100644 --- a/packages/engine/src/services/actor-depth.ts +++ b/packages/engine/src/services/actor-depth.ts @@ -9,10 +9,18 @@ import { depthZ, isDepthSorted } from './depth-sort.ts' * `DepthSortSystem` when that plane sorts. The one entry point for * spawn, the hero's spawn and a layer's plane change, so they cannot * disagree about the formula. The `visual` component's `depth` override - * (read off `SpriteRefComponent`) wins over the sort. The foot is the bottom edge of the actor's - * box, which is its picture. + * (read off `SpriteRefComponent`) wins over the sort. The foot is the bottom + * edge of the actor's PICTURE: `footOffset` (distance from the position to it) + * defaults to half the actor's box, which is the picture for a placement; the + * hero's box is shrunk to his foot collider, so he passes his sprite height / 2. */ -export function applyActorDepth(actor: Actor, plane: LayerPlane, mapHeight: number, elevation = 0): void { +export function applyActorDepth( + actor: Actor, + plane: LayerPlane, + mapHeight: number, + elevation = 0, + footOffset?: number, +): void { // A thing pinned behind / in front is flat like an off-plane one: no // marker, so `DepthSortSystem` never revisits it. const depth = actor.get(SpriteRefComponent)?.depth ?? 'sorted' @@ -21,7 +29,8 @@ export function applyActorDepth(actor: Actor, plane: LayerPlane, mapHeight: numb actor.z = depthZ({ plane, footY: 0, mapHeightPx: mapHeight, elevation, depth }) return } - const sort = actor.get(DepthSortComponent) ?? new DepthSortComponent(actor.height / 2, actor.pos.y, elevation) + const sort = + actor.get(DepthSortComponent) ?? new DepthSortComponent(footOffset ?? actor.height / 2, actor.pos.y, elevation) if (!actor.has(DepthSortComponent)) actor.addComponent(sort) sort.lastY = actor.pos.y actor.z = depthZ({ plane, footY: actor.pos.y + sort.footOffset, mapHeightPx: mapHeight, elevation }) diff --git a/packages/engine/src/systems/depth-sort.system.spec.ts b/packages/engine/src/systems/depth-sort.system.spec.ts index 6962217d..7e147a1b 100644 --- a/packages/engine/src/systems/depth-sort.system.spec.ts +++ b/packages/engine/src/systems/depth-sort.system.spec.ts @@ -39,6 +39,31 @@ export default async () => { expect(hero.z).toBeGreaterThan(tree.z) }) + await it('takes the hero foot from his sprite, not from his 8 px foot collider', async () => { + const scene = new Scene() + const system = new DepthSortSystem(mapResource) + system.onInitialize(scene.world, scene) + // Sign one tile south of the hero's centre: foot at y 168 + 8 = 176. + const sign = new Actor({ x: 80, y: 168, width: 16, height: 16 }) + applyActorDepth(sign, 'hero', HEIGHT) + // 16x32 sprite, box shrunk to the foot collider like PlayerSystem does. + const hero = new Actor({ x: 80, y: 160, width: 16, height: 8 }) + hero.addComponent(new MovementComponent(4)) + applyActorDepth(hero, 'hero', HEIGHT, 0, 16) + scene.add(sign) + scene.add(hero) + // Hero foot 176 equals the sign's foot 176: spawn order decides, not a + // 4 px offset that would put the hero 12 px short and behind the sign. + expect(hero.z).toBe(sign.z) + hero.pos.y = 164 + system.onUpdate(16) + expect(hero.z).toBeGreaterThan(sign.z) + // North of the sign: the sign draws over him. + hero.pos.y = 140 + system.onUpdate(16) + expect(hero.z).toBeLessThan(sign.z) + }) + await it('writes z only when y changed', async () => { const { system, hero } = rig() hero.z = 12345 diff --git a/packages/engine/src/systems/object-spawn.system.spec.ts b/packages/engine/src/systems/object-spawn.system.spec.ts new file mode 100644 index 00000000..5d6ce9c8 --- /dev/null +++ b/packages/engine/src/systems/object-spawn.system.spec.ts @@ -0,0 +1,52 @@ +import { describe, expect, it } from '@gjsify/unit' +import { type Actor, GraphicsGroup, Scene } from 'excalibur' +import { RuntimeModeComponent } from '../components/index.ts' +import type { MapResource } from '../resource/MapResource.ts' +import type { EntityDefinition, LayerData } from '../types/data/index.ts' +import { SessionState } from '../utils/session-state.ts' +import { ObjectSpawnSystem } from './object-spawn.system.ts' + +const layer: LayerData = { id: 'l1', name: 'L', visible: true, sprites: [] } +const def: EntityDefinition = { + id: 'spot', + name: 'Spot', + components: [{ type: 'trigger', on: 'walk-onto' }], + editorData: { template: 'trigger' }, +} +const mapResource = { + mapData: { + tileWidth: 16, + tileHeight: 16, + rows: 8, + columns: 8, + layers: [layer], + objectPlacements: [{ id: 'p1', layerId: 'l1', tileX: 1, tileY: 1, inline: def }], + }, + getSpriteSetResource: () => undefined, +} as unknown as MapResource + +export default async () => { + await describe('ObjectSpawnSystem', async () => { + // Graphics need a DOM canvas, which only GJS / the browser provide. + const domAvailable = typeof document !== 'undefined' + + const spawn = (runtime: boolean): Actor | undefined => { + const scene = new Scene() + if (runtime) SessionState.set(scene, new RuntimeModeComponent()) + new ObjectSpawnSystem(mapResource).onInitialize(scene.world, scene) + return scene.actors.find((a) => a.name.endsWith(':p1')) + } + + await it('draws no editor frame when the scene is built mid-run', async () => { + if (!domAvailable) return + const group = spawn(true)?.graphics.current + expect(group instanceof GraphicsGroup && group.members.length).toBe(0) + }) + + await it('keeps the editor frame and marker in the editor', async () => { + if (!domAvailable) return + const group = spawn(false)?.graphics.current + expect(group instanceof GraphicsGroup && group.members.length).toBe(2) + }) + }) +} diff --git a/packages/engine/src/systems/object-spawn.system.ts b/packages/engine/src/systems/object-spawn.system.ts index 031cfcb7..6b36722d 100644 --- a/packages/engine/src/systems/object-spawn.system.ts +++ b/packages/engine/src/systems/object-spawn.system.ts @@ -5,6 +5,7 @@ import { BUILT_IN_COMPONENT_SPECS } from '../entity/registry.ts' import { buildPlacementEntity, placementSpawnWarnings } from '../entity/spawn-placement.ts' import type { MapResource } from '../resource/MapResource.ts' import { areObjectsVisible } from '../services/editor-view.ts' +import { isRuntimeModeActive } from '../services/runtime-mode.ts' import type { EntityDefinition } from '../types/data/index.ts' /** @@ -53,6 +54,8 @@ export class ObjectSpawnSystem extends System { const layersById = new Map(mapData.layers.map((l) => [l.id, l])) const objectsVisible = areObjectsVisible(scene) + // A scene built while a run is on (teleport) must not get editor frames. + const runtime = isRuntimeModeActive(scene) for (const placement of mapData.objectPlacements) { const def = resolvePlacementDefinition(placement, this.entityLibrary) // Surface integrity gaps (dangling defId, incomplete required fields) @@ -61,7 +64,7 @@ export class ObjectSpawnSystem extends System { this.logger.warn(`[ObjectSpawnSystem] ${warning}`) } if (!def) continue - const entity = buildPlacementEntity(placement, def, this.mapResource, layersById, this.registry) + const entity = buildPlacementEntity(placement, def, this.mapResource, layersById, this.registry, { runtime }) // Respect the global objects toggle (Layers page "Objects" row). if (entity instanceof Actor && !objectsVisible) entity.graphics.isVisible = false scene.add(entity) diff --git a/packages/engine/src/systems/player.system.ts b/packages/engine/src/systems/player.system.ts index b9c910be..200d63b2 100644 --- a/packages/engine/src/systems/player.system.ts +++ b/packages/engine/src/systems/player.system.ts @@ -183,7 +183,9 @@ export class PlayerSystem extends System { // MovementComponent is what DepthSortSystem looks for; the speed is // read from the view model, so this one is only the marker. actor.addComponent(new MovementComponent(speedPxPerSec / tw)) - applyActorDepth(actor, 'hero', mapHeightPx(this.mapResource.mapData)) + // The foot is the bottom of the sprite, not of the 8 px foot collider + // that replaced the actor's box above. + applyActorDepth(actor, 'hero', mapHeightPx(this.mapResource.mapData), 0, actorHeight / 2) scene.add(actor) diff --git a/packages/engine/src/test.mts b/packages/engine/src/test.mts index 8be150b4..9c7bc7c9 100644 --- a/packages/engine/src/test.mts +++ b/packages/engine/src/test.mts @@ -115,6 +115,7 @@ import tileEditorSystemSuite from './systems/tile-editor.system.spec.js' import walkOnTileSuite from './systems/walk-on-tile.system.spec.js' import depthSortSuite from './services/depth-sort.spec.js' import depthSortSystemSuite from './systems/depth-sort.system.spec.js' +import objectSpawnSystemSuite from './systems/object-spawn.system.spec.js' import textLayoutSuite from './services/text-layout.spec.js' import fontChainSuite from './services/font-chain.spec.js' import fontMetricsSuite from './services/font-metrics.spec.js' @@ -235,6 +236,7 @@ run({ walkOnTileSuite, depthSortSuite, depthSortSystemSuite, + objectSpawnSystemSuite, textLayoutSuite, fontChainSuite, fontMetricsSuite,