Skip to content

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.

feat(game): enemies that stand somewhere, and a wound that survives a door

esengine2a80d754d6d3
diff --git a/examples/celestial-heights/src/main.ts b/examples/celestial-heights/src/main.ts
index b2412e795..c22516c69 100644
--- a/examples/celestial-heights/src/main.ts
+++ b/examples/celestial-heights/src/main.ts
@@ -21,7 +21,7 @@ import { pauseInputSystem, pauseTimeSystem, pauseOverlaySystem } from './systems
import { fallenSystem, fallenOverlaySystem } from './systems/fallen';
import { applySettingsSystem, cycleLanguageSystem } from './systems/settings';
import {
- saveRunSystem, loadRunSystem, applyRestoreSystem, rememberRosterSystem,
+ saveRunSystem, loadRunSystem, applyRestoreSystem, rememberRosterSystem, vitalitySystem,
} from './systems/save';
import {
pickupSystem, inventoryInputSystem, inventoryBuildSystem, inventorySyncSystem,
@@ -46,6 +46,7 @@ addSystemToSchedule(Schedule.PreUpdate, areaLabelSystem);
addSystemToSchedule(Schedule.PreUpdate, spawnerSystem);
addSystemToSchedule(Schedule.PreUpdate, rememberRosterSystem);
addSystemToSchedule(Schedule.PreUpdate, applyRestoreSystem);
+addSystemToSchedule(Schedule.PreUpdate, vitalitySystem);
addSystemToSchedule(Schedule.PreUpdate, inventoryBuildSystem);
// Everything the pause freezes says so once, as a set with one run condition,
diff --git a/examples/celestial-heights/src/state.ts b/examples/celestial-heights/src/state.ts
index 3552a584e..94590f61d 100644
--- a/examples/celestial-heights/src/state.ts
+++ b/examples/celestial-heights/src/state.ts
@@ -19,6 +19,13 @@ export const session = {
effects: true,
/** Seconds left of the fallen screen; > 0 means the run is over and waiting. */
fallenFor: 0,
+ /**
+ * Lyra's health, carried between areas. Each area authors its own Lyra, so
+ * without this a gate is a full heal — which is not a door, it is a rest
+ * stop, and it flattens everything the areas were trying to build up.
+ * `null` until the first area has been played.
+ */
+ vitality: null as number | null,
};
/** Whether the world is being held still — by the player, or by her death. */
diff --git a/examples/celestial-heights/src/systems/save.ts b/examples/celestial-heights/src/systems/save.ts
index b41fc09ab..15ead7a7c 100644
--- a/examples/celestial-heights/src/systems/save.ts
+++ b/examples/celestial-heights/src/systems/save.ts
@@ -78,6 +78,35 @@ export const applyRestoreSystem = defineSystem(
{ name: 'ApplyRestoreSystem' },
);
+/**
+ * Carries Lyra's health across a gate. Each area authors its own Lyra at full
+ * health, so what she walked in with has to be remembered by the run rather
+ * than by the entity — the same reason the pack lives in `session`.
+ */
+let lastArea = '';
+
+export const vitalitySystem = defineSystem(
+ [Query(Mut(Health), Player), Res(SceneManager)],
+ (players, scenes) => {
+ const area = scenes.getActive();
+ if (!area || scenes.isTransitioning()) return;
+ for (const [, health] of players) {
+ if (area !== lastArea) {
+ lastArea = area;
+ // Not on a restore — that owns the health it is about to write —
+ // and not on nothing, which is what a death left behind: coming
+ // back from one is the one arrival that should be at full.
+ if (!session.restore && session.vitality !== null && session.vitality > 0) {
+ health.current = Math.min(session.vitality, health.max);
+ }
+ }
+ session.vitality = health.current;
+ return;
+ }
+ },
+ { name: 'VitalitySystem' },
+);
+
/** Records the enemies an area was authored with, so a save can say what is gone. */
export const rememberRosterSystem = defineSystem(
[Query(Name, Enemy), Res(SceneManager)],
diff --git a/examples/celestial-heights/src/systems/world.ts b/examples/celestial-heights/src/systems/world.ts
index 6bab0d349..e2c24a784 100644
--- a/examples/celestial-heights/src/systems/world.ts
+++ b/examples/celestial-heights/src/systems/world.ts
@@ -3,6 +3,7 @@ import {
Transform, Camera, FollowTarget, TilemapLayer, RuntimeOnly, Text, Sprite, Prefabs,
Nav, navGridFromTilemapLayer, SceneManager, transitionTo,
} from 'esengine';
+import type { NavGrid } from 'esengine';
import { Area, AreaLabel, Player, Gate, NavGridBuilt, Spawner, Spawned } from '../components';
import { session } from '../state';
@@ -140,8 +141,13 @@ export const gateLookSystem = defineSystem(
* boss's reinforcements cannot describe different creatures.
*/
export const spawnerSystem = defineSystem(
- [Query(Transform, Spawner).without(Spawned), Res(Prefabs), Commands()],
- (spawners, prefabs, commands) => {
+ [Query(Transform, Spawner).without(Spawned), Res(Prefabs), Res(Nav), Commands()],
+ (spawners, prefabs, nav, commands) => {
+ // Nothing may be placed before the terrain has said where the ground is:
+ // the grid is derived from the tilemap by a system that runs just ahead
+ // of this one, but not on the frame the area arrives.
+ if (!nav.hasGrid()) return;
+ const grid = nav.grid;
for (const [entity, transform, spawner] of spawners) {
commands.entity(entity).insert(Spawned, {}).insert(RuntimeOnly, {});
if (!spawner.prefab) continue;
@@ -149,16 +155,16 @@ export const spawnerSystem = defineSystem(
// A ring, foreshortened like everything else on this plane, so a
// marker reads as a group holding a place rather than a stack.
const angle = (Math.PI * 2 * i) / spawner.count + entity * 0.37;
+ const at = standable(grid, {
+ x: transform.position.x + Math.cos(angle) * spawner.radius,
+ y: transform.position.y + Math.sin(angle) * spawner.radius * 0.6,
+ });
void prefabs.instantiate(spawner.prefab, {
overrides: [{
type: 'property',
componentType: 'Transform',
propertyName: 'position',
- value: {
- x: transform.position.x + Math.cos(angle) * spawner.radius,
- y: transform.position.y + Math.sin(angle) * spawner.radius * 0.6,
- z: 0,
- },
+ value: { x: at.x, y: at.y, z: 0 },
}],
});
}
@@ -166,3 +172,16 @@ export const spawnerSystem = defineSystem(
},
{ name: 'SpawnerSystem' },
);
+
+/**
+ * The nearest place on the ring that is actually ground. A body dropped inside
+ * a pillar is not merely stuck: line of sight is a raycast, so it never sees
+ * the player, never chases and never swings — an enemy that is scenery.
+ */
+function standable(grid: NavGrid | null, at: { x: number; y: number }): { x: number; y: number } {
+ if (!grid) return at;
+ const cell = grid.worldToCell(at.x, at.y);
+ if (grid.isWalkable(cell.x, cell.y)) return at;
+ const near = grid.nearestWalkable(cell.x, cell.y, 6);
+ return near ? grid.cellToWorld(near.x, near.y) : at;
+}