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): rooms worth walking around, and gates that ask for something

esengineeb038cc70f54
diff --git a/examples/celestial-heights/assets/prefabs/wisp.esprefab b/examples/celestial-heights/assets/prefabs/wisp.esprefab
index 5c31c4056..b01d57718 100644
--- a/examples/celestial-heights/assets/prefabs/wisp.esprefab
+++ b/examples/celestial-heights/assets/prefabs/wisp.esprefab
@@ -74,7 +74,7 @@
{
"type": "Perceiver",
"data": {
- "range": 520,
+ "range": 420,
"fovDegrees": 360
}
},
diff --git a/examples/celestial-heights/assets/scenes/main.esscene b/examples/celestial-heights/assets/scenes/main.esscene
index 748117599..60c0ef93b 100644
--- a/examples/celestial-heights/assets/scenes/main.esscene
+++ b/examples/celestial-heights/assets/scenes/main.esscene
@@ -130,7 +130,9 @@
"type": "Gate",
"data": {
"toScene": "moonlit-gallery",
- "radius": 90
+ "radius": 90,
+ "requires": "petal",
+ "requiresCount": 2
}
}
]
@@ -1047,6 +1049,258 @@
}
}
]
+ },
+ {
+ "id": 600,
+ "name": "Spawner_6_3",
+ "parent": null,
+ "children": [],
+ "folder": "Actors",
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": -864,
+ "y": 544,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Spawner",
+ "data": {
+ "prefab": "assets/prefabs/wisp.esprefab",
+ "count": 2,
+ "radius": 180
+ }
+ }
+ ]
+ },
+ {
+ "id": 601,
+ "name": "Spawner_30_8",
+ "parent": null,
+ "children": [],
+ "folder": "Actors",
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 672,
+ "y": 224,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Spawner",
+ "data": {
+ "prefab": "assets/prefabs/wisp.esprefab",
+ "count": 2,
+ "radius": 180
+ }
+ }
+ ]
+ },
+ {
+ "id": 602,
+ "name": "Spawner_7_20",
+ "parent": null,
+ "children": [],
+ "folder": "Actors",
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": -800,
+ "y": -544,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Spawner",
+ "data": {
+ "prefab": "assets/prefabs/wisp.esprefab",
+ "count": 2,
+ "radius": 180
+ }
+ }
+ ]
+ },
+ {
+ "id": 603,
+ "name": "Drop_petal_10_2",
+ "parent": null,
+ "children": [],
+ "folder": "Environment",
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": -608,
+ "y": 608,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Sprite",
+ "data": {
+ "size": {
+ "x": 44,
+ "y": 44
+ },
+ "color": {
+ "r": 0.937,
+ "g": 0.588,
+ "b": 0.749,
+ "a": 1
+ },
+ "layer": 2
+ }
+ },
+ {
+ "type": "Item",
+ "data": {
+ "kind": "petal"
+ }
+ }
+ ]
+ },
+ {
+ "id": 604,
+ "name": "Drop_shard_34_10",
+ "parent": null,
+ "children": [],
+ "folder": "Environment",
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 928,
+ "y": 96,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Sprite",
+ "data": {
+ "size": {
+ "x": 44,
+ "y": 44
+ },
+ "color": {
+ "r": 0.545,
+ "g": 0.902,
+ "b": 0.918,
+ "a": 1
+ },
+ "layer": 2
+ }
+ },
+ {
+ "type": "Item",
+ "data": {
+ "kind": "shard"
+ }
+ }
+ ]
+ },
+ {
+ "id": 605,
+ "name": "Drop_ember_20_21",
+ "parent": null,
+ "children": [],
+ "folder": "Environment",
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 32,
+ "y": -608,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Sprite",
+ "data": {
+ "size": {
+ "x": 44,
+ "y": 44
+ },
+ "color": {
+ "r": 0.976,
+ "g": 0.706,
+ "b": 0.376,
+ "a": 1
+ },
+ "layer": 2
+ }
+ },
+ {
+ "type": "Item",
+ "data": {
+ "kind": "ember"
+ }
+ }
+ ]
+ },
+ {
+ "id": 606,
+ "name": "Drop_petal_35_2",
+ "parent": null,
+ "children": [],
+ "folder": "Environment",
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 992,
+ "y": 608,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Sprite",
+ "data": {
+ "size": {
+ "x": 44,
+ "y": 44
+ },
+ "color": {
+ "r": 0.937,
+ "g": 0.588,
+ "b": 0.749,
+ "a": 1
+ },
+ "layer": 2
+ }
+ },
+ {
+ "type": "Item",
+ "data": {
+ "kind": "petal"
+ }
+ }
+ ]
}
]
}
diff --git a/examples/celestial-heights/assets/scenes/moonlit-gallery.esscene b/examples/celestial-heights/assets/scenes/moonlit-gallery.esscene
index 41c3b0608..fb9d99532 100644
--- a/examples/celestial-heights/assets/scenes/moonlit-gallery.esscene
+++ b/examples/celestial-heights/assets/scenes/moonlit-gallery.esscene
@@ -130,7 +130,9 @@
"type": "Gate",
"data": {
"toScene": "spire",
- "radius": 90
+ "radius": 90,
+ "requires": "shard",
+ "requiresCount": 3
}
}
]
@@ -818,6 +820,258 @@
}
}
]
+ },
+ {
+ "id": 600,
+ "name": "Spawner_8_2",
+ "parent": null,
+ "children": [],
+ "folder": "Actors",
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": -480,
+ "y": 480,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Spawner",
+ "data": {
+ "prefab": "assets/prefabs/wisp.esprefab",
+ "count": 2,
+ "radius": 180
+ }
+ }
+ ]
+ },
+ {
+ "id": 601,
+ "name": "Spawner_24_7",
+ "parent": null,
+ "children": [],
+ "folder": "Actors",
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 544,
+ "y": 160,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Spawner",
+ "data": {
+ "prefab": "assets/prefabs/wisp.esprefab",
+ "count": 2,
+ "radius": 180
+ }
+ }
+ ]
+ },
+ {
+ "id": 602,
+ "name": "Spawner_16_12",
+ "parent": null,
+ "children": [],
+ "folder": "Actors",
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 32,
+ "y": -160,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Spawner",
+ "data": {
+ "prefab": "assets/prefabs/wisp.esprefab",
+ "count": 2,
+ "radius": 180
+ }
+ }
+ ]
+ },
+ {
+ "id": 603,
+ "name": "Drop_shard_6_12",
+ "parent": null,
+ "children": [],
+ "folder": "Environment",
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": -608,
+ "y": -160,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Sprite",
+ "data": {
+ "size": {
+ "x": 44,
+ "y": 44
+ },
+ "color": {
+ "r": 0.545,
+ "g": 0.902,
+ "b": 0.918,
+ "a": 1
+ },
+ "layer": 2
+ }
+ },
+ {
+ "type": "Item",
+ "data": {
+ "kind": "shard"
+ }
+ }
+ ]
+ },
+ {
+ "id": 604,
+ "name": "Drop_shard_26_2",
+ "parent": null,
+ "children": [],
+ "folder": "Environment",
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 672,
+ "y": 480,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Sprite",
+ "data": {
+ "size": {
+ "x": 44,
+ "y": 44
+ },
+ "color": {
+ "r": 0.545,
+ "g": 0.902,
+ "b": 0.918,
+ "a": 1
+ },
+ "layer": 2
+ }
+ },
+ {
+ "type": "Item",
+ "data": {
+ "kind": "shard"
+ }
+ }
+ ]
+ },
+ {
+ "id": 605,
+ "name": "Drop_petal_12_7",
+ "parent": null,
+ "children": [],
+ "folder": "Environment",
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": -224,
+ "y": 160,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Sprite",
+ "data": {
+ "size": {
+ "x": 44,
+ "y": 44
+ },
+ "color": {
+ "r": 0.937,
+ "g": 0.588,
+ "b": 0.749,
+ "a": 1
+ },
+ "layer": 2
+ }
+ },
+ {
+ "type": "Item",
+ "data": {
+ "kind": "petal"
+ }
+ }
+ ]
+ },
+ {
+ "id": 606,
+ "name": "Drop_ember_28_17",
+ "parent": null,
+ "children": [],
+ "folder": "Environment",
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 800,
+ "y": -480,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Sprite",
+ "data": {
+ "size": {
+ "x": 44,
+ "y": 44
+ },
+ "color": {
+ "r": 0.976,
+ "g": 0.706,
+ "b": 0.376,
+ "a": 1
+ },
+ "layer": 2
+ }
+ },
+ {
+ "type": "Item",
+ "data": {
+ "kind": "ember"
+ }
+ }
+ ]
}
]
}
diff --git a/examples/celestial-heights/assets/scenes/spire.esscene b/examples/celestial-heights/assets/scenes/spire.esscene
index eb634d938..37db4110f 100644
--- a/examples/celestial-heights/assets/scenes/spire.esscene
+++ b/examples/celestial-heights/assets/scenes/spire.esscene
@@ -630,6 +630,146 @@
}
}
]
+ },
+ {
+ "id": 600,
+ "name": "Spawner_20_6",
+ "parent": null,
+ "children": [],
+ "folder": "Actors",
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": -224,
+ "y": 608,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Spawner",
+ "data": {
+ "prefab": "assets/prefabs/wisp.esprefab",
+ "count": 2,
+ "radius": 180
+ }
+ }
+ ]
+ },
+ {
+ "id": 601,
+ "name": "Spawner_40_25",
+ "parent": null,
+ "children": [],
+ "folder": "Actors",
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 1056,
+ "y": -608,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Spawner",
+ "data": {
+ "prefab": "assets/prefabs/wisp.esprefab",
+ "count": 2,
+ "radius": 180
+ }
+ }
+ ]
+ },
+ {
+ "id": 602,
+ "name": "Drop_petal_16_3",
+ "parent": null,
+ "children": [],
+ "folder": "Environment",
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": -480,
+ "y": 800,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Sprite",
+ "data": {
+ "size": {
+ "x": 44,
+ "y": 44
+ },
+ "color": {
+ "r": 0.937,
+ "g": 0.588,
+ "b": 0.749,
+ "a": 1
+ },
+ "layer": 2
+ }
+ },
+ {
+ "type": "Item",
+ "data": {
+ "kind": "petal"
+ }
+ }
+ ]
+ },
+ {
+ "id": 603,
+ "name": "Drop_shard_44_15",
+ "parent": null,
+ "children": [],
+ "folder": "Environment",
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 1312,
+ "y": 32,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Sprite",
+ "data": {
+ "size": {
+ "x": 44,
+ "y": 44
+ },
+ "color": {
+ "r": 0.545,
+ "g": 0.902,
+ "b": 0.918,
+ "a": 1
+ },
+ "layer": 2
+ }
+ },
+ {
+ "type": "Item",
+ "data": {
+ "kind": "shard"
+ }
+ }
+ ]
}
]
}
diff --git a/examples/celestial-heights/src/components.ts b/examples/celestial-heights/src/components.ts
index c4c68fa95..84f6b3864 100644
--- a/examples/celestial-heights/src/components.ts
+++ b/examples/celestial-heights/src/components.ts
@@ -67,12 +67,35 @@ export const Area = defineComponent('Area', {
/** The HUD line that names the current area, filled in from {@link Area}. */
export const AreaLabel = defineTag('AreaLabel');
-/** A way out of an area. `toScene` is the packaged scene name, e.g. `main`. */
+/**
+ * A way out of an area. `toScene` is the packaged scene name, e.g. `main`.
+ * A gate that asks for something stays shut until the pack holds that many of
+ * `requires` — which is what makes an area worth walking around rather than
+ * straight through.
+ */
export const Gate = defineComponent('Gate', {
toScene: '',
radius: 90,
+ requires: '',
+ requiresCount: 0,
+});
+
+/**
+ * A place an area's enemies come from. An area declares where and how many
+ * rather than carrying a copy of the wisp per enemy: eleven authored entities
+ * per body is what makes a room expensive to populate, and a room nobody wants
+ * to populate is a game that stays a demo.
+ */
+export const Spawner = defineComponent('Spawner', {
+ prefab: '',
+ count: 3,
+ /** How far from the marker they are placed, in world units. */
+ radius: 220,
});
+/** Set once a spawner has placed its bodies, so it does so exactly once. */
+export const Spawned = defineTag('Spawned');
+
/** Set once a tilemap's navigation grid has been derived from its tiles. */
export const NavGridBuilt = defineTag('NavGridBuilt');
diff --git a/examples/celestial-heights/src/main.ts b/examples/celestial-heights/src/main.ts
index 6ce389553..b2412e795 100644
--- a/examples/celestial-heights/src/main.ts
+++ b/examples/celestial-heights/src/main.ts
@@ -14,7 +14,8 @@ import {
} from './systems/feedback';
import { bossPhaseSystem, summonSystem, chargeSystem } from './systems/boss';
import {
- cameraBindSystem, navFromTerrainSystem, gateSystem, hudSystem, areaLabelSystem,
+ cameraBindSystem, navFromTerrainSystem, gateSystem, gateLookSystem, hudSystem,
+ areaLabelSystem, spawnerSystem,
} from './systems/world';
import { pauseInputSystem, pauseTimeSystem, pauseOverlaySystem } from './systems/pause';
import { fallenSystem, fallenOverlaySystem } from './systems/fallen';
@@ -42,6 +43,7 @@ addSystemToSchedule(Schedule.PreUpdate, hudSystem);
addSystemToSchedule(Schedule.PreUpdate, navFromTerrainSystem);
addSystemToSchedule(Schedule.PreUpdate, cameraBindSystem);
addSystemToSchedule(Schedule.PreUpdate, areaLabelSystem);
+addSystemToSchedule(Schedule.PreUpdate, spawnerSystem);
addSystemToSchedule(Schedule.PreUpdate, rememberRosterSystem);
addSystemToSchedule(Schedule.PreUpdate, applyRestoreSystem);
addSystemToSchedule(Schedule.PreUpdate, inventoryBuildSystem);
@@ -77,6 +79,7 @@ addSystemToSchedule(Schedule.PostUpdate, fallenOverlaySystem);
addSystemToSchedule(Schedule.PostUpdate, healthBarSystem);
addSystemToSchedule(Schedule.PostUpdate, vitalityMeterSystem);
addSystemToSchedule(Schedule.PostUpdate, bossMeterSystem);
+addSystemToSchedule(Schedule.PostUpdate, gateLookSystem);
addSystemToSchedule(Schedule.PostUpdate, hitFlashSystem);
addSystemToSchedule(Schedule.PostUpdate, inventorySyncSystem);
addSystemToSchedule(Schedule.PostUpdate, achievementSystem);
diff --git a/examples/celestial-heights/src/systems/world.ts b/examples/celestial-heights/src/systems/world.ts
index 384a2b2a0..6bab0d349 100644
--- a/examples/celestial-heights/src/systems/world.ts
+++ b/examples/celestial-heights/src/systems/world.ts
@@ -1,9 +1,10 @@
import {
defineSystem, Query, Mut, Res, Commands, GetWorld,
- Transform, Camera, FollowTarget, TilemapLayer, RuntimeOnly, Text,
+ Transform, Camera, FollowTarget, TilemapLayer, RuntimeOnly, Text, Sprite, Prefabs,
Nav, navGridFromTilemapLayer, SceneManager, transitionTo,
} from 'esengine';
-import { Area, AreaLabel, Player, Gate, NavGridBuilt } from '../components';
+import { Area, AreaLabel, Player, Gate, NavGridBuilt, Spawner, Spawned } from '../components';
+import { session } from '../state';
/**
* Points the camera at whoever is playing. The camera is authored per area and
@@ -94,13 +95,19 @@ export const areaLabelSystem = defineSystem(
{ name: 'AreaLabelSystem' },
);
-/** Walking into a gate hands the game to the next area. */
+/** Whether the pack holds what a gate asks for. */
+export function gateIsOpen(gate: { requires: string; requiresCount: number }): boolean {
+ if (!gate.requires || gate.requiresCount <= 0) return true;
+ return (session.inventory[gate.requires] ?? 0) >= gate.requiresCount;
+}
+
+/** Walking into an open gate hands the game to the next area. */
export const gateSystem = defineSystem(
[Query(Transform, Gate), Query(Transform, Player), Res(SceneManager)],
(gates, players, scenes) => {
if (scenes.isTransitioning()) return;
for (const [, gateTransform, gate] of gates) {
- if (!gate.toScene) continue;
+ if (!gate.toScene || !gateIsOpen(gate)) continue;
for (const [, playerTransform] of players) {
const dx = playerTransform.position.x - gateTransform.position.x;
const dy = playerTransform.position.y - gateTransform.position.y;
@@ -112,3 +119,50 @@ export const gateSystem = defineSystem(
},
{ name: 'GateSystem' },
);
+
+/** A shut gate reads as shut, so nobody walks into it twice wondering why. */
+export const gateLookSystem = defineSystem(
+ [Query(Mut(Sprite), Gate)],
+ (gates) => {
+ for (const [, sprite, gate] of gates) {
+ const open = gateIsOpen(gate);
+ sprite.color.r = open ? 0.545 : 0.404;
+ sprite.color.g = open ? 0.902 : 0.376;
+ sprite.color.b = open ? 0.918 : 0.463;
+ }
+ },
+ { name: 'GateLookSystem' },
+);
+
+/**
+ * Places an area's enemies from its spawner markers, once each. They are
+ * instances of the same wisp prefab Vesper calls, so an area's population and a
+ * boss's reinforcements cannot describe different creatures.
+ */
+export const spawnerSystem = defineSystem(
+ [Query(Transform, Spawner).without(Spawned), Res(Prefabs), Commands()],
+ (spawners, prefabs, commands) => {
+ for (const [entity, transform, spawner] of spawners) {
+ commands.entity(entity).insert(Spawned, {}).insert(RuntimeOnly, {});
+ if (!spawner.prefab) continue;
+ for (let i = 0; i < spawner.count; i++) {
+ // 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;
+ 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,
+ },
+ }],
+ });
+ }
+ }
+ },
+ { name: 'SpawnerSystem' },
+);