squishjs 0.7.1 → 0.7.4

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.1",
3
+ "version": "0.7.4",
4
4
  "description": "squish & unsquish stuff",
5
5
  "scripts": {
6
6
  "test": "node testRunner.js"
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/Squisher.js CHANGED
@@ -14,10 +14,12 @@ class Squisher {
14
14
  this.customBottomLayer = customBottomLayer;
15
15
  this.customTopLayer = customTopLayer;
16
16
 
17
+ this.playerFrames = {};
18
+
17
19
  this.listeners = new Set();
18
20
  this.scale = scale || {x: 1, y: 1};
19
21
  this.state = this.squish(this.game.getLayers());
20
- this.spectatorState = this.game.getSpectatorLayers ? this.squish(this.game.getSpectatorLayers()) : [];
22
+ // this.spectatorState = this.game.getSpectatorLayers ? this.squish(this.game.getSpectatorLayers()) : [];
21
23
  this.assets = {};
22
24
  if (this.game.tick) {
23
25
  const tickRate = this.gameMetadata && this.gameMetadata.tickRate ? this.gameMetadata.tickRate : DEFAULT_TICK_RATE;
@@ -43,7 +45,7 @@ class Squisher {
43
45
  }
44
46
 
45
47
  squish(layers, scale = null) {
46
-
48
+
47
49
  if (!layers) {
48
50
  return [];
49
51
  }
@@ -53,9 +55,6 @@ class Squisher {
53
55
  let toSquish = [];
54
56
 
55
57
  const playerMap = {};
56
- Object.keys(this.game.players).forEach(playerId => {
57
- playerMap[Number(playerId)] = [];
58
- });
59
58
 
60
59
  if (this.customBottomLayer) {
61
60
  const squishedLayer = [];
@@ -77,19 +76,22 @@ class Squisher {
77
76
  }
78
77
 
79
78
 
80
- if (this.customTopLayer) {
79
+ if (this.customTopLayer) {
81
80
  const squishedLayer = [];
82
81
  this.squishHelper(this.customTopLayer.root, squishedLayer, this.customTopLayer.scale, playerMap);
83
82
  toSquish.push(squishedLayer);
84
83
  }
85
84
 
86
- // todo: remove/move this side effect
87
- this.playerStates = playerMap;
85
+ this.playerFrames = playerMap;
88
86
 
89
87
  return toSquish.flat();
90
88
  }
91
89
 
92
- squishHelper(node, squishedNodes, scale = {x: 1, y: 1}, playerMap = {}) {
90
+ getPlayerFrame(playerId) {
91
+ return this.playerFrames[playerId];
92
+ }
93
+
94
+ squishHelper(node, squishedNodes, scale = {x: 1, y: 1}, playerMap = {}, playerIdFilter = new Set()) {
93
95
  if (!node.node.listeners.has(this)) {
94
96
  node.addListener(this);
95
97
  }
@@ -97,24 +99,32 @@ class Squisher {
97
99
  const squished = squish(node, scale);
98
100
  squishedNodes.push(squished);
99
101
 
100
- if (playerMap) {
101
- if (node.node.playerIds && node.node.playerIds.length) {
102
- node.node.playerIds.forEach(playerId => {
103
- if (playerMap[playerId]) {
104
- playerMap[playerId].push(squished);
105
- } else {
106
- console.warn(`Node references unknown player ID: ${playerId}`);
107
- }
108
- });
109
- } else {
110
- Object.keys(playerMap).forEach(playerId => {
111
- playerMap[playerId].push(squished);
112
- })
102
+ if (node.node.playerIds && node.node.playerIds.length > 0) {
103
+ node.node.playerIds.forEach(pId => playerIdFilter.add(pId));
104
+ }
105
+
106
+ if (playerIdFilter.size > 0) {
107
+ for (let playerId of playerIdFilter) {
108
+ if (!playerMap[playerId]) {
109
+ playerMap[Number(playerId)] = [];
110
+ }
111
+
112
+ playerMap[playerId].push(squished);
113
113
  }
114
+ } else {
115
+ Object.keys(playerMap).forEach(playerId => {
116
+ if (!playerMap[playerId]) {
117
+ playerMap[Number(playerId)] = [];
118
+ }
119
+ playerMap[playerId].push(squished);
120
+ })
114
121
  }
115
122
 
116
123
  for (let i = 0; i < node.node.children.length; i++) {
117
- this.squishHelper(node.node.children[i], squishedNodes, scale, playerMap);
124
+ // make a new set so child calls within a single generation arent
125
+ // modifying the same filter set
126
+ const pathFilter = new Set(playerIdFilter);
127
+ this.squishHelper(node.node.children[i], squishedNodes, scale, playerMap, pathFilter);
118
128
  }
119
129
 
120
130
  }
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