squishjs 0.7.3 → 0.7.51

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "squishjs",
3
- "version": "0.7.3",
3
+ "version": "0.7.51",
4
4
  "description": "squish & unsquish stuff",
5
5
  "scripts": {
6
6
  "test": "node testRunner.js"
package/src/BaseNode.js CHANGED
@@ -68,10 +68,10 @@ class BaseNode {
68
68
  }
69
69
 
70
70
  findChild(id) {
71
- return this.#findChildHelper(id, this);
71
+ return this.findChildHelper(id, this);
72
72
  }
73
73
 
74
- #findChildHelper(nodeId, node, found = null) {
74
+ findChildHelper(nodeId, node, found = null) {
75
75
  if (node.node.id === nodeId) {
76
76
  found = node;
77
77
  }
@@ -80,7 +80,7 @@ class BaseNode {
80
80
  if (found) {
81
81
  return found
82
82
  }
83
- found = this.#findChildHelper(nodeId, node.node.children[i], found);
83
+ found = this.findChildHelper(nodeId, node.node.children[i], found);
84
84
  }
85
85
 
86
86
  return found;
package/src/Game.js CHANGED
@@ -1,18 +1,9 @@
1
1
  class Game {
2
2
  constructor() {
3
- this.players = {};
4
- this.spectators = {};
5
3
  this.listeners = new Set();
6
4
  this.intervals = [];
7
5
  this.timeouts = [];
8
- }
9
-
10
- _hgAddPlayer(player) {
11
- this.players[player.id] = player;
12
- }
13
-
14
- _hgRemovePlayer(playerId) {
15
- delete this.players[playerId];
6
+ this.playerInfoMap = {};
16
7
  }
17
8
 
18
9
  addStateListener(listener) {
package/src/GameNode.js CHANGED
@@ -36,7 +36,7 @@ class Shape extends BaseNode {
36
36
  }
37
37
 
38
38
  clone({ handleClick, input, id }) {
39
- const _id = id || null;
39
+ const _id = Number(id) || null;
40
40
  return new Shape({
41
41
  color: this.node.color,
42
42
  onClick: handleClick,
@@ -2,7 +2,7 @@ let _id = 0;
2
2
 
3
3
  class InternalGameNode {
4
4
  constructor(color, onClick, coordinates2d, border, fill, text, asset, playerIds = [], effects = null, input = null, subType = null, id = null) {
5
- this.id = id ? Number(id) : _id++;
5
+ this.id = id ? Number(id) : Number(_id++);
6
6
  this.children = new Array();
7
7
  this.color = color;
8
8
  this.handleClick = onClick;
package/src/Squisher.js CHANGED
@@ -6,21 +6,24 @@ const INVISIBLE_PLAYER_ID = 0;
6
6
  const DEFAULT_TICK_RATE = 60;
7
7
 
8
8
  class Squisher {
9
- constructor({ game, scale, customBottomLayer, customTopLayer }) {
9
+ constructor({ game, scale, customBottomLayer, customTopLayer, onAssetUpdate }) {
10
10
  this.ids = new Set();
11
11
 
12
12
  this.game = game;
13
+ this.assets = {};
13
14
 
14
15
  this.customBottomLayer = customBottomLayer;
15
16
  this.customTopLayer = customTopLayer;
16
17
 
18
+ this.initialize();
19
+ this.onAssetUpdate = onAssetUpdate;
20
+
17
21
  this.playerFrames = {};
18
22
 
19
23
  this.listeners = new Set();
20
24
  this.scale = scale || {x: 1, y: 1};
21
25
  this.state = this.squish(this.game.getLayers());
22
- // this.spectatorState = this.game.getSpectatorLayers ? this.squish(this.game.getSpectatorLayers()) : [];
23
- this.assets = {};
26
+
24
27
  if (this.game.tick) {
25
28
  const tickRate = this.gameMetadata && this.gameMetadata.tickRate ? this.gameMetadata.tickRate : DEFAULT_TICK_RATE;
26
29
  setInterval(this.game.tick.bind(this.game), 1000 / tickRate);
@@ -45,7 +48,7 @@ class Squisher {
45
48
  }
46
49
 
47
50
  squish(layers, scale = null) {
48
-
51
+
49
52
  if (!layers) {
50
53
  return [];
51
54
  }
@@ -56,10 +59,6 @@ class Squisher {
56
59
 
57
60
  const playerMap = {};
58
61
 
59
- Object.keys(this.game.players).forEach(playerId => {
60
- playerMap[Number(playerId)] = [];
61
- });
62
-
63
62
  if (this.customBottomLayer) {
64
63
  const squishedLayer = [];
65
64
  this.squishHelper(this.customBottomLayer.root, squishedLayer, this.customBottomLayer.scale, playerMap);
@@ -95,6 +94,12 @@ class Squisher {
95
94
  return this.playerFrames[playerId];
96
95
  }
97
96
 
97
+ handleNewAsset(key, asset) {
98
+ return new Promise((resolve, reject) => {
99
+ this.initialize().then(resolve);
100
+ });
101
+ }
102
+
98
103
  squishHelper(node, squishedNodes, scale = {x: 1, y: 1}, playerMap = {}, playerIdFilter = new Set()) {
99
104
  if (!node.node.listeners.has(this)) {
100
105
  node.addListener(this);
@@ -109,14 +114,17 @@ class Squisher {
109
114
 
110
115
  if (playerIdFilter.size > 0) {
111
116
  for (let playerId of playerIdFilter) {
112
- if (playerMap[playerId]) {
113
- playerMap[playerId].push(squished);
114
- } else {
115
- console.warn(`Node references unknown player ID: ${playerId}`);
116
- }
117
- }
117
+ if (!playerMap[playerId]) {
118
+ playerMap[Number(playerId)] = [];
119
+ }
120
+
121
+ playerMap[playerId].push(squished);
122
+ }
118
123
  } else {
119
124
  Object.keys(playerMap).forEach(playerId => {
125
+ if (!playerMap[playerId]) {
126
+ playerMap[Number(playerId)] = [];
127
+ }
120
128
  playerMap[playerId].push(squished);
121
129
  })
122
130
  }
@@ -144,29 +152,34 @@ class Squisher {
144
152
 
145
153
  initialize() {
146
154
  return new Promise((resolve, reject) => {
147
- const gameMetadata = this.game.constructor.metadata && this.game.constructor.metadata();
155
+
148
156
 
149
- const gameAssets = gameMetadata && gameMetadata.assets ? gameMetadata.assets : {};
157
+ const assets = Object.assign({}, this.assets || {});
150
158
 
151
- if (this.customBottomLayer && this.customBottomLayer.assets) {
152
- Object.assign(gameAssets, this.customBottomLayer.assets);
153
- }
154
-
155
- if (this.customTopLayer && this.customTopLayer.assets) {
156
- Object.assign(gameAssets, this.customTopLayer.assets);
157
- }
159
+ const gameMetadata = this.game.constructor.metadata && this.game.constructor.metadata();
160
+
161
+ const gameAssets = gameMetadata && gameMetadata.assets ? gameMetadata.assets : {};
162
+
163
+ if (this.customBottomLayer && this.customBottomLayer.assets) {
164
+ Object.assign(gameAssets, this.customBottomLayer.assets);
165
+ }
166
+
167
+ if (this.customTopLayer && this.customTopLayer.assets) {
168
+ Object.assign(gameAssets, this.customTopLayer.assets);
169
+ }
170
+
171
+ if (this.game.getAssets && this.game.getAssets()) {
172
+ Object.assign(gameAssets, this.game.getAssets());
173
+ }
174
+
175
+ const allAssets = Object.assign(assets, gameAssets);
158
176
 
159
- if (this.game.getAssets && this.game.getAssets()) {
160
- Object.assign(gameAssets, this.game.getAssets());
161
- }
162
-
163
- const initializeAssetBundle = () => new Promise((resolve, reject) => {
164
177
  let assetBundleSize = 0;
165
178
  let finishedCount = 0;
166
- const totalCount = Object.keys(gameAssets).length;
179
+ const totalCount = Object.keys(allAssets).length;
167
180
 
168
- for (const key in gameAssets) {
169
- gameAssets[key].getData().then(buf => {
181
+ for (const key in allAssets) {
182
+ allAssets[key].getData().then(buf => {
170
183
  const assetKeyLength = 32;
171
184
  let keyIndex = 0;
172
185
  const assetKeyArray = new Array(32);
@@ -177,7 +190,7 @@ class Squisher {
177
190
 
178
191
  const encodedLength = (buf.length + assetKeyLength).toString(36);
179
192
 
180
- const assetType = gameAssets[key].info.type === 'image' ? 1 : 2;
193
+ const assetType = allAssets[key].info.type === 'image' ? 1 : 2;
181
194
 
182
195
  const encodedMaxLength = 10;
183
196
  let encodedLengthString = '';
@@ -205,16 +218,11 @@ class Squisher {
205
218
  }
206
219
  }
207
220
  }
208
- resolve(newAssetBundle);
221
+ this.assetBundle = newAssetBundle;
222
+ resolve(newAssetBundle);
209
223
  }
210
224
  });
211
225
  }
212
- });
213
-
214
- initializeAssetBundle().then((newAssetBundle) => {
215
- this.assetBundle = newAssetBundle;
216
- resolve();
217
- });
218
226
 
219
227
  });
220
228
  }
@@ -12,15 +12,12 @@ const makePlane = (s) => {
12
12
  };
13
13
 
14
14
  class ViewableGame extends Game {
15
- #plane;
16
- #fakeRoot;
17
- #planeSize;
18
15
  constructor(planeSize) {
19
16
  super();
20
- this.#planeSize = planeSize;
21
- this.#plane = makePlane(this.planeSize);
17
+ this.planeSize = planeSize;
18
+ this.plane = makePlane(this.planeSize);
22
19
 
23
- this.#fakeRoot = new GameNode.Shape({
20
+ this.fakeRoot = new GameNode.Shape({
24
21
  shapeType: Shapes.POLYGON,
25
22
  coordinates2d: ShapeUtils.rectangle(0, 0, 0, 0),
26
23
  fill: Colors.COLORS.BLACK,
@@ -28,14 +25,14 @@ class ViewableGame extends Game {
28
25
 
29
26
  this.layers = [
30
27
  {
31
- root: this.#fakeRoot
28
+ root: this.fakeRoot
32
29
  }
33
30
  ];
34
31
  }
35
32
 
36
33
  updatePlaneSize(s) {
37
- this.#planeSize = s;
38
- this.#plane.node.coordinates2d = ShapeUtils.rectangle(0, 0, s, s);
34
+ this.planeSize = s;
35
+ this.plane.node.coordinates2d = ShapeUtils.rectangle(0, 0, s, s);
39
36
  }
40
37
 
41
38
  getLayers() {
@@ -43,15 +40,15 @@ class ViewableGame extends Game {
43
40
  }
44
41
 
45
42
  getPlane() {
46
- return this.#plane;
43
+ return this.plane;
47
44
  }
48
45
 
49
46
  getPlaneSize() {
50
- return this.#planeSize;
47
+ return this.planeSize;
51
48
  }
52
49
 
53
50
  getViewRoot() {
54
- return this.#fakeRoot;
51
+ return this.fakeRoot;
55
52
  }
56
53
 
57
54
  }
package/src/squish.js CHANGED
@@ -73,7 +73,13 @@ const unsquish = (squished) => {
73
73
  }
74
74
 
75
75
  const constructor = TYPE_TO_CONSTRUCTOR[squished[2]];
76
- return new constructor({ node: constructedInternalNode });
76
+ if (constructor) {
77
+ return new constructor({ node: constructedInternalNode });
78
+ }
79
+
80
+ return {
81
+ node: constructedInternalNode
82
+ }
77
83
  }
78
84
 
79
85
  // When squishing, we need to make sure that properties that other properties depend on are inserted first.
@@ -1,4 +1,4 @@
1
- const { getFractional } = require('../util')
1
+ const { getFractional } = require('../util');
2
2
 
3
3
  const ASSET_SUBTYPE = 48;
4
4
 
@@ -6,7 +6,7 @@ const squishAsset = {
6
6
  type: ASSET_SUBTYPE,
7
7
  squish: (a, scale) => {
8
8
  const assetKey = Object.keys(a)[0];
9
- const squishedAssets = new Array(8 + assetKey.length);
9
+ const squishedAssets = new Array(10 + assetKey.length);
10
10
 
11
11
  const asset = a[assetKey];
12
12
 
@@ -16,6 +16,8 @@ const squishAsset = {
16
16
  const sizeX = scale ? scale.x * asset.size.x : asset.size.x;
17
17
  const sizeY = scale ? scale.y * asset.size.y : asset.size.y;
18
18
 
19
+ const startTimeSecond = asset.startTime || 0;
20
+
19
21
  squishedAssets[0] = Math.floor(posX);
20
22
  squishedAssets[1] = getFractional(posX);
21
23
 
@@ -28,8 +30,11 @@ const squishAsset = {
28
30
  squishedAssets[6] = Math.floor(sizeY);
29
31
  squishedAssets[7] = getFractional(sizeY);
30
32
 
33
+ squishedAssets[8] = Math.floor(startTimeSecond);
34
+ squishedAssets[9] = getFractional(startTimeSecond);
35
+
31
36
  for (let i = 0; i < assetKey.length; i++) {
32
- squishedAssets[8 + i] = assetKey.codePointAt(i);
37
+ squishedAssets[10 + i] = assetKey.codePointAt(i);
33
38
  }
34
39
 
35
40
  return squishedAssets;
@@ -41,7 +46,10 @@ const squishAsset = {
41
46
  const assetSizeX = squished[4] + squished[5] / 100;
42
47
  const assetSizeY = squished[6] + squished[7] / 100;
43
48
 
44
- const assetKey = String.fromCodePoint.apply(null, squished.slice(8));
49
+ const startTime = squished[8] + squished[9] / 100;
50
+
51
+ const assetKey = String.fromCodePoint.apply(null, squished.slice(10));
52
+
45
53
  return {
46
54
  [assetKey]: {
47
55
  pos: {
@@ -51,7 +59,8 @@ const squishAsset = {
51
59
  size: {
52
60
  x: assetSizeX,
53
61
  y: assetSizeY
54
- }
62
+ },
63
+ startTime
55
64
  }
56
65
  }
57
66
  }
@@ -3,10 +3,43 @@ const ID_SUBTYPE = 43;
3
3
  const squishId = {
4
4
  type: ID_SUBTYPE,
5
5
  squish: (i) => {
6
- return [i];
6
+ // max val: 10 000 000 000 000 000 (ten quadrillion)
7
+ const idStr = i.toString();
8
+ let strChunks = [];
9
+ if (idStr.length > 2) {
10
+ let j;
11
+ for (j = 0; j + 2 <= idStr.length; j += 2) {
12
+ strChunks.push(idStr.substring(j, j + 2));
13
+ }
14
+
15
+ if (j == idStr.length - 1) {
16
+ strChunks.push(idStr.substring(j, j + 2));
17
+ }
18
+ } else {
19
+ if (idStr.length === 1) {
20
+ strChunks.push('0' + idStr);
21
+ } else {
22
+ strChunks.push(idStr);
23
+ }
24
+ }
25
+
26
+ if (strChunks.length < 8) {
27
+ while (strChunks.length < 8) {
28
+ strChunks = ["00", ...strChunks];
29
+ }
30
+ }
31
+
32
+ return strChunks.map(c => new Number(c));
7
33
  },
8
34
  unsquish: (arr) => {
9
- return arr[0];
35
+ // build up from right to left then parse
36
+ let str = '';
37
+ for (let i = arr.length - 1; i >= 0; i--) {
38
+ const val = arr[i];
39
+ str = val + str
40
+ }
41
+
42
+ return new Number(str);
10
43
  }
11
44
  }
12
45
 
package/src/util/views.js CHANGED
@@ -4,7 +4,7 @@ const ShapeUtils = require('./shapes');
4
4
  const GeometryUtils = require('./geometry');
5
5
  const Colors = require('../Colors');
6
6
 
7
- const getView = (plane, view, playerIds, translation = {}) => {
7
+ const getView = (plane, view, playerIds, translation = {}, scale = {}) => {
8
8
 
9
9
  const wouldBeCollisions = GeometryUtils.checkCollisions(plane, {node: {coordinates2d: ShapeUtils.rectangle(view.x, view.y, view.w, view.h)}}, (node) => {
10
10
  return node.node.id !== plane.node.id;
@@ -35,11 +35,15 @@ const getView = (plane, view, playerIds, translation = {}) => {
35
35
 
36
36
  for (let coorPairIndex in vertices) {
37
37
  const coordPair = vertices[coorPairIndex];
38
+
38
39
  const x = coordPair[0];
39
40
  const y = coordPair[1];
40
41
  let translatedX = Math.max(Math.min(x - view.x, 100), 0);
41
42
  let translatedY = Math.max(Math.min(y - view.y, 100), 0);
42
43
 
44
+ translatedX = (scale.x || 1) * translatedX;
45
+ translatedY = (scale.y || 1) * translatedY;
46
+
43
47
  const shouldTranslate = translation.filter ? translation.filter(node) : true;
44
48
 
45
49
  if (shouldTranslate) {
@@ -51,20 +55,19 @@ const getView = (plane, view, playerIds, translation = {}) => {
51
55
  translatedY += translation.y;
52
56
  }
53
57
 
54
- if (translatedX < 0 || translatedX > 100) {
55
- shouldInclude = false;
56
- }
57
-
58
- if (translatedY < 0 || translatedY > 100) {
59
- shouldInclude = false;
60
- }
61
58
  }
62
59
 
63
- const xScale = 1//100 / (view.w || 100);
64
- const yScale = 1//100 / (view.h || 100);
60
+ if (translatedX < 0) {
61
+ translatedX = 0;
62
+ } else if (translatedX > 100) {
63
+ translatedX = 100;
64
+ }
65
65
 
66
- translatedX = xScale * translatedX;
67
- translatedY = yScale * translatedY;
66
+ if (translatedY < 0) {
67
+ translatedY = 0;
68
+ } else if (translatedY > 100) {
69
+ translatedY = 100;
70
+ }
68
71
 
69
72
  translatedCoords.push([translatedX, translatedY]);
70
73
  }
package/test/main.test.js CHANGED
@@ -48,6 +48,8 @@ const compareSquished = (preSquish, unsquished) => {
48
48
  assert(preSquish[key][k] === unsquished[key][k]);
49
49
  }
50
50
  }
51
+ } else if (key === 'id') {
52
+ assert(Number(preSquish[key]) === Number(unsquished[key]));
51
53
  } else {
52
54
  assert(preSquish[key] === unsquished[key]);
53
55
  }
@@ -120,14 +122,13 @@ test("Text node with unicode", () => {
120
122
  color: COLORS.BLACK
121
123
  }
122
124
  });
123
- console.log("123:")
124
- const squishedNode = new Uint8ClampedArray(squish(gameNode.node));
125
+ const squishedNode = new Uint8ClampedArray(squish(gameNode));
125
126
  const unsquishedNode = unsquish(squishedNode);
126
127
  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));
128
+ assert(unsquishedNode.node.text.text.length === 6);
129
+ assert([...unsquishedNode.node.text.text].length === 3);
130
+ assert(unsquishedNode.node.text.text.codePointAt(0) === '💯'.codePointAt(0));
131
+ assert(unsquishedNode.node.text.text.codePointAt(4) === '💯'.codePointAt(0));
131
132
  });
132
133
 
133
134
 
package/test/utils.js CHANGED
@@ -31,7 +31,7 @@ const verifyArrayEquality = (actual, expected) => {
31
31
  if (expected[i].length && actual[i].length) {
32
32
  verifyArrayEquality(expected[i], actual[i]);
33
33
  } else {
34
- assert(expected[i] === actual[i]);
34
+ assert(expected[i] - actual[i] === 0);
35
35
  }
36
36
  }
37
37
  };
package/oldsvgexport.js DELETED
@@ -1,89 +0,0 @@
1
- const fs = require('fs');
2
- const { unsquish } = require('../src/squish');
3
- const subtypes = require('../src/subtypes');
4
-
5
-
6
- const svgText = (gameNode, container) => {
7
- const fill = gameNode.node.text.color;
8
- const align = gameNode.node.text.align;
9
-
10
- let svgAlign;
11
- if (align === 'left') {
12
- svgAlign = 'end';
13
- }
14
- if (align === 'center') {
15
- svgAlign = 'middle';
16
- }
17
- if (align === 'right') {
18
- svgAlign = 'start';
19
- }
20
-
21
- const fillString = `rgba(${fill[0]}, ${fill[1]}, ${fill[2]}, ${fill[3]})`
22
-
23
- return `<text text-anchor="${svgAlign}" x="${scaleX(gameNode.node.text.x, container.x)}" y="${scaleX(gameNode.node.text.y, container.y)}" fill="${fillString}">${gameNode.node.text.text}</text>`;
24
- }
25
-
26
- const svgPolygon = (gameNode, container) => {
27
- const coordPairs = gameNode.node.coordinates2d;
28
- const fill = gameNode.node.fill;
29
-
30
- const fillString = `rgba(${fill[0]}, ${fill[1]}, ${fill[2]}, ${fill[3]})`
31
-
32
- const scaledCoords = coordPairs.map(coordPair => {
33
- return [scaleX(coordPair[0], container.x), scaleY(coordPair[1], container.y)];
34
- });
35
-
36
- return `<polygon points="${scaledCoords.join(' ')}"
37
- fill="${fillString}" stroke="black" />`;
38
- };
39
-
40
- const scaleX = (x, containerWidth) => {
41
- const widthPercentage = x / 100;
42
- return widthPercentage * containerWidth;
43
- };
44
-
45
- const scaleY = (y, containerHeight) => {
46
- const heightPercentage = y / 100;
47
- return heightPercentage * containerHeight;
48
- };
49
-
50
- const svgHelper = (gameNode, container) => {
51
-
52
- if (gameNode.node.subType === subtypes.SHAPE_2D_POLYGON) {
53
- console.log('sdfdsfdsf');
54
- return svgPolygon(gameNode, container);
55
- } else if (gameNode.node.subType === subtypes.SHAPE_2D_LINE) {
56
- // svgString += svgPolygon(gameNode);
57
- } else if (gameNode.node.subType === subtypes.SHAPE_2D_CIRCLE) {
58
- // svgString += svgPolygon(gameNode);
59
- } else if (gameNode.node.subType === subtypes.ASSET) {
60
- // svgString += svgPolygon(gameNode);
61
- } else if (gameNode.node.subType === subtypes.TEXT) {
62
- console.log('yooooo his dgjdf top of the morning');
63
- return svgText(gameNode, container);
64
- }
65
-
66
- // for (let i = 0; i < gameNode.node.children.length; i++) {
67
- // svgHelper(gameNode.node.children[i], container, svgString);
68
- // }
69
-
70
- // return svgString;
71
- };
72
-
73
- module.exports = (squishedLayers, outFile) => {
74
- const pageWidth = 600;
75
- const pageHeight = 600;
76
- let svgString = `<svg height="${pageHeight}" width="${pageWidth}" xmlns="http://www.w3.org/2000/svg" xmlns:xlink= "http://www.w3.org/1999/xlink">`;
77
- // <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
78
- // </svg>`;
79
- const container = { x: pageWidth, y: pageHeight };
80
- squishedLayers.forEach(squishedNode => {
81
- console.log('squished node');
82
- console.log(squishedNode);
83
- const node = unsquish(squishedNode);
84
- svgString += svgHelper(node, container);
85
- });
86
-
87
- svgString += `</svg>`
88
- fs.writeFileSync(outFile, svgString);
89
- };