squishjs 0.7.61 → 0.7.65

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.61",
3
+ "version": "0.7.65",
4
4
  "description": "squish & unsquish stuff",
5
5
  "scripts": {
6
6
  "test": "node testRunner.js"
package/src/Squisher.js CHANGED
@@ -190,7 +190,13 @@ class Squisher {
190
190
 
191
191
  const encodedLength = (buf.length + assetKeyLength).toString(36);
192
192
 
193
- const assetType = allAssets[key].info.type === 'image' ? 1 : 2;
193
+ const assetTypeMap = {
194
+ 'image': 1,
195
+ 'audio': 2,
196
+ 'font': 3
197
+ };
198
+
199
+ const assetType = assetTypeMap[allAssets[key].info.type];
194
200
 
195
201
  const encodedMaxLength = 10;
196
202
  let encodedLengthString = '';
package/src/physics.js CHANGED
@@ -10,6 +10,24 @@ const Physics = {
10
10
  const newY = curY + yVel;
11
11
 
12
12
  if (newX < 0 || newX > endX || newY < 0 || newY > endY) {
13
+ let _newX = newX;
14
+ let _newY = newY;
15
+ if (newX < 0) {
16
+ _newX = 0;
17
+ } else if (newX > endX) {
18
+ _newX = endX;
19
+ }
20
+
21
+ if (newY < 0) {
22
+ _newY = 0;
23
+ } else if (newY > endY) {
24
+ _newY = endY;
25
+ }
26
+
27
+ if (!(path[path.length - 1][0] === _newX || path[path.length - 1][1] === _newY)) {
28
+ path.push([_newX, _newY]);
29
+ }
30
+
13
31
  withinEdges = false;
14
32
  } else {
15
33
  path.push([newX, newY]);
@@ -11,7 +11,8 @@ const squishText = {
11
11
  const textY = scale ? (t.y * scale.y) + Math.round(100 * (1 - scale.y)) / 2 : t.y;
12
12
 
13
13
  const align = t.align || 'left';
14
- const squishedText = new Array(t.text.length + 10 + align.length);
14
+ const font = t.font || 'default';
15
+ const squishedText = new Array(t.text.length + 10 + align.length + font.length);
15
16
 
16
17
  squishedText[0] = Math.floor(textX);
17
18
  squishedText[1] = Math.round(100 * (textX - Math.floor(textX)));
@@ -33,6 +34,7 @@ const squishText = {
33
34
  }
34
35
 
35
36
  squishedText[6 + squishedTextColor.length] = 3 * [...align].length;
37
+ squishedText[6 + squishedTextColor.length + 1] = 3 * [...font].length;// (3*[...align].length) + 1] = 3 * [...font].length;
36
38
 
37
39
  let j = 0;
38
40
  for (let i = 0; i < [...align].length; i++) {
@@ -52,12 +54,38 @@ const squishText = {
52
54
  } else {
53
55
  ting = [`${codePointString.charAt(0)}${codePointString.charAt(1)}`, `${codePointString.charAt(2)}${codePointString.charAt(3)}`, `${codePointString.charAt(4)}${codePointString.charAt(5)}`];
54
56
  }
55
- squishedText[6 + squishedTextColor.length + 1 + j] = Number(ting[0]);
56
- squishedText[6 + squishedTextColor.length + 1 + j + 1] = Number(ting[1]);
57
- squishedText[6 + squishedTextColor.length + 1 + j + 2] = Number(ting[2]);
57
+ squishedText[6 + squishedTextColor.length + 2 + j] = Number(ting[0]);
58
+ squishedText[6 + squishedTextColor.length + 2 + j + 1] = Number(ting[1]);
59
+ squishedText[6 + squishedTextColor.length + 2 + j + 2] = Number(ting[2]);
58
60
  j += 3;
59
61
  }
60
62
 
63
+ let f = 0;
64
+ for (let i = 0; i < [...font].length; i++) {
65
+ const codePointToInsert = [...font][i].codePointAt(0);
66
+ const codePointString = codePointToInsert.toString();
67
+ let ting;
68
+ if (codePointString.length == 1) {
69
+ ting = [`00`, `00`, `0${codePointString}`];
70
+ } else if (codePointString.length == 2) {
71
+ ting = [`00`, `00`, `${codePointString}`];
72
+ } else if (codePointString.length == 3) {
73
+ ting = [`00`, `0${codePointString.charAt(0)}`, `${codePointString.charAt(1)}${codePointString.charAt(2)}`];
74
+ } else if (codePointString.length == 4) {
75
+ ting = [`00`, `${codePointString.charAt(0)}${codePointString.charAt(1)}`, `${codePointString.charAt(2)}${codePointString.charAt(3)}`];
76
+ } else if (codePointString.length == 5) {
77
+ ting = [`0${codePointString.charAt(0)}`, `${codePointString.charAt(1)}${codePointString.charAt(2)}`, `${codePointString.charAt(3)}${codePointString.charAt(4)}`];
78
+ } else {
79
+ ting = [`${codePointString.charAt(0)}${codePointString.charAt(1)}`, `${codePointString.charAt(2)}${codePointString.charAt(3)}`, `${codePointString.charAt(4)}${codePointString.charAt(5)}`];
80
+ }
81
+ let startIndex = 6 + squishedTextColor.length + 2 + (3 * [...align].length);
82
+ squishedText[startIndex + f] = Number(ting[0]);
83
+ squishedText[startIndex + f + 1] = Number(ting[1]);
84
+ squishedText[startIndex + f + 2] = Number(ting[2]);
85
+
86
+ f += 3;
87
+ }
88
+
61
89
  let k = 0;
62
90
  for (let i = 0; i < [...t.text].length; i++) {
63
91
  const codePointToInsert = [...t.text][i].codePointAt(0);
@@ -76,9 +104,11 @@ const squishText = {
76
104
  } else {
77
105
  ting = [`${codePointString.charAt(0)}${codePointString.charAt(1)}`, `${codePointString.charAt(2)}${codePointString.charAt(3)}`, `${codePointString.charAt(4)}${codePointString.charAt(5)}`];
78
106
  }
79
- squishedText[6 + squishedTextColor.length + 1 + j + k] = Number(ting[0]);
80
- squishedText[6 + squishedTextColor.length + 1 + j + k + 1] = Number(ting[1]);
81
- squishedText[6 + squishedTextColor.length + 1 + j + k + 2] = Number(ting[2]);
107
+
108
+ let startIndex = 6 + squishedTextColor.length + 2 + (3 * [...align].length) + (3 * [...font].length);
109
+ squishedText[startIndex + k] = Number(ting[0]);
110
+ squishedText[startIndex + k + 1] = Number(ting[1]);
111
+ squishedText[startIndex + k + 2] = Number(ting[2]);
82
112
 
83
113
  k += 3;
84
114
  }
@@ -91,8 +121,10 @@ const squishText = {
91
121
  const textSize = squished[4] + squished[5] / 100;
92
122
  const textColor = squished.slice(6, 10);
93
123
  const textAlignLength = squished[10];
94
- const textAlignVal = squished.slice(11, 11 + textAlignLength);
95
- const textVal = squished.slice(11 + textAlignLength);
124
+ const textFontLength = squished[11];
125
+ const textAlignVal = squished.slice(12, 12 + textAlignLength);
126
+ const textFontVal = squished.slice(12 + textAlignLength, 12 + textAlignLength + textFontLength);
127
+ const textVal = squished.slice(12 + textAlignLength + textFontLength);
96
128
 
97
129
  let alignCodePoints = [];
98
130
  for (let i = 0; i < textAlignVal.length; i+=3) {
@@ -114,6 +146,29 @@ const squishText = {
114
146
  const codePoint = firstChunk + secondChunk + thirdChunk;
115
147
  alignCodePoints.push(codePoint);
116
148
  }
149
+ const align = String.fromCodePoint.apply(null, alignCodePoints);
150
+
151
+ let fontCodePoints = [];
152
+ for (let i = 0; i < textFontVal.length; i+=3) {
153
+ let firstChunk = textFontVal[i].toString();
154
+ if (firstChunk.length == 1) {
155
+ firstChunk = `0${firstChunk}`;
156
+ }
157
+
158
+ let secondChunk = textFontVal[i + 1].toString();
159
+ if (secondChunk.length == 1) {
160
+ secondChunk = `0${secondChunk}`;
161
+ }
162
+
163
+ let thirdChunk = textFontVal[i + 2].toString();
164
+ if (thirdChunk.length == 1) {
165
+ thirdChunk = `0${thirdChunk}`;
166
+ }
167
+
168
+ const codePoint = firstChunk + secondChunk + thirdChunk;
169
+ fontCodePoints.push(codePoint);
170
+ }
171
+ const font = String.fromCodePoint.apply(null, fontCodePoints);
117
172
 
118
173
  const textCodePoints = [];
119
174
  for (let i = 0; i < textVal.length; i+=3) {
@@ -134,8 +189,7 @@ const squishText = {
134
189
  textCodePoints.push(codePoint);
135
190
  }
136
191
 
137
- const align = String.fromCodePoint.apply(null, alignCodePoints);
138
- const text = String.fromCodePoint.apply(null, textCodePoints);
192
+ const text = String.fromCodePoint.apply(null, textCodePoints);
139
193
 
140
194
  return {
141
195
  x: textPosX,
@@ -143,7 +197,8 @@ const squishText = {
143
197
  text: text,
144
198
  size: textSize,
145
199
  color: textColor,
146
- align
200
+ align,
201
+ font
147
202
  };
148
203
  }
149
204
  }
package/test/main.test.js CHANGED
@@ -149,6 +149,25 @@ test("Text node", () => {
149
149
  compareSquished(squishedNode.node, unsquishedNode);
150
150
  });
151
151
 
152
+ test("Text node with custom font", () => {
153
+ const gameNode = new GameNode.Text({
154
+ textInfo: {
155
+ text: 'ayy lmao',
156
+ font: 'test-font',
157
+ x: 40,
158
+ y: 40,
159
+ size: 1,
160
+ align: 'center',
161
+ color: COLORS.BLACK
162
+ }
163
+ });
164
+
165
+ const squishedNode = squish(gameNode);
166
+ const unsquishedNode = unsquish(squishedNode).node;
167
+ assert(unsquishedNode.text.font === 'test-font');
168
+ compareSquished(squishedNode.node, unsquishedNode);
169
+ });
170
+
152
171
  test("Asset node", () => {
153
172
  const gameNode = new GameNode.Asset({
154
173
  coordinates2d: ShapeUtils.rectangle(0, 0, 10, 10),