squishjs 0.7.55 → 0.7.61
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/index.js +5 -1
- package/metest.js +26 -0
- package/package.json +1 -1
- package/src/Asset.js +182 -0
- package/src/physics.js +24 -0
- package/src/squishHelpers/id.js +2 -2
package/index.js
CHANGED
|
@@ -10,7 +10,9 @@ const viewUtils = require('./src/util/views');
|
|
|
10
10
|
const { terrainGenerator } = require('./src/terrain');
|
|
11
11
|
const subtypes = require('./src/subtypes');
|
|
12
12
|
const Squisher = require('./src/Squisher');
|
|
13
|
+
const Asset = require('./src/Asset');
|
|
13
14
|
const GeometryUtils = require('./src/util/geometry');
|
|
15
|
+
const Physics = require('./src/physics');
|
|
14
16
|
|
|
15
17
|
module.exports = {
|
|
16
18
|
squish,
|
|
@@ -26,5 +28,7 @@ module.exports = {
|
|
|
26
28
|
ShapeUtils: shapeUtils,
|
|
27
29
|
ViewUtils: viewUtils,
|
|
28
30
|
TerrainGenerator: terrainGenerator,
|
|
29
|
-
GeometryUtils
|
|
31
|
+
GeometryUtils,
|
|
32
|
+
Asset,
|
|
33
|
+
Physics
|
|
30
34
|
};
|
package/metest.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
const { GameNode, squish, unsquish } = require('./');
|
|
2
|
+
|
|
3
|
+
const ting = new GameNode.Shape({
|
|
4
|
+
coordinates2d: [
|
|
5
|
+
[0, 0],
|
|
6
|
+
[0, 0],
|
|
7
|
+
[0, 0],
|
|
8
|
+
[0, 0]
|
|
9
|
+
],
|
|
10
|
+
shapeType: 1
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
ting.node.id = 529025047768;
|
|
14
|
+
ting.id = 529025047768;
|
|
15
|
+
|
|
16
|
+
const squished = squish(ting);
|
|
17
|
+
|
|
18
|
+
const unsquished = unsquish(squished);
|
|
19
|
+
|
|
20
|
+
if (unsquished.node.id != Number(529025047768)) {
|
|
21
|
+
console.log('fuck man');
|
|
22
|
+
console.log(unsquished.node.id);
|
|
23
|
+
console.log(Number(ting.node.id));
|
|
24
|
+
} else {
|
|
25
|
+
console.log('um what');
|
|
26
|
+
}
|
package/package.json
CHANGED
package/src/Asset.js
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
class Asset {
|
|
2
|
+
constructor(info, data = null) {
|
|
3
|
+
this.info = info;
|
|
4
|
+
if (data) {
|
|
5
|
+
this.data = data;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
// this is dumb. was trying something but made it dumb.
|
|
9
|
+
this.https = require('https');
|
|
10
|
+
this.http = require('http');
|
|
11
|
+
this.fs = require('fs');
|
|
12
|
+
this.crypto = require('crypto');
|
|
13
|
+
this.path = require('path');
|
|
14
|
+
this.process = require('process');
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
getConfigValue(key, _default = undefined) {
|
|
18
|
+
const config = this.getConfig();
|
|
19
|
+
|
|
20
|
+
let envValue = process.env[key] && `${process.env[key]}`;
|
|
21
|
+
if (envValue !== undefined) {
|
|
22
|
+
if (envValue === 'true') {
|
|
23
|
+
envValue = true;
|
|
24
|
+
} else if (envValue === 'false') {
|
|
25
|
+
envValue = false;
|
|
26
|
+
}
|
|
27
|
+
console.log(`Using environment value: ${envValue} for key: ${key}`);
|
|
28
|
+
return envValue;
|
|
29
|
+
}
|
|
30
|
+
if (config[key] === undefined && _default === undefined) {
|
|
31
|
+
throw new Error(`No value for ${key} found in config`);
|
|
32
|
+
} else if (config[key] === undefined && _default !== undefined) {
|
|
33
|
+
return _default;
|
|
34
|
+
}
|
|
35
|
+
console.log(`Found value ${config[key]} in config`);
|
|
36
|
+
return config[key];
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
getConfig() {
|
|
40
|
+
|
|
41
|
+
if (this.cachedConfig) {
|
|
42
|
+
return this.cachedConfig;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const options = [process.cwd(), require.main.filename, process.mainModule.filename, __dirname]
|
|
46
|
+
let _config = {};
|
|
47
|
+
|
|
48
|
+
for (let i = 0; i < options.length; i++) {
|
|
49
|
+
if (this.fs.existsSync(`${options[i]}/config.json`)) {
|
|
50
|
+
console.log(`Using config at ${options[i]}`);
|
|
51
|
+
_config = JSON.parse(this.fs.readFileSync(`${options[i]}/config.json`));
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
this.cachedConfig = _config;
|
|
57
|
+
|
|
58
|
+
return _config;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
getHash(str) {
|
|
63
|
+
const shasum = this.crypto.createHash('sha1');
|
|
64
|
+
shasum.update(str);
|
|
65
|
+
return shasum.digest('hex');
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
getFileLocation() {
|
|
69
|
+
|
|
70
|
+
const HG_ASSET_PATH = this.getConfigValue('HG_ASSET_PATH', `${process.cwd()}/.asset_cache`);
|
|
71
|
+
|
|
72
|
+
const fileHash = this.getHash(this.info.id);
|
|
73
|
+
return `${HG_ASSET_PATH}/${fileHash}`;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
existsLocallySync() {
|
|
77
|
+
const fileLocation = this.getFileLocation(this.info.id);
|
|
78
|
+
return fs.existsSync(fileLocation);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
existsLocally() {
|
|
82
|
+
return new Promise((resolve, reject) => {
|
|
83
|
+
if (this.data) {
|
|
84
|
+
resolve(true);
|
|
85
|
+
}
|
|
86
|
+
const fileLocation = this.getFileLocation(this.info.id);
|
|
87
|
+
this.fs.exists(fileLocation, (exists) => {
|
|
88
|
+
resolve(exists && fileLocation);
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async downloadSync(force) {
|
|
94
|
+
|
|
95
|
+
const HG_ASSET_PATH = this.getConfigValue('HG_ASSET_PATH', `${process.cwd()}/.asset_cache`);
|
|
96
|
+
|
|
97
|
+
if (!fs.existsSync(HG_ASSET_PATH)) {
|
|
98
|
+
fs.mkdirSync(HG_ASSET_PATH);
|
|
99
|
+
}
|
|
100
|
+
const fileLocationExists = this.existsLocallySync();
|
|
101
|
+
const fileLocation = this.getFileLocation(this.info.id);
|
|
102
|
+
|
|
103
|
+
if (fileLocationExists && !force) {
|
|
104
|
+
this.initialized = true;
|
|
105
|
+
return fileLocation;
|
|
106
|
+
} else {
|
|
107
|
+
const fileLocation2 = await downloadFileSync(this.info.id, HG_ASSET_PATH);
|
|
108
|
+
this.initialized = true;
|
|
109
|
+
return fileLocation;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
download(force) {
|
|
114
|
+
|
|
115
|
+
const HG_ASSET_PATH = this.getConfigValue('HG_ASSET_PATH', `${process.cwd()}/.asset_cache`);
|
|
116
|
+
|
|
117
|
+
if (!this.fs.existsSync(HG_ASSET_PATH)) {
|
|
118
|
+
this.fs.mkdirSync(HG_ASSET_PATH);
|
|
119
|
+
}
|
|
120
|
+
return new Promise((resolve, reject) => {
|
|
121
|
+
this.existsLocally().then(fileLocation => {
|
|
122
|
+
if (fileLocation && !force) {
|
|
123
|
+
this.initialized = true;
|
|
124
|
+
resolve(fileLocation);
|
|
125
|
+
} else {
|
|
126
|
+
this.doDownload(this.info.id, HG_ASSET_PATH).then((fileLocation) => {
|
|
127
|
+
this.initialized = true;
|
|
128
|
+
resolve(fileLocation);
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
getData() {
|
|
136
|
+
return new Promise((resolve, reject) => {
|
|
137
|
+
if (this.data) {
|
|
138
|
+
resolve(this.data);
|
|
139
|
+
} else {
|
|
140
|
+
this.download().then(fileLocation => {
|
|
141
|
+
this.fs.readFile(fileLocation, (err, buf) => {
|
|
142
|
+
if (err) {
|
|
143
|
+
reject(err);
|
|
144
|
+
} else {
|
|
145
|
+
resolve(buf);
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
doDownload(assetId, path) {
|
|
154
|
+
return new Promise((resolve, reject) => {
|
|
155
|
+
const fileHash = this.getHash(assetId);
|
|
156
|
+
const filePath = `${path}/${fileHash}`;
|
|
157
|
+
|
|
158
|
+
const writeStream = this.fs.createWriteStream(filePath);
|
|
159
|
+
const getModule = this.https;
|
|
160
|
+
|
|
161
|
+
writeStream.on('close', () => {
|
|
162
|
+
resolve(filePath);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
let data = '';
|
|
166
|
+
getModule.get(`https://assets.homegames.io/${assetId}`, (res) => {
|
|
167
|
+
res.on('data', (chunk) => {
|
|
168
|
+
data += chunk;
|
|
169
|
+
writeStream.write(chunk);
|
|
170
|
+
});
|
|
171
|
+
res.on('end', () => {
|
|
172
|
+
writeStream.end();
|
|
173
|
+
});
|
|
174
|
+
}).on('error', error => {
|
|
175
|
+
reject(error);
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
module.exports = Asset;
|
package/src/physics.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const Physics = {
|
|
2
|
+
getPath: (startX, startY, xVel, yVel, endX = 100, endY = 100) => {
|
|
3
|
+
const path = [];
|
|
4
|
+
let withinEdges = true;
|
|
5
|
+
while (withinEdges) {
|
|
6
|
+
const curX = path.length === 0 ? startX : path[path.length - 1][0];
|
|
7
|
+
const curY = path.length === 0 ? startY : path[path.length - 1][1];
|
|
8
|
+
|
|
9
|
+
const newX = curX + xVel;
|
|
10
|
+
const newY = curY + yVel;
|
|
11
|
+
|
|
12
|
+
if (newX < 0 || newX > endX || newY < 0 || newY > endY) {
|
|
13
|
+
withinEdges = false;
|
|
14
|
+
} else {
|
|
15
|
+
path.push([newX, newY]);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return path;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
module.exports = Physics;
|
|
24
|
+
|
package/src/squishHelpers/id.js
CHANGED
|
@@ -35,7 +35,7 @@ const squishId = {
|
|
|
35
35
|
// build up from right to left then parse
|
|
36
36
|
let str = '';
|
|
37
37
|
for (let i = arr.length - 1; i >= 0; i--) {
|
|
38
|
-
const val = arr[i];
|
|
38
|
+
const val = arr[i] > 9 ? arr[i] : '0' + arr[i];
|
|
39
39
|
str = val + str
|
|
40
40
|
}
|
|
41
41
|
|
|
@@ -46,4 +46,4 @@ const squishId = {
|
|
|
46
46
|
module.exports = {
|
|
47
47
|
ID_SUBTYPE,
|
|
48
48
|
squishId
|
|
49
|
-
};
|
|
49
|
+
};
|