rewrite converters.py to add unlimited layers

This commit is contained in:
bgiraudr 2021-08-18 02:10:31 +02:00
parent 0f16d9545e
commit bc6cfde444
5 changed files with 34 additions and 33 deletions

View File

@ -28,9 +28,8 @@ def convert_map(input, output, params, target):
tileset.close()
tile_value = {}
info_map = bytearray()
#create a dictionnary between tile-type
#create a dictionnary between tile id-type
for i in data_tileset["tiles"]:
try:
id = i["id"]+1
@ -61,33 +60,36 @@ def convert_map(input, output, params, target):
o += fxconv.u32(w) + fxconv.u32(h) + fxconv.u32(nblayer)
o += fxconv.ref("img_tileset")
#To preserve performance, max 2 layers
if(nblayer <= 2):
#generate the array of tiles from the layer
#generation of the collision map (take the maximum of the layer except for bridges)
#bridges are always walkable
info_map = bytes()
maxValue = 0
bridge = False
for x in range(w*h):
for i in range(nblayer):
tiles = data["layers"][i]["data"]
value = tile_value.get(data["layers"][i]["data"][x])
if value == None: value = TILE_AIR
if value > maxValue: maxValue = value
if value == TILE_BRIDGE:
maxValue = TILE_AIR
bridge = True
if bridge:
if value != TILE_AIR:
maxValue = value
info_map += fxconv.u16(maxValue)
maxValue = 0
bridge = False
o += fxconv.ref(info_map)
byte_tiles = bytearray()
for j in tiles:
byte_tiles += bytearray(fxconv.u16(j))
o += fxconv.ref(byte_tiles)
#generate the array of tiles from the layer
for layer in data["layers"]:
layer_data = bytes()
#generation of the collision map (take the maximum of the layer except for bridges)
#bridges is always walkable
for x in range(w*h):
value1 = tile_value.get(data["layers"][0]["data"][x])
if(nblayer >= 2):
value2 = tile_value.get(data["layers"][1]["data"][x])
if value1 == None: value1 = TILE_AIR
if value2 == None: value2 = TILE_AIR
if value2 == TILE_BRIDGE: value1 = value2 = TILE_AIR
info_map += bytearray(fxconv.u16(max(value1, value2)))
else:
if value1 == TILE_BRIDGE: value1 = TILE_AIR
info_map += bytearray(fxconv.u16(value1))
o += fxconv.ref(info_map)
else:
raise fxconv.FxconvError(f"There is too much layer ! {nblayer} found for a max of 2")
for tile in layer["data"]:
layer_data += fxconv.u16(tile)
o += fxconv.ref(layer_data)
#generate !
fxconv.elf(o, output, "_" + params["name"], **target)

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.5" tiledversion="1.7.2" orientation="orthogonal" renderorder="right-down" width="56" height="38" tilewidth="16" tileheight="16" infinite="0" nextlayerid="8" nextobjectid="25">
<map version="1.5" tiledversion="1.7.2" orientation="orthogonal" renderorder="right-down" width="56" height="38" tilewidth="16" tileheight="16" infinite="0" nextlayerid="9" nextobjectid="25">
<editorsettings>
<export target="testCarte.json" format="json"/>
</editorsettings>

View File

@ -8,12 +8,10 @@ struct Map {
int w, h, nb_layer;
/*the tileset to use*/
bopti_image_t *tileset;
/*list of all background tiles (layer 1)*/
short *tiles_layer1;
/*list of all "physical" tiles (layer 2)*/
short *tiles_layer2;
/*state of each tile on the map (solid, air ...)*/
short *info_map;
/*list of all the tiles*/
short *layers[];
};
enum map_state {

View File

@ -70,8 +70,7 @@ void engine_draw_map_around_player(struct Game const *game) {
int y = j + (posy-ycentre);
for(int layer = 0; layer < game->map->nb_layer; layer++) {
int tile;
tile = layer == 0 ? game->map->tiles_layer1[i] - 1 : game->map->tiles_layer2[i] - 1;
int tile = game->map->layers[layer][i]-1;
if(tile != -1) {
int tile_x = tile % TILESET_WIDTH;
int tile_y = tile / TILESET_WIDTH;

View File

@ -7,9 +7,11 @@ int map_walkable(struct Map const *map, int x, int y) {
int tile = map->info_map[x + map->w * y];
if(x < 0 || x > map->w-1 || y < 0 || y > map->h-1) return 0;
return (tile != TILE_SOLID && tile != TILE_CHARACTER);
return 1;
}
/*get the tile under the player*/
int map_get_player_tile(struct Game const *game) {
return game->map->info_map[game->player->x + game->map->w * game->player->y];
return 0;
}