@nodetool-ai/game-runtime 0.8.1 → 0.8.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +43 -0
- package/dist/.nodetool-build-stamp +1 -1
- package/dist/audio-mixer-references.d.ts +10 -0
- package/dist/audio-mixer-references.d.ts.map +1 -0
- package/dist/audio-mixer-references.js +21 -0
- package/dist/audio-mixer-references.js.map +1 -0
- package/dist/audio-source-ops.d.ts +24 -0
- package/dist/audio-source-ops.d.ts.map +1 -0
- package/dist/audio-source-ops.js +27 -0
- package/dist/audio-source-ops.js.map +1 -0
- package/dist/authoring-identity.d.ts +3 -0
- package/dist/authoring-identity.d.ts.map +1 -0
- package/dist/authoring-identity.js +12 -0
- package/dist/authoring-identity.js.map +1 -0
- package/dist/authoring-reconcile.d.ts +23 -0
- package/dist/authoring-reconcile.d.ts.map +1 -0
- package/dist/authoring-reconcile.js +375 -0
- package/dist/authoring-reconcile.js.map +1 -0
- package/dist/autoplay.d.ts.map +1 -1
- package/dist/autoplay.js +32 -5
- package/dist/autoplay.js.map +1 -1
- package/dist/document-ops.d.ts +2657 -368
- package/dist/document-ops.d.ts.map +1 -1
- package/dist/document-ops.js +135 -16
- package/dist/document-ops.js.map +1 -1
- package/dist/document-ops3d.d.ts +13390 -0
- package/dist/document-ops3d.d.ts.map +1 -0
- package/dist/document-ops3d.js +493 -0
- package/dist/document-ops3d.js.map +1 -0
- package/dist/gameplay/lifecycle.d.ts +91 -0
- package/dist/gameplay/lifecycle.d.ts.map +1 -0
- package/dist/gameplay/lifecycle.js +135 -0
- package/dist/gameplay/lifecycle.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -1
- package/dist/open-session.d.ts +32 -0
- package/dist/open-session.d.ts.map +1 -0
- package/dist/open-session.js +51 -0
- package/dist/open-session.js.map +1 -0
- package/dist/ownership-ops.d.ts +45 -0
- package/dist/ownership-ops.d.ts.map +1 -0
- package/dist/ownership-ops.js +173 -0
- package/dist/ownership-ops.js.map +1 -0
- package/dist/prepared-collider3d.d.ts +11 -0
- package/dist/prepared-collider3d.d.ts.map +1 -0
- package/dist/prepared-collider3d.js +41 -0
- package/dist/prepared-collider3d.js.map +1 -0
- package/dist/sample3d.d.ts +4 -0
- package/dist/sample3d.d.ts.map +1 -0
- package/dist/sample3d.js +62 -0
- package/dist/sample3d.js.map +1 -0
- package/dist/script-clock.d.ts +19 -0
- package/dist/script-clock.d.ts.map +1 -0
- package/dist/script-clock.js +32 -0
- package/dist/script-clock.js.map +1 -0
- package/dist/script-declarations.d.ts +7 -0
- package/dist/script-declarations.d.ts.map +1 -0
- package/dist/script-declarations.js +99 -0
- package/dist/script-declarations.js.map +1 -0
- package/dist/script-lifecycle.d.ts +108 -0
- package/dist/script-lifecycle.d.ts.map +1 -0
- package/dist/script-lifecycle.js +253 -0
- package/dist/script-lifecycle.js.map +1 -0
- package/dist/script-params.d.ts +25 -0
- package/dist/script-params.d.ts.map +1 -0
- package/dist/script-params.js +63 -0
- package/dist/script-params.js.map +1 -0
- package/dist/script-persistence.d.ts +3 -0
- package/dist/script-persistence.d.ts.map +1 -0
- package/dist/script-persistence.js +67 -0
- package/dist/script-persistence.js.map +1 -0
- package/dist/script-props.d.ts +21 -0
- package/dist/script-props.d.ts.map +1 -0
- package/dist/script-props.js +42 -0
- package/dist/script-props.js.map +1 -0
- package/dist/script-transport.d.ts +5 -0
- package/dist/script-transport.d.ts.map +1 -0
- package/dist/script-transport.js +64 -0
- package/dist/script-transport.js.map +1 -0
- package/dist/script-types.d.ts +5 -1
- package/dist/script-types.d.ts.map +1 -1
- package/dist/script-types.js +13 -11
- package/dist/script-types.js.map +1 -1
- package/dist/script-types3d.d.ts +6 -0
- package/dist/script-types3d.d.ts.map +1 -0
- package/dist/script-types3d.js +28 -0
- package/dist/script-types3d.js.map +1 -0
- package/dist/script-world.d.ts +41 -0
- package/dist/script-world.d.ts.map +1 -0
- package/dist/script-world.js +169 -0
- package/dist/script-world.js.map +1 -0
- package/dist/scripts.d.ts +107 -4
- package/dist/scripts.d.ts.map +1 -1
- package/dist/scripts.js +513 -111
- package/dist/scripts.js.map +1 -1
- package/dist/scripts3d.d.ts +34 -0
- package/dist/scripts3d.d.ts.map +1 -0
- package/dist/scripts3d.js +14 -0
- package/dist/scripts3d.js.map +1 -0
- package/dist/session.d.ts +8 -3
- package/dist/session.d.ts.map +1 -1
- package/dist/session.js +249 -795
- package/dist/session.js.map +1 -1
- package/dist/session3d.d.ts +30 -0
- package/dist/session3d.d.ts.map +1 -0
- package/dist/session3d.js +603 -0
- package/dist/session3d.js.map +1 -0
- package/dist/spatial3d/camera.d.ts +7 -0
- package/dist/spatial3d/camera.d.ts.map +1 -0
- package/dist/spatial3d/camera.js +43 -0
- package/dist/spatial3d/camera.js.map +1 -0
- package/dist/spatial3d/digest.d.ts +4 -0
- package/dist/spatial3d/digest.d.ts.map +1 -0
- package/dist/spatial3d/digest.js +30 -0
- package/dist/spatial3d/digest.js.map +1 -0
- package/dist/spatial3d/layers.d.ts +30 -0
- package/dist/spatial3d/layers.d.ts.map +1 -0
- package/dist/spatial3d/layers.js +93 -0
- package/dist/spatial3d/layers.js.map +1 -0
- package/dist/spatial3d/math.d.ts +23 -0
- package/dist/spatial3d/math.d.ts.map +1 -0
- package/dist/spatial3d/math.js +39 -0
- package/dist/spatial3d/math.js.map +1 -0
- package/dist/spatial3d/state.d.ts +37 -0
- package/dist/spatial3d/state.d.ts.map +1 -0
- package/dist/spatial3d/state.js +4 -0
- package/dist/spatial3d/state.js.map +1 -0
- package/dist/spatial3d/world.d.ts +68 -0
- package/dist/spatial3d/world.d.ts.map +1 -0
- package/dist/spatial3d/world.js +446 -0
- package/dist/spatial3d/world.js.map +1 -0
- package/dist/systems/animation-graph3d.d.ts +24 -0
- package/dist/systems/animation-graph3d.d.ts.map +1 -0
- package/dist/systems/animation-graph3d.js +262 -0
- package/dist/systems/animation-graph3d.js.map +1 -0
- package/dist/systems/animation3d.d.ts +3 -0
- package/dist/systems/animation3d.d.ts.map +1 -0
- package/dist/systems/animation3d.js +14 -0
- package/dist/systems/animation3d.js.map +1 -0
- package/dist/systems/character3d.d.ts +3 -0
- package/dist/systems/character3d.d.ts.map +1 -0
- package/dist/systems/character3d.js +6 -0
- package/dist/systems/character3d.js.map +1 -0
- package/dist/systems/collision2d.d.ts +27 -0
- package/dist/systems/collision2d.d.ts.map +1 -0
- package/dist/systems/collision2d.js +154 -0
- package/dist/systems/collision2d.js.map +1 -0
- package/dist/systems/contacts2d.d.ts +3 -0
- package/dist/systems/contacts2d.d.ts.map +1 -0
- package/dist/systems/contacts2d.js +36 -0
- package/dist/systems/contacts2d.js.map +1 -0
- package/dist/systems/contacts3d.d.ts +3 -0
- package/dist/systems/contacts3d.d.ts.map +1 -0
- package/dist/systems/contacts3d.js +55 -0
- package/dist/systems/contacts3d.js.map +1 -0
- package/dist/systems/context2d.d.ts +41 -0
- package/dist/systems/context2d.d.ts.map +1 -0
- package/dist/systems/context2d.js +2 -0
- package/dist/systems/context2d.js.map +1 -0
- package/dist/systems/context3d.d.ts +67 -0
- package/dist/systems/context3d.d.ts.map +1 -0
- package/dist/systems/context3d.js +2 -0
- package/dist/systems/context3d.js.map +1 -0
- package/dist/systems/gameplay2d.d.ts +3 -0
- package/dist/systems/gameplay2d.d.ts.map +1 -0
- package/dist/systems/gameplay2d.js +88 -0
- package/dist/systems/gameplay2d.js.map +1 -0
- package/dist/systems/gameplay3d.d.ts +4 -0
- package/dist/systems/gameplay3d.d.ts.map +1 -0
- package/dist/systems/gameplay3d.js +170 -0
- package/dist/systems/gameplay3d.js.map +1 -0
- package/dist/systems/hierarchy3d.d.ts +3 -0
- package/dist/systems/hierarchy3d.d.ts.map +1 -0
- package/dist/systems/hierarchy3d.js +27 -0
- package/dist/systems/hierarchy3d.js.map +1 -0
- package/dist/systems/input2d.d.ts +3 -0
- package/dist/systems/input2d.d.ts.map +1 -0
- package/dist/systems/input2d.js +6 -0
- package/dist/systems/input2d.js.map +1 -0
- package/dist/systems/input3d.d.ts +3 -0
- package/dist/systems/input3d.d.ts.map +1 -0
- package/dist/systems/input3d.js +16 -0
- package/dist/systems/input3d.js.map +1 -0
- package/dist/systems/physics2d.d.ts +3 -0
- package/dist/systems/physics2d.d.ts.map +1 -0
- package/dist/systems/physics2d.js +244 -0
- package/dist/systems/physics2d.js.map +1 -0
- package/dist/systems/physics3d.d.ts +3 -0
- package/dist/systems/physics3d.d.ts.map +1 -0
- package/dist/systems/physics3d.js +4 -0
- package/dist/systems/physics3d.js.map +1 -0
- package/dist/systems/pipeline.d.ts +23 -0
- package/dist/systems/pipeline.d.ts.map +1 -0
- package/dist/systems/pipeline.js +65 -0
- package/dist/systems/pipeline.js.map +1 -0
- package/dist/systems/presentation2d.d.ts +9 -0
- package/dist/systems/presentation2d.d.ts.map +1 -0
- package/dist/systems/presentation2d.js +240 -0
- package/dist/systems/presentation2d.js.map +1 -0
- package/dist/systems/presentation3d.d.ts +9 -0
- package/dist/systems/presentation3d.d.ts.map +1 -0
- package/dist/systems/presentation3d.js +115 -0
- package/dist/systems/presentation3d.js.map +1 -0
- package/dist/systems/scripts2d.d.ts +7 -0
- package/dist/systems/scripts2d.d.ts.map +1 -0
- package/dist/systems/scripts2d.js +274 -0
- package/dist/systems/scripts2d.js.map +1 -0
- package/dist/systems/scripts3d.d.ts +7 -0
- package/dist/systems/scripts3d.d.ts.map +1 -0
- package/dist/systems/scripts3d.js +262 -0
- package/dist/systems/scripts3d.js.map +1 -0
- package/dist/systems/state2d.d.ts +65 -0
- package/dist/systems/state2d.d.ts.map +1 -0
- package/dist/systems/state2d.js +2 -0
- package/dist/systems/state2d.js.map +1 -0
- package/dist/systems/tick2d.d.ts +7 -0
- package/dist/systems/tick2d.d.ts.map +1 -0
- package/dist/systems/tick2d.js +15 -0
- package/dist/systems/tick2d.js.map +1 -0
- package/dist/systems/tick3d.d.ts +7 -0
- package/dist/systems/tick3d.d.ts.map +1 -0
- package/dist/systems/tick3d.js +17 -0
- package/dist/systems/tick3d.js.map +1 -0
- package/dist/validate-animation-graph3d.d.ts +8 -0
- package/dist/validate-animation-graph3d.d.ts.map +1 -0
- package/dist/validate-animation-graph3d.js +115 -0
- package/dist/validate-animation-graph3d.js.map +1 -0
- package/dist/validate.d.ts +5 -0
- package/dist/validate.d.ts.map +1 -1
- package/dist/validate.js +61 -2
- package/dist/validate.js.map +1 -1
- package/dist/validate3d.d.ts +20 -0
- package/dist/validate3d.d.ts.map +1 -0
- package/dist/validate3d.js +325 -0
- package/dist/validate3d.js.map +1 -0
- package/package.json +10 -3
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import { MAX_GAME_EVENTS_PER_TICK } from "../gameplay/lifecycle.js";
|
|
2
|
+
import { contactKey, obstacleStates, gravityScaleOf, obstaclesOf, canCollideObstacle, TOUCH_EPSILON, standsOn, colliderBox, touchingOf, solidBelowAhead, canCollide, sweepAabb, overlaps } from "./collision2d.js";
|
|
3
|
+
import { faceOf, sweepBox } from "../physics.js";
|
|
4
|
+
export function stepPhysics2D(context) {
|
|
5
|
+
const dt = 1 / context.document.tickRate;
|
|
6
|
+
const motionStarts = new Map(context.states.map((state) => [state.definition.id, { x: state.x, y: state.y }]));
|
|
7
|
+
const currentContacts = new Map();
|
|
8
|
+
const recordContact = (moving, other, hit) => {
|
|
9
|
+
const key = contactKey(moving.definition.id, other.definition.id);
|
|
10
|
+
if (!currentContacts.has(key)) {
|
|
11
|
+
if (context.events.length + currentContacts.size >= MAX_GAME_EVENTS_PER_TICK) {
|
|
12
|
+
throw new Error(`Game event limit exceeded (${MAX_GAME_EVENTS_PER_TICK} per tick)`);
|
|
13
|
+
}
|
|
14
|
+
currentContacts.set(key, {
|
|
15
|
+
entityId: moving.definition.id,
|
|
16
|
+
otherId: other.definition.id,
|
|
17
|
+
sensor: Boolean(moving.definition.collider2d?.sensor || other.definition.collider2d?.sensor),
|
|
18
|
+
normalX: hit.normalX,
|
|
19
|
+
normalY: hit.normalY
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
const kinematic = context.states.filter((state) => state.active && state.definition.body2d?.type === "kinematic");
|
|
24
|
+
const obstacles = obstacleStates(context);
|
|
25
|
+
const gravity = context.scene.gravity;
|
|
26
|
+
if (gravity) {
|
|
27
|
+
for (const state of kinematic) {
|
|
28
|
+
const scale = gravityScaleOf(context, state);
|
|
29
|
+
state.velocityX += gravity.x * scale * dt;
|
|
30
|
+
state.velocityY += gravity.y * scale * dt;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const moveBody = (state, dx, dy, exclude) => {
|
|
34
|
+
const collider = state.definition.collider2d;
|
|
35
|
+
const blocked = { blockedX: 0, blockedY: 0 };
|
|
36
|
+
if (!collider) {
|
|
37
|
+
return blocked;
|
|
38
|
+
}
|
|
39
|
+
const halfWidth = collider.width / 2;
|
|
40
|
+
const halfHeight = collider.height / 2;
|
|
41
|
+
let remainingX = dx;
|
|
42
|
+
let remainingY = dy;
|
|
43
|
+
for (let pass = 0; pass < 2 && (remainingX !== 0 || remainingY !== 0); pass += 1) {
|
|
44
|
+
const box = { x: state.x, y: state.y, halfWidth, halfHeight };
|
|
45
|
+
const found = [];
|
|
46
|
+
const minX = state.x - halfWidth + Math.min(0, remainingX);
|
|
47
|
+
const maxX = state.x + halfWidth + Math.max(0, remainingX);
|
|
48
|
+
const minY = state.y - halfHeight + Math.min(0, remainingY);
|
|
49
|
+
const maxY = state.y + halfHeight + Math.max(0, remainingY);
|
|
50
|
+
for (const other of obstacles) {
|
|
51
|
+
if (other !== state && other !== exclude) {
|
|
52
|
+
obstaclesOf(other, minX, minY, maxX, maxY, found);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const hits = [];
|
|
56
|
+
for (const obstacle of found) {
|
|
57
|
+
if (!canCollideObstacle(state, obstacle)) {
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
const hit = sweepBox(box, obstacle.box, remainingX, remainingY, obstacle.sensor || collider.sensor);
|
|
61
|
+
if (!hit) {
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
const blocks = obstacle.solid && !obstacle.sensor && !collider.sensor;
|
|
65
|
+
if (blocks) {
|
|
66
|
+
// A face shared with a neighboring tile is inside the wall, so it never stops a body.
|
|
67
|
+
if ((obstacle.internal & faceOf(hit)) !== 0) {
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
// A one-way solid stops only a body that falls onto its top from above.
|
|
71
|
+
if (obstacle.oneWay && !(hit.normalY > 0 && state.y - halfHeight >= obstacle.box.y + obstacle.box.halfHeight - TOUCH_EPSILON)) {
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
hits.push({ obstacle, hit, blocks });
|
|
76
|
+
}
|
|
77
|
+
hits.sort((a, b) => a.hit.time - b.hit.time ||
|
|
78
|
+
(a.obstacle.owner.definition.id < b.obstacle.owner.definition.id
|
|
79
|
+
? -1
|
|
80
|
+
: a.obstacle.owner.definition.id > b.obstacle.owner.definition.id
|
|
81
|
+
? 1
|
|
82
|
+
: 0));
|
|
83
|
+
const solid = hits.find((item) => item.blocks);
|
|
84
|
+
const travel = solid?.hit.time ?? 1;
|
|
85
|
+
for (const { obstacle, hit, blocks } of hits) {
|
|
86
|
+
if (hit.time > travel) {
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
if (!blocks || obstacle === solid?.obstacle) {
|
|
90
|
+
recordContact(state, obstacle.owner, hit);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
state.x += remainingX * travel;
|
|
94
|
+
state.y += remainingY * travel;
|
|
95
|
+
if (!solid) {
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
if (solid.hit.normalX !== 0) {
|
|
99
|
+
blocked.blockedX = solid.hit.normalX;
|
|
100
|
+
}
|
|
101
|
+
if (solid.hit.normalY !== 0) {
|
|
102
|
+
blocked.blockedY = solid.hit.normalY;
|
|
103
|
+
}
|
|
104
|
+
remainingX = solid.hit.normalX === 0 ? remainingX * (1 - travel) : 0;
|
|
105
|
+
remainingY = solid.hit.normalY === 0 ? remainingY * (1 - travel) : 0;
|
|
106
|
+
}
|
|
107
|
+
return blocked;
|
|
108
|
+
};
|
|
109
|
+
// Static bodies with a velocity are moving solids. They move first and carry the bodies that
|
|
110
|
+
// stand on them; a body in the way is pushed out along the motion.
|
|
111
|
+
const carries = new Map();
|
|
112
|
+
for (const platform of context.states) {
|
|
113
|
+
if (!platform.active || platform.definition.body2d?.type !== "static" || (platform.velocityX === 0 && platform.velocityY === 0)) {
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
const dx = platform.velocityX * dt;
|
|
117
|
+
const dy = platform.velocityY * dt;
|
|
118
|
+
const riders = kinematic.filter((body) => !carries.has(body) && standsOn(context, body, platform));
|
|
119
|
+
platform.x += dx;
|
|
120
|
+
platform.y += dy;
|
|
121
|
+
for (const rider of riders) {
|
|
122
|
+
carries.set(rider, { dx, dy, platform });
|
|
123
|
+
}
|
|
124
|
+
for (const body of kinematic) {
|
|
125
|
+
const box = colliderBox(body);
|
|
126
|
+
if (!box || carries.get(body)?.platform === platform || body.definition.collider2d?.sensor) {
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
const found = [];
|
|
130
|
+
obstaclesOf(platform, body.x - box.halfWidth + Math.min(0, dx), body.y - box.halfHeight + Math.min(0, dy), body.x + box.halfWidth + Math.max(0, dx), body.y + box.halfHeight + Math.max(0, dy), found);
|
|
131
|
+
for (const obstacle of found) {
|
|
132
|
+
if (!obstacle.solid || obstacle.sensor || obstacle.oneWay || !canCollideObstacle(body, obstacle)) {
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
const o = obstacle.box;
|
|
136
|
+
// Sweep relative to the platform's starting position, then push through the regular
|
|
137
|
+
// collision solver so a platform cannot teleport its passenger through another wall.
|
|
138
|
+
const hit = sweepBox(box, { ...o, x: o.x - dx, y: o.y - dy }, -dx, -dy, false);
|
|
139
|
+
if (!hit || (obstacle.internal & faceOf(hit)) !== 0) {
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
recordContact(body, platform, hit);
|
|
143
|
+
// An earlier tile can already have pushed the body beyond this face.
|
|
144
|
+
const pushX = hit.normalX * Math.max(0, (o.x - body.x) * hit.normalX + o.halfWidth + box.halfWidth);
|
|
145
|
+
const pushY = hit.normalY * Math.max(0, (o.y - body.y) * hit.normalY + o.halfHeight + box.halfHeight);
|
|
146
|
+
moveBody(body, pushX, pushY, platform);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
for (const state of kinematic) {
|
|
151
|
+
if (!state.definition.collider2d) {
|
|
152
|
+
state.x += state.velocityX * dt;
|
|
153
|
+
state.y += state.velocityY * dt;
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
const carry = carries.get(state);
|
|
157
|
+
if (carry) {
|
|
158
|
+
moveBody(state, carry.dx, carry.dy, carry.platform);
|
|
159
|
+
}
|
|
160
|
+
const { blockedX, blockedY } = moveBody(state, state.velocityX * dt, state.velocityY * dt, undefined);
|
|
161
|
+
// A blocked body loses the velocity that pushed into the solid, so gravity cannot build up while resting.
|
|
162
|
+
if (blockedX * state.velocityX < 0) {
|
|
163
|
+
state.velocityX = 0;
|
|
164
|
+
}
|
|
165
|
+
if (blockedY * state.velocityY < 0) {
|
|
166
|
+
state.velocityY = 0;
|
|
167
|
+
}
|
|
168
|
+
const patrol = state.definition.behaviors.find((behavior) => behavior.kind === "patrol");
|
|
169
|
+
if (patrol?.kind === "patrol" && patrol.axis === "x") {
|
|
170
|
+
const direction = state.patrolDirection ?? 1;
|
|
171
|
+
const ledge = patrol.turnAtLedges && touchingOf(context, state).down && !solidBelowAhead(context, state, direction);
|
|
172
|
+
if (blockedX === -direction || ledge) {
|
|
173
|
+
state.patrolDirection = direction === 1 ? -1 : 1;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
for (let index = 0; index < context.states.length; index += 1) {
|
|
178
|
+
const state = context.states[index];
|
|
179
|
+
if (!state.active || state.definition.body2d?.type !== "kinematic") {
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
for (let otherIndex = index + 1; otherIndex < context.states.length; otherIndex += 1) {
|
|
183
|
+
const other = context.states[otherIndex];
|
|
184
|
+
if (other.definition.body2d?.type !== "kinematic" || !canCollide(state, other)) {
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
const stateStart = motionStarts.get(state.definition.id);
|
|
188
|
+
const otherStart = motionStarts.get(other.definition.id);
|
|
189
|
+
if (!stateStart || !otherStart) {
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
const relativeX = state.x - stateStart.x - (other.x - otherStart.x);
|
|
193
|
+
const relativeY = state.y - stateStart.y - (other.y - otherStart.y);
|
|
194
|
+
const atStart = { ...state, x: stateStart.x, y: stateStart.y };
|
|
195
|
+
const targetAtStart = { ...other, x: otherStart.x, y: otherStart.y };
|
|
196
|
+
const hit = sweepAabb(atStart, targetAtStart, relativeX, relativeY);
|
|
197
|
+
if (hit) {
|
|
198
|
+
recordContact(state, other, hit);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
const recordTouch = (state, other, a, b) => {
|
|
203
|
+
const horizontalGap = Math.abs(a.x - b.x) - (a.halfWidth + b.halfWidth);
|
|
204
|
+
const verticalGap = Math.abs(a.y - b.y) - (a.halfHeight + b.halfHeight);
|
|
205
|
+
const horizontal = horizontalGap >= verticalGap;
|
|
206
|
+
recordContact(state, other, {
|
|
207
|
+
time: 0,
|
|
208
|
+
normalX: horizontal ? Math.sign(a.x - b.x) : 0,
|
|
209
|
+
normalY: horizontal ? 0 : Math.sign(a.y - b.y)
|
|
210
|
+
});
|
|
211
|
+
};
|
|
212
|
+
for (const state of kinematic) {
|
|
213
|
+
const box = colliderBox(state);
|
|
214
|
+
if (!box) {
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
for (const other of kinematic) {
|
|
218
|
+
if (state !== other && canCollide(state, other) && overlaps(state, other)) {
|
|
219
|
+
recordTouch(state, other, box, colliderBox(other));
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
const found = [];
|
|
223
|
+
for (const other of obstacles) {
|
|
224
|
+
if (other !== state) {
|
|
225
|
+
obstaclesOf(other, box.x - box.halfWidth, box.y - box.halfHeight, box.x + box.halfWidth, box.y + box.halfHeight, found);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
for (const obstacle of found) {
|
|
229
|
+
if (!canCollideObstacle(state, obstacle)) {
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
const o = obstacle.box;
|
|
233
|
+
const gapX = Math.abs(box.x - o.x) - (box.halfWidth + o.halfWidth);
|
|
234
|
+
const gapY = Math.abs(box.y - o.y) - (box.halfHeight + o.halfHeight);
|
|
235
|
+
const overlapping = gapX < 0 && gapY < 0;
|
|
236
|
+
const touching = gapX <= 0 && gapY <= 0 && obstacle.solid && !obstacle.sensor;
|
|
237
|
+
if (overlapping || touching) {
|
|
238
|
+
recordTouch(state, obstacle.owner, box, o);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
context.observations = currentContacts;
|
|
243
|
+
}
|
|
244
|
+
//# sourceMappingURL=physics2d.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"physics2d.js","sourceRoot":"","sources":["../../src/systems/physics2d.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAE,MAAM,0BAA0B,CAAC;AACpE,OAAO,EACL,UAAU,EACV,cAAc,EACd,cAAc,EACd,WAAW,EACX,kBAAkB,EAClB,aAAa,EACb,QAAQ,EACR,WAAW,EACX,UAAU,EACV,eAAe,EACf,UAAU,EACV,SAAS,EACT,QAAQ,EACT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AAU,CAAC,CAAC;YACrE,MAAM,WAAW,GAAG,IAAI,GAAG,CAAC,IAAI,IAAI,GAAG,CAAC,CAAC;YACzC,MAAM,QAAQ,GAAG,IAAI,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,IAAI,QAAQ,CAAC,KAAK,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC;YAC9E,IAAI,WAAW,IAAI,QAAQ,EAAE,CAAC;gBAC5B,WAAW,CAAC,KAAK,EAAE,QAAQ,CAAC,KAAK,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC;YAC7C,CAAC;QACH,CAAC;IACH,CAAC;IACD,OAAO,CAAC,YAAY,GAAG,eAAe,CAAC;AACzC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"physics3d.d.ts","sourceRoot":"","sources":["../../src/systems/physics3d.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AAC1D,wBAAgB,aAAa,CAAC,OAAO,EAAE,mBAAmB,GAAG,IAAI,CAEhE"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"physics3d.js","sourceRoot":"","sources":["../../src/systems/physics3d.ts"],"names":[],"mappings":"AACA,MAAM,UAAU,aAAa,CAAC,OAA4B;IACxD,OAAO,CAAC,cAAc,EAAE,CAAC,WAAW,EAAE,CAAC;AACzC,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { GameStepTimings } from "@nodetool-ai/protocol";
|
|
2
|
+
/** Systems mutate the session-owned simulation state through their tick context. */
|
|
3
|
+
export interface GameSystem<Document, Scene, Context, Snapshot = null> {
|
|
4
|
+
readonly name: string;
|
|
5
|
+
init(document: Document, scene: Scene): void;
|
|
6
|
+
step(context: Context): void;
|
|
7
|
+
snapshot(): Snapshot;
|
|
8
|
+
restore(snapshot: Snapshot): void;
|
|
9
|
+
}
|
|
10
|
+
/** Ordered simulation stages. Untimed steps never read a clock or allocate timing records. */
|
|
11
|
+
export declare class GameSystemPipeline<Document, Scene, Context, Snapshot = null> {
|
|
12
|
+
readonly systems: readonly GameSystem<Document, Scene, Context, Snapshot>[];
|
|
13
|
+
constructor(systems: readonly GameSystem<Document, Scene, Context, Snapshot>[]);
|
|
14
|
+
init(document: Document, scene: Scene): void;
|
|
15
|
+
snapshot(): Readonly<Record<string, Snapshot>>;
|
|
16
|
+
restore(saved: Readonly<Record<string, Snapshot>>): void;
|
|
17
|
+
step(context: Context): void;
|
|
18
|
+
stepTimed(context: Context, clock?: () => number): GameStepTimings;
|
|
19
|
+
}
|
|
20
|
+
/** Stateless stage adapter. Persistent state remains in the existing session snapshot. */
|
|
21
|
+
export declare function gameSystem<Document, Scene, Context>(name: string, step: (context: Context) => void): GameSystem<Document, Scene, Context>;
|
|
22
|
+
export declare function statefulGameSystem<Document, Scene, Context, Snapshot>(name: string, step: (context: Context) => void, snapshot: () => Snapshot, restore: (saved: Snapshot) => void): GameSystem<Document, Scene, Context, Snapshot>;
|
|
23
|
+
//# sourceMappingURL=pipeline.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pipeline.d.ts","sourceRoot":"","sources":["../../src/systems/pipeline.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAoB,MAAM,uBAAuB,CAAC;AAE/E,oFAAoF;AACpF,MAAM,WAAW,UAAU,CAAC,QAAQ,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,GAAG,IAAI;IACnE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,IAAI,CAAC,QAAQ,EAAE,QAAQ,EAAE,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;IAC7C,IAAI,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI,CAAC;IAC7B,QAAQ,IAAI,QAAQ,CAAC;IACrB,OAAO,CAAC,QAAQ,EAAE,QAAQ,GAAG,IAAI,CAAC;CACnC;AAED,8F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,CAEhD"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/** Ordered simulation stages. Untimed steps never read a clock or allocate timing records. */
|
|
2
|
+
export class GameSystemPipeline {
|
|
3
|
+
systems;
|
|
4
|
+
constructor(systems) {
|
|
5
|
+
this.systems = systems;
|
|
6
|
+
const names = new Set();
|
|
7
|
+
for (const system of systems) {
|
|
8
|
+
if (names.has(system.name)) {
|
|
9
|
+
throw new Error(`Duplicate game system ${system.name}`);
|
|
10
|
+
}
|
|
11
|
+
names.add(system.name);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
init(document, scene) {
|
|
15
|
+
for (const system of this.systems) {
|
|
16
|
+
system.init(document, scene);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
snapshot() {
|
|
20
|
+
return Object.fromEntries(this.systems.map((system) => [system.name, system.snapshot()]));
|
|
21
|
+
}
|
|
22
|
+
restore(saved) {
|
|
23
|
+
// Later stages own the entity set on which earlier spatial adapters depend.
|
|
24
|
+
for (const system of [...this.systems].reverse()) {
|
|
25
|
+
const state = saved[system.name];
|
|
26
|
+
if (state === undefined) {
|
|
27
|
+
throw new Error(`Missing snapshot for game system ${system.name}`);
|
|
28
|
+
}
|
|
29
|
+
system.restore(state);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
step(context) {
|
|
33
|
+
for (const system of this.systems) {
|
|
34
|
+
system.step(context);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
stepTimed(context, clock = () => performance.now()) {
|
|
38
|
+
const systems = [];
|
|
39
|
+
const started = clock();
|
|
40
|
+
for (const system of this.systems) {
|
|
41
|
+
const before = clock();
|
|
42
|
+
system.step(context);
|
|
43
|
+
systems.push({ system: system.name, durationMs: clock() - before });
|
|
44
|
+
}
|
|
45
|
+
return { systems, totalMs: clock() - started };
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/** Stateless stage adapter. Persistent state remains in the existing session snapshot. */
|
|
49
|
+
export function gameSystem(name, step) {
|
|
50
|
+
return {
|
|
51
|
+
name,
|
|
52
|
+
init() { },
|
|
53
|
+
step,
|
|
54
|
+
snapshot: () => null,
|
|
55
|
+
restore(saved) {
|
|
56
|
+
if (saved !== null) {
|
|
57
|
+
throw new Error(`Stateless game system ${name} cannot restore state`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
export function statefulGameSystem(name, step, snapshot, restore) {
|
|
63
|
+
return { name, init() { }, step, snapshot, restore };
|
|
64
|
+
}
|
|
65
|
+
//# sourceMappingURL=pipeline.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pipeline.js","sourceRoot":"","sources":["../../src/systems/pipeline.ts"],"names":[],"mappings":"AAWA,8FAA8F;AAC9F,MAAM,OAAO,kBAAkB;IAElB,OAAO;IADlB,YACW,OAAkE;uBAAlE,OAAO;QAEhB,MAAM,KAAK,GAAG,IAAI,GAAG,EAAU,CAAC;QAChC,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;YAC7B,IAAI,KAAK,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;gBAC3B,MAAM,IAAI,KAAK,CAAC,yBAAyB,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC;YAC1D,CAAC;YACD,KAAK,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QACzB,CAAC;IACH,CAAC;IACD,IAAI,CAAC,QAAkB,EAAE,KAAY;QACnC,KAAK,MAAM,MAAM,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YAClC,MAAM,CAAC,IAAI,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;QAC/B,CAAC;IACH,CAAC;IACD,QAAQ;QACN,OAAO,MAAM,CAAC,WAAW,CACvB,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,MAAM,CAAC,IAAI,EAAE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;IAEhC,OAAO;QACL,IAAI;QACJ,IAAI,KAAU,CAAC;QACf,IAAI;QACJ,QAAQ,EAAE,GAAG,EAAE,CAAC,IAAI;QACpB,OAAO,CAAC,KAAK;YACX,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;gBACnB,MAAM,IAAI,KAAK,CAAC,yBAAyB,IAAI,uBAAuB,CAAC,CAAC;YACxE,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,kBAAkB,CAChC,IAAY,EACZ,IAAgC,EAChC,QAAwB,EACxB,OAAkC;IAElC,OAAO,EAAE,IAAI,EAAE,IAAI,KAAU,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC;AAC5D,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { EntityState } from "./state2d.js";
|
|
2
|
+
import { type GameDocument, type GameHudLabel, type GameRenderFrame, type GameScene, type GameUiOverride } from "@nodetool-ai/protocol";
|
|
3
|
+
import type { GameSystemContext2D } from "./context2d.js";
|
|
4
|
+
type PresentationContext2D = Readonly<Pick<GameSystemContext2D, "tick" | "events" | "frameFor" | "document" | "scene" | "states" | "score" | "won" | "hud" | "ui" | "scriptStats" | "queues">> & Pick<GameSystemContext2D, "presentationEvents" | "result">;
|
|
5
|
+
export declare function stepPresentation2D(context: PresentationContext2D): void;
|
|
6
|
+
export declare function readStepResult2D(context: GameSystemContext2D): NonNullable<GameSystemContext2D["result"]>;
|
|
7
|
+
export declare function frameFor(document: GameDocument, scene: GameScene, states: readonly EntityState[], tick: number, score: number, won: boolean, hud: ReadonlyMap<string, GameHudLabel>, ui: ReadonlyMap<string, GameUiOverride>): GameRenderFrame;
|
|
8
|
+
export {};
|
|
9
|
+
//# sourceMappingURL=presentation2d.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"presentation2d.d.ts","sourceRoot":"","sources":["../../src/systems/presentation2d.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,EAAsB,KAAK,YAAY,EAAE,KAAK,YAAY,EAAE,KAAK,eAAe,EAAE,KAAK,SAAS,EAAE,KAAK,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAG5J,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AAC1D,KAAK,qBAAqB,GAAG,QAAQ,CACnC,IAAI,CAAC,mBAAmB,EAAE,MAAM,GAAG,QAAQ,GAAG,UAAU,GAAG,UAAU,GAAG,OAAO,GAAG,QAAQ,GAAG,OAAO,GAAG,KAAK,GAAG,KAAK,GAAG,IAAI,GAAG,aAAa,GAAG,QAAQ,CAAC,CACxJ,GACC,IAAI,CAAC,mBAAmB,EAAE,oBAAoB,GAAG,QAAQ,CAAC,CAAC;AAC7D,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,qBAAqB,GAAG,IAAI,CAUvE;AACD,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,mBAAmB,GAAG,WAAW,CAAC,mBAAmB,CAAC,QAAQ,CAAC,CAAC,CAKzG;AAYD,wBAAgB,QAAQ,CACtB,QAAQ,EAAE,YAAY,EACtB,KAAK,EAAE,SAAS,EAChB,MAAM,EAAE,SAAS,WAAW,EAAE,EAC9B,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,MAAM,EACb,GAAG,EAAE,OAAO,EACZ,GAAG,EAAE,WAAW,CAAC,MAAM,EAAE,YAAY,CAAC,EACtC,EAAE,EAAE,WAAW,CAAC,MAAM,EAAE,cAAc,CAAC,GACtC,eAAe,CAkOjB"}
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { resolveGameUiFrame } from "@nodetool-ai/protocol";
|
|
2
|
+
import { projectGameplayHud } from "../gameplay/lifecycle.js";
|
|
3
|
+
import { evaluateVisual } from "../visual-animation.js";
|
|
4
|
+
export function stepPresentation2D(context) {
|
|
5
|
+
context.presentationEvents = [...structuredClone(context.events), ...(context.queues.particles ?? [])];
|
|
6
|
+
context.result = {
|
|
7
|
+
tick: context.tick,
|
|
8
|
+
events: context.events,
|
|
9
|
+
frame: context.frameFor(context.document, context.scene, context.states, context.tick, context.score, context.won, context.hud, context.ui)
|
|
10
|
+
};
|
|
11
|
+
if (context.scriptStats) {
|
|
12
|
+
context.result = { ...context.result, scriptStats: context.scriptStats };
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
export function readStepResult2D(context) {
|
|
16
|
+
if (!context.result) {
|
|
17
|
+
throw new Error("Presentation stage did not produce a frame");
|
|
18
|
+
}
|
|
19
|
+
return context.result;
|
|
20
|
+
}
|
|
21
|
+
/** The animator's current frame: the selected clip, or the default frames from spawn. */
|
|
22
|
+
function animationFrame(state, tick) {
|
|
23
|
+
const animator = state.definition.animator;
|
|
24
|
+
if (!animator) {
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
const clip = (state.animation !== undefined ? animator.clips?.[state.animation] : undefined) ?? animator;
|
|
28
|
+
const age = tick - (state.animation !== undefined ? (state.animationTick ?? state.spawnTick) : state.spawnTick);
|
|
29
|
+
const index = Math.floor(Math.max(0, age) / clip.ticksPerFrame);
|
|
30
|
+
return clip.frames[clip.loop ? index % clip.frames.length : Math.min(index, clip.frames.length - 1)];
|
|
31
|
+
}
|
|
32
|
+
export function frameFor(document, scene, states, tick, score, won, hud, ui) {
|
|
33
|
+
const cameraState = states.find((state) => state.active && state.definition.camera2d);
|
|
34
|
+
const camera = cameraState?.definition.camera2d;
|
|
35
|
+
const sprites = [];
|
|
36
|
+
const tiles = [];
|
|
37
|
+
const particles = [];
|
|
38
|
+
const audioEmitters = [];
|
|
39
|
+
const cameraX = cameraState?.x ?? 0;
|
|
40
|
+
const cameraY = cameraState?.y ?? 0;
|
|
41
|
+
// Tiles and lights outside the view plus a margin never reach the renderer, so large levels stay cheap.
|
|
42
|
+
const viewHalfWidth = (camera?.width ?? 16) / (2 * (camera?.zoom ?? 1));
|
|
43
|
+
const viewHalfHeight = (camera?.height ?? 9) / (2 * (camera?.zoom ?? 1));
|
|
44
|
+
const cullHalfWidth = viewHalfWidth * 1.5 + 2;
|
|
45
|
+
const cullHalfHeight = viewHalfHeight * 1.5 + 2;
|
|
46
|
+
const entityLights = [];
|
|
47
|
+
for (const state of states) {
|
|
48
|
+
if (!state.active) {
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
const entity = state.definition;
|
|
52
|
+
if (entity.audioSource?.spatial) {
|
|
53
|
+
audioEmitters.push({ entityId: entity.id, x: state.x, y: state.y, previousX: state.previousX, previousY: state.previousY });
|
|
54
|
+
}
|
|
55
|
+
if (entity.sprite) {
|
|
56
|
+
const spriteDefinition = entity.sprite;
|
|
57
|
+
const binding = document.assets[entity.sprite.assetId];
|
|
58
|
+
const age = tick - state.spawnTick;
|
|
59
|
+
const lifetime = entity.behaviors.find((behavior) => behavior.kind === "lifetime");
|
|
60
|
+
const progress = lifetime?.kind === "lifetime" ? Math.min(1, age / lifetime.ticks) : 0;
|
|
61
|
+
const previousProgress = lifetime?.kind === "lifetime" ? Math.min(1, Math.max(0, age - 1) / lifetime.ticks) : 0;
|
|
62
|
+
const lifeScale = lifetime?.kind === "lifetime" ? 1 + (lifetime.endScale - 1) * progress : 1;
|
|
63
|
+
const previousLifeScale = lifetime?.kind === "lifetime" ? 1 + (lifetime.endScale - 1) * previousProgress : 1;
|
|
64
|
+
const lifeOpacity = lifetime?.kind === "lifetime" && lifetime.fade ? 1 - progress : 1;
|
|
65
|
+
const previousLifeOpacity = lifetime?.kind === "lifetime" && lifetime.fade ? 1 - previousProgress : 1;
|
|
66
|
+
const visual = state.visual;
|
|
67
|
+
const authored = evaluateVisual(entity, age, state.rotation, state.scaleX, state.scaleY);
|
|
68
|
+
const previousAuthored = evaluateVisual(entity, Math.max(0, age - 1), state.rotation, state.scaleX, state.scaleY);
|
|
69
|
+
const opacity = (visual?.opacity ?? authored.opacity) * lifeOpacity;
|
|
70
|
+
const previousOpacity = (visual?.opacity ?? previousAuthored.opacity) * previousLifeOpacity;
|
|
71
|
+
const sprite = {
|
|
72
|
+
entityId: entity.id,
|
|
73
|
+
assetId: entity.sprite.assetId,
|
|
74
|
+
x: state.x,
|
|
75
|
+
y: state.y,
|
|
76
|
+
previousX: state.previousX,
|
|
77
|
+
previousY: state.previousY,
|
|
78
|
+
rotation: visual?.rotation ?? authored.rotation,
|
|
79
|
+
previousRotation: visual?.rotation ?? previousAuthored.rotation,
|
|
80
|
+
scaleX: (visual?.scaleX ?? authored.scaleX) * lifeScale,
|
|
81
|
+
previousScaleX: (visual?.scaleX ?? previousAuthored.scaleX) * previousLifeScale,
|
|
82
|
+
scaleY: (visual?.scaleY ?? authored.scaleY) * lifeScale,
|
|
83
|
+
previousScaleY: (visual?.scaleY ?? previousAuthored.scaleY) * previousLifeScale,
|
|
84
|
+
width: entity.sprite.width,
|
|
85
|
+
height: entity.sprite.height,
|
|
86
|
+
layer: entity.sprite.layer
|
|
87
|
+
};
|
|
88
|
+
const spriteFrame = animationFrame(state, tick) ?? entity.sprite.frame ?? binding?.frame;
|
|
89
|
+
if (document.schemaVersion !== 1 && binding) {
|
|
90
|
+
const sourceWidth = spriteFrame?.width ?? binding.trim?.sourceWidth ?? binding.width;
|
|
91
|
+
const sourceHeight = spriteFrame?.height ?? binding.trim?.sourceHeight ?? binding.height;
|
|
92
|
+
const renderedWidth = spriteFrame?.width ?? binding.width;
|
|
93
|
+
const renderedHeight = spriteFrame?.height ?? binding.height;
|
|
94
|
+
const cropX = spriteFrame ? 0 : (binding.trim?.x ?? 0);
|
|
95
|
+
const cropY = spriteFrame ? 0 : (binding.trim?.y ?? 0);
|
|
96
|
+
const anchorOffset = (rotation, scaleX, scaleY) => {
|
|
97
|
+
const x = ((cropX + renderedWidth / 2) / sourceWidth - binding.pivot.x) * spriteDefinition.width * scaleX;
|
|
98
|
+
const y = (binding.pivot.y - (cropY + renderedHeight / 2) / sourceHeight) * spriteDefinition.height * scaleY;
|
|
99
|
+
const cosine = Math.cos(rotation);
|
|
100
|
+
const sine = Math.sin(rotation);
|
|
101
|
+
return { x: x * cosine - y * sine, y: x * sine + y * cosine };
|
|
102
|
+
};
|
|
103
|
+
const current = anchorOffset(sprite.rotation, sprite.scaleX, sprite.scaleY);
|
|
104
|
+
const previous = anchorOffset(sprite.previousRotation ?? sprite.rotation, sprite.previousScaleX ?? sprite.scaleX, sprite.previousScaleY ?? sprite.scaleY);
|
|
105
|
+
sprite.x += current.x;
|
|
106
|
+
sprite.y += current.y;
|
|
107
|
+
sprite.previousX += previous.x;
|
|
108
|
+
sprite.previousY += previous.y;
|
|
109
|
+
sprite.width *= renderedWidth / sourceWidth;
|
|
110
|
+
sprite.height *= renderedHeight / sourceHeight;
|
|
111
|
+
}
|
|
112
|
+
if (spriteFrame) {
|
|
113
|
+
sprite.frame = { ...spriteFrame };
|
|
114
|
+
}
|
|
115
|
+
const tint = visual?.tint ?? authored.tint;
|
|
116
|
+
if (tint) {
|
|
117
|
+
sprite.tint = tint;
|
|
118
|
+
}
|
|
119
|
+
const previousTint = visual?.tint ?? previousAuthored.tint;
|
|
120
|
+
if (previousTint) {
|
|
121
|
+
sprite.previousTint = previousTint;
|
|
122
|
+
}
|
|
123
|
+
if (opacity !== 1 || entity.sprite.opacity !== undefined) {
|
|
124
|
+
sprite.opacity = Math.max(0, Math.min(1, opacity));
|
|
125
|
+
}
|
|
126
|
+
if (previousOpacity !== 1 || entity.sprite.opacity !== undefined) {
|
|
127
|
+
sprite.previousOpacity = Math.max(0, Math.min(1, previousOpacity));
|
|
128
|
+
}
|
|
129
|
+
if (entity.sprite.blend === "additive") {
|
|
130
|
+
sprite.blend = "additive";
|
|
131
|
+
}
|
|
132
|
+
if (entity.sprite.unlit) {
|
|
133
|
+
sprite.unlit = true;
|
|
134
|
+
}
|
|
135
|
+
const facing = entity.sprite.faceMotion;
|
|
136
|
+
const turned = facing !== undefined && state.velocityX !== 0 && state.velocityX < 0 !== (facing === "left");
|
|
137
|
+
if (visual?.flipX ?? (entity.sprite.flipX || turned)) {
|
|
138
|
+
sprite.flipX = true;
|
|
139
|
+
}
|
|
140
|
+
if (document.assets[entity.sprite.assetId]?.sampling === "linear") {
|
|
141
|
+
sprite.sampling = "linear";
|
|
142
|
+
}
|
|
143
|
+
sprites.push(sprite);
|
|
144
|
+
}
|
|
145
|
+
if (entity.tilemap) {
|
|
146
|
+
for (const tile of entity.tilemap.tiles) {
|
|
147
|
+
if (Math.abs(state.x + tile.x - cameraX) - tile.width / 2 > cullHalfWidth ||
|
|
148
|
+
Math.abs(state.y + tile.y - cameraY) - tile.height / 2 > cullHalfHeight) {
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
const item = {
|
|
152
|
+
entityId: entity.id,
|
|
153
|
+
assetId: entity.tilemap.assetId,
|
|
154
|
+
x: state.x + tile.x,
|
|
155
|
+
y: state.y + tile.y,
|
|
156
|
+
width: tile.width,
|
|
157
|
+
height: tile.height,
|
|
158
|
+
layer: entity.tilemap.layer
|
|
159
|
+
};
|
|
160
|
+
const tileFrame = tile.frame ?? document.assets[entity.tilemap.assetId]?.frame;
|
|
161
|
+
if (tileFrame) {
|
|
162
|
+
item.frame = { ...tileFrame };
|
|
163
|
+
}
|
|
164
|
+
if (document.assets[entity.tilemap.assetId]?.sampling === "linear") {
|
|
165
|
+
item.sampling = "linear";
|
|
166
|
+
}
|
|
167
|
+
tiles.push(item);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
if (entity.particles) {
|
|
171
|
+
particles.push({ entityId: entity.id, x: state.x, y: state.y, rotation: state.visual?.rotation ?? state.rotation, particles: entity.particles });
|
|
172
|
+
}
|
|
173
|
+
if (entity.light2d && scene.lighting) {
|
|
174
|
+
const light = entity.light2d;
|
|
175
|
+
const x = state.x + (light.offset?.x ?? 0);
|
|
176
|
+
const y = state.y + (light.offset?.y ?? 0);
|
|
177
|
+
if (Math.abs(x - cameraX) - light.radius <= viewHalfWidth && Math.abs(y - cameraY) - light.radius <= viewHalfHeight) {
|
|
178
|
+
entityLights.push({
|
|
179
|
+
x,
|
|
180
|
+
y,
|
|
181
|
+
color: light.color,
|
|
182
|
+
intensity: light.intensity,
|
|
183
|
+
radius: light.radius,
|
|
184
|
+
falloff: light.falloff,
|
|
185
|
+
distance: Math.hypot(x - cameraX, y - cameraY)
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
sprites.sort((a, b) => a.layer - b.layer);
|
|
191
|
+
tiles.sort((a, b) => a.layer - b.layer);
|
|
192
|
+
const frame = {
|
|
193
|
+
gameId: document.id,
|
|
194
|
+
fonts: Object.fromEntries(Object.entries(document.assets).filter(([, binding]) => binding.mediaKind === "font")),
|
|
195
|
+
tick,
|
|
196
|
+
width: camera?.width ?? 16,
|
|
197
|
+
height: camera?.height ?? 9,
|
|
198
|
+
pixelsPerUnit: document.pixelsPerUnit,
|
|
199
|
+
camera: {
|
|
200
|
+
x: cameraState?.x ?? 0,
|
|
201
|
+
y: cameraState?.y ?? 0,
|
|
202
|
+
previousX: cameraState?.previousX ?? 0,
|
|
203
|
+
previousY: cameraState?.previousY ?? 0,
|
|
204
|
+
zoom: camera?.zoom ?? 1
|
|
205
|
+
},
|
|
206
|
+
sprites,
|
|
207
|
+
tiles,
|
|
208
|
+
backgrounds: (scene.backgrounds ?? []).map((layer) => document.assets[layer.assetId]?.sampling === "linear" ? { ...layer, sampling: "linear" } : layer),
|
|
209
|
+
hud: projectGameplayHud(usesCollectibles(document), score, won, hud)
|
|
210
|
+
};
|
|
211
|
+
if (particles.length > 0) {
|
|
212
|
+
frame.particles = particles;
|
|
213
|
+
}
|
|
214
|
+
if (audioEmitters.length > 0) {
|
|
215
|
+
frame.audioEmitters = audioEmitters;
|
|
216
|
+
}
|
|
217
|
+
const uiFrame = resolveGameUiFrame(document.ui, scene.ui, ui, (entityId) => {
|
|
218
|
+
const state = states.find((candidate) => candidate.definition.id === entityId);
|
|
219
|
+
const health = state?.definition.behaviors.find((behavior) => behavior.kind === "health");
|
|
220
|
+
return state && health?.kind === "health" ? { value: state.health ?? 0, max: health.maximum } : undefined;
|
|
221
|
+
});
|
|
222
|
+
if (uiFrame) {
|
|
223
|
+
frame.ui = uiFrame;
|
|
224
|
+
}
|
|
225
|
+
if (scene.lighting) {
|
|
226
|
+
// Entity lights follow their entities; the nearest ones fill the slots the scene's fixed lights leave.
|
|
227
|
+
const free = Math.max(0, MAX_LIGHTS - scene.lighting.points.length);
|
|
228
|
+
const moving = entityLights
|
|
229
|
+
.sort((a, b) => a.distance - b.distance)
|
|
230
|
+
.slice(0, free)
|
|
231
|
+
.map(({ distance: _distance, ...point }) => point);
|
|
232
|
+
frame.lighting = moving.length === 0 ? scene.lighting : { ...scene.lighting, points: [...scene.lighting.points, ...moving] };
|
|
233
|
+
}
|
|
234
|
+
return frame;
|
|
235
|
+
}
|
|
236
|
+
const MAX_LIGHTS = 32;
|
|
237
|
+
function usesCollectibles(document) {
|
|
238
|
+
return document.scenes.some((scene) => scene.entities.some((entity) => entity.behaviors.some((behavior) => behavior.kind === "collectible" || behavior.kind === "winWhenCollected")));
|
|
239
|
+
}
|
|
240
|
+
//# sourceMappingURL=presentation2d.js.map
|