dopaminejs 2.4.0 → 3.0.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/README.md +40 -50
- package/dist/DataService-E4pGQ1V3.cjs +1 -0
- package/dist/DataService-MfmvkqXR.js +737 -0
- package/dist/core.cjs +1 -0
- package/dist/core.js +2 -0
- package/dist/dopamine.cjs +1 -37
- package/dist/dopamine.js +3 -858
- package/dist/games.cjs +1 -0
- package/dist/games.js +82 -0
- package/dist/host-CkRL74ck.js +814 -0
- package/dist/host-Pzb-QBPU.cjs +37 -0
- package/dist/style.css +395 -2
- package/dist/testing.cjs +1 -0
- package/dist/testing.js +474 -0
- package/package.json +28 -8
- package/spec/README.md +227 -0
- package/spec/VERSION +1 -0
- package/spec/fixtures/achievements.json +611 -0
- package/spec/fixtures/levels.json +421 -0
- package/spec/fixtures/merge.json +1739 -0
- package/spec/fixtures/migrate.json +916 -0
- package/spec/fixtures/scores.json +755 -0
- package/spec/fixtures/streaks.json +924 -0
- package/spec/schema.json +61 -0
- package/src/dopamine/audio/SoundManager.js +78 -24
- package/src/dopamine/core/DataService.js +25 -5
- package/src/dopamine/core/DopamineError.js +7 -0
- package/src/dopamine/core/RewardSystem.js +366 -368
- package/src/dopamine/core/achievements.js +49 -0
- package/src/dopamine/core/clock.js +11 -0
- package/src/dopamine/core/levels.js +4 -0
- package/src/dopamine/core/merge.js +68 -0
- package/src/dopamine/core/middleware.js +58 -0
- package/src/dopamine/core/store.js +261 -0
- package/src/dopamine/core/streaks.js +55 -0
- package/src/dopamine/effects/ParticleSystem.js +33 -3
- package/src/dopamine/feel/haptics.js +21 -0
- package/src/dopamine/feel/motion.js +9 -0
- package/src/dopamine/host.js +358 -0
- package/src/dopamine/ui/RewardUI.js +569 -0
- package/src/games.js +138 -0
- package/src/index.js +14 -33
- package/src/progression.js +16 -0
- package/src/testing.js +489 -0
- package/types/core.d.ts +35 -0
- package/types/games.d.ts +46 -0
- package/types/index.d.ts +348 -373
- package/types/testing.d.ts +96 -0
- package/dist/ScreenShake-9jgNXPS6.cjs +0 -1
- package/dist/ScreenShake-CLvO1wx4.js +0 -827
- package/dist/engine.cjs +0 -1
- package/dist/engine.js +0 -2
- package/src/core/Animator.js +0 -62
- package/src/core/Collider.js +0 -86
- package/src/core/Component.js +0 -38
- package/src/core/DopamineKernel.js +0 -170
- package/src/core/EventBus.js +0 -206
- package/src/core/Game.js +0 -90
- package/src/core/GameObject.js +0 -99
- package/src/core/PluginRegistry.js +0 -359
- package/src/core/Scene.js +0 -107
- package/src/core/Sprite.js +0 -51
- package/src/core/SystemRegistry.js +0 -263
- package/src/core/Vector2.js +0 -50
- package/src/dopamine/components/ParticleEmitter.js +0 -50
- package/src/dopamine/components/ScreenShake.js +0 -50
- package/src/dopamine/components/index.js +0 -2
- package/src/dopamine/index.js +0 -93
- package/src/dopamine/ui/GameUI.js +0 -420
- package/src/dopamine/utils/TextureGenerator.js +0 -90
- package/src/engine.js +0 -38
- package/src/interfaces/IAudioSystem.js +0 -58
- package/src/interfaces/IParticleSystem.js +0 -51
- package/src/interfaces/IPhysicsSystem.js +0 -52
- package/src/interfaces/ISystem.js +0 -48
- package/src/interfaces/index.js +0 -7
- package/src/renderer/Renderer.js +0 -45
- package/src/systems/Director.js +0 -34
- package/src/systems/Input.js +0 -81
- package/src/systems/Loader.js +0 -48
- package/src/systems/Physics.js +0 -210
- package/src/systems/Ticker.js +0 -91
- package/src/systems/deprecate.js +0 -38
package/dist/engine.cjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("./ScreenShake-9jgNXPS6.cjs");exports.Animator=e.r,exports.Collider=e._,exports.Component=e.v,exports.Director=e.c,exports.DopamineKernel=e.u,exports.EventBus=e.C,exports.Game=e.s,exports.GameObject=e.o,exports.GlobalInput=e.p,exports.GlobalLoader=e.d,exports.GlobalPhysics=e.h,exports.Input=e.m,exports.Loader=e.f,exports.ParticleEmitter=e.n,exports.Physics=e.g,exports.PluginRegistry=e.x,exports.Renderer=e.b,exports.Scene=e.l,exports.ScreenShake=e.t,exports.Sprite=e.i,exports.SystemRegistry=e.S,exports.Ticker=e.y,exports.Vector2=e.a;
|
package/dist/engine.js
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
import { C as e, S as t, _ as n, a as r, b as i, c as a, d as o, f as s, g as c, h as l, i as u, l as d, m as f, n as p, o as m, p as h, r as g, s as _, t as v, u as y, v as b, x, y as S } from "./ScreenShake-CLvO1wx4.js";
|
|
2
|
-
export { g as Animator, n as Collider, b as Component, a as Director, y as DopamineKernel, e as EventBus, _ as Game, m as GameObject, h as GlobalInput, o as GlobalLoader, l as GlobalPhysics, f as Input, s as Loader, p as ParticleEmitter, c as Physics, x as PluginRegistry, i as Renderer, d as Scene, v as ScreenShake, u as Sprite, t as SystemRegistry, S as Ticker, r as Vector2 };
|
package/src/core/Animator.js
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import { Component } from '../core/Component.js';
|
|
2
|
-
import { Sprite } from './Sprite.js';
|
|
3
|
-
|
|
4
|
-
export class Animator extends Component {
|
|
5
|
-
constructor() {
|
|
6
|
-
super();
|
|
7
|
-
this.animations = {}; // { name: { frames: [{x,y,w,h}], fps: 10, loop: true } }
|
|
8
|
-
this.currentAnim = null;
|
|
9
|
-
this.timer = 0;
|
|
10
|
-
this.currentFrameIndex = 0;
|
|
11
|
-
this.sprite = null;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
onAttach() {
|
|
15
|
-
this.sprite = this.gameObject.getComponent(Sprite);
|
|
16
|
-
if (!this.sprite) {
|
|
17
|
-
console.warn('Animator attached to GameObject without a Sprite component.');
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
addAnimation(name, frames, fps = 10, loop = true) {
|
|
22
|
-
this.animations[name] = { frames, fps, loop };
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
play(name) {
|
|
26
|
-
if (this.currentAnim && this.currentAnim.name === name) return;
|
|
27
|
-
|
|
28
|
-
if (this.animations[name]) {
|
|
29
|
-
this.currentAnim = { name, ...this.animations[name] };
|
|
30
|
-
this.currentFrameIndex = 0;
|
|
31
|
-
this.timer = 0;
|
|
32
|
-
this._updateFrame();
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
update(dt) {
|
|
37
|
-
if (!this.currentAnim || !this.sprite) return;
|
|
38
|
-
|
|
39
|
-
this.timer += dt;
|
|
40
|
-
const interval = 1.0 / this.currentAnim.fps;
|
|
41
|
-
|
|
42
|
-
if (this.timer >= interval) {
|
|
43
|
-
this.timer -= interval;
|
|
44
|
-
this.currentFrameIndex++;
|
|
45
|
-
|
|
46
|
-
if (this.currentFrameIndex >= this.currentAnim.frames.length) {
|
|
47
|
-
if (this.currentAnim.loop) {
|
|
48
|
-
this.currentFrameIndex = 0;
|
|
49
|
-
} else {
|
|
50
|
-
this.currentFrameIndex = this.currentAnim.frames.length - 1;
|
|
51
|
-
// Could emit 'animationComplete' here
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
this._updateFrame();
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
_updateFrame() {
|
|
59
|
-
const frame = this.currentAnim.frames[this.currentFrameIndex];
|
|
60
|
-
this.sprite.setFrame(frame.x, frame.y, frame.w, frame.h);
|
|
61
|
-
}
|
|
62
|
-
}
|
package/src/core/Collider.js
DELETED
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
import { Component } from './Component.js';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Collider Component
|
|
5
|
-
* Handles collision detection for GameObjects.
|
|
6
|
-
*/
|
|
7
|
-
export class Collider extends Component {
|
|
8
|
-
constructor(type = 'box', width = 50, height = 50, radius = 25) {
|
|
9
|
-
super();
|
|
10
|
-
this.type = type; // 'box' or 'circle'
|
|
11
|
-
this.width = width;
|
|
12
|
-
this.height = height;
|
|
13
|
-
this.radius = radius;
|
|
14
|
-
this.tag = null; // For collision filtering
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
get kernel() {
|
|
18
|
-
return this._kernel ?? null;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* The kernel can arrive after onAttach(), when the collider was added to
|
|
23
|
-
* an object before that object joined a scene. Registering only in
|
|
24
|
-
* onAttach() left such colliders out of physics for good.
|
|
25
|
-
*/
|
|
26
|
-
set kernel(value) {
|
|
27
|
-
if (value === this._kernel) return;
|
|
28
|
-
|
|
29
|
-
this._unregister();
|
|
30
|
-
this._kernel = value;
|
|
31
|
-
this._register();
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
onAttach() {
|
|
35
|
-
super.onAttach();
|
|
36
|
-
this._attached = true;
|
|
37
|
-
this._register();
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
onDetach() {
|
|
41
|
-
super.onDetach();
|
|
42
|
-
this._attached = false;
|
|
43
|
-
this._unregister();
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* @private
|
|
48
|
-
*/
|
|
49
|
-
_register() {
|
|
50
|
-
if (this._physics || !this._attached) return;
|
|
51
|
-
|
|
52
|
-
const physics = this.physics;
|
|
53
|
-
if (physics) {
|
|
54
|
-
physics.add(this);
|
|
55
|
-
this._physics = physics;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* @private
|
|
61
|
-
*/
|
|
62
|
-
_unregister() {
|
|
63
|
-
if (this._physics) {
|
|
64
|
-
this._physics.remove(this);
|
|
65
|
-
this._physics = null;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
getBounds() {
|
|
70
|
-
const go = this.gameObject;
|
|
71
|
-
if (this.type === 'box') {
|
|
72
|
-
return {
|
|
73
|
-
left: go.x - this.width / 2,
|
|
74
|
-
right: go.x + this.width / 2,
|
|
75
|
-
top: go.y - this.height / 2,
|
|
76
|
-
bottom: go.y + this.height / 2,
|
|
77
|
-
};
|
|
78
|
-
} else if (this.type === 'circle') {
|
|
79
|
-
return {
|
|
80
|
-
x: go.x,
|
|
81
|
-
y: go.y,
|
|
82
|
-
radius: this.radius,
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
}
|
package/src/core/Component.js
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Base class for all logic components attached to GameObjects.
|
|
3
|
-
*/
|
|
4
|
-
export class Component {
|
|
5
|
-
constructor() {
|
|
6
|
-
this.gameObject = null;
|
|
7
|
-
this.kernel = null; // Injected by GameObject
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Access systems through kernel (no more globals!)
|
|
12
|
-
*/
|
|
13
|
-
get physics() {
|
|
14
|
-
return this.kernel?.systems.get('physics');
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
get input() {
|
|
18
|
-
return this.kernel?.systems.get('input');
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
get loader() {
|
|
22
|
-
return this.kernel?.systems.get('loader');
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
get events() {
|
|
26
|
-
return this.kernel?.events;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
onAttach() {
|
|
30
|
-
// Subclasses can override
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
onDetach() { }
|
|
34
|
-
|
|
35
|
-
update(dt) { }
|
|
36
|
-
|
|
37
|
-
render(ctx) { }
|
|
38
|
-
}
|
|
@@ -1,170 +0,0 @@
|
|
|
1
|
-
import { EventBus } from './EventBus.js';
|
|
2
|
-
import { SystemRegistry } from './SystemRegistry.js';
|
|
3
|
-
import { PluginRegistry } from './PluginRegistry.js';
|
|
4
|
-
import { Renderer } from '../renderer/Renderer.js';
|
|
5
|
-
import { Ticker } from '../systems/Ticker.js';
|
|
6
|
-
import { Physics } from '../systems/Physics.js';
|
|
7
|
-
import { Input } from '../systems/Input.js';
|
|
8
|
-
import { Loader } from '../systems/Loader.js';
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* DopamineKernel - Central orchestrator for the engine
|
|
12
|
-
*
|
|
13
|
-
* Replaces global singletons and provides dependency injection for all systems.
|
|
14
|
-
* This is the new foundation that enables the plugin architecture.
|
|
15
|
-
*/
|
|
16
|
-
export class DopamineKernel {
|
|
17
|
-
constructor(config = {}) {
|
|
18
|
-
this.config = config;
|
|
19
|
-
|
|
20
|
-
// Bound once. Binding inside start() would hand the ticker a new
|
|
21
|
-
// function object on every restart, stacking duplicate update loops.
|
|
22
|
-
this._boundUpdate = this._update.bind(this);
|
|
23
|
-
|
|
24
|
-
// Core registries
|
|
25
|
-
this.events = new EventBus();
|
|
26
|
-
this.systems = new SystemRegistry(this);
|
|
27
|
-
this.plugins = new PluginRegistry(this);
|
|
28
|
-
|
|
29
|
-
// Register core systems
|
|
30
|
-
this._registerCoreSystems(config);
|
|
31
|
-
|
|
32
|
-
// Fixed timestep for physics (60 FPS)
|
|
33
|
-
this.fixedTimestep = config.fixedTimestep || 1 / 60;
|
|
34
|
-
this._accumulator = 0;
|
|
35
|
-
this._maxAccumulator = this.fixedTimestep * 5; // Prevent spiral of death
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Register core engine systems
|
|
40
|
-
* These can be replaced by plugins
|
|
41
|
-
* @private
|
|
42
|
-
*/
|
|
43
|
-
_registerCoreSystems(config) {
|
|
44
|
-
// Ticker - highest priority, runs first
|
|
45
|
-
this.systems.register('ticker', new Ticker(), {
|
|
46
|
-
priority: 100
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
// Renderer
|
|
50
|
-
const rendererOptions = {
|
|
51
|
-
width: config.width,
|
|
52
|
-
height: config.height,
|
|
53
|
-
backgroundColor: config.backgroundColor,
|
|
54
|
-
canvas: config.canvas,
|
|
55
|
-
...config.renderer
|
|
56
|
-
};
|
|
57
|
-
this.systems.register('renderer', new Renderer(rendererOptions), {
|
|
58
|
-
priority: 90
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
// Input system
|
|
62
|
-
this.systems.register('input', new Input(), {
|
|
63
|
-
priority: 80
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
// Loader/Asset system
|
|
67
|
-
this.systems.register('loader', new Loader(), {
|
|
68
|
-
priority: 70
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
// Physics - depends on nothing, but runs in fixedUpdate
|
|
72
|
-
this.systems.register('physics', new Physics(), {
|
|
73
|
-
priority: 60
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Start the kernel (begins game loop)
|
|
79
|
-
*/
|
|
80
|
-
start() {
|
|
81
|
-
const ticker = this.systems.get('ticker');
|
|
82
|
-
if (ticker) {
|
|
83
|
-
ticker.add(this._boundUpdate);
|
|
84
|
-
ticker.start();
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
this.events.emit('kernel_started');
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Stop the kernel
|
|
92
|
-
*/
|
|
93
|
-
stop() {
|
|
94
|
-
const ticker = this.systems.get('ticker');
|
|
95
|
-
if (ticker) {
|
|
96
|
-
ticker.stop();
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
this.events.emit('kernel_stopped');
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Main update loop
|
|
104
|
-
* @private
|
|
105
|
-
*/
|
|
106
|
-
_update(dt) {
|
|
107
|
-
// Emit tick event (high priority listeners go first)
|
|
108
|
-
this.events.emit(EventBus.Events.TICK, { dt });
|
|
109
|
-
|
|
110
|
-
// Fixed timestep for physics
|
|
111
|
-
this._accumulator += dt;
|
|
112
|
-
|
|
113
|
-
// Clamp accumulator to prevent spiral of death
|
|
114
|
-
if (this._accumulator > this._maxAccumulator) {
|
|
115
|
-
this._accumulator = this._maxAccumulator;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
// Run fixed updates
|
|
119
|
-
while (this._accumulator >= this.fixedTimestep) {
|
|
120
|
-
this.events.emit(EventBus.Events.FIXED_UPDATE, { dt: this.fixedTimestep });
|
|
121
|
-
this.systems.fixedUpdate(this.fixedTimestep);
|
|
122
|
-
this._accumulator -= this.fixedTimestep;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
// Run variable timestep updates
|
|
126
|
-
this.systems.update(dt);
|
|
127
|
-
|
|
128
|
-
// Emit render event
|
|
129
|
-
this.events.emit(EventBus.Events.RENDER, { dt });
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* Shorthand accessors for common systems (backward compatibility)
|
|
134
|
-
*/
|
|
135
|
-
get physics() {
|
|
136
|
-
return this.systems.get('physics');
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
get input() {
|
|
140
|
-
return this.systems.get('input');
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
get loader() {
|
|
144
|
-
return this.systems.get('loader');
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
get renderer() {
|
|
148
|
-
return this.systems.get('renderer');
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
get ticker() {
|
|
152
|
-
return this.systems.get('ticker');
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Destroy the kernel and cleanup all systems
|
|
157
|
-
*/
|
|
158
|
-
destroy() {
|
|
159
|
-
this.stop();
|
|
160
|
-
|
|
161
|
-
const ticker = this.systems.get('ticker');
|
|
162
|
-
if (ticker) {
|
|
163
|
-
ticker.remove(this._boundUpdate);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
this.plugins.clear();
|
|
167
|
-
this.systems.clear();
|
|
168
|
-
this.events.clear();
|
|
169
|
-
}
|
|
170
|
-
}
|
package/src/core/EventBus.js
DELETED
|
@@ -1,206 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* EventBus - Central event system for loose coupling between systems
|
|
3
|
-
*
|
|
4
|
-
* Performance optimizations:
|
|
5
|
-
* - Priority queues for critical events (tick, render)
|
|
6
|
-
* - Object pooling for event data (future enhancement)
|
|
7
|
-
* - Pre-allocated arrays to minimize GC
|
|
8
|
-
*/
|
|
9
|
-
export class EventBus {
|
|
10
|
-
constructor() {
|
|
11
|
-
// Map<eventName, Set<{callback, priority}>>
|
|
12
|
-
this._listeners = new Map();
|
|
13
|
-
this._onceListeners = new Map();
|
|
14
|
-
|
|
15
|
-
// Cache for sorted listeners by priority
|
|
16
|
-
this._sortedCache = new Map();
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Register an event listener
|
|
21
|
-
* @param {string} event - Event name
|
|
22
|
-
* @param {Function} callback - Callback function
|
|
23
|
-
* @param {number} priority - Higher priority = called first (default: 0)
|
|
24
|
-
* @returns {EventBus} - For chaining
|
|
25
|
-
*/
|
|
26
|
-
on(event, callback, priority = 0) {
|
|
27
|
-
if (!this._listeners.has(event)) {
|
|
28
|
-
this._listeners.set(event, new Set());
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
this._listeners.get(event).add({ callback, priority });
|
|
32
|
-
|
|
33
|
-
// Invalidate cache for this event
|
|
34
|
-
this._sortedCache.delete(event);
|
|
35
|
-
|
|
36
|
-
return this;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Register a one-time event listener
|
|
41
|
-
* @param {string} event - Event name
|
|
42
|
-
* @param {Function} callback - Callback function
|
|
43
|
-
* @returns {EventBus} - For chaining
|
|
44
|
-
*/
|
|
45
|
-
once(event, callback) {
|
|
46
|
-
if (!this._onceListeners.has(event)) {
|
|
47
|
-
this._onceListeners.set(event, new Set());
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
this._onceListeners.get(event).add(callback);
|
|
51
|
-
return this;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Remove an event listener
|
|
56
|
-
* @param {string} event - Event name
|
|
57
|
-
* @param {Function} callback - Callback function to remove
|
|
58
|
-
* @returns {EventBus} - For chaining
|
|
59
|
-
*/
|
|
60
|
-
off(event, callback) {
|
|
61
|
-
const listeners = this._listeners.get(event);
|
|
62
|
-
if (listeners) {
|
|
63
|
-
// Remove every registration of this callback. on() stores a fresh
|
|
64
|
-
// wrapper object per call, so the same function can be in here
|
|
65
|
-
// more than once and stopping at the first match would leave a
|
|
66
|
-
// live listener behind.
|
|
67
|
-
for (const listener of [...listeners]) {
|
|
68
|
-
if (listener.callback === callback) {
|
|
69
|
-
listeners.delete(listener);
|
|
70
|
-
this._sortedCache.delete(event);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const onceListeners = this._onceListeners.get(event);
|
|
76
|
-
if (onceListeners) {
|
|
77
|
-
onceListeners.delete(callback);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
return this;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Emit an event to all registered listeners
|
|
85
|
-
* @param {string} event - Event name
|
|
86
|
-
* @param {*} data - Event data
|
|
87
|
-
*/
|
|
88
|
-
emit(event, data) {
|
|
89
|
-
// Get sorted listeners (cached for performance)
|
|
90
|
-
const listeners = this._getSortedListeners(event);
|
|
91
|
-
|
|
92
|
-
// Call regular listeners
|
|
93
|
-
for (let i = 0; i < listeners.length; i++) {
|
|
94
|
-
this._invoke(listeners[i].callback, data, event);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// Call and remove once listeners.
|
|
98
|
-
const onceListeners = this._onceListeners.get(event);
|
|
99
|
-
if (onceListeners && onceListeners.size > 0) {
|
|
100
|
-
// Snapshot and clear *before* dispatching. Iterating the live Set
|
|
101
|
-
// would pick up any listener a callback registers for this same
|
|
102
|
-
// event, firing it immediately and then clearing it unfired.
|
|
103
|
-
const batch = [...onceListeners];
|
|
104
|
-
onceListeners.clear();
|
|
105
|
-
|
|
106
|
-
for (const callback of batch) {
|
|
107
|
-
this._invoke(callback, data, event);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Call one listener without letting it abort the dispatch.
|
|
114
|
-
*
|
|
115
|
-
* A throwing listener used to take out every lower-priority listener for
|
|
116
|
-
* that event, which on 'tick' means the game silently stops updating.
|
|
117
|
-
*
|
|
118
|
-
* @private
|
|
119
|
-
*/
|
|
120
|
-
_invoke(callback, data, event) {
|
|
121
|
-
try {
|
|
122
|
-
callback(data);
|
|
123
|
-
} catch (error) {
|
|
124
|
-
console.error(`[DopamineJS] Listener for "${event}" threw:`, error);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Get sorted listeners for an event (with caching)
|
|
130
|
-
* @private
|
|
131
|
-
*/
|
|
132
|
-
_getSortedListeners(event) {
|
|
133
|
-
// Check cache first
|
|
134
|
-
if (this._sortedCache.has(event)) {
|
|
135
|
-
return this._sortedCache.get(event);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
const listeners = this._listeners.get(event);
|
|
139
|
-
if (!listeners || listeners.size === 0) {
|
|
140
|
-
return [];
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
// Sort by priority (higher first)
|
|
144
|
-
const sorted = Array.from(listeners).sort((a, b) => b.priority - a.priority);
|
|
145
|
-
|
|
146
|
-
// Cache the result
|
|
147
|
-
this._sortedCache.set(event, sorted);
|
|
148
|
-
|
|
149
|
-
return sorted;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Remove all listeners for an event, or all events if no event specified
|
|
154
|
-
* @param {string} [event] - Event name (optional)
|
|
155
|
-
*/
|
|
156
|
-
clear(event) {
|
|
157
|
-
if (event) {
|
|
158
|
-
this._listeners.delete(event);
|
|
159
|
-
this._onceListeners.delete(event);
|
|
160
|
-
this._sortedCache.delete(event);
|
|
161
|
-
} else {
|
|
162
|
-
this._listeners.clear();
|
|
163
|
-
this._onceListeners.clear();
|
|
164
|
-
this._sortedCache.clear();
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
/**
|
|
169
|
-
* Check if an event has listeners
|
|
170
|
-
* @param {string} event - Event name
|
|
171
|
-
* @returns {boolean}
|
|
172
|
-
*/
|
|
173
|
-
hasListeners(event) {
|
|
174
|
-
const listeners = this._listeners.get(event);
|
|
175
|
-
const onceListeners = this._onceListeners.get(event);
|
|
176
|
-
return Boolean(listeners?.size) || Boolean(onceListeners?.size);
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
* Standard event names for performance-critical events
|
|
182
|
-
* Using constants avoids string allocation
|
|
183
|
-
*/
|
|
184
|
-
EventBus.Events = {
|
|
185
|
-
// Core game loop events
|
|
186
|
-
TICK: 'tick',
|
|
187
|
-
FIXED_UPDATE: 'fixed_update',
|
|
188
|
-
RENDER: 'render',
|
|
189
|
-
|
|
190
|
-
// Physics events
|
|
191
|
-
COLLISION_ENTER: 'collision_enter',
|
|
192
|
-
COLLISION_EXIT: 'collision_exit',
|
|
193
|
-
|
|
194
|
-
// Dopamine events (from RewardSystem)
|
|
195
|
-
XP_GAINED: 'xp_gained',
|
|
196
|
-
LEVEL_UP: 'level_up',
|
|
197
|
-
ACHIEVEMENT_UNLOCKED: 'achievement_unlocked',
|
|
198
|
-
NEW_HIGH_SCORE: 'new_high_score',
|
|
199
|
-
STREAK_UPDATED: 'streak_updated',
|
|
200
|
-
|
|
201
|
-
// System lifecycle events
|
|
202
|
-
SYSTEM_REGISTERED: 'system_registered',
|
|
203
|
-
SYSTEM_UNREGISTERED: 'system_unregistered',
|
|
204
|
-
PLUGIN_LOADED: 'plugin_loaded',
|
|
205
|
-
PLUGIN_UNLOADED: 'plugin_unloaded',
|
|
206
|
-
};
|
package/src/core/Game.js
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import { DopamineKernel } from './DopamineKernel.js';
|
|
2
|
-
import { Scene } from './Scene.js';
|
|
3
|
-
import { Director } from '../systems/Director.js';
|
|
4
|
-
import { EventBus } from './EventBus.js';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* The main Game entry point.
|
|
8
|
-
* Now uses DopamineKernel for system management.
|
|
9
|
-
*/
|
|
10
|
-
export class Game {
|
|
11
|
-
constructor(config = {}) {
|
|
12
|
-
this.config = config;
|
|
13
|
-
|
|
14
|
-
// NEW: Use kernel for system management
|
|
15
|
-
this.kernel = new DopamineKernel(config);
|
|
16
|
-
|
|
17
|
-
// Initialize Director
|
|
18
|
-
this.director = new Director(this);
|
|
19
|
-
this.kernel.systems.register('director', this.director);
|
|
20
|
-
|
|
21
|
-
// Bind to kernel tick event for scene updates
|
|
22
|
-
this.kernel.events.on(EventBus.Events.TICK, this._update.bind(this), 10);
|
|
23
|
-
this.kernel.events.on(EventBus.Events.RENDER, this._render.bind(this), 10);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Backward compatibility accessors
|
|
28
|
-
*/
|
|
29
|
-
get renderer() {
|
|
30
|
-
return this.kernel.renderer;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
get ticker() {
|
|
34
|
-
return this.kernel.ticker;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
get physics() {
|
|
38
|
-
return this.kernel.physics;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
get input() {
|
|
42
|
-
return this.kernel.input;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Start the game loop.
|
|
47
|
-
*/
|
|
48
|
-
start() {
|
|
49
|
-
this.kernel.start();
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Stop the game loop.
|
|
54
|
-
*/
|
|
55
|
-
stop() {
|
|
56
|
-
this.kernel.stop();
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Switch to a new scene.
|
|
61
|
-
* @param {Scene} newScene
|
|
62
|
-
*/
|
|
63
|
-
setScene(newScene) {
|
|
64
|
-
// Inject kernel into scene
|
|
65
|
-
newScene.kernel = this.kernel;
|
|
66
|
-
this.director.run(newScene);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
_update({ dt }) {
|
|
70
|
-
// Update scene logic
|
|
71
|
-
if (this.director.currentScene) {
|
|
72
|
-
this.director.currentScene.update(dt);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
_render({ dt }) {
|
|
77
|
-
// Clear and render
|
|
78
|
-
this.renderer.clear();
|
|
79
|
-
if (this.director.currentScene) {
|
|
80
|
-
this.director.currentScene.render(this.renderer.ctx);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Destroy the game and cleanup
|
|
86
|
-
*/
|
|
87
|
-
destroy() {
|
|
88
|
-
this.kernel.destroy();
|
|
89
|
-
}
|
|
90
|
-
}
|