Skip to content

Commit 05bb2b6

Browse files
committed
standard
1 parent d2094bf commit 05bb2b6

32 files changed

+4315
-4096
lines changed

.eslintrc.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

.prettierrc

Lines changed: 0 additions & 3 deletions
This file was deleted.

index.js

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
1-
const opn = require("open");
2-
const express = require("express");
3-
const app = express();
4-
const helmet = require("helmet");
5-
const compression = require("compression");
6-
const port = process.env.PORT || 8080;
7-
var netApi = require("net-browserify");
1+
const opn = require('open')
2+
const express = require('express')
3+
const app = express()
4+
const helmet = require('helmet')
5+
const compression = require('compression')
6+
const port = process.env.PORT || 8080
7+
const netApi = require('net-browserify')
8+
const path = require('path')
9+
810
app.use(
9-
helmet({
10-
contentSecurityPolicy: false,
11-
})
12-
);
13-
app.use(netApi({ allowOrigin: "*" }));
14-
app.use(compression());
11+
helmet({
12+
contentSecurityPolicy: false
13+
})
14+
)
15+
app.use(netApi({ allowOrigin: '*' }))
16+
app.use(compression())
1517

16-
const mode = process.argv[2];
17-
if (mode === "production") {
18-
app.use(express.static(`${__dirname}/src/dist`));
19-
} else if (mode === "development") {
20-
const webpack = require("webpack");
21-
const middleware = require("webpack-dev-middleware");
22-
const devconfig = require(`${__dirname}/src/webpack.dev.js`);
23-
const compiler = webpack(devconfig);
24-
app.use(middleware(compiler));
18+
const mode = process.argv[2]
19+
if (mode === 'production') {
20+
app.use(express.static(path.join(__dirname, 'src/dist')))
21+
} else if (mode === 'development') {
22+
const webpack = require('webpack')
23+
const middleware = require('webpack-dev-middleware')
24+
const devconfig = require('./src/webpack.dev.js')
25+
const compiler = webpack(devconfig)
26+
app.use(middleware(compiler))
2527
} else {
26-
console.log("Incorrect mode!");
28+
console.log('Incorrect mode!')
2729
}
2830
app.listen(port, function () {
29-
opn(`http://localhost:${port}`);
30-
return console.log(`Server is running on \x1b[34m*:${port}\x1b[0m`);
31-
});
31+
opn(`http://localhost:${port}`)
32+
return console.log(`Server is running on \x1b[34m*:${port}\x1b[0m`)
33+
})

lib/atlasCreator.js

Lines changed: 141 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -1,167 +1,167 @@
1-
const path = require("path");
1+
const path = require('path')
22

3-
const fs = require("fs");
3+
const fs = require('fs')
44

5-
const Canvas = require("canvas");
5+
const Canvas = require('canvas')
66

