Started entity parser
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
import sys
|
||||
from constants import CHUNK_WIDTH, CHUNK_HEIGHT, CHUNK_TILE_COUNT, TILE_WIDTH_HEIGHT
|
||||
from entityParser import parseEntity
|
||||
import math
|
||||
|
||||
def parseChunkLayerData(layer, mapData, chunkData):
|
||||
@ -101,7 +102,11 @@ def parseChunk(chunkX, chunkY, mapData):
|
||||
if ob['y'] >= (chunkData['topLeftTileY'] + CHUNK_HEIGHT) * TILE_WIDTH_HEIGHT:
|
||||
continue
|
||||
|
||||
chunkData['entities'].append(ob)
|
||||
ent = parseEntity(ob, chunkData)
|
||||
if ent is None:
|
||||
continue
|
||||
|
||||
chunkData['entities'].append(ent)
|
||||
|
||||
return chunkData
|
||||
|
||||
|
14
tools/mapcompile/entityParser.py
Normal file
14
tools/mapcompile/entityParser.py
Normal file
@ -0,0 +1,14 @@
|
||||
from constants import TILE_WIDTH_HEIGHT, ENTITY_TYPE_MAP
|
||||
|
||||
def parseEntity(obj, chunkData):
|
||||
if 'type' in obj and obj['type'] not in ENTITY_TYPE_MAP:
|
||||
return None
|
||||
|
||||
obj['localX'] = round(obj['x'] - (chunkData['topLeftTileX'] * TILE_WIDTH_HEIGHT))
|
||||
obj['localY'] = round(obj['y'] - (chunkData['topLeftTileY'] * TILE_WIDTH_HEIGHT))
|
||||
obj['dir'] = 'ENTITY_DIR_SOUTH'
|
||||
obj['type'] = 'ENTITY_TYPE_NPC'
|
||||
|
||||
print(obj)
|
||||
|
||||
return obj
|
@ -5,8 +5,8 @@ import math
|
||||
from helper import floatToFixed248
|
||||
from inputParser import parseInputFile
|
||||
from mapParser import parseMap
|
||||
from chunkParser import chunkGetLocalTileX, chunkGetLocalTileY, chunkGetTileIndex, chunkGetOutputTileIndex, parseChunk
|
||||
from constants import CHUNK_WIDTH, CHUNK_HEIGHT, TILE_WIDTH_HEIGHT, ENTITY_TYPE_MAP, CHUNK_TILE_COUNT
|
||||
from chunkParser import parseChunk
|
||||
from constants import CHUNK_WIDTH, CHUNK_HEIGHT
|
||||
|
||||
# Check if the script is run with the correct arguments
|
||||
parser = argparse.ArgumentParser(description="Generate chunk header files")
|
||||
@ -79,19 +79,13 @@ for chunkY in range(mapData['mapHeightInRealChunks']):
|
||||
|
||||
f.write(f" .entities = {{\n")
|
||||
for entity in chunkData['entities']:
|
||||
# Entities are center aligned in tiled.
|
||||
localX = round(entity['x'] - (chunkData['topLeftTileX'] * TILE_WIDTH_HEIGHT))
|
||||
localY = round(entity['y'] - (chunkData['topLeftTileY'] * TILE_WIDTH_HEIGHT))
|
||||
|
||||
if 'type' in entity and entity['type'] not in ENTITY_TYPE_MAP:
|
||||
continue
|
||||
|
||||
f.write(" {\n")
|
||||
f.write(f" .id = {entity['id']},\n")
|
||||
f.write(f" .type = ENTITY_TYPE_NPC,\n")
|
||||
f.write(f" .x = {localX},\n")
|
||||
f.write(f" .y = {localY},\n")
|
||||
f.write(f" .dir = ENTITY_DIR_SOUTH,\n")
|
||||
f.write(f" .type = {entity['type']},\n")
|
||||
f.write(f" .x = {entity['localX']},\n")
|
||||
f.write(f" .y = {entity['localY']},\n")
|
||||
f.write(f" .dir = {entity['dir']},\n")
|
||||
f.write(" },\n")
|
||||
f.write(f" }},\n")
|
||||
|
||||
@ -106,12 +100,18 @@ with open(headerPath, 'w') as f:
|
||||
|
||||
# Now, for each chunk, include its header file
|
||||
for (x, y) in chunksDone:
|
||||
chunk_header = f"world/chunk/chunk_{x}_{y}.h"
|
||||
f.write(f"#include \"{chunk_header}\"\n")
|
||||
f.write(f"#include \"world/chunk/chunk_{x}_{y}.h\"\n")
|
||||
|
||||
f.write("\n")
|
||||
f.write(f"#define WORLD_WIDTH {worldWidth}\n")
|
||||
f.write(f"#define WORLD_HEIGHT {worldHeight}\n\n")
|
||||
f.write(f"#define WORLD_HEIGHT {worldHeight}\n")
|
||||
|
||||
# Write out other global variables.
|
||||
f.write(f"#define WORLD_PLAYER_SPAWN_X ((fixed248_t){floatToFixed248(mapData['playerSpawnX'])})\n")
|
||||
f.write(f"#define WORLD_PLAYER_SPAWN_Y ((fixed248_t){floatToFixed248(mapData['playerSpawnY'])})\n")
|
||||
|
||||
f.write("\n")
|
||||
|
||||
f.write(f"static const chunkdata_t* WORLD_CHUNKS[] = {{\n")
|
||||
for i in range(worldHeight):
|
||||
f.write(" ")
|
||||
@ -122,7 +122,5 @@ with open(headerPath, 'w') as f:
|
||||
f.write("NULL, ")
|
||||
f.write("\n")
|
||||
f.write("};\n\n")
|
||||
f.write(f"#define WORLD_PLAYER_SPAWN_X ((fixed248_t){floatToFixed248(mapData['playerSpawnX'])})\n")
|
||||
f.write(f"#define WORLD_PLAYER_SPAWN_Y ((fixed248_t){floatToFixed248(mapData['playerSpawnY'])})\n")
|
||||
|
||||
print(f"chunks.h generated at: {headerPath}")
|
Reference in New Issue
Block a user