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/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
- // Internal listener to forward to onEvent
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
- // Listeners for raw input
118
- this.deps.keyboard.onKeyEvent = (scancode, keycode, type) => {
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.deps.timer.isSleeping()) {
136
- this.deps.timer.update(dt);
137
- const inputEvents = this.deps.input.update();
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.deps.graphics.clear();
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
- setSize(width, height) {
154
- this.canvas.width = width;
155
- this.canvas.height = height;
156
- }
157
- getTime() {
158
- return this.deps?.timer.getTime() ?? 0;
159
- }
160
- getCanvas() {
161
- return this.canvas;
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
- transformMousePosition(cssX, cssY) {
167
- return this.canvasManager.transformMousePosition(cssX, cssY);
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
  */
@@ -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;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E1D;;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 { like, graphics } from 'like2d/callback' (Love2D-style callbacks)
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 { like, graphics } from 'like2d/callback';
23
+ // import { createLike, graphics } from 'like2d/callback';
24
24
  // import { SceneRunner, Scene } from 'like2d/scene';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "like2d",
3
- "version": "2.4.0",
3
+ "version": "2.5.0",
4
4
  "description": "A web-native game framework inspired by Love2D",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",