Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/engine/src/scenes/map.scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 13 additions & 4 deletions packages/engine/src/services/actor-depth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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 })
Expand Down
25 changes: 25 additions & 0 deletions packages/engine/src/systems/depth-sort.system.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 52 additions & 0 deletions packages/engine/src/systems/object-spawn.system.spec.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
}
5 changes: 4 additions & 1 deletion packages/engine/src/systems/object-spawn.system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

/**
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion packages/engine/src/systems/player.system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 2 additions & 0 deletions packages/engine/src/test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -235,6 +236,7 @@ run({
walkOnTileSuite,
depthSortSuite,
depthSortSystemSuite,
objectSpawnSystemSuite,
textLayoutSuite,
fontChainSuite,
fontMetricsSuite,
Expand Down
Loading