squishjs 0.6.42 → 0.7.2
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 +230 -0
- package/src/ViewableGame.js +60 -0
- package/src/node-types.js +15 -0
- package/src/squish.js +90 -432
- 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/src/terrain/defs.ts +16 -0
- 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 +35 -37
- package/test/utils.js +91 -0
- package/testapp.html +26 -0
- package/testapp.js +988 -0
- package/tsconfig.json +22 -0
- 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++) {
|
|
@@ -115,14 +113,14 @@ test("Text node with unicode", () => {
|
|
|
115
113
|
const gameNode = new GameNode.Text({
|
|
116
114
|
textInfo: {
|
|
117
115
|
text: '💯😂💯',
|
|
118
|
-
x: 40,
|
|
116
|
+
x: 40,
|
|
119
117
|
y: 40,
|
|
120
118
|
size: 1,
|
|
121
119
|
align: '😂😂😂😂😂',//center',
|
|
122
120
|
color: COLORS.BLACK
|
|
123
121
|
}
|
|
124
122
|
});
|
|
125
|
-
|
|
123
|
+
console.log("123:")
|
|
126
124
|
const squishedNode = new Uint8ClampedArray(squish(gameNode.node));
|
|
127
125
|
const unsquishedNode = unsquish(squishedNode);
|
|
128
126
|
compareSquished(squishedNode.node, unsquishedNode);
|
|
@@ -137,7 +135,7 @@ test("Text node", () => {
|
|
|
137
135
|
const gameNode = new GameNode.Text({
|
|
138
136
|
textInfo: {
|
|
139
137
|
text: 'ayy lmao',
|
|
140
|
-
x: 40,
|
|
138
|
+
x: 40,
|
|
141
139
|
y: 40,
|
|
142
140
|
size: 1,
|
|
143
141
|
align: 'center',
|
|
@@ -145,8 +143,8 @@ test("Text node", () => {
|
|
|
145
143
|
}
|
|
146
144
|
});
|
|
147
145
|
|
|
148
|
-
const squishedNode = squish(gameNode
|
|
149
|
-
const unsquishedNode = unsquish(squishedNode);
|
|
146
|
+
const squishedNode = squish(gameNode);
|
|
147
|
+
const unsquishedNode = unsquish(squishedNode).node;
|
|
150
148
|
compareSquished(squishedNode.node, unsquishedNode);
|
|
151
149
|
});
|
|
152
150
|
|
|
@@ -166,8 +164,8 @@ test("Asset node", () => {
|
|
|
166
164
|
}
|
|
167
165
|
}
|
|
168
166
|
});
|
|
169
|
-
const squishedNode = squish(gameNode
|
|
170
|
-
const unsquishedNode = unsquish(squishedNode);
|
|
167
|
+
const squishedNode = squish(gameNode);
|
|
168
|
+
const unsquishedNode = unsquish(squishedNode).node;
|
|
171
169
|
compareSquished(squishedNode.node, unsquishedNode);
|
|
172
170
|
});
|
|
173
171
|
|
|
@@ -185,8 +183,8 @@ test("Shape with shadow", () => {
|
|
|
185
183
|
}
|
|
186
184
|
});
|
|
187
185
|
|
|
188
|
-
const squishedNode = squish(gameNode
|
|
189
|
-
const unsquishedNode = unsquish(squishedNode);
|
|
186
|
+
const squishedNode = squish(gameNode);
|
|
187
|
+
const unsquishedNode = unsquish(squishedNode).node;
|
|
190
188
|
compareSquished(squishedNode.node, unsquishedNode);
|
|
191
189
|
|
|
192
190
|
});
|
|
@@ -201,8 +199,8 @@ test("Shape with onClick", () => {
|
|
|
201
199
|
}
|
|
202
200
|
});
|
|
203
201
|
|
|
204
|
-
const squished = squish(gameNode
|
|
205
|
-
const unsquished = unsquish(squished);
|
|
202
|
+
const squished = squish(gameNode);
|
|
203
|
+
const unsquished = unsquish(squished).node;
|
|
206
204
|
|
|
207
205
|
compareSquished(gameNode.node, unsquished);
|
|
208
206
|
});
|
|
@@ -211,7 +209,7 @@ test("Text with text input", () => {
|
|
|
211
209
|
const gameNode = new GameNode.Text({
|
|
212
210
|
textInfo: {
|
|
213
211
|
text: 'ayy lmao',
|
|
214
|
-
x: 40,
|
|
212
|
+
x: 40,
|
|
215
213
|
y: 40,
|
|
216
214
|
size: 1,
|
|
217
215
|
align: 'center',
|
|
@@ -225,8 +223,8 @@ test("Text with text input", () => {
|
|
|
225
223
|
}
|
|
226
224
|
});
|
|
227
225
|
|
|
228
|
-
const squishedNode = squish(gameNode
|
|
229
|
-
const unsquishedNode = unsquish(squishedNode);
|
|
226
|
+
const squishedNode = squish(gameNode);
|
|
227
|
+
const unsquishedNode = unsquish(squishedNode).node;
|
|
230
228
|
compareSquished(gameNode.node, unsquishedNode);
|
|
231
229
|
});
|
|
232
230
|
|
|
@@ -241,22 +239,22 @@ test("scaled shape", () => {
|
|
|
241
239
|
// scaled down to 80%
|
|
242
240
|
// .8 (x scale factor) * 100 (width) = 80
|
|
243
241
|
// .8 (y scale factor) * 100 (height) = 80
|
|
244
|
-
// This gets you the correct size.
|
|
242
|
+
// This gets you the correct size.
|
|
245
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.
|
|
246
244
|
// So we removed 20 units from the width, and now we need to shift everything right 10 units to keep it horizontally centered.
|
|
247
245
|
// Then we need to repeat this for the height.
|
|
248
246
|
// This would result in the scaled top left corner being at (10, 10) and the bottom right corner at (90, 90)
|
|
249
247
|
|
|
250
|
-
const squishedScaledNode = squish(gameNode
|
|
251
|
-
const unsquishedNode = unsquish(squishedScaledNode);
|
|
248
|
+
const squishedScaledNode = squish(gameNode, {x: .8, y: .8});
|
|
249
|
+
const unsquishedNode = unsquish(squishedScaledNode).node;
|
|
252
250
|
|
|
253
251
|
// top left
|
|
254
|
-
assert(unsquishedNode.coordinates2d[0] == 10);
|
|
255
|
-
assert(unsquishedNode.coordinates2d[1] == 10);
|
|
252
|
+
assert(unsquishedNode.coordinates2d[0][0] == 10);
|
|
253
|
+
assert(unsquishedNode.coordinates2d[0][1] == 10);
|
|
256
254
|
|
|
257
255
|
// bottom right
|
|
258
|
-
assert(unsquishedNode.coordinates2d[
|
|
259
|
-
assert(unsquishedNode.coordinates2d[
|
|
256
|
+
assert(unsquishedNode.coordinates2d[2][0] == 90);
|
|
257
|
+
assert(unsquishedNode.coordinates2d[2][1] == 90);
|
|
260
258
|
});
|
|
261
259
|
|
|
262
260
|
test("scaled text", () => {
|
|
@@ -276,8 +274,8 @@ test("scaled text", () => {
|
|
|
276
274
|
|
|
277
275
|
const scaledTextSize = 5 * hypLength(xScale, yScale);
|
|
278
276
|
|
|
279
|
-
const squishedScaledNode = squish(gameNode
|
|
280
|
-
const unsquishedNode = unsquish(squishedScaledNode);
|
|
277
|
+
const squishedScaledNode = squish(gameNode, {x: xScale, y: yScale});
|
|
278
|
+
const unsquishedNode = unsquish(squishedScaledNode).node;
|
|
281
279
|
|
|
282
280
|
assert(unsquishedNode.text.x.toFixed(2) === (4 * xScale + Math.round((1 - xScale) * 100) / 2).toFixed(2));
|
|
283
281
|
assert(unsquishedNode.text.y.toFixed(2) === (20 * yScale + Math.round((1 - yScale) * 100) / 2).toFixed(2));
|
|
@@ -298,8 +296,8 @@ test("scaled asset node", () => {
|
|
|
298
296
|
}
|
|
299
297
|
});
|
|
300
298
|
|
|
301
|
-
const squishedNode = squish(gameNode
|
|
302
|
-
const unsquishedNode = unsquish(squishedNode);
|
|
299
|
+
const squishedNode = squish(gameNode, {x: .85, y: .85});
|
|
300
|
+
const unsquishedNode = unsquish(squishedNode).node;
|
|
303
301
|
|
|
304
302
|
const asset = unsquishedNode.asset['some-asset-ref'];
|
|
305
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>
|