squishjs 0.6.41 → 0.7.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 (50) hide show
  1. package/.github/workflows/node.js.yml +1 -1
  2. package/LICENSE.md +21 -0
  3. package/README.md +116 -42
  4. package/index.js +11 -1
  5. package/oldsvgexport.js +89 -0
  6. package/package.json +1 -1
  7. package/render-tester.js +120 -0
  8. package/src/BaseNode.js +101 -0
  9. package/src/Game.js +15 -3
  10. package/src/GameNode.js +82 -79
  11. package/src/InternalGameNode.js +4 -3
  12. package/src/Squisher.js +224 -0
  13. package/src/ViewableGame.js +60 -0
  14. package/src/node-types.js +15 -0
  15. package/src/squish.js +90 -349
  16. package/src/squishHelpers/asset.js +63 -0
  17. package/src/squishHelpers/border.js +16 -0
  18. package/src/squishHelpers/color.js +16 -0
  19. package/src/squishHelpers/coordinates2d.js +98 -0
  20. package/src/squishHelpers/effect.js +55 -0
  21. package/src/squishHelpers/fill.js +16 -0
  22. package/src/squishHelpers/handleClick.js +16 -0
  23. package/src/squishHelpers/id.js +16 -0
  24. package/src/squishHelpers/input.js +22 -0
  25. package/src/squishHelpers/playerIds.js +12 -0
  26. package/src/squishHelpers/pos.js +23 -0
  27. package/src/squishHelpers/size.js +23 -0
  28. package/src/squishHelpers/subType.js +16 -0
  29. package/src/squishHelpers/text.js +154 -0
  30. package/src/subtype-mappings.js +10 -0
  31. package/src/subtypes.js +7 -0
  32. package/src/terrain/defs.ts +16 -0
  33. package/src/terrain/index.ts +156 -0
  34. package/src/terrain/terrainFunctions.ts +142 -0
  35. package/src/util/geometry.js +45 -0
  36. package/src/util/index.ts +6 -0
  37. package/src/util/listenable.ts +20 -0
  38. package/src/util/shapes.ts +18 -0
  39. package/src/util/views.js +108 -0
  40. package/test/Layers.test.js +63 -0
  41. package/test/Scale.test.js +235 -0
  42. package/test/Squisher.test.js +49 -0
  43. package/test/StateUpdates.js +10 -0
  44. package/test/Terrain.test.js +0 -1
  45. package/test/main.test.js +55 -35
  46. package/test/utils.js +91 -0
  47. package/testapp.html +26 -0
  48. package/testapp.js +988 -0
  49. package/tsconfig.json +22 -0
  50. package/license.txt +0 -674
@@ -0,0 +1,10 @@
1
+ const { COLORS } = require("../src/Colors");
2
+ const assert = require("assert");
3
+ const Squisher = require('../src/Squisher');
4
+ const { unsquish } = require('../src/squish');
5
+
6
+ const { FakeGame, verifyArrayEquality, rectNode, polygonNode, circleNode } = require('./utils');
7
+
8
+ test("two layers - square over square", () => {
9
+ console.log("two layers - one square over another");
10
+ });
@@ -13,7 +13,6 @@ const outputTerrainMap = (board) => {
13
13
  });
14
14
  rtnString += '\n';
15
15
  });
16
- console.log(rtnString);
17
16
  };
18
17
 
19
18
  const hasTerrain = (board) => {
package/test/main.test.js CHANGED
@@ -1,8 +1,9 @@
1
1
  const { squish, unsquish } = require('../src/squish');
2
2
  const { GameNode } = require('../src/GameNode');
3
- const { COLORS, randomColor } = require("../src/Colors");
3
+ const { COLORS } = require("../src/Colors");
4
4
  const Shapes = require('../src/Shapes');
5
5
  const ShapeUtils = require('../src/util/shapes');
6
+ const { verifyArrayEquality } = require('./utils');
6
7
 
7
8
  const assert = require('assert');
8
9
 
@@ -21,10 +22,7 @@ const compareSquished = (preSquish, unsquished) => {
21
22
  if (!preSquish.coordinates2d) {
22
23
  assert(!unsquished.coordinates2d);
23
24
  } else {
24
- const preSquishFlat = preSquish.coordinates2d.flat();
25
- for (let i = 0; i < unsquished.coordinates2d.length; i++) {
26
- assert(preSquishFlat[i] === unsquished.coordinates2d[i]);
27
- }
25
+ verifyArrayEquality(preSquish.coordinates2d, unsquished.coordinates2d);
28
26
  }
29
27
  } else if (key === 'input' || key == 'text') {
30
28
  // TODO: Handle all of this in a more generic way
@@ -67,8 +65,8 @@ test("Simple shape", () => {
67
65
  coordinates2d: ShapeUtils.rectangle(10, 10, 50, 50),
68
66
  shapeType: Shapes.POLYGON
69
67
  });
70
- const squishedGameNode = squish(gameNode.node);
71
- const unsquishedGameNode = unsquish(squishedGameNode);
68
+ const squishedGameNode = squish(gameNode);
69
+ const unsquishedGameNode = unsquish(squishedGameNode).node;
72
70
  compareSquished(gameNode.node, unsquishedGameNode);
73
71
  });
