File size: 7,965 Bytes
90cbf22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8cbe088
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import * as CONFIG from './leconfig.js' 
import * as UTIL   from './eutils.js'
import { g_ctx }  from './lecontext.js' // global context



function generate_preamble() {
    const mapfile_preamble = '' +
        '// Map generated by assettool.js [' + new Date() + ']\n' +
        '\n' +
        'export const tilesetpath = "' + g_ctx.tilesetpath + '"\n' +
        'export const tiledim = ' + g_ctx.tiledimx + '\n' +
        'export const screenxtiles = ' + g_ctx.tilesettilew + '\n' +
        'export const screenytiles = ' + g_ctx.tilesettileh + '\n' +
        'export const tilesetpxw = ' + g_ctx.tilesetpxw + '\n' +
        'export const tilesetpxh = ' + g_ctx.tilesetpxh + '\n\n'

       return mapfile_preamble; 
}

const bgtile_string_start = '' +
    'export const bgtiles = [\n' +
    '   [\n'

function write_map_file(bg_tiles_0, bg_tiles_1, obj_tiles_1, obj_tiles_2, animated_tiles){
    let text = generate_preamble(); 
    text += bgtile_string_start;

    for(let row = 0; row < bg_tiles_0.length; row++) {
        text += '[ ';
        for(let column = 0; column < bg_tiles_0[row].length; column++) {
            text += bg_tiles_0[row][column];
            if (column != bg_tiles_0.length - 1){
                text += ' , ';
            }
        }
        text += '],\n';
    }
    text += '],\n';
    text += '[\n';
    for(let row = 0; row < bg_tiles_1.length; row++) {
        text += '[ ';
        for(let column = 0; column < bg_tiles_1[row].length; column++) {
            text += bg_tiles_1[row][column];
            if (column != bg_tiles_1.length - 1){
                text += ' , ';
            }
        }
        text += '],\n';
    }
    text += '],];\n\n';

    text += ''+
    'export const objmap = [\n'+
    '[\n';

    for(let row = 0; row < obj_tiles_1.length; row++) {
        text += '[ ';
        for(let column = 0; column < obj_tiles_1[row].length; column++) {
            text += obj_tiles_1[row][column];
            if (column != obj_tiles_1.length - 1){
                text += ' , ';
            }
        }
        text += '],\n';
    }
    text += '],\n';
    text += '[\n';

    for(let row = 0; row < obj_tiles_2.length; row++) {
        text += '[ ';
        for(let column = 0; column < obj_tiles_2[row].length; column++) {
            text += obj_tiles_2[row][column];
            if (column != obj_tiles_2.length - 1){
                text += ' , ';
            }
        }
        text += '],\n';
    }
    text += '],];\n';


    text += ''+
    'export const animatedsprites = [\n';

    for(let x = 0 ; x < animated_tiles.length; x++){
        let atile = animated_tiles[x];
        text += '{ x: '+atile.x+", y: "+ atile.y+ ", w: "+ atile.width+ ", h: "+ atile.height ; 
        text += ', layer: '+atile.layer;
        text += ', sheet: "'+ atile.spritesheetname+ '", animation: "'+ atile.animationname+'" },\n';
    }

    text += '];\n\n';
    text += 'export const mapwidth = bgtiles[0][0].length;\n';
    text += 'export const mapheight = bgtiles[0].length;\n';

    UTIL.download(text, "map.js", "text/plain");
}