77
const AtlasCreator = class AtlasCreator {
8-
constructor(options) {
9-
this.pref = options.pref;
10-
this.oneFrame = options.oneFrame;
11-
this.toxelSize = options.toxelSize;
12-
this.loadPath = options.loadPath;
13-
this.buildPath = options.buildPath;
14-
this.atlasSize = options.atlasSize;
15-
this.canvas = Canvas.createCanvas(
16-
this.atlasSize * this.toxelSize,
17-
this.atlasSize * this.toxelSize
18-
);
19-
this.ctx = this.canvas.getContext("2d");
20-
this.toxelX = 1;
21-
this.toxelY = 1;
22-
this.loadedImages = 0;
23-
this.images = {};
24-
this.textureMapping = {};
25-
this.emptyDir();
26-
this.firstLoad();
27-
}
8+
constructor (options) {
9+
this.pref = options.pref
10+
this.oneFrame = options.oneFrame
11+
this.toxelSize = options.toxelSize
12+
this.loadPath = options.loadPath
13+
this.buildPath = options.buildPath
14+
this.atlasSize = options.atlasSize
15+
this.canvas = Canvas.createCanvas(
16+
this.atlasSize * this.toxelSize,
17+
this.atlasSize * this.toxelSize
18+
)
19+
this.ctx = this.canvas.getContext('2d')
20+
this.toxelX = 1
21+
this.toxelY = 1
22+
this.loadedImages = 0
23+
this.images = {}
24+
this.textureMapping = {}
25+
this.emptyDir()
26+
this.firstLoad()
27+
}
2828

29-
emptyDir() {
30-
if (!fs.existsSync(this.buildPath)) {
31-
fs.mkdirSync(this.buildPath);
32-
}
29+
emptyDir () {
30+
if (!fs.existsSync(this.buildPath)) {
31+
fs.mkdirSync(this.buildPath)
3332
}
33+
}
3434

35-
firstLoad() {
36-
fs.readdir(this.loadPath, (err, files) => {
37-
let totalImages = 0;
38-
files.forEach((file) => {
39-
if (path.extname(file) === ".png") {
40-
totalImages += 1;
41-
}
42-
});
43-
this.totalImages = totalImages;
44-
files.forEach((file) => {
45-
const filePath = `${this.loadPath}/${file}`;
46-
if (path.extname(file) === ".png") {
47-
// console.log filePath
48-
this.addImageToLoad(filePath, file);
49-
}
50-
});
51-
});
52-
}
35+
firstLoad () {
36+
fs.readdir(this.loadPath, (_err, files) => {
37+
let totalImages = 0
38+
files.forEach((file) => {
39+
if (path.extname(file) === '.png') {
40+
totalImages += 1
41+
}
42+
})
43+
this.totalImages = totalImages
44+
files.forEach((file) => {
45+
const filePath = `${this.loadPath}/${file}`
46+
if (path.extname(file) === '.png') {
47+
// console.log filePath
48+
this.addImageToLoad(filePath, file)
49+
}
50+
})
51+
})
52+
}
5353

54-
addImageToLoad(filePath, name) {
55-
const img = new Canvas.Image();
56-
img.onload = () => {
57-
this.images[name] = img;
58-
this.loadedImages++;
59-
if (this.loadedImages === this.totalImages) {
60-
return this.forEachToxel();
61-
}
62-
};
63-
img.src = filePath;
54+
addImageToLoad (filePath, name) {
55+
const img = new Canvas.Image()
56+
img.onload = () => {
57+
this.images[name] = img
58+
this.loadedImages++
59+
if (this.loadedImages === this.totalImages) {
60+
return this.forEachToxel()
61+
}
6462
}
63+
img.src = filePath
64+
}
6565

66-
forEachToxel() {
67-
Object.keys(this.images).forEach((name) => {
68-
const img = this.images[name];
69-
this.addToxelToAtlas(img, name);
70-
});
71-
return this.updateAtlas();
72-
}
66+
forEachToxel () {
67+
Object.keys(this.images).forEach((name) => {
68+
const img = this.images[name]
69+
this.addToxelToAtlas(img, name)
70+
})
71+
return this.updateAtlas()
72+
}
7373

74-
addToxelToAtlas(img, name) {
75-
const w = img.width / this.toxelSize;
76-
const h = img.height / this.toxelSize;
77-
if (this.oneFrame) {
74+
addToxelToAtlas (img, name) {
75+
const w = img.width / this.toxelSize
76+
const h = img.height / this.toxelSize
77+
if (this.oneFrame) {
78+
this.ctx.drawImage(
79+
img,
80+
0,
81+
0,
82+
this.toxelSize,
83+
this.toxelSize,
84+
(this.toxelX - 1) * this.toxelSize,
85+
(this.toxelY - 1) * this.toxelSize,
86+
this.toxelSize,
87+
this.toxelSize
88+
)
89+
this.textureMapping[`${name.substr(0, name.length - 4)}`] = {
90+
x: this.toxelX,
91+
y: this.toxelY
92+
}
93+
this.moveToxel()
94+
} else {
95+
if (w > 1 || h > 1) {
96+
for (let _i = 0; _i < w; _i++) {
97+
for (let _j = 0; _j < h; _j++) {
7898
this.ctx.drawImage(
79-
img,
80-
0,
81-
0,
82-
this.toxelSize,
83-
this.toxelSize,
84-
(this.toxelX - 1) * this.toxelSize,
85-
(this.toxelY - 1) * this.toxelSize,
86-
this.toxelSize,
87-
this.toxelSize
88-
);
89-
this.textureMapping[`${name.substr(0, name.length - 4)}`] = {
90-
x: this.toxelX,
91-
y: this.toxelY,
92-
};
93-
this.moveToxel();
94-
} else {
95-
if (w > 1 || h > 1) {
96-
for (let _i = 0; _i < w; _i++) {
97-
for (let _j = 0; _j < h; _j++) {
98-
this.ctx.drawImage(
99-
img,
100-
_i * this.toxelSize,
101-
_j * this.toxelSize,
102-
this.toxelSize,
103-
this.toxelSize,
104-
(this.toxelX - 1) * this.toxelSize,
105-
(this.toxelY - 1) * this.toxelSize,
106-
this.toxelSize,
107-
this.toxelSize
108-
);
109-
this.textureMapping[
99+
img,
100+
_i * this.toxelSize,
101+
_j * this.toxelSize,
102+
this.toxelSize,
103+
this.toxelSize,
104+
(this.toxelX - 1) * this.toxelSize,
105+
(this.toxelY - 1) * this.toxelSize,
106+
this.toxelSize,
107+
this.toxelSize
108+
)
109+
this.textureMapping[
110110
`${name.substr(0, name.length - 4)}@${_i}@${_j}`
111-
] = {
112-
x: this.toxelX,
113-
y: this.toxelY,
114-
};
115-
this.moveToxel();
116-
}
117-
}
118-
} else {
119-
this.ctx.drawImage(
120-
img,
121-
(this.toxelX - 1) * this.toxelSize,
122-
(this.toxelY - 1) * this.toxelSize,
123-
this.toxelSize,
124-
this.toxelSize
125-
);
126-
this.textureMapping[name.substr(0, name.length - 4)] = {
127-
x: this.toxelX,
128-
y: this.toxelY,
129-
};
130-
this.moveToxel();
111+
] = {
112+
x: this.toxelX,
113+
y: this.toxelY
131114
}
115+
this.moveToxel()
116+
}
117+
}
118+
} else {
119+
this.ctx.drawImage(
120+
img,
121+
(this.toxelX - 1) * this.toxelSize,
122+
(this.toxelY - 1) * this.toxelSize,
123+
this.toxelSize,
124+
this.toxelSize
125+
)
126+
this.textureMapping[name.substr(0, name.length - 4)] = {
127+
x: this.toxelX,
128+
y: this.toxelY
132129
}
130+
this.moveToxel()
131+
}
133132
}
133+
}
134134

135-
moveToxel() {
136-
if (this.toxelX === this.atlasSize) {
137-
this.toxelX = 1;
138-
this.toxelY += 1;
139-
} else {
140-
this.toxelX += 1;
141-
}
135+
moveToxel () {
136+
if (this.toxelX === this.atlasSize) {
137+
this.toxelX = 1
138+
this.toxelY += 1
139+
} else {
140+
this.toxelX += 1
142141
}
142+
}
143143

144-
updateAtlas() {
145-
console.log(`\x1b[33m[${this.pref} Atlas]`);
146-
console.log(`\x1b[32mTotal images: ${this.totalImages}`);
147-
fs.writeFileSync(
144+
updateAtlas () {
145+
console.log(`\x1b[33m[${this.pref} Atlas]`)
146+
console.log(`\x1b[32mTotal images: ${this.totalImages}`)
147+
fs.writeFileSync(
148148
`${this.buildPath}/${this.pref}-Atlas.png`,
149-
this.canvas.toBuffer("image/png")
150-
);
151-
console.log(
149+
this.canvas.toBuffer('image/png')
150+
)
151+
console.log(
152152
`\x1b[33mFull atlas: ${this.buildPath}/${this.pref}-Atlas.png`
153-
);
154-
fs.writeFileSync(
153+
)
154+
fs.writeFileSync(
155155
`${this.buildPath}/${this.pref}-Mapping.json`,
156156
JSON.stringify(this.textureMapping, null, 2)
157-
);
158-
console.log(
157+
)
158+
console.log(
159159
`\x1b[33mFull atlas mapping: ${this.buildPath}/${this.pref}-Mapping.json`
160-
);
161-
console.log(
160+
)
161+
console.log(
162162
`\x1b[32mSuccessfully generated ${this.canvas.width}x${this.canvas.height} Texture Atlas!\n\x1b[0m`
163-
);
164-
}
165-
};
163+
)
164+
}
165+
}
166166

167-
module.exports = AtlasCreator;
167+
module.exports = AtlasCreator

0 commit comments

Comments
 (0)