like2d 2.4.0 → 2.5.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 +27 -18
- package/dist/adapters/callback/index.d.ts +20 -41
- package/dist/adapters/callback/index.d.ts.map +1 -1
- package/dist/adapters/callback/index.js +29 -79
- package/dist/adapters/scene/index.d.ts +6 -31
- package/dist/adapters/scene/index.d.ts.map +1 -1
- package/dist/adapters/scene/index.js +22 -80
- package/dist/adapters/scene/scene.d.ts +14 -13
- package/dist/adapters/scene/scene.d.ts.map +1 -1
- package/dist/adapters/scene/startup-scene.d.ts +5 -6
- package/dist/adapters/scene/startup-scene.d.ts.map +1 -1
- package/dist/adapters/scene/startup-scene.js +12 -19
- package/dist/core/events.d.ts +21 -49
- package/dist/core/events.d.ts.map +1 -1
- package/dist/core/graphics.d.ts +38 -32
- package/dist/core/graphics.d.ts.map +1 -1
- package/dist/core/graphics.js +192 -275
- package/dist/core/like.d.ts +54 -0
- package/dist/core/like.d.ts.map +1 -0
- package/dist/core/like.js +1 -0
- package/dist/core/player-movement.d.ts +16 -0
- package/dist/core/player-movement.d.ts.map +1 -0
- package/dist/core/player-movement.js +20 -0
- package/dist/engine.d.ts +7 -27
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +68 -57
- package/dist/index.d.ts +8 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -3
- package/package.json +1 -1
package/dist/engine.js
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
import { Audio } from './core/audio';
|
|
2
|
+
import { Input } from './core/input';
|
|
3
|
+
import { Timer } from './core/timer';
|
|
4
|
+
import { Keyboard } from './core/keyboard';
|
|
5
|
+
import { Mouse } from './core/mouse';
|
|
6
|
+
import { Gamepad } from './core/gamepad';
|
|
7
|
+
import { newState, bindGraphics } from './core/graphics';
|
|
1
8
|
import { CanvasManager } from './core/canvas-manager';
|
|
2
9
|
export class Engine {
|
|
3
10
|
constructor(container) {
|
|
@@ -13,12 +20,6 @@ export class Engine {
|
|
|
13
20
|
writable: true,
|
|
14
21
|
value: void 0
|
|
15
22
|
});
|
|
16
|
-
Object.defineProperty(this, "deps", {
|
|
17
|
-
enumerable: true,
|
|
18
|
-
configurable: true,
|
|
19
|
-
writable: true,
|
|
20
|
-
value: null
|
|
21
|
-
});
|
|
22
23
|
Object.defineProperty(this, "isRunning", {
|
|
23
24
|
enumerable: true,
|
|
24
25
|
configurable: true,
|
|
@@ -49,6 +50,13 @@ export class Engine {
|
|
|
49
50
|
writable: true,
|
|
50
51
|
value: null
|
|
51
52
|
});
|
|
53
|
+
// Public Like object with all systems - initialized in constructor
|
|
54
|
+
Object.defineProperty(this, "like", {
|
|
55
|
+
enumerable: true,
|
|
56
|
+
configurable: true,
|
|
57
|
+
writable: true,
|
|
58
|
+
value: void 0
|
|
59
|
+
});
|
|
52
60
|
this.canvas = document.createElement('canvas');
|
|
53
61
|
this.canvas.style.border = '1px solid #ccc';
|
|
54
62
|
this.canvas.style.display = 'block';
|
|
@@ -59,7 +67,42 @@ export class Engine {
|
|
|
59
67
|
this.container = container;
|
|
60
68
|
this.container.appendChild(this.canvas);
|
|
61
69
|
this.canvasManager = new CanvasManager(this.canvas, this.container, this.ctx, { pixelResolution: null, fullscreen: false });
|
|
62
|
-
//
|
|
70
|
+
// Create graphics state and bind it
|
|
71
|
+
const gfxState = newState(this.ctx);
|
|
72
|
+
const gfx = bindGraphics(gfxState);
|
|
73
|
+
// Create all subsystems
|
|
74
|
+
const audio = new Audio();
|
|
75
|
+
const timer = new Timer();
|
|
76
|
+
const keyboard = new Keyboard();
|
|
77
|
+
const mouse = new Mouse((cssX, cssY) => this.canvasManager.transformMousePosition(cssX, cssY));
|
|
78
|
+
const gamepad = new Gamepad();
|
|
79
|
+
const input = new Input({ keyboard, mouse, gamepad });
|
|
80
|
+
// Create the Like object with all systems
|
|
81
|
+
this.like = {
|
|
82
|
+
audio,
|
|
83
|
+
timer,
|
|
84
|
+
input,
|
|
85
|
+
keyboard,
|
|
86
|
+
mouse,
|
|
87
|
+
gamepad,
|
|
88
|
+
gfx,
|
|
89
|
+
setMode: (m) => this.setMode(m),
|
|
90
|
+
getMode: () => this.canvasManager.getMode(),
|
|
91
|
+
getCanvasSize: () => [this.canvas.width, this.canvas.height],
|
|
92
|
+
};
|
|
93
|
+
// Set up input event handlers
|
|
94
|
+
keyboard.onKeyEvent = (scancode, keycode, type) => {
|
|
95
|
+
this.dispatchEvent(type === 'keydown' ? 'keypressed' : 'keyreleased', [scancode, keycode]);
|
|
96
|
+
};
|
|
97
|
+
mouse.onMouseEvent = (clientX, clientY, button, type) => {
|
|
98
|
+
const [x, y] = this.canvasManager.transformMousePosition(clientX, clientY);
|
|
99
|
+
const b = (button ?? 0) + 1;
|
|
100
|
+
this.dispatchEvent(type === 'mousedown' ? 'mousepressed' : 'mousereleased', [x, y, b]);
|
|
101
|
+
};
|
|
102
|
+
gamepad.onButtonEvent = (gpIndex, buttonIndex, buttonName, pressed) => {
|
|
103
|
+
this.dispatchEvent(pressed ? 'gamepadpressed' : 'gamepadreleased', [gpIndex, buttonIndex, buttonName]);
|
|
104
|
+
};
|
|
105
|
+
// Internal listener to forward resize events
|
|
63
106
|
this.canvasManager.onResize = (size, pixelSize, fullscreen) => {
|
|
64
107
|
this.dispatchEvent('resize', [size, pixelSize, fullscreen]);
|
|
65
108
|
};
|
|
@@ -87,60 +130,31 @@ export class Engine {
|
|
|
87
130
|
}
|
|
88
131
|
this.canvasManager.setMode(mode);
|
|
89
132
|
}
|
|
90
|
-
getMode() {
|
|
91
|
-
return this.canvasManager.getMode();
|
|
92
|
-
}
|
|
93
|
-
setDeps(deps) {
|
|
94
|
-
this.deps = deps;
|
|
95
|
-
}
|
|
96
|
-
dispose() {
|
|
97
|
-
this.isRunning = false;
|
|
98
|
-
this.deps?.keyboard.dispose();
|
|
99
|
-
this.deps?.mouse.dispose();
|
|
100
|
-
this.deps?.gamepad.dispose();
|
|
101
|
-
this.canvasManager.dispose();
|
|
102
|
-
if (this.canvas.parentNode === this.container) {
|
|
103
|
-
this.container.removeChild(this.canvas);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
133
|
dispatchEvent(type, args) {
|
|
107
134
|
if (this.onEvent) {
|
|
108
135
|
this.onEvent({ type, args, timestamp: performance.now() });
|
|
109
136
|
}
|
|
110
137
|
}
|
|
111
|
-
start(onEvent) {
|
|
112
|
-
if (!this.deps)
|
|
113
|
-
throw new Error('Engine dependencies not set. Call setDeps() before start().');
|
|
138
|
+
async start(onEvent) {
|
|
114
139
|
this.onEvent = onEvent;
|
|
115
140
|
this.isRunning = true;
|
|
116
141
|
this.lastTime = performance.now();
|
|
117
|
-
//
|
|
118
|
-
this.
|
|
119
|
-
this.dispatchEvent(type === 'keydown' ? 'keypressed' : 'keyreleased', [scancode, keycode]);
|
|
120
|
-
};
|
|
121
|
-
this.deps.mouse.onMouseEvent = (clientX, clientY, button, type) => {
|
|
122
|
-
const [x, y] = this.transformMousePosition(clientX, clientY);
|
|
123
|
-
const b = (button ?? 0) + 1;
|
|
124
|
-
this.dispatchEvent(type === 'mousedown' ? 'mousepressed' : 'mousereleased', [x, y, b]);
|
|
125
|
-
};
|
|
126
|
-
this.deps.gamepad.onButtonEvent = (gpIndex, buttonIndex, buttonName, pressed) => {
|
|
127
|
-
this.dispatchEvent(pressed ? 'gamepadpressed' : 'gamepadreleased', [gpIndex, buttonIndex, buttonName]);
|
|
128
|
-
};
|
|
142
|
+
// Initialize gamepad
|
|
143
|
+
await this.like.gamepad.init();
|
|
129
144
|
const loop = () => {
|
|
130
145
|
if (!this.isRunning)
|
|
131
146
|
return;
|
|
132
147
|
const currentTime = performance.now();
|
|
133
148
|
const dt = (currentTime - this.lastTime) / 1000;
|
|
134
149
|
this.lastTime = currentTime;
|
|
135
|
-
if (!this.
|
|
136
|
-
this.
|
|
137
|
-
const inputEvents = this.
|
|
150
|
+
if (!this.like.timer.isSleeping()) {
|
|
151
|
+
this.like.timer.update(dt);
|
|
152
|
+
const inputEvents = this.like.input.update();
|
|
138
153
|
inputEvents.pressed.forEach(action => this.dispatchEvent('actionpressed', [action]));
|
|
139
154
|
inputEvents.released.forEach(action => this.dispatchEvent('actionreleased', [action]));
|
|
140
155
|
this.dispatchEvent('update', [dt]);
|
|
141
156
|
}
|
|
142
|
-
this.
|
|
143
|
-
this.dispatchEvent('draw', [this.canvas]);
|
|
157
|
+
this.dispatchEvent('draw', []);
|
|
144
158
|
this.canvasManager.present();
|
|
145
159
|
requestAnimationFrame(loop);
|
|
146
160
|
};
|
|
@@ -150,20 +164,17 @@ export class Engine {
|
|
|
150
164
|
stop() {
|
|
151
165
|
this.isRunning = false;
|
|
152
166
|
}
|
|
153
|
-
|
|
154
|
-
this.
|
|
155
|
-
this.
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
}
|
|
163
|
-
getContext() {
|
|
164
|
-
return this.ctx;
|
|
167
|
+
dispose() {
|
|
168
|
+
this.isRunning = false;
|
|
169
|
+
this.like.keyboard.dispose();
|
|
170
|
+
this.like.mouse.dispose();
|
|
171
|
+
this.like.gamepad.dispose();
|
|
172
|
+
this.canvasManager.dispose();
|
|
173
|
+
if (this.canvas.parentNode === this.container) {
|
|
174
|
+
this.container.removeChild(this.canvas);
|
|
175
|
+
}
|
|
165
176
|
}
|
|
166
|
-
|
|
167
|
-
return this.
|
|
177
|
+
getCanvasSize() {
|
|
178
|
+
return [this.canvas.width, this.canvas.height];
|
|
168
179
|
}
|
|
169
180
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Main API interface containing all core systems.
|
|
3
|
+
* Passed as first argument to all callbacks.
|
|
4
|
+
*/
|
|
5
|
+
export type { Like } from './core/like';
|
|
1
6
|
/**
|
|
2
7
|
* 2D vector as a tuple: [x, y]
|
|
3
8
|
*/
|
|
@@ -14,7 +19,8 @@ export { Rect } from './core/rect';
|
|
|
14
19
|
/**
|
|
15
20
|
* Common event structure for all engine events.
|
|
16
21
|
*/
|
|
17
|
-
export type { Like2DEvent, EventType } from './core/events';
|
|
22
|
+
export type { Like2DEvent, EventType, EventMap } from './core/events';
|
|
23
|
+
export type { CanvasMode, PartialCanvasMode } from './core/canvas-config';
|
|
18
24
|
/**
|
|
19
25
|
* Graphics types for drawing operations.
|
|
20
26
|
*/
|
|
@@ -22,7 +28,7 @@ export type { Color, Quad, ShapeProps, DrawProps, PrintProps } from './core/grap
|
|
|
22
28
|
/**
|
|
23
29
|
* Handle to an image asset that may be loading in the background.
|
|
24
30
|
*/
|
|
25
|
-
export { ImageHandle } from './core/graphics';
|
|
31
|
+
export { ImageHandle, newImage } from './core/graphics';
|
|
26
32
|
/**
|
|
27
33
|
* Audio source types for sound playback.
|
|
28
34
|
*/
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAKA;;GAEG;AACH,YAAY,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAE9C;;GAEG;AACH,OAAO,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAEtC;;;GAGG;AACH,OAAO,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAEnC;;GAEG;AACH,YAAY,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAKA;;;GAGG;AACH,YAAY,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAExC;;GAEG;AACH,YAAY,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAE9C;;GAEG;AACH,OAAO,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAEtC;;;GAGG;AACH,OAAO,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAEnC;;GAEG;AACH,YAAY,EAAE,WAAW,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACtE,YAAY,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAE1E;;GAEG;AACH,YAAY,EAAE,KAAK,EAAE,IAAI,EAAE,UAAU,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAEtF;;GAEG;AACH,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAExD;;GAEG;AACH,YAAY,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAE1D;;GAEG;AACH,YAAY,EAAE,KAAK,EAAE,MAAM,wBAAwB,CAAC;AAEpD;;GAEG;AACH,OAAO,EAAE,SAAS,EAAE,EAAE,EAAE,MAAM,gBAAgB,CAAC;AAC/C,YAAY,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Like2D - Multi-adapter game framework
|
|
2
2
|
// Use specific adapters for different patterns:
|
|
3
|
-
// - import {
|
|
3
|
+
// - import { createLike, graphics } from 'like2d/callback' (Love2D-style callbacks)
|
|
4
4
|
// - import { SceneRunner, Scene } from 'like2d/scene' (Class-based scenes)
|
|
5
5
|
/**
|
|
6
6
|
* 2D vector math and utility functions
|
|
@@ -14,11 +14,11 @@ export { Rect } from './core/rect';
|
|
|
14
14
|
/**
|
|
15
15
|
* Handle to an image asset that may be loading in the background.
|
|
16
16
|
*/
|
|
17
|
-
export { ImageHandle } from './core/graphics';
|
|
17
|
+
export { ImageHandle, newImage } from './core/graphics';
|
|
18
18
|
/**
|
|
19
19
|
* Gamepad utility functions and types.
|
|
20
20
|
*/
|
|
21
21
|
export { getGPName, GP } from './core/gamepad';
|
|
22
22
|
// Note: For actual usage, import from specific adapters:
|
|
23
|
-
// import {
|
|
23
|
+
// import { createLike, graphics } from 'like2d/callback';
|
|
24
24
|
// import { SceneRunner, Scene } from 'like2d/scene';
|