74
72
 
@@ -80,8 +78,8 @@ test("Simple shape visible to 2 players", () => {
80
78
  playerIds: [1, 2]
81
79
  });
82
80
 
83
- const squishedGameNode = squish(gameNode.node);
84
- const unsquishedGameNode = unsquish(squishedGameNode);
81
+ const squishedGameNode = squish(gameNode);
82
+ const unsquishedGameNode = unsquish(squishedGameNode).node;
85
83
  compareSquished(gameNode.node, unsquishedGameNode);
86
84
  assert(unsquishedGameNode.playerIds.length == 2);
87
85
  assert(unsquishedGameNode.playerIds[0] == 1);
@@ -98,12 +96,12 @@ test("Simple text visible to 255 players", () => {
98
96
  size: 5,
99
97
  align: 'center',
100
98
  color: COLORS.RED
101
- },
99
+ },
102
100
  playerIds
103
101
  });
104
102
 
105
- const squishedGameNode = squish(gameNode.node);
106
- const unsquishedGameNode = unsquish(squishedGameNode);
103
+ const squishedGameNode = squish(gameNode);
104
+ const unsquishedGameNode = unsquish(squishedGameNode).node;
107
105
  compareSquished(gameNode.node, unsquishedGameNode);
108
106
  assert(unsquishedGameNode.playerIds.length == 255);
109
107
  for (let i = 0; i < playerIds.length; i++) {
@@ -111,11 +109,33 @@ test("Simple text visible to 255 players", () => {
111
109
  }
112
110
  });
113
111
 
112
+ test("Text node with unicode", () => {
113
+ const gameNode = new GameNode.Text({
114
+ textInfo: {
115
+ text: '💯😂💯',
116
+ x: 40,
117
+ y: 40,
118
+ size: 1,
119
+ align: '😂😂😂😂😂',//center',
120
+ color: COLORS.BLACK
121
+ }
122
+ });
123
+ console.log("123:")
124
+ const squishedNode = new Uint8ClampedArray(squish(gameNode.node));
125
+ const unsquishedNode = unsquish(squishedNode);
126
+ compareSquished(squishedNode.node, unsquishedNode);
127
+ assert(unsquishedNode.text.text.length === 6);
128
+ assert([...unsquishedNode.text.text].length === 3);
129
+ assert(unsquishedNode.text.text.codePointAt(0) === '💯'.codePointAt(0));
130
+ assert(unsquishedNode.text.text.codePointAt(4) === '💯'.codePointAt(0));
131
+ });
132
+
133
+
114
134
  test("Text node", () => {
115
135
  const gameNode = new GameNode.Text({
116
136
  textInfo: {
117
137
  text: 'ayy lmao',
118
- x: 40,
138
+ x: 40,
119
139
  y: 40,
120
140
  size: 1,
121
141
  align: 'center',
@@ -123,8 +143,8 @@ test("Text node", () => {
123
143
  }
124
144
  });
125
145
 
126
- const squishedNode = squish(gameNode.node);
127
- const unsquishedNode = unsquish(squishedNode);
146
+ const squishedNode = squish(gameNode);
147
+ const unsquishedNode = unsquish(squishedNode).node;
128
148
  compareSquished(squishedNode.node, unsquishedNode);
129
149
  });
130
150
 
@@ -144,8 +164,8 @@ test("Asset node", () => {
144
164
  }
145
165
  }
146
166
  });
