squishjs 0.6.43 → 0.7.3
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/.github/workflows/node.js.yml +1 -1
- package/LICENSE.md +21 -0
- package/README.md +116 -42
- package/index.js +11 -1
- package/oldsvgexport.js +89 -0
- package/package.json +1 -1
- package/render-tester.js +120 -0
- package/src/BaseNode.js +101 -0
- package/src/Game.js +15 -3
- package/src/GameNode.js +82 -79
- package/src/InternalGameNode.js +4 -3
- package/src/Squisher.js +235 -0
- package/src/ViewableGame.js +60 -0
- package/src/node-types.js +15 -0
- package/src/squish.js +90 -349
- package/src/squishHelpers/asset.js +63 -0
- package/src/squishHelpers/border.js +16 -0
- package/src/squishHelpers/color.js +16 -0
- package/src/squishHelpers/coordinates2d.js +98 -0
- package/src/squishHelpers/effect.js +55 -0
- package/src/squishHelpers/fill.js +16 -0
- package/src/squishHelpers/handleClick.js +16 -0
- package/src/squishHelpers/id.js +16 -0
- package/src/squishHelpers/input.js +22 -0
- package/src/squishHelpers/playerIds.js +12 -0
- package/src/squishHelpers/pos.js +23 -0
- package/src/squishHelpers/size.js +23 -0
- package/src/squishHelpers/subType.js +16 -0
- package/src/squishHelpers/text.js +154 -0
- package/src/subtype-mappings.js +10 -0
- package/src/subtypes.js +7 -0
- package/{dist/terrain/defs.d.ts → src/terrain/defs.ts} +7 -6
- package/src/terrain/index.ts +156 -0
- package/src/terrain/terrainFunctions.ts +142 -0
- package/src/util/geometry.js +45 -0
- package/src/util/index.ts +6 -0
- package/src/util/listenable.ts +20 -0
- package/src/util/shapes.ts +18 -0
- package/src/util/views.js +108 -0
- package/test/Layers.test.js +63 -0
- package/test/Scale.test.js +235 -0
- package/test/Squisher.test.js +49 -0
- package/test/StateUpdates.js +10 -0
- package/test/Terrain.test.js +0 -1
- package/test/main.test.js +55 -35
- package/test/utils.js +91 -0
- package/testapp.html +26 -0
- package/testapp.js +988 -0
- package/tsconfig.json +22 -0
- package/dist/Colors.d.ts +0 -7
- package/dist/Colors.js +0 -124
- package/dist/Game.d.ts +0 -20
- package/dist/Game.js +0 -48
- package/dist/GameNode.d.ts +0 -66
- package/dist/GameNode.js +0 -148
- package/dist/InternalGameNode.d.ts +0 -20
- package/dist/InternalGameNode.js +0 -32
- package/dist/Shapes.d.ts +0 -6
- package/dist/Shapes.js +0 -8
- package/dist/index.d.ts +0 -7
- package/dist/index.js +0 -18
- package/dist/sharedDefs.d.ts +0 -72
- package/dist/sharedDefs.js +0 -13
- package/dist/squish.d.ts +0 -8
- package/dist/squish.js +0 -336
- package/dist/terrain/defs.js +0 -5
- package/dist/terrain/index.d.ts +0 -2
- package/dist/terrain/index.js +0 -151
- package/dist/terrain/terrainFunctions.d.ts +0 -6
- package/dist/terrain/terrainFunctions.js +0 -152
- package/dist/util/index.d.ts +0 -2
- package/dist/util/index.js +0 -11
- package/dist/util/listenable.d.ts +0 -3
- package/dist/util/listenable.js +0 -21
- package/dist/util/shapes.d.ts +0 -2
- package/dist/util/shapes.js +0 -23
- 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
|
+
});
|
package/test/Terrain.test.js
CHANGED
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
|
|
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
|
-
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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[
|
|
237
|
-
assert(unsquishedNode.coordinates2d[
|
|
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
|
|
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
|
|
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>
|