dopaminejs 2.3.0 → 2.4.0
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/dist/ScreenShake-9jgNXPS6.cjs +1 -0
- package/dist/ScreenShake-CLvO1wx4.js +827 -0
- package/dist/dopamine.cjs +4 -4
- package/dist/dopamine.js +851 -1397
- package/dist/engine.cjs +1 -1
- package/dist/engine.js +2 -1158
- package/dist/style.css +2 -1
- package/package.json +1 -1
- package/src/core/PluginRegistry.js +90 -5
- package/src/dopamine/audio/SoundManager.js +7 -2
- package/src/systems/Physics.js +89 -0
- package/types/index.d.ts +37 -7
package/dist/style.css
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
#game-ui-overlay,.game-ui-overlay{
|
|
1
|
+
#game-ui-overlay,.game-ui-overlay{pointer-events:none;z-index:1000;width:100%;font-family:Inter,-apple-system,BlinkMacSystemFont,Segoe UI,sans-serif;position:fixed;top:0;left:0}.game-ui-top-bar{-webkit-backdrop-filter:blur(10px);backdrop-filter:blur(10px);background:#0000004d;align-items:center;gap:12px;padding:12px 16px;display:flex}.level-badge{background:linear-gradient(135deg,#667eea 0%,#764ba2 100%);border-radius:12px;flex-direction:column;align-items:center;min-width:60px;padding:8px 12px;display:flex;box-shadow:0 4px 12px #667eea66}.level-label{color:#fffc;letter-spacing:.5px;font-size:10px;font-weight:600}.level-number{color:#fff;font-size:24px;font-weight:800;line-height:1}.xp-container{flex-direction:column;flex:1;gap:4px;display:flex}.xp-bar-bg{background:#fff3;border-radius:6px;height:12px;overflow:hidden;box-shadow:inset 0 2px 4px #0003}.xp-bar-fill{background:linear-gradient(90deg,#4facfe 0%,#00f2fe 100%);border-radius:6px;height:100%;transition:width .5s ease-out;box-shadow:0 0 10px #4facfe99}.xp-text{color:#fff;text-align:center;text-shadow:0 1px 2px #00000080;font-size:11px;font-weight:600}.streak-badge{background:linear-gradient(135deg,#f80 0%,#f40 100%);border-radius:12px;align-items:center;gap:6px;padding:8px 12px;display:flex;box-shadow:0 4px 12px #f806}.streak-icon{font-size:20px;animation:2s infinite flicker}.streak-number{color:#fff;font-size:18px;font-weight:800}.achievement-popup{z-index:10000;background:linear-gradient(135deg,#667eea 0%,#764ba2 100%);border-radius:16px;max-width:320px;padding:16px;animation:.5s cubic-bezier(.68,-.55,.265,1.55) slideIn;position:fixed;top:80px;right:20px;overflow:hidden;box-shadow:0 8px 32px #0006}.achievement-shine{background:linear-gradient(90deg,#0000,#ffffff4d,#0000);width:100%;height:100%;animation:2s infinite shine;position:absolute;top:0;left:-100%}.achievement-content{align-items:center;gap:12px;display:flex;position:relative}.achievement-icon{font-size:48px;animation:.6s ease-out bounce}.achievement-info{flex:1}.achievement-title{color:#ffffffe6;text-transform:uppercase;letter-spacing:.5px;font-size:12px;font-weight:600}.achievement-name{color:#fff;margin:2px 0;font-size:18px;font-weight:800}.achievement-desc{color:#fffc;margin-bottom:4px;font-size:13px}.achievement-xp{color:gold;background:#fff3;border-radius:8px;padding:4px 8px;font-size:12px;font-weight:700;display:inline-block}.level-up-overlay{z-index:10001;background:#000000d9;justify-content:center;align-items:center;width:100%;height:100%;animation:.3s ease-out fadeIn;display:flex;position:fixed;top:0;left:0}.level-up-content{text-align:center;animation:.5s cubic-bezier(.68,-.55,.265,1.55) scaleIn}.level-up-title{background:linear-gradient(135deg,gold 0%,#ffed4e 100%);-webkit-text-fill-color:transparent;text-transform:uppercase;letter-spacing:4px;-webkit-background-clip:text;background-clip:text;margin-bottom:16px;font-size:32px;font-weight:800}.level-up-number{color:#fff;text-shadow:0 0 20px #ffd700cc,0 0 40px #ffd70099,0 0 60px #ffd70066;font-size:120px;font-weight:900;animation:1s ease-in-out infinite alternate glow}.level-up-subtitle{color:#fffc;margin-top:16px;font-size:24px;font-weight:600}.floating-text{text-shadow:2px 2px #000,-1px -1px #000,1px -1px #000,-1px 1px #000;pointer-events:none;z-index:9998;font-weight:800;animation:2s ease-out forwards floatUp;position:fixed}@keyframes slideIn{0%{opacity:0;transform:translate(400px)}to{opacity:1;transform:translate(0)}}@keyframes slideOut{0%{opacity:1;transform:translate(0)}to{opacity:0;transform:translate(400px)}}@keyframes fadeIn{0%{opacity:0}to{opacity:1}}@keyframes scaleIn{0%{opacity:0;transform:scale(.5)}to{opacity:1;transform:scale(1)}}@keyframes bounce{0%,to{transform:translateY(0)}50%{transform:translateY(-10px)}}@keyframes glow{0%{text-shadow:0 0 20px #ffd700cc,0 0 40px #ffd70099,0 0 60px #ffd70066}to{text-shadow:0 0 30px gold,0 0 60px #ffd700cc,0 0 90px #ffd70099}}@keyframes shine{0%{left:-100%}30%{left:100%}to{left:100%}}@keyframes flicker{0%,to{opacity:1}50%{opacity:.8}}@keyframes floatUp{0%{opacity:1;transform:translateY(0)}to{opacity:0;transform:translateY(-100px)}}@keyframes xp-pulse{0%{transform:scaleY(1)}50%{transform:scaleY(1.2)}to{transform:scaleY(1)}}@keyframes level-pulse{0%{transform:scale(1)}50%{transform:scale(1.1)}to{transform:scale(1)}}@media (width<=480px){.game-ui-top-bar{gap:8px;padding:10px 12px}.level-badge{min-width:50px;padding:6px 10px}.level-number{font-size:20px}.xp-bar-bg{height:10px}.xp-text{font-size:10px}.achievement-popup{max-width:calc(100% - 24px);right:12px}.level-up-number{font-size:80px}}@media (prefers-reduced-motion:reduce){.game-ui-overlay *,.achievement-popup,.achievement-popup *,.level-up-overlay,.level-up-overlay *{transition:none!important;animation:none!important}.floating-text{animation:none!important}}
|
|
2
|
+
/*$vite$:1*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dopaminejs",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.4.0",
|
|
4
4
|
"description": "Progression mechanics for web apps: XP, levels, achievements and timezone-correct daily streaks, with the particles and sound to make them land. Zero dependencies.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/dopamine.cjs",
|
|
@@ -20,6 +20,19 @@ export class PluginRegistry {
|
|
|
20
20
|
// Async inits in flight. Each notes the systems other plugins added
|
|
21
21
|
// meanwhile, so a failed one removes only its own.
|
|
22
22
|
this._inFlight = new Set();
|
|
23
|
+
|
|
24
|
+
// Map<pluginName, Map<systemName, system>>: what each plugin
|
|
25
|
+
// registered during init, so remove() can unregister it.
|
|
26
|
+
this._owned = new Map();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Every registered system by name.
|
|
31
|
+
* @private
|
|
32
|
+
*/
|
|
33
|
+
_snapshot() {
|
|
34
|
+
const systems = this.kernel.systems;
|
|
35
|
+
return new Map(systems.getSystemNames().map((name) => [name, systems.get(name)]));
|
|
23
36
|
}
|
|
24
37
|
|
|
25
38
|
/**
|
|
@@ -29,6 +42,57 @@ export class PluginRegistry {
|
|
|
29
42
|
return this.kernel.systems.getSystemNames().filter((name) => !before.has(name));
|
|
30
43
|
}
|
|
31
44
|
|
|
45
|
+
/**
|
|
46
|
+
* Names of the systems added or replaced since a snapshot. A plugin that
|
|
47
|
+
* swaps in its own 'audio' adds no new name, so names alone miss it.
|
|
48
|
+
* @private
|
|
49
|
+
*/
|
|
50
|
+
_changedSince(before) {
|
|
51
|
+
const systems = this.kernel.systems;
|
|
52
|
+
return systems.getSystemNames().filter((name) => systems.get(name) !== before.get(name));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Remember the systems a plugin registered during init.
|
|
57
|
+
* @private
|
|
58
|
+
*/
|
|
59
|
+
_own(pluginName, names) {
|
|
60
|
+
const systems = this.kernel.systems;
|
|
61
|
+
const owned = new Map();
|
|
62
|
+
|
|
63
|
+
for (const name of names) {
|
|
64
|
+
const system = systems.get(name);
|
|
65
|
+
|
|
66
|
+
// A plugin loaded from inside this one's init() already owns it.
|
|
67
|
+
const taken = [...this._owned.values()].some((other) => other.get(name) === system);
|
|
68
|
+
if (!taken) owned.set(name, system);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
this._owned.set(pluginName, owned);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Unregister the systems a removed plugin registered during init.
|
|
76
|
+
* @private
|
|
77
|
+
*/
|
|
78
|
+
_release(pluginName) {
|
|
79
|
+
const owned = this._owned.get(pluginName);
|
|
80
|
+
if (!owned) return;
|
|
81
|
+
|
|
82
|
+
this._owned.delete(pluginName);
|
|
83
|
+
|
|
84
|
+
for (const [name, system] of owned) {
|
|
85
|
+
// Replaced or unregistered since. It is no longer this plugin's.
|
|
86
|
+
if (this.kernel.systems.get(name) !== system) continue;
|
|
87
|
+
|
|
88
|
+
try {
|
|
89
|
+
this.kernel.systems.unregister(name);
|
|
90
|
+
} catch (error) {
|
|
91
|
+
console.error(`[PluginRegistry] Error removing system "${name}":`, error);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
32
96
|
/**
|
|
33
97
|
* Mark systems as belonging to a plugin that loaded.
|
|
34
98
|
* @private
|
|
@@ -107,18 +171,20 @@ export class PluginRegistry {
|
|
|
107
171
|
this._loadOrder.push(plugin.name);
|
|
108
172
|
|
|
109
173
|
// Initialize plugin
|
|
110
|
-
const systemsBefore =
|
|
174
|
+
const systemsBefore = this._snapshot();
|
|
111
175
|
|
|
112
176
|
try {
|
|
113
177
|
plugin.init(this.kernel);
|
|
114
178
|
|
|
179
|
+
const mine = this._changedSince(systemsBefore);
|
|
180
|
+
this._own(plugin.name, mine);
|
|
181
|
+
this._claim(mine);
|
|
182
|
+
|
|
115
183
|
// Emit event
|
|
116
184
|
this.kernel.events.emit('plugin_loaded', {
|
|
117
185
|
name: plugin.name,
|
|
118
186
|
version: plugin.version
|
|
119
187
|
});
|
|
120
|
-
|
|
121
|
-
this._claim(this._systemsSince(systemsBefore));
|
|
122
188
|
} catch (error) {
|
|
123
189
|
console.error(`[PluginRegistry] Failed to initialize plugin "${plugin.name}":`, error);
|
|
124
190
|
this._forget(plugin.name);
|
|
@@ -156,7 +222,7 @@ export class PluginRegistry {
|
|
|
156
222
|
this._loadOrder.push(plugin.name);
|
|
157
223
|
|
|
158
224
|
const pending = {
|
|
159
|
-
before:
|
|
225
|
+
before: this._snapshot(),
|
|
160
226
|
foreign: new Set()
|
|
161
227
|
};
|
|
162
228
|
const own = () => this._systemsSince(pending.before).filter((name) => !pending.foreign.has(name));
|
|
@@ -167,7 +233,18 @@ export class PluginRegistry {
|
|
|
167
233
|
await plugin.init(this.kernel);
|
|
168
234
|
|
|
169
235
|
this._inFlight.delete(pending);
|
|
170
|
-
|
|
236
|
+
|
|
237
|
+
const systems = this.kernel.systems;
|
|
238
|
+
const mine = this._changedSince(pending.before).filter((name) => !pending.foreign.has(name));
|
|
239
|
+
|
|
240
|
+
// With another init still running, a system that appeared after
|
|
241
|
+
// that one began could be its work. Nobody owns those, so they
|
|
242
|
+
// stay registered when either plugin is removed.
|
|
243
|
+
const others = [...this._inFlight];
|
|
244
|
+
this._own(plugin.name, mine.filter(
|
|
245
|
+
(name) => others.every((other) => other.before.get(name) === systems.get(name))
|
|
246
|
+
));
|
|
247
|
+
this._claim(mine);
|
|
171
248
|
|
|
172
249
|
// Emit event
|
|
173
250
|
this.kernel.events.emit('plugin_loaded', {
|
|
@@ -196,6 +273,12 @@ export class PluginRegistry {
|
|
|
196
273
|
|
|
197
274
|
/**
|
|
198
275
|
* Remove a plugin
|
|
276
|
+
*
|
|
277
|
+
* Calls the plugin's destroy(), then unregisters the systems it
|
|
278
|
+
* registered during init() that are still in place. Not tracked, and so
|
|
279
|
+
* the plugin's to unregister in destroy(): a system it registers after
|
|
280
|
+
* init(), and one registered while two useAsync() inits overlapped.
|
|
281
|
+
*
|
|
199
282
|
* @param {string} name - Plugin name
|
|
200
283
|
* @returns {boolean} - True if plugin was found and removed
|
|
201
284
|
*/
|
|
@@ -222,6 +305,8 @@ export class PluginRegistry {
|
|
|
222
305
|
this._loadOrder.splice(index, 1);
|
|
223
306
|
}
|
|
224
307
|
|
|
308
|
+
this._release(name);
|
|
309
|
+
|
|
225
310
|
// Emit event
|
|
226
311
|
this.kernel.events.emit('plugin_unloaded', { name });
|
|
227
312
|
|
|
@@ -128,11 +128,16 @@ export class SoundManager {
|
|
|
128
128
|
}
|
|
129
129
|
|
|
130
130
|
/**
|
|
131
|
-
*
|
|
131
|
+
* Register and load multiple sounds. Before the audio context exists the
|
|
132
|
+
* files cannot be decoded, so they load when initAudio() creates it.
|
|
132
133
|
* @param {Object} soundMap - { key: url }
|
|
133
134
|
*/
|
|
134
135
|
async preloadSounds(soundMap) {
|
|
135
|
-
const promises = Object.entries(soundMap).map(([key, url]) =>
|
|
136
|
+
const promises = Object.entries(soundMap).map(([key, url]) => {
|
|
137
|
+
// play() only loads keys it finds here.
|
|
138
|
+
this.customSounds[key] = url;
|
|
139
|
+
return this.loadSound(key, url);
|
|
140
|
+
});
|
|
136
141
|
await Promise.allSettled(promises);
|
|
137
142
|
}
|
|
138
143
|
|
package/src/systems/Physics.js
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
import { deprecatedGlobal } from './deprecate.js';
|
|
2
2
|
|
|
3
3
|
import { Collider } from '../core/Collider.js';
|
|
4
|
+
import { EventBus } from '../core/EventBus.js';
|
|
4
5
|
|
|
5
6
|
export class Physics {
|
|
6
7
|
constructor() {
|
|
7
8
|
this.colliders = [];
|
|
8
9
|
this.kernel = null;
|
|
10
|
+
|
|
11
|
+
// Map<Collider, Map<Collider, step>>: the pairs that overlapped as of
|
|
12
|
+
// the last step, each with the step that last saw it. Without it
|
|
13
|
+
// there is no telling a new overlap from one that is still going.
|
|
14
|
+
this._pairs = new Map();
|
|
15
|
+
this._step = 0;
|
|
9
16
|
}
|
|
10
17
|
|
|
11
18
|
/**
|
|
@@ -27,6 +34,7 @@ export class Physics {
|
|
|
27
34
|
*/
|
|
28
35
|
destroy() {
|
|
29
36
|
this.colliders = [];
|
|
37
|
+
this._pairs.clear();
|
|
30
38
|
}
|
|
31
39
|
|
|
32
40
|
add(collider) {
|
|
@@ -36,13 +44,41 @@ export class Physics {
|
|
|
36
44
|
remove(collider) {
|
|
37
45
|
const idx = this.colliders.indexOf(collider);
|
|
38
46
|
if (idx > -1) this.colliders.splice(idx, 1);
|
|
47
|
+
|
|
48
|
+
// A removed collider overlaps nothing, so its pairs end here. Left
|
|
49
|
+
// in the map they would also keep the collider alive.
|
|
50
|
+
const others = this._pairs.get(collider);
|
|
51
|
+
if (others) {
|
|
52
|
+
this._pairs.delete(collider);
|
|
53
|
+
|
|
54
|
+
// Emptied before emitting: step() may be part way through this
|
|
55
|
+
// map when an exit listener removes the collider, and would end
|
|
56
|
+
// the same pairs again.
|
|
57
|
+
const ended = [...others.keys()];
|
|
58
|
+
others.clear();
|
|
59
|
+
for (const other of ended) {
|
|
60
|
+
this._emit(EventBus.Events.COLLISION_EXIT, collider, other);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
for (const [first, seconds] of this._pairs) {
|
|
64
|
+
if (seconds.delete(collider)) {
|
|
65
|
+
this._emit(EventBus.Events.COLLISION_EXIT, first, collider);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
39
68
|
}
|
|
40
69
|
|
|
41
70
|
/**
|
|
42
71
|
* Main Physics Step.
|
|
43
72
|
* Checks all pairs and triggers callbacks.
|
|
73
|
+
*
|
|
74
|
+
* Emits `collision_enter` on the kernel event bus when a pair starts to
|
|
75
|
+
* overlap and `collision_exit` when it stops, each with `{ a, b }` (the
|
|
76
|
+
* two colliders). `onCollisionEnter` on a component is older and runs on
|
|
77
|
+
* every step while the pair overlaps.
|
|
44
78
|
*/
|
|
45
79
|
step() {
|
|
80
|
+
const step = ++this._step;
|
|
81
|
+
|
|
46
82
|
// Naive O(N^2)
|
|
47
83
|
for (let i = 0; i < this.colliders.length; i++) {
|
|
48
84
|
for (let j = i + 1; j < this.colliders.length; j++) {
|
|
@@ -50,12 +86,65 @@ export class Physics {
|
|
|
50
86
|
const b = this.colliders[j];
|
|
51
87
|
|
|
52
88
|
if (this._intersects(a, a.getBounds(), b, b.getBounds())) {
|
|
89
|
+
this._seen(a, b, step);
|
|
90
|
+
|
|
53
91
|
// Notify GameObjects
|
|
54
92
|
this._notifyCollision(a.gameObject, b.gameObject);
|
|
55
93
|
this._notifyCollision(b.gameObject, a.gameObject);
|
|
56
94
|
}
|
|
57
95
|
}
|
|
58
96
|
}
|
|
97
|
+
|
|
98
|
+
for (const [a, others] of this._pairs) {
|
|
99
|
+
for (const [b, seen] of others) {
|
|
100
|
+
if (seen === step) continue;
|
|
101
|
+
|
|
102
|
+
// A collider removed during the loop above shifts the list,
|
|
103
|
+
// and the loop then passes over some pairs. Not being seen
|
|
104
|
+
// is not enough to end one.
|
|
105
|
+
if (this._intersects(a, a.getBounds(), b, b.getBounds())) {
|
|
106
|
+
others.set(b, step);
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
others.delete(b);
|
|
111
|
+
this._emit(EventBus.Events.COLLISION_EXIT, a, b);
|
|
112
|
+
}
|
|
113
|
+
if (others.size === 0) this._pairs.delete(a);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Record that a pair overlaps on this step. Emits `collision_enter` when
|
|
119
|
+
* it did not overlap on the step before.
|
|
120
|
+
* @private
|
|
121
|
+
*/
|
|
122
|
+
_seen(a, b, step) {
|
|
123
|
+
// A pair is stored under the collider that was first in the list
|
|
124
|
+
// when it began. `colliders` is public and can be reordered, so look
|
|
125
|
+
// under the other one too.
|
|
126
|
+
if (this._pairs.get(b)?.has(a)) {
|
|
127
|
+
this._pairs.get(b).set(a, step);
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
let others = this._pairs.get(a);
|
|
132
|
+
if (!others) {
|
|
133
|
+
others = new Map();
|
|
134
|
+
this._pairs.set(a, others);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const known = others.has(b);
|
|
138
|
+
others.set(b, step);
|
|
139
|
+
if (!known) this._emit(EventBus.Events.COLLISION_ENTER, a, b);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* @private
|
|
144
|
+
*/
|
|
145
|
+
_emit(event, a, b) {
|
|
146
|
+
// GlobalPhysics and a Physics built by hand have no kernel.
|
|
147
|
+
this.kernel?.events?.emit(event, { a, b });
|
|
59
148
|
}
|
|
60
149
|
|
|
61
150
|
_notifyCollision(go, other) {
|
package/types/index.d.ts
CHANGED
|
@@ -218,12 +218,15 @@ export interface ParticleConfig {
|
|
|
218
218
|
x: number;
|
|
219
219
|
y: number;
|
|
220
220
|
count?: number;
|
|
221
|
-
|
|
221
|
+
/** One colour, or a list to pick from at random per particle. */
|
|
222
|
+
color?: string | string[];
|
|
222
223
|
size?: number;
|
|
223
224
|
life?: number;
|
|
224
225
|
decay?: number;
|
|
225
226
|
gravity?: number;
|
|
226
227
|
spread?: number;
|
|
228
|
+
/** Direction in radians. */
|
|
229
|
+
angle?: number;
|
|
227
230
|
speed?: number;
|
|
228
231
|
type?: string;
|
|
229
232
|
sprite?: string;
|
|
@@ -281,6 +284,7 @@ export class SoundManager {
|
|
|
281
284
|
toggleMute(): boolean;
|
|
282
285
|
|
|
283
286
|
registerSound(key: string, url: string): void;
|
|
287
|
+
/** Registers every key, then loads the files. Before the audio context exists they load when it is created. */
|
|
284
288
|
preloadSounds(soundMap: Record<string, string>): Promise<void>;
|
|
285
289
|
loadSound(key: string, url: string): Promise<void>;
|
|
286
290
|
play(key: string): Promise<void>;
|
|
@@ -369,6 +373,7 @@ export class PluginRegistry {
|
|
|
369
373
|
constructor(kernel: DopamineKernel);
|
|
370
374
|
use(plugin: Plugin): this;
|
|
371
375
|
useAsync(plugin: Plugin): Promise<this>;
|
|
376
|
+
/** Calls the plugin's `destroy()`, then unregisters the systems it registered during `init()`. */
|
|
372
377
|
remove(name: string): boolean;
|
|
373
378
|
get(name: string): Plugin | undefined;
|
|
374
379
|
has(name: string): boolean;
|
|
@@ -433,19 +438,29 @@ export class Component {
|
|
|
433
438
|
render(ctx: CanvasRenderingContext2D): void;
|
|
434
439
|
}
|
|
435
440
|
|
|
436
|
-
export interface
|
|
441
|
+
export interface BoxBounds {
|
|
442
|
+
left: number;
|
|
443
|
+
right: number;
|
|
444
|
+
top: number;
|
|
445
|
+
bottom: number;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
export interface CircleBounds {
|
|
437
449
|
x: number;
|
|
438
450
|
y: number;
|
|
439
|
-
|
|
440
|
-
height: number;
|
|
451
|
+
radius: number;
|
|
441
452
|
}
|
|
442
453
|
|
|
454
|
+
/** A box collider returns edges, a circle collider returns centre and radius. */
|
|
455
|
+
export type Bounds = BoxBounds | CircleBounds;
|
|
456
|
+
|
|
443
457
|
export class Collider extends Component {
|
|
444
458
|
constructor(type?: 'box' | 'circle', width?: number, height?: number, radius?: number);
|
|
445
459
|
type: 'box' | 'circle';
|
|
446
460
|
width: number;
|
|
447
461
|
height: number;
|
|
448
462
|
radius: number;
|
|
463
|
+
tag: string | null;
|
|
449
464
|
getBounds(): Bounds;
|
|
450
465
|
}
|
|
451
466
|
|
|
@@ -462,11 +477,13 @@ export class Animator extends Component {
|
|
|
462
477
|
|
|
463
478
|
export class GameObject {
|
|
464
479
|
constructor(x?: number, y?: number);
|
|
465
|
-
|
|
480
|
+
x: number;
|
|
481
|
+
y: number;
|
|
466
482
|
rotation: number;
|
|
467
|
-
scale:
|
|
483
|
+
scale: { x: number; y: number };
|
|
468
484
|
components: Component[];
|
|
469
485
|
children: GameObject[];
|
|
486
|
+
parent: GameObject | null;
|
|
470
487
|
kernel: DopamineKernel | null;
|
|
471
488
|
tag?: string;
|
|
472
489
|
addChild(child: GameObject): GameObject;
|
|
@@ -520,14 +537,27 @@ export class Input {
|
|
|
520
537
|
mouse: { x: number; y: number };
|
|
521
538
|
}
|
|
522
539
|
|
|
540
|
+
/** Payload of `collision_enter` and `collision_exit` on the kernel event bus. */
|
|
541
|
+
export interface CollisionEvent {
|
|
542
|
+
a: Collider;
|
|
543
|
+
b: Collider;
|
|
544
|
+
}
|
|
545
|
+
|
|
523
546
|
export class Physics {
|
|
524
547
|
init(kernel: DopamineKernel): void;
|
|
525
548
|
fixedUpdate(dt: number): void;
|
|
526
549
|
destroy(): void;
|
|
527
550
|
add(collider: Collider): void;
|
|
551
|
+
/** Emits `collision_exit` for each pair the collider was overlapping in. */
|
|
528
552
|
remove(collider: Collider): void;
|
|
553
|
+
/**
|
|
554
|
+
* Emits `collision_enter` once when a pair starts to overlap and
|
|
555
|
+
* `collision_exit` once when it stops. `onCollisionEnter` on a component
|
|
556
|
+
* runs on every step while the pair overlaps.
|
|
557
|
+
*/
|
|
529
558
|
step(): void;
|
|
530
|
-
|
|
559
|
+
/** Every collider that overlaps `source`. With a tag, only colliders carrying it. */
|
|
560
|
+
checkOverlap(source: Collider, targetTag?: string): Collider[];
|
|
531
561
|
addBody(body: Collider): void;
|
|
532
562
|
removeBody(body: Collider): void;
|
|
533
563
|
checkCollision(a: Collider, b: Collider): boolean;
|