-
Notifications
You must be signed in to change notification settings - Fork 2
/
map_source_generator.js
85 lines (73 loc) · 2.28 KB
/
map_source_generator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const fs = require('fs');
var path = require("path");
var Image = require("./Image_util.js");
const lvl0 = require('./lvl0.js').map_info;
const lvl1 = require('./lvl1.js').map_info;
const lvl2 = require('./lvl2.js').map_info;
const map_info = lvl1; // Update
var image_name = "lvl1_upd.png"; // Update
const is_big_img = true; // Update. true if has tiles of 16x16 and false is 1x1
// Note: if .fm file is not being generated, check the image size.
// Width and height must be a multiple of 16
function main(){
if (is_big_img) {
Image.read_big_image("img/maps/"+image_name)
.then( image_info => {
const map_content = set_content(image_info, map_info)
save_fm_file(image_name, map_content)
.then(res => console.log(res))
.catch(err => console.log(err));
})
.catch( err => console.warn(err));
} else {
Image.read_image("img/maps/"+image_name)
.then( image_info => {
const map_content = set_content(image_info, map_info)
save_fm_file(image_name, map_content)
.then(res => console.log(res))
.catch(err => console.log(err));
})
.catch( err => console.warn(err));
}
}
// "index.js: got an error of MIME for Buffer from Jimp"
function set_content(image_info, map_info){
// Image_info: 74x74
var map_content = "";
var line = "";
const pixels = image_info.pixels;
pixels.forEach( pixel => {
line += color_is_equal(pixel, map_info);
if (is_full_line(line, image_info.width)){
map_content += '"'+line+'",\n';
line = "";
}
})
return map_content;
}
function is_same_color(pixel, color) {
return pixel.r === color.r && pixel.g === color.g && pixel.b === color.b;
}
function color_is_equal(pixel, map_info){
let code = "";
for (var i = 0; i < map_info.length; i++) {
let obj = map_info[i];
code += is_same_color(pixel.color, obj.color) ? obj.code : ""
}
return code === "" ? "b." : code;
}
function is_full_line(line, width){
return line.length === width*2 ? true : false; // x2 = code os 2 characteres
}
async function save_fm_file(image_name, content){
var image_name = image_name.slice(0,-4);
var path = "./fm_images/map_code/"+image_name+".fm";
var content = content;
try {
fs.writeFileSync(path, content);
return "Saved "+path;
} catch (e) {
throw e;
}
}
main()