147
- const squishedNode = squish(gameNode.node);
148
- const unsquishedNode = unsquish(squishedNode);
167
+ const squishedNode = squish(gameNode);
168
+ const unsquishedNode = unsquish(squishedNode).node;
149
169
  compareSquished(squishedNode.node, unsquishedNode);
150
170
  });
151
171
 
@@ -163,8 +183,8 @@ test("Shape with shadow", () => {
163
183
  }
164
184
  });
165
185
 
166
- const squishedNode = squish(gameNode.node);
167
- const unsquishedNode = unsquish(squishedNode);
186
+ const squishedNode = squish(gameNode);
187
+ const unsquishedNode = unsquish(squishedNode).node;
168
188
  compareSquished(squishedNode.node, unsquishedNode);
169
189
 
170
190
  });
@@ -179,8 +199,8 @@ test("Shape with onClick", () => {
179
199
  }
180
200
  });
181
201
 
182
- const squished = squish(gameNode.node);
183
- const unsquished = unsquish(squished);
202
+ const squished = squish(gameNode);
203
+ const unsquished = unsquish(squished).node;
184
204
 
185
205
  compareSquished(gameNode.node, unsquished);
186
206
  });
@@ -189,7 +209,7 @@ test("Text with text input", () => {
189
209
  const gameNode = new GameNode.Text({
190
210
  textInfo: {
191
211
  text: 'ayy lmao',
192
- x: 40,
212
+ x: 40,
193
213
  y: 40,
194
214
  size: 1,
195
215
  align: 'center',
@@ -203,8 +223,8 @@ test("Text with text input", () => {
203
223
  }
204
224
  });
205
225
 
206
- const squishedNode = squish(gameNode.node);
207
- const unsquishedNode = unsquish(squishedNode);
226
+ const squishedNode = squish(gameNode);
227
+ const unsquishedNode = unsquish(squishedNode).node;
208
228
  compareSquished(gameNode.node, unsquishedNode);
209
229
  });
210
230
 
@@ -219,22 +239,22 @@ test("scaled shape", () => {
219
239
  // scaled down to 80%
220
240
  // .8 (x scale factor) * 100 (width) = 80
221
241
  // .8 (y scale factor) * 100 (height) = 80
222
- // This gets you the correct size.
242
+ // This gets you the correct size.
223
243
  // To scale the position to re-center the data, we need to shift x and y by 1/2 of the amount of space we just removed in each direction.
224
244
  // So we removed 20 units from the width, and now we need to shift everything right 10 units to keep it horizontally centered.
225
245
  // Then we need to repeat this for the height.
226
246
  // This would result in the scaled top left corner being at (10, 10) and the bottom right corner at (90, 90)
227
247
 
228
- const squishedScaledNode = squish(gameNode.node, {x: .8, y: .8});
229
- const unsquishedNode = unsquish(squishedScaledNode);
248
+ const squishedScaledNode = squish(gameNode, {x: .8, y: .8});
249
+ const unsquishedNode = unsquish(squishedScaledNode).node;
230
250
 
231
251
  // top left
232
- assert(unsquishedNode.coordinates2d[0] == 10);
233
- assert(unsquishedNode.coordinates2d[1] == 10);
252
+ assert(unsquishedNode.coordinates2d[0][0] == 10);
253
+ assert(unsquishedNode.coordinates2d[0][1] == 10);
234
254
 
235
255
  // bottom right
236
- assert(unsquishedNode.coordinates2d[4] == 90);
237
- assert(unsquishedNode.coordinates2d[5] == 90);
256
+ assert(unsquishedNode.coordinates2d[2][0] == 90);
257
+ assert(unsquishedNode.coordinates2d[2][1] == 90);
238
258
  });
239
259
 
240
260
  test("scaled text", () => {
@@ -254,8 +274,8 @@ test("scaled text", () => {
254
274
 
255
275
  const scaledTextSize = 5 * hypLength(xScale, yScale);
256
276
 
257
- const squishedScaledNode = squish(gameNode.node, {x: xScale, y: yScale});
258
- const unsquishedNode = unsquish(squishedScaledNode);
277
+ const squishedScaledNode = squish(gameNode, {x: xScale, y: yScale});
278
+ const unsquishedNode = unsquish(squishedScaledNode).node;
259
279
 
260
280
  assert(unsquishedNode.text.x.toFixed(2) === (4 * xScale + Math.round((1 - xScale) * 100) / 2).toFixed(2));
261
281
  assert(unsquishedNode.text.y.toFixed(2) === (20 * yScale + Math.round((1 - yScale) * 100) / 2).toFixed(2));
@@ -276,8 +296,8 @@ test("scaled asset node", () => {
276
296
  }
277
297
  });
278
298
 
279
- const squishedNode = squish(gameNode.node, {x: .85, y: .85});
280
- const unsquishedNode = unsquish(squishedNode);
299
+ const squishedNode = squish(gameNode, {x: .85, y: .85});
300
+ const unsquishedNode = unsquish(squishedNode).node;
281
301
 
282
302
  const asset = unsquishedNode.asset['some-asset-ref'];
283
303
 
package/test/utils.js ADDED
@@ -0,0 +1,91 @@
1
+ const Game = require('../src/Game');
2
+ const { COLORS } = require('../src/Colors');
3
+ const { GameNode } = require('../src/GameNode');
4
+ const ShapeUtils = require('../src/util/shapes');
5
+ const Shapes = require('../src/Shapes');
6
+ const subtypes = require('../src/subtypes');
7
+
8
+ const assert = require('assert');
9
+
10
+ class FakeGame extends Game {
11
+ static metadata() {
12
+ return {
13
+ name: 'Test game'
14
+ };
15
+ }
16
+
17
+ constructor(layers) {
18
+ super();
19
+ this.layers = layers;
20
+ }
21
+
22
+ getLayers() {
23
+ return this.layers;
24
+ }
25
+ }
26
+
27
+ const verifyArrayEquality = (actual, expected) => {
28
+ assert(expected.length == actual.length);
29
+
30
+ for (let i = 0; i < expected.length; i++) {
31
+ if (expected[i].length && actual[i].length) {
32
+ verifyArrayEquality(expected[i], actual[i]);
33
+ } else {
34
+ assert(expected[i] === actual[i]);
35
+ }
36
+ }
37
+ };
38
+
39
+ const polygonNode = ({ coordinates, fill }) => {
40
+ return new GameNode.Shape({
41
+ fill: fill,
42
+ coordinates2d: coordinates,
43
+ shapeType: Shapes.POLYGON
44
+ });
45
+ };
46
+
47
+ const circleNode = ({ center, radius, fill }) => {
48
+ return new GameNode.Shape({
49
+ fill,
50
+ coordinates2d: [center.x, center.y, radius],
51
+ shapeType: Shapes.CIRCLE
52
+ });
53
+ }
54
+
55
+ const rectNode = ({ x, y, width, height, fill = COLORS.RED }) => {
56
+ return new GameNode.Shape({
57
+ fill: fill,
58
+ coordinates2d: ShapeUtils.rectangle(x, y, width, height),
59
+ shapeType: Shapes.POLYGON
60
+ });
61
+ };
62
+
63
+ const textNode = ({ text, x, y, align, color }) => {
64
+ return new GameNode.Text({
65
+ textInfo: {
66
+ x,
67
+ y,
68
+ align,
69
+ color,
70
+ text
71
+ }
72
+ });
73
+ };
74
+
75
+ const lineNode = ({ color, coords }) => {
76
+ return new GameNode.Shape({
77
+ color,
78
+ coordinates2d: coords,
79
+ shapeType: Shapes.LINE
80
+ });
81
+ };
82
+
83
+ module.exports = {
84
+ FakeGame,
85
+ verifyArrayEquality,
86
+ rectNode,
87
+ polygonNode,
88
+ circleNode,
89
+ textNode,
90
+ lineNode
91
+ }
package/testapp.html ADDED
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE HTML>
2
+ <html>
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <title>Homegames</title>
6
+ </head>
7
+ <body>
8
+ <div>
9
+ <canvas id='tester-canvas'></canvas>
10
+ </div>
11
+ </body>
12
+ <script src="src/squish.js"></script>
13
+ <script src="render-tester.js"></script>
14
+ <style>
15
+
16
+ body {
17
+ margin: 0;
18
+ }
19
+
20
+ canvas, body {
21
+ width: 100vw;
22
+ height: 100vh;
23
+ }
24
+
25
+ </style>
26
+ </html>