A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
feat(game): a death that ends the run and starts the next one
esengine12145bc47f3c
diff --git a/examples/celestial-heights/assets/i18n/en.eslocale b/examples/celestial-heights/assets/i18n/en.eslocaleindex b9a72d643..dd1f96746 100644--- a/examples/celestial-heights/assets/i18n/en.eslocale+++ b/examples/celestial-heights/assets/i18n/en.eslocale@@ -15,6 +15,7 @@"achievement.wayfarer": "Unlocked — Wayfarer","area.spire": "Spire of Vesper","boss.vesper": "Vesper of the Waning Moon",- "achievement.spire-cleared": "Unlocked — Starfall"+ "achievement.spire-cleared": "Unlocked — Starfall",+ "menu.fallen": "Fallen"}}diff --git a/examples/celestial-heights/assets/i18n/zh-CN.eslocale b/examples/celestial-heights/assets/i18n/zh-CN.eslocaleindex 05f4dfb23..0700ed573 100644--- a/examples/celestial-heights/assets/i18n/zh-CN.eslocale+++ b/examples/celestial-heights/assets/i18n/zh-CN.eslocale@@ -15,6 +15,7 @@"achievement.wayfarer": "达成——远行者","area.spire": "暮星塔顶","boss.vesper": "暮星 · 维斯珀",- "achievement.spire-cleared": "达成——星陨"+ "achievement.spire-cleared": "达成——星陨",+ "menu.fallen": "陨落"}}diff --git a/examples/celestial-heights/assets/scenes/hud.esscene b/examples/celestial-heights/assets/scenes/hud.essceneindex ed4f8b897..6128c1c24 100644--- a/examples/celestial-heights/assets/scenes/hud.esscene+++ b/examples/celestial-heights/assets/scenes/hud.esscene@@ -11,7 +11,8 @@51,60,70,- 80+ 80,+ 84],"visible": true,"components": [@@ -718,6 +719,116 @@"data": {}}]+ },+ {+ "id": 84,+ "name": "FallenPanel",+ "parent": 50,+ "children": [+ 85+ ],+ "visible": true,+ "components": [+ {+ "type": "Transform",+ "data": {}+ },+ {+ "type": "UINode",+ "data": {+ "position": 1,+ "display": 1,+ "insetLeft": {+ "value": 0,+ "unit": 0+ },+ "insetRight": {+ "value": 0,+ "unit": 0+ },+ "insetTop": {+ "value": 0,+ "unit": 0+ },+ "insetBottom": {+ "value": 0,+ "unit": 0+ }+ }+ },+ {+ "type": "FlexContainer",+ "data": {+ "direction": 1,+ "justifyContent": 1,+ "alignItems": 1,+ "gap": {+ "x": 0,+ "y": 0+ }+ }+ },+ {+ "type": "UIVisual",+ "data": {+ "visualType": 1,+ "color": {+ "r": 0.039,+ "g": 0.024,+ "b": 0.063,+ "a": 0.82+ },+ "enabled": true+ }+ },+ {+ "type": "FallenOverlay",+ "data": {}+ }+ ]+ },+ {+ "id": 85,+ "name": "FallenLabel",+ "parent": 84,+ "children": [],+ "visible": true,+ "components": [+ {+ "type": "Transform",+ "data": {}+ },+ {+ "type": "UINode",+ "data": {+ "height": {+ "value": 64,+ "unit": 0+ },+ "width": {+ "value": 100,+ "unit": 1+ }+ }+ },+ {+ "type": "Text",+ "data": {+ "content": "",+ "i18nKey": "menu.fallen",+ "fontSize": 56,+ "bold": true,+ "color": {+ "r": 0.906,+ "g": 0.353,+ "b": 0.412,+ "a": 1+ },+ "align": 1,+ "verticalAlign": 1+ }+ }+ ]}]}diff --git a/examples/celestial-heights/src/components.ts b/examples/celestial-heights/src/components.tsindex 9c76fd591..c4c68fa95 100644--- a/examples/celestial-heights/src/components.ts+++ b/examples/celestial-heights/src/components.ts@@ -82,6 +82,9 @@ export const VitalityMeter = defineTag('VitalityMeter');/** The panel shown while the game is paused. */export const PauseOverlay = defineTag('PauseOverlay');+/** The screen shown for the couple of seconds a run takes to end and restart. */+export const FallenOverlay = defineTag('FallenOverlay');+/** The HUD's boss bar: shown only while there is a boss to report on. */export const BossPanel = defineTag('BossPanel');diff --git a/examples/celestial-heights/src/main.ts b/examples/celestial-heights/src/main.tsindex f78fdb3f3..6ce389553 100644--- a/examples/celestial-heights/src/main.ts+++ b/examples/celestial-heights/src/main.ts@@ -4,7 +4,7 @@ import {import './components';import './actions';-import { session } from './state';+import { frozen } from './state';import { playerMoveSystem, playerAttackSystem } from './systems/player';import {meleeResolveSystem, damageSystem, invulnerabilitySystem, deathSystem,@@ -17,6 +17,7 @@ import {cameraBindSystem, navFromTerrainSystem, gateSystem, hudSystem, areaLabelSystem,} from './systems/world';import { pauseInputSystem, pauseTimeSystem, pauseOverlaySystem } from './systems/pause';+import { fallenSystem, fallenOverlaySystem } from './systems/fallen';import { applySettingsSystem, cycleLanguageSystem } from './systems/settings';import {saveRunSystem, loadRunSystem, applyRestoreSystem, rememberRosterSystem,@@ -52,25 +53,27 @@ addSystemSetToSchedule(Schedule.Update, defineSystemSet('gameplay', {playerMoveSystem, playerAttackSystem, perceiverFacingSystem, gateSystem, pickupSystem,bossPhaseSystem, summonSystem, chargeSystem,],- runIf: () => !session.paused,+ runIf: () => !frozen(),}));// Damage resolves after everyone has decided to swing, and death after damage,// so a hit lands in the frame it was thrown rather than the next one.addSystemSetToSchedule(Schedule.PostUpdate, defineSystemSet('combat', {systems: [meleeResolveSystem, damageSystem, deathSystem, invulnerabilitySystem, hitSparkSystem],- runIf: () => !session.paused,+ runIf: () => !frozen(),}));// The pause itself and everything that reports state keep running: one has to be// able to lift the pause, the rest have to be able to show it.addSystemToSchedule(Schedule.Update, pauseInputSystem);+addSystemToSchedule(Schedule.Update, fallenSystem);addSystemToSchedule(Schedule.Update, pauseTimeSystem);addSystemToSchedule(Schedule.Update, cycleLanguageSystem);addSystemToSchedule(Schedule.Update, saveRunSystem);addSystemToSchedule(Schedule.Update, loadRunSystem);addSystemToSchedule(Schedule.Update, inventoryInputSystem);addSystemToSchedule(Schedule.PostUpdate, pauseOverlaySystem);+addSystemToSchedule(Schedule.PostUpdate, fallenOverlaySystem);addSystemToSchedule(Schedule.PostUpdate, healthBarSystem);addSystemToSchedule(Schedule.PostUpdate, vitalityMeterSystem);addSystemToSchedule(Schedule.PostUpdate, bossMeterSystem);diff --git a/examples/celestial-heights/src/state.ts b/examples/celestial-heights/src/state.tsindex 761b95d66..3552a584e 100644--- a/examples/celestial-heights/src/state.ts+++ b/examples/celestial-heights/src/state.ts@@ -17,4 +17,11 @@ export const session = {inventoryOpen: false,/** Settings: whether hit sparks and other flourishes are drawn. */effects: true,+ /** Seconds left of the fallen screen; > 0 means the run is over and waiting. */+ fallenFor: 0,};++/** Whether the world is being held still — by the player, or by her death. */+export function frozen(): boolean {+ return session.paused || session.fallenFor > 0;+}diff --git a/examples/celestial-heights/src/systems/fallen.ts b/examples/celestial-heights/src/systems/fallen.tsnew file mode 100644index 000000000..01664e2b3--- /dev/null+++ b/examples/celestial-heights/src/systems/fallen.ts@@ -0,0 +1,58 @@+import {+ defineSystem, Query, Mut, Res, EventReader, Time, SceneManager, UINode, UIDisplay,+} from 'esengine';+import { FallenOverlay } from '../components';+import { Died } from '../events';+import { saves, SLOT, type RunState } from '../save';+import { session } from '../state';++/** How long the fallen screen holds before the run picks itself back up. */+const FALLEN_SECONDS = 2.2;++/**+ * What happens when Lyra runs out. A death that only empties a bar is a game+ * that stopped without saying so, so this is the one place the run ends and+ * starts again: the last save if there is one, and otherwise the area she fell+ * in, brought back up from its own data.+ */+export const fallenSystem = defineSystem(+ [EventReader(Died), Res(Time), Res(SceneManager)],+ (deaths, time, scenes) => {+ for (const death of deaths) {+ if (death.isPlayer && session.fallenFor <= 0) session.fallenFor = FALLEN_SECONDS;+ }+ if (session.fallenFor <= 0) return;++ // Real seconds: the fallen screen is not part of the simulation, and the+ // simulation is exactly what has been stopped.+ session.fallenFor -= time.unscaledDelta;+ if (session.fallenFor > 0) return;+ session.fallenFor = 0;++ const run = saves.load<RunState>(SLOT);+ if (run) {+ session.inventory = { ...(run.pack ?? {}) };+ session.restore = run;+ if (scenes.getActive() !== run.area) {+ void scenes.switchTo(run.area, { transition: 'fade', duration: 0.3 });+ return;+ }+ }+ // No save, or one taken in this very area: either way the area itself has+ // to come back — its enemies are as dead as she is.+ void scenes.reload({ transition: 'fade', duration: 0.3 });+ },+ { name: 'FallenSystem' },+);++/** Freezes the world while the fallen screen is up, and shows it. */+export const fallenOverlaySystem = defineSystem(+ [Query(Mut(UINode), FallenOverlay)],+ (overlays) => {+ const display = session.fallenFor > 0 ? UIDisplay.Flex : UIDisplay.None;+ for (const [, node] of overlays) {+ if (node.display !== display) node.display = display;+ }+ },+ { name: 'FallenOverlaySystem' },+);diff --git a/examples/celestial-heights/src/systems/pause.ts b/examples/celestial-heights/src/systems/pause.tsindex e90dad1d3..47d6134ad 100644--- a/examples/celestial-heights/src/systems/pause.ts+++ b/examples/celestial-heights/src/systems/pause.ts@@ -1,7 +1,7 @@import { defineSystem, Query, Mut, ResMut, Time, UINode, UIDisplay } from 'esengine';import { Actions } from '../actions';import { PauseOverlay } from '../components';-import { session } from '../state';+import { frozen, session } from '../state';// These systems stay OUT of the gameplay set: one has to be able to lift the// pause, and the others have to be able to draw it and to stop the world.@@ -15,15 +15,14 @@ export const pauseInputSystem = defineSystem();/**- * Stops the simulation. The run condition on the gameplay set only reaches this- * project's systems; everything the engine drives — the character controller- * integrating the velocity last written, navigation, behaviour trees — advances- * by `Time.delta`, so setting the scale to zero is what actually holds the world.+ * Stops the simulation, for a pause or for a death. The run condition on the+ * gameplay set only reaches this project's systems; everything the engine drives+ * advances by `Time.delta`, so a zero scale is what actually holds the world.*/export const pauseTimeSystem = defineSystem([ResMut(Time)],(time) => {- const scale = session.paused ? 0 : 1;+ const scale = frozen() ? 0 : 1;if (time.get().scale !== scale) time.modify((t) => { t.scale = scale; });},{ name: 'PauseTimeSystem' },