core2d 2.10.4 → 2.11.1

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 CHANGED
@@ -1,54 +1,128 @@
1
1
  ![core2d logo](./core2d.jpg)
2
2
 
3
3
  # About
4
+
4
5
  Core2D is the powerhouse used by [Maragato マラガト](https://maragato.itch.io) apps, among others. It is the evolution of Videogame, which in turn was the evolution of Quick. In its current form, it adopts [JavaScript modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules), leveraging the full power of [Object-oriented programming](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object-oriented_programming).
5
6
 
6
7
  ## Concept
8
+
7
9
  Apps created with Core2D are made of one or more [scenes](src/Scene.mjs), which may contain one or more [sprites](src/Sprite.mjs). These objects have properties that can be customized to shape their behavior. It's that simple.
8
10
 
9
11
  # Get Started
12
+
10
13
  [Download](https://github.com/dgchrt/core2d-skel/archive/refs/heads/main.zip) or clone/fork the [skeleton project](https://github.com/dgchrt/core2d-skel/) to start building your app. Alternatively, the library can be also installed to your existing/new project:
14
+
11
15
  ```shell
12
16
  npm install core2d
13
17
  ```
14
18
 
15
19
  ## Learn
20
+
16
21
  The best way to learn is by doing, and you can see what Core2D is capable of through existing open-sourced apps. Check the [Hall of Fame](#hall-of-fame) below for some source code.
17
22
 
18
23
  ## Support
24
+
19
25
  Please consider joining the [Discussions](https://github.com/dgchrt/core2d/discussions) for collaboration and support.
20
26
 
21
27
  # Features
22
28
 
29
+ ## Scene Management
30
+
31
+ Core2D facilitates the organization of your app into one or more [scenes](src/Scene.mjs), which can be transitioned between. Each scene can contain multiple [sprites](src/Sprite.mjs), which are the basic building blocks for game objects.
32
+
33
+ ## Sound Management
34
+
35
+ The engine provides a simple yet powerful sound system that allows for playing sound effects and background music. It also supports fading out the current theme, which is useful for smooth transitions between different music tracks.
36
+
23
37
  ## Collision Detection
38
+
24
39
  Translated to callbacks, to keep update logic clean.
25
40
 
26
41
  ## Assets Caching
42
+
27
43
  Assets and their transformations are reused automatically to keep a solid performance.
28
44
 
45
+ ## Transformations
46
+
47
+ Core2D provides a set of functions to manipulate images, including rotation, flipping, and colorization. These transformations are cached, so they are only applied once per image.
48
+
49
+ ## Tilemap
50
+
51
+ The `Scene` class provides a `build` method that allows for creating tile-based scenes from a map. This is useful for creating levels in platformers, RPGs, and other genres that use tile-based graphics.
52
+
29
53
  ## User Input
54
+
30
55
  Human interaction is unified via abstractions, so that apps will just work, regardless of the devices in use.
31
56
 
32
57
  ### Controllers
58
+
33
59
  ![controllers](controller.png)
34
60
 
35
61
  Gamepads or keyboard. When using a keyboard, sensible defaults (minding accessibility) are used, as seen in [KeyMap.mjs](https://github.com/dgchrt/core2d/blob/main/src/KeyMap.mjs).
36
62
 
37
63
  ### Pointers
64
+
38
65
  ![pointer](pointer.png)
39
66
 
40
67
  Mice or touch screen.
41
68
 
42
69
  ## Virtual Resolution
70
+
43
71
  Internal geometry frees the app logic from displays, i.e. your app can have an internal logic resolution of 800x600, while running on any display size.
44
72
 
73
+ # Plugins
74
+
75
+ Core2D comes with a set of optional plugins that can be used to extend its functionality. These plugins are located in the `src/plugin` directory and can be used by importing them into your project.
76
+
77
+ - **BaseTile:** A basic tile sprite that can be used with the `Scene.build` method.
78
+ - **ClickableSprite:** A sprite that can be clicked or tapped.
79
+ - **ControllableSprite:** A sprite that can be controlled by a gamepad or keyboard.
80
+ - **CursorSprite:** A sprite that follows the pointer.
81
+ - **Fog:** A simple fog effect.
82
+ - **FontSprite:** A sprite that displays text using a custom font.
83
+ - **JumperSprite:** A sprite that can jump.
84
+ - **RandomRectTransition:** A transition that fills the screen with random rectangles.
85
+ - **Starfield:** A simple starfield effect.
86
+
45
87
  # Contributing
88
+
46
89
  The core of the library (under `src/`) should remain agnostic and lean. Updates to the core library are usually related to technology developments in the platform (web API advances), while staying true to the basic concepts of the library, which are common to all apps.
47
90
 
48
91
  Opinionated functionality should be implemented in the form of a plugin (under `src/plugin/`). Plugins can add features that are domain driven, such as elements that can be reused by multiple apps, but not necessarily by every app.
49
92
 
93
+ ## Development Setup
94
+
95
+ To contribute to Core2D, you'll need to set up your development environment.
96
+
97
+ 1. **Version Management:**
98
+ This project uses [asdf](https://asdf-vm.com/) to manage the Node.js version. To install the correct version, run:
99
+ ```shell
100
+ asdf install
101
+ ```
102
+ 2. **Install Dependencies:**
103
+ ```shell
104
+ npm install
105
+ ```
106
+ 3. **Recommended VS Code Extensions:**
107
+ This project includes a list of recommended VS Code extensions in `.vscode/extensions.json`. When you open the project in VS Code, you should be prompted to install them. These extensions will help you with code formatting and linting.
108
+ 4. **Formatting and Linting:**
109
+ This project uses [Prettier](https://prettier.io/) for code formatting and [ESLint](https://eslint.org/) for linting. The configuration for these tools is in the `.prettierrc.json` and `eslint.config.js` files, respectively.
110
+ To format the code, run:
111
+ ```shell
112
+ npm run format
113
+ ```
114
+ To lint the code, run:
115
+ ```shell
116
+ npm run lint
117
+ ```
118
+ 5. **Git Hooks:**
119
+ This project uses [husky](https://typicode.github.io/husky/) to manage Git hooks. A `pre-commit` hook is configured to run `npm test` before each commit. This ensures that all tests pass and the code is linted before it's committed. After running `npm install`, the hooks will be automatically configured.
120
+
121
+
50
122
  # Hall of Fame
123
+
51
124
  Apps created with Core2D:
125
+
52
126
  - [Asteroids Remake](https://chamun.github.io/asteroids-remake/) ([source](https://github.com/chamun/asteroids-remake))
53
127
  - [Cityscape](https://puter.com/app/cityscape) - Single-player arcade survival game
54
128
  - [Cucurbita's Halloween](https://www.kongregate.com/games/bbastudios/cucurbitas-halloween)
package/package.json CHANGED
@@ -1,15 +1,17 @@
1
1
  {
2
2
  "name": "core2d",
3
3
  "type": "module",
4
- "version": "2.10.4",
4
+ "version": "2.11.1",
5
5
  "description": "Multiplatform 2D interaction engine",
6
6
  "files": [
7
7
  "src"
8
8
  ],
9
9
  "scripts": {
10
+ "format": "prettier --write .",
10
11
  "lint": "eslint",
11
12
  "prepublishOnly": "git checkout main && git pull --rebase && npm test && git push && git push --tags",
12
- "test": "npm run lint && for test in ./test/*.mjs; do node $test; done"
13
+ "test": "npm run lint && for test in ./test/*.mjs; do node $test; done",
14
+ "prepare": "husky"
13
15
  },
14
16
  "repository": {
15
17
  "type": "git",
@@ -45,6 +47,9 @@
45
47
  "devDependencies": {
46
48
  "@eslint/js": "^9.30.1",
47
49
  "eslint": "^9.30.1",
48
- "globals": "^16.3.0"
50
+ "eslint-config-prettier": "^10.1.5",
51
+ "globals": "^16.3.0",
52
+ "husky": "^9.1.7",
53
+ "prettier": "^3.6.2"
49
54
  }
50
55
  }
package/src/ACL.mjs CHANGED
@@ -3,7 +3,7 @@
3
3
  /**
4
4
  * Sets up the environment based on its runtime (browser or not).
5
5
  */
6
- if (typeof(global) != "undefined") {
6
+ if (typeof global != "undefined") {
7
7
  global.addEventListener = () => {};
8
8
 
9
9
  global.document = {
@@ -43,7 +43,7 @@ if (typeof(global) != "undefined") {
43
43
  },
44
44
  getElementsByTagName: () => {
45
45
  return [];
46
- }
46
+ },
47
47
  };
48
48
 
49
49
  global.localStorage = {};
@@ -59,7 +59,7 @@ if (typeof(global) != "undefined") {
59
59
  focus: () => {},
60
60
  innerHeight: 600,
61
61
  innerWidth: 800,
62
- requestAnimationFrame: () => true
62
+ requestAnimationFrame: () => true,
63
63
  };
64
64
  }
65
65
 
package/src/Animation.mjs CHANGED
@@ -2,29 +2,58 @@
2
2
 
3
3
  import { Frame } from "./Frame.mjs";
4
4
 
5
+ /**
6
+ * Represents an animation, which is a sequence of frames.
7
+ */
5
8
  export class Animation {
9
+ /**
10
+ * Creates a new Animation.
11
+ * @param {Frame[]} frames The frames of the animation.
12
+ */
6
13
  constructor(frames) {
7
14
  this._frames = frames;
8
15
  this._index = 0;
9
16
  this._tick = 0;
10
17
  }
11
18
 
19
+ /**
20
+ * Creates a new Animation from a list of images.
21
+ * @param {HTMLImageElement[]|HTMLCanvasElement[]} images The images of the animation.
22
+ * @param {number} duration The duration of each frame in ticks.
23
+ * @returns {Animation} The new animation.
24
+ */
12
25
  static fromImages(images, duration) {
13
- return new this(images.map(image => new Frame(image, duration)));
26
+ return new this(images.map((image) => new Frame(image, duration)));
14
27
  }
15
28
 
29
+ /**
30
+ * The image of the current frame.
31
+ * @type {HTMLImageElement|HTMLCanvasElement}
32
+ */
16
33
  get image() {
17
34
  return this._frames[this._index].image;
18
35
  }
19
36
 
37
+ /**
38
+ * The width of the current frame.
39
+ * @type {number}
40
+ */
20
41
  get width() {
21
42
  return this._frames[this._index].width;
22
43
  }
23
44
 
45
+ /**
46
+ * The height of the current frame.
47
+ * @type {number}
48
+ */
24
49
  get height() {
25
50
  return this._frames[this._index].height;
26
51
  }
27
52
 
53
+ /**
54
+ * Sets the index of the current frame.
55
+ * @param {number} index The index of the frame.
56
+ */
28
57
  setFrameIndex(index) {
29
58
  if (index < this._frames.length) {
30
59
  this._index = index;
@@ -32,10 +61,18 @@ export class Animation {
32
61
  }
33
62
  }
34
63
 
64
+ /**
65
+ * The index of the current frame.
66
+ * @type {number}
67
+ */
35
68
  set frameIndex(index) {
36
69
  this.setFrameIndex(index);
37
70
  }
38
71
 
72
+ /**
73
+ * Synchronizes the animation.
74
+ * @returns {boolean} Whether the animation has looped.
75
+ */
39
76
  sync() {
40
77
  const DURATION = this._frames[this._index].duration;
41
78
  let hasLooped = false;
package/src/Axis.mjs CHANGED
@@ -2,9 +2,4 @@
2
2
 
3
3
  import { Static } from "./Static.mjs";
4
4
 
5
- export const Axis = Static.makeEnum([
6
- "LEFT_X",
7
- "LEFT_Y",
8
- "RIGHT_X",
9
- "RIGHT_Y",
10
- ]);
5
+ export const Axis = Static.makeEnum(["LEFT_X", "LEFT_Y", "RIGHT_X", "RIGHT_Y"]);
@@ -39,5 +39,5 @@ export const ButtonLayout = {
39
39
  "DOWN",
40
40
  "LEFT",
41
41
  "RIGHT",
42
- ])
42
+ ]),
43
43
  };
@@ -1,5 +1,5 @@
1
1
  "use static";
2
2
 
3
3
  export const ButtonLayoutMap = {
4
- "8bitdo s": "reversed"
4
+ "8bitdo s": "reversed",
5
5
  };
@@ -2,8 +2,18 @@
2
2
 
3
3
  import { Command } from "./Command.mjs";
4
4
 
5
+ /**
6
+ * Represents a controller, which can be a gamepad or a keyboard.
7
+ */
5
8
  export class Controller {
9
+ /**
10
+ * Creates a new Controller.
11
+ */
6
12
  constructor() {
13
+ /**
14
+ * The tolerance for the command sequence.
15
+ * @type {number}
16
+ */
7
17
  this.tolerance = 0;
8
18
  this._active = {};
9
19
  this._device = null;
@@ -12,9 +22,17 @@ export class Controller {
12
22
  this._tick = 0;
13
23
  }
14
24
 
25
+ /**
26
+ * Checks if a sequence of commands was performed.
27
+ * @param {Command[]} commands The sequence of commands.
28
+ * @returns {boolean} Whether the sequence of commands was performed.
29
+ */
15
30
  didPerform(commands) {
16
31
  for (let i = 1; i <= commands.length; ++i) {
17
- if (this._sequence[this._sequence.length - i] != commands[commands.length - i]) {
32
+ if (
33
+ this._sequence[this._sequence.length - i] !=
34
+ commands[commands.length - i]
35
+ ) {
18
36
  return false;
19
37
  }
20
38
  }
@@ -23,18 +41,35 @@ export class Controller {
23
41
  return true;
24
42
  }
25
43
 
44
+ /**
45
+ * Checks if a command is being held down.
46
+ * @param {Command} command The command to check.
47
+ * @returns {boolean} Whether the command is being held down.
48
+ */
26
49
  keyDown(command) {
27
50
  return this._active[command];
28
51
  }
29
52
 
53
+ /**
54
+ * Checks if a command was just pushed.
55
+ * @param {Command} command The command to check.
56
+ * @returns {boolean} Whether the command was just pushed.
57
+ */
30
58
  keyPush(command) {
31
59
  return this._active[command] && !this._hold[command];
32
60
  }
33
61
 
62
+ /**
63
+ * Sets the device of the controller.
64
+ * @param {object} device The device.
65
+ */
34
66
  setDevice(device) {
35
67
  this._device = device;
36
68
  }
37
69
 
70
+ /**
71
+ * Updates the controller.
72
+ */
38
73
  update() {
39
74
  if (!this._device) {
40
75
  return;
package/src/Core2D.mjs CHANGED
@@ -8,128 +8,276 @@ import { Rect } from "./Rect.mjs";
8
8
  import { Scene } from "./Scene.mjs";
9
9
  import { Sprite } from "./Sprite.mjs";
10
10
 
11
+ /**
12
+ * The main class of the Core2D library.
13
+ */
11
14
  export class Core2D {
15
+ /**
16
+ * Initializes the engine.
17
+ * @param {Scene} scene The initial scene.
18
+ */
12
19
  static init(scene) {
13
20
  Engine.init(scene);
14
21
  }
15
22
 
23
+ /**
24
+ * Whether the current frame is an even frame.
25
+ * @type {boolean}
26
+ */
16
27
  static get everyOther() {
17
28
  return Engine.everyOther;
18
29
  }
19
30
 
31
+ /**
32
+ * Adds a controller device.
33
+ * @param {object} device The controller device.
34
+ */
20
35
  static addControllerDevice(device) {
21
36
  Engine.addControllerDevice(device);
22
37
  }
23
38
 
39
+ /**
40
+ * Clears the canvas.
41
+ */
24
42
  static clear() {
25
43
  Engine.clear();
26
44
  }
27
45
 
46
+ /**
47
+ * Colorizes an image.
48
+ * @param {HTMLImageElement|HTMLCanvasElement} image The image to colorize.
49
+ * @param {string} fillStyle The color to use.
50
+ * @returns {HTMLCanvasElement} The colorized image.
51
+ */
28
52
  static colorize(image, fillStyle) {
29
53
  return Engine.colorize(image, fillStyle);
30
54
  }
31
55
 
56
+ /**
57
+ * Fades out the current theme.
58
+ */
32
59
  static fadeOut() {
33
60
  Engine.fadeOut();
34
61
  }
35
62
 
63
+ /**
64
+ * Flips an image vertically.
65
+ * @param {string} id The ID of the image to flip.
66
+ * @returns {HTMLCanvasElement} The flipped image.
67
+ */
36
68
  static flip(id) {
37
69
  return Engine.flip(id);
38
70
  }
39
71
 
72
+ /**
73
+ * Gets a controller.
74
+ * @param {number} id The ID of the controller.
75
+ * @returns {Controller} The controller.
76
+ */
40
77
  static getController(id) {
41
78
  return Engine.getController(id);
42
79
  }
43
80
 
81
+ /**
82
+ * Gets a pointer.
83
+ * @param {number} id The ID of the pointer.
84
+ * @returns {Pointer} The pointer.
85
+ */
44
86
  static getPointer(id) {
45
87
  return Engine.getPointer(id);
46
88
  }
47
89
 
90
+ /**
91
+ * Gets an image.
92
+ * @param {string} id The ID of the image.
93
+ * @param {boolean} isMirror Whether to mirror the image.
94
+ * @param {boolean} isFlip Whether to flip the image.
95
+ * @returns {HTMLImageElement|HTMLCanvasElement} The image.
96
+ */
48
97
  static image(id, isMirror, isFlip) {
49
98
  return Engine.image(id, isMirror, isFlip);
50
99
  }
51
100
 
101
+ /**
102
+ * Loads data from local storage.
103
+ * @param {string} namespace The namespace to use.
104
+ * @returns {object} The loaded data.
105
+ */
52
106
  static load(namespace) {
53
107
  return Engine.load(namespace);
54
108
  }
55
109
 
110
+ /**
111
+ * Mirrors an image horizontally.
112
+ * @param {string} id The ID of the image to mirror.
113
+ * @returns {HTMLCanvasElement} The mirrored image.
114
+ */
56
115
  static mirror(id) {
57
116
  return Engine.mirror(id);
58
117
  }
59
118
 
119
+ /**
120
+ * Mutes or unmutes the sound.
121
+ */
60
122
  static mute() {
61
123
  Engine.mute();
62
124
  }
63
125
 
126
+ /**
127
+ * Paints a renderable object.
128
+ * @param {object} renderable The renderable object.
129
+ * @param {number} index The layer index.
130
+ */
64
131
  static paint(renderable, index) {
65
132
  Engine.paint(renderable, index);
66
133
  }
67
134
 
135
+ /**
136
+ * Plays a sound.
137
+ * @param {string} id The ID of the sound to play.
138
+ * @param {number} volume The volume to play the sound at.
139
+ */
68
140
  static play(id, volume) {
69
141
  Engine.play(id, volume);
70
142
  }
71
143
 
144
+ /**
145
+ * Plays a theme.
146
+ * @param {string} name The name of the theme to play.
147
+ */
72
148
  static playTheme(name) {
73
149
  Engine.playTheme(name);
74
150
  }
75
151
 
152
+ /**
153
+ * Generates a random number.
154
+ * @param {number} max The maximum value.
155
+ * @returns {number} The random number.
156
+ */
76
157
  static random(max) {
77
158
  return Engine.random(max);
78
159
  }
79
160
 
161
+ /**
162
+ * Rotates an image.
163
+ * @param {HTMLImageElement|HTMLCanvasElement} image The image to rotate.
164
+ * @param {number} degrees The number of degrees to rotate the image.
165
+ * @returns {HTMLCanvasElement} The rotated image.
166
+ */
80
167
  static rotate(image, degrees) {
81
168
  return Engine.rotate(image, degrees);
82
169
  }
83
170
 
171
+ /**
172
+ * Saves data to local storage.
173
+ * @param {object} data The data to save.
174
+ * @param {string} namespace The namespace to use.
175
+ */
84
176
  static save(data, namespace) {
85
177
  Engine.save(data, namespace);
86
178
  }
87
179
 
180
+ /**
181
+ * Sets whether to automatically scale the canvas.
182
+ * @param {boolean} autoScale Whether to automatically scale the canvas.
183
+ */
88
184
  static setAutoScale(autoScale) {
89
185
  Engine.setAutoScale(autoScale);
90
186
  }
91
187
 
188
+ /**
189
+ * Sets the frame time.
190
+ * @param {number} frameTime The frame time in milliseconds.
191
+ */
92
192
  static setFrameTime(frameTime) {
93
193
  Engine.setFrameTime(frameTime);
94
194
  }
95
195
 
196
+ /**
197
+ * Sets whether to use full screen.
198
+ * @param {boolean} fullScreen Whether to use full screen.
199
+ */
96
200
  static setFullScreen(fullScreen) {
97
201
  Engine.setFullScreen(fullScreen);
98
202
  }
99
203
 
204
+ /**
205
+ * Sets whether to keep the aspect ratio when scaling the canvas.
206
+ * @param {boolean} keepAspect Whether to keep the aspect ratio.
207
+ */
100
208
  static setKeepAspect(keepAspect) {
101
209
  Engine.setKeepAspect(keepAspect);
102
210
  }
103
211
 
212
+ /**
213
+ * Sets the name of the game.
214
+ * @param {string} name The name of the game.
215
+ */
104
216
  static setName(name) {
105
217
  Engine.setName(name);
106
218
  }
107
219
 
220
+ /**
221
+ * Stops the current theme.
222
+ */
108
223
  static stopTheme() {
109
224
  Engine.stopTheme();
110
225
  }
111
226
 
112
- // factories
227
+ /**
228
+ * Creates a new Animation.
229
+ * @param {Frame[]} frames The frames of the animation.
230
+ * @returns {Animation} The new animation.
231
+ */
113
232
  static animation(frames) {
114
233
  return new Animation(frames);
115
234
  }
116
235
 
236
+ /**
237
+ * Creates a new Frame.
238
+ * @param {HTMLImageElement|HTMLCanvasElement} image The image of the frame.
239
+ * @param {number} duration The duration of the frame in ticks.
240
+ * @returns {Frame} The new frame.
241
+ */
117
242
  static frame(image, duration) {
118
243
  return new Frame(image, duration);
119
244
  }
120
245
 
246
+ /**
247
+ * Creates a new Point.
248
+ * @param {number} x The x-coordinate of the point.
249
+ * @param {number} y The y-coordinate of the point.
250
+ * @returns {Point} The new point.
251
+ */
121
252
  static point(x, y) {
122
253
  return new Point(x, y);
123
254
  }
124
255
 
256
+ /**
257
+ * Creates a new Rect.
258
+ * @param {number} x The x-coordinate of the rectangle.
259
+ * @param {number} y The y-coordinate of the rectangle.
260
+ * @param {number} width The width of the rectangle.
261
+ * @param {number} height The height of the rectangle.
262
+ * @returns {Rect} The new rectangle.
263
+ */
125
264
  static rect(x, y, width, height) {
126
265
  return new Rect(x, y, width, height);
127
266
  }
128
267
 
268
+ /**
269
+ * Creates a new Scene.
270
+ * @returns {Scene} The new scene.
271
+ */
129
272
  static scene() {
130
273
  return new Scene();
131
274
  }
132
275
 
276
+ /**
277
+ * Creates a new Sprite.
278
+ * @param {Scene} scene The scene of the sprite.
279
+ * @returns {Sprite} The new sprite.
280
+ */
133
281
  static sprite(scene) {
134
282
  return new Sprite(scene);
135
283
  }