@aminuose/arcade-core 1.0.0 → 1.0.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.
Files changed (3) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/README.md +37 -0
  3. package/package.json +10 -2
package/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.0.1
4
+
5
+ No changes to the game logic.
6
+
7
+ - Added `repository`, `bugs`, and `homepage` to `package.json`, so the npm page links to the GitHub repository.
8
+ - Added a Svelte 5 example to the README that shows `slide`, `canMove`, `hasMoves`, and `spawn` working together.
9
+
3
10
  ## 1.0.0
4
11
 
5
12
  Initial release.
package/README.md CHANGED
@@ -31,6 +31,43 @@ if (!hasMoves(board)) {
31
31
  }
32
32
  ```
33
33
 
34
+ ## Using it in a Svelte 5 app
35
+
36
+ The package never imports Svelte, so the component below is the only place that knows about it. It keeps the board in `$state`, derives game over from it, and uses `slide` to get the points for each move.
37
+
38
+ ```svelte
39
+ <script>
40
+ import { slide, spawn, canMove, hasMoves } from '@aminuose/arcade-core';
41
+
42
+ const emptyBoard = () => Array.from({ length: 4 }, () => [0, 0, 0, 0]);
43
+ const keys = { ArrowLeft: 'left', ArrowRight: 'right', ArrowUp: 'up', ArrowDown: 'down' };
44
+
45
+ let board = $state(spawn(spawn(emptyBoard())));
46
+ let score = $state(0);
47
+ let gameOver = $derived(!hasMoves(board));
48
+
49
+ function play(direction) {
50
+ if (!canMove(board, direction)) return;
51
+ const result = slide(board, direction);
52
+ board = spawn(result.board);
53
+ score += result.gained;
54
+ }
55
+
56
+ function onkeydown(event) {
57
+ const direction = keys[event.key];
58
+ if (direction) {
59
+ event.preventDefault();
60
+ play(direction);
61
+ }
62
+ }
63
+ </script>
64
+
65
+ <svelte:window {onkeydown} />
66
+
67
+ <p>Score: {score}</p>
68
+ {#if gameOver}<p>Game over</p>{/if}
69
+ ```
70
+
34
71
  ## Board and directions
35
72
 
36
73
  A board is a 4×4 array of rows. `0` is an empty cell and every other value is a tile.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aminuose/arcade-core",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Pure game logic for 2048: move, spawn, hasMoves, and canMove.",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -9,5 +9,13 @@
9
9
  "scripts": {
10
10
  "test": "node --test"
11
11
  },
12
- "keywords": ["2048", "game", "arcade"]
12
+ "keywords": ["2048", "game", "arcade"],
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/AMINUOSE/arcade-core.git"
16
+ },
17
+ "bugs": {
18
+ "url": "https://github.com/AMINUOSE/arcade-core/issues"
19
+ },
20
+ "homepage": "https://github.com/AMINUOSE/arcade-core#readme"
13
21
  }