export function generate_level_file() {
    let layer0 = g_ctx.g_layers[0];
    let layer1 = g_ctx.g_layers[1];
    let layer2 = g_ctx.g_layers[2];
    let layer3 = g_ctx.g_layers[3];

    let animated_tiles = [];

    // level0 
    var tile_array0 = Array.from(Array(CONFIG.leveltilewidth), () => new Array(CONFIG.leveltileheight));
    for (let x = 0; x < CONFIG.leveltilewidth; x++) {
        for (let y = 0; y < CONFIG.leveltileheight; y++) {
            tile_array0[x][y] = -1;
        }
    }
    for (var i = 0; i < layer0.container.children.length; i++) {
        var child = layer0.container.children[i];

        // check if it's an animated sprite
        if(child.hasOwnProperty('animationSpeed')){
            child.layer = 0;
            animated_tiles.push(child);
            continue;
        }

        if (!child.hasOwnProperty('index')) {
            continue;
        }
        let x_coord = child.x / g_ctx.tiledimx;
        let y_coord = child.y / g_ctx.tiledimy;

        if (typeof tile_array0[x_coord] == 'undefined'){
            console.log("**Error xcoord undefined ", x_coord);

        }
        else if (typeof tile_array0[x_coord][y_coord] == 'undefined'){
            console.log("**Error xcoord/ycoord undefined ", x_coord, y_coord);
        }else{
            tile_array0[x_coord][y_coord] = child.index;
        }
    }

    // level1 
    var tile_array1 = Array.from(Array(CONFIG.leveltilewidth), () => new Array(CONFIG.leveltileheight));
    for (let x = 0; x < CONFIG.leveltilewidth; x++) {
        for (let y = 0; y < CONFIG.leveltileheight; y++) {
            tile_array1[x][y] = -1;
        }
    }
    for (var i = 0; i < layer1.container.children.length; i++) {
        var child = layer1.container.children[i];

        // check if it's an animated sprite
        if(child.hasOwnProperty('animationSpeed')){
            child.layer = 1;
            animated_tiles.push(child);
            continue;
        }

        if (!child.hasOwnProperty('index')) {
            continue;
        }
        let x_coord = child.x / g_ctx.tiledimx;
        let y_coord = child.y / g_ctx.tiledimy;
        if (typeof tile_array1[x_coord] == 'undefined'){
            console.log("**Error xcoord undefined ", x_coord);

        }
        else if (typeof tile_array1[x_coord][y_coord] == 'undefined'){
            console.log("**Error xcoord/ycoord undefined ", x_coord, y_coord);
        }else{
            tile_array1[x_coord][y_coord] = child.index;
        }
    }

    //  object level
    var tile_array2 = Array.from(Array(CONFIG.leveltilewidth), () => new Array(CONFIG.leveltileheight));
    for (let x = 0; x < CONFIG.leveltilewidth; x++) {
        for (let y = 0; y < CONFIG.leveltileheight; y++) {
            tile_array2[x][y] = -1;
        }
    }
    for (var i = 0; i < layer2.container.children.length; i++) {
        var child = layer2.container.children[i];

        // check if it's an animated sprite
        if(child.hasOwnProperty('animationSpeed')){
            child.layer = 2;
            animated_tiles.push(child);
            continue;

        }

        if (!child.hasOwnProperty('index')) {
            continue;
        }
        let x_coord = child.x / g_ctx.tiledimx;
        let y_coord = child.y / g_ctx.tiledimy;
        if (typeof tile_array2[x_coord] == 'undefined'){
            console.log("**Error xcoord undefined ", x_coord);

        }
        else if (typeof tile_array2[x_coord][y_coord] == 'undefined'){
            console.log("**Error xcoord/ycoord undefined ", x_coord, y_coord);
        }else{
        tile_array2[x_coord][y_coord] = child.index;
        }
    }

    //  object level
    var tile_array3 = Array.from(Array(CONFIG.leveltilewidth), () => new Array(CONFIG.leveltileheight));
    for (let x = 0; x < CONFIG.leveltilewidth; x++) {
        for (let y = 0; y < CONFIG.leveltileheight; y++) {
            tile_array3[x][y] = -1;
        }
    }
    for (var i = 0; i < layer3.container.children.length; i++) {
        var child = layer3.container.children[i];

        // check if it's an animated sprite
        if(child.hasOwnProperty('animationSpeed')){
            child.layer = 3;
            animated_tiles.push(child);
            continue;

        }

        if (!child.hasOwnProperty('index')) {
            continue;
        }
        let x_coord = child.x / g_ctx.tiledimx;
        let y_coord = child.y / g_ctx.tiledimy;
        if (typeof tile_array3[x_coord] == 'undefined'){
            console.log("**Error xcoord undefined ", x_coord);

        }
        else if (typeof tile_array3[x_coord][y_coord] == 'undefined'){
            console.log("**Error xcoord/ycoord undefined ", x_coord, y_coord);
        }else{
        tile_array3[x_coord][y_coord] = child.index;
        }
    }


    write_map_file(tile_array0, tile_array1, tile_array2, tile_array3, animated_tiles);
}