Add rendering for all tiles and entities

main
En Yi 2025-01-20 21:47:09 +08:00
parent b51d505814
commit ee43a87f8a
1 changed files with 62 additions and 38 deletions

View File

@ -44,39 +44,66 @@ ENUMIDS_TILETYPE_MAPPING = {
'Urchin': 25, 'Urchin': 25,
} }
DANGER_COLOUR = (239,79,81,255)
WCRATE_COLOUR = (160,117,48,255)
MCRATE_COLOUR = (110,110,110,255)
REC_DRAW_FUNCTION = lambda ctx,x,y,s,c : ctx.rectangle(((x,y), (x+s-1, y+s-1)), c) REC_DRAW_FUNCTION = lambda ctx,x,y,s,c : ctx.rectangle(((x,y), (x+s-1, y+s-1)), c)
UPHALFREC_DRAW_FUNCTION = lambda ctx,x,y,s,c : ctx.rectangle(((x,y), (x+s-1, y+s//2-1)), c) UPHALFREC_DRAW_FUNCTION = lambda ctx,x,y,s,c : ctx.rectangle(((x,y), (x+s-1, y+s//2-1)), c)
DOWNHALFREC_DRAW_FUNCTION = lambda ctx,x,y,s,c : ctx.rectangle(((x, y+s//2), (x+s-1,y+s-1)), c) DOWNHALFREC_DRAW_FUNCTION = lambda ctx,x,y,s,c : ctx.rectangle(((x, y+s//2), (x+s-1,y+s-1)), c)
LEFTHALFREC_DRAW_FUNCTION = lambda ctx,x,y,s,c : ctx.rectangle(((x,y), (x+s//2-1, y+s-1)), c) LEFTHALFREC_DRAW_FUNCTION = lambda ctx,x,y,s,c : ctx.rectangle(((x,y), (x+s//2-1, y+s-1)), c)
RIGHTHALFREC_DRAW_FUNCTION = lambda ctx,x,y,s,c : ctx.rectangle(((x+s//2,y), (x+s-1, y+s-1)), c) RIGHTHALFREC_DRAW_FUNCTION = lambda ctx,x,y,s,c : ctx.rectangle(((x+s//2,y), (x+s-1, y+s-1)), c)
CIRCLE_DRAW_FUNCTION = lambda ctx,x,y,s,c : ctx.circle((x+s//2, y+s//2), s//2, c) CIRCLE_DRAW_FUNCTION = lambda ctx,x,y,s,c : ctx.circle((x+s//2, y+s//2), s//2, c)
TRIANGLE_DRAW_FUNCTION = lambda ctx,x,y,s,c : ctx.regular_polygon(((x+s//2, y+s//2), s//2), 3, 0, c)
def draw_bomb_tile(ctx, x, y, s, c):
REC_DRAW_FUNCTION(ctx, x, y, s, c)
ctx.line((x,y,x+s-1,y+s-1), DANGER_COLOUR, 1)
ctx.line((x,y+s-1,x+s-1,y), DANGER_COLOUR, 1)
def draw_arrow_tile(ctx, x, y, s, c, d):
REC_DRAW_FUNCTION(ctx, x, y, s, c)
if d == 0:
ctx.line((x+s//2,y+s//2,x+s//2,y), DANGER_COLOUR, 1)
elif d == 1:
ctx.line((x+s//2,y+s//2,x+s-1,y+s//2), DANGER_COLOUR, 1)
elif d == 2:
ctx.line((x+s//2,y+s//2,x+s//2,y+s-1), DANGER_COLOUR, 1)
elif d == 3:
ctx.line((x+s//2,y+s//2,x,y+s//2), DANGER_COLOUR, 1)
def draw_urchin_tile(ctx, x, y, s, c):
ctx.line((x,y,x+s-1,y+s-1), c, 1)
ctx.line((x,y+s-1,x+s-1,y), c, 1)
ctx.line((x+(s-1)//2,y,x+(s-1)//2,y+s-1), c, 1)
ctx.line((x,y+(s-1)//2,x+s-1,y+(s-1)//2), c, 1)
TILETYPE_SHAPE_MAP = { TILETYPE_SHAPE_MAP = {
'Solid': (REC_DRAW_FUNCTION, (0,0,0,255)), 'Solid': (REC_DRAW_FUNCTION, (0,0,0,255)),
'WoodenPlat': (UPHALFREC_DRAW_FUNCTION, (128,64,0,255)), 'WoodenPlat': (UPHALFREC_DRAW_FUNCTION, (128,64,0,255)),
'Ladder': (REC_DRAW_FUNCTION, (214,141,64,255)), 'Ladder': (REC_DRAW_FUNCTION, (214,141,64,255)),
'LSpike': (LEFTHALFREC_DRAW_FUNCTION, (239,79,81,255)), 'LSpike': (LEFTHALFREC_DRAW_FUNCTION, DANGER_COLOUR),
'RSpike': (RIGHTHALFREC_DRAW_FUNCTION, (239,79,81,255)), 'RSpike': (RIGHTHALFREC_DRAW_FUNCTION, DANGER_COLOUR),
'USpike': (UPHALFREC_DRAW_FUNCTION, (239,79,81,255)), 'USpike': (UPHALFREC_DRAW_FUNCTION, DANGER_COLOUR),
'DSpike': (DOWNHALFREC_DRAW_FUNCTION, (239,79,81,255)), 'DSpike': (DOWNHALFREC_DRAW_FUNCTION, DANGER_COLOUR),
'EmptyWCrate': (REC_DRAW_FUNCTION, (160,117,48,255)), 'EmptyWCrate': (REC_DRAW_FUNCTION, WCRATE_COLOUR),
#'LArrowWCrate': 9, 'LArrowWCrate': (lambda ctx,x,y,s,c : draw_arrow_tile(ctx,x,y,s,c,3), WCRATE_COLOUR),
#'RArrowWCrate': 10, 'RArrowWCrate': (lambda ctx,x,y,s,c : draw_arrow_tile(ctx,x,y,s,c,1), WCRATE_COLOUR),
#'UArrowWCrate': 11, 'UArrowWCrate': (lambda ctx,x,y,s,c : draw_arrow_tile(ctx,x,y,s,c,0), WCRATE_COLOUR),
#'DArrowWCrate': 12, 'DArrowWCrate': (lambda ctx,x,y,s,c : draw_arrow_tile(ctx,x,y,s,c,2), WCRATE_COLOUR),
#'BombWCrate': 13, 'BombWCrate': (draw_bomb_tile, WCRATE_COLOUR),
'EmptyMCrate': (REC_DRAW_FUNCTION, (225,225,225,255)), 'EmptyMCrate': (REC_DRAW_FUNCTION, MCRATE_COLOUR),
#'LArrowMCrate': 15, 'LArrowMCrate': (lambda ctx,x,y,s,c : draw_arrow_tile(ctx,x,y,s,c,3), MCRATE_COLOUR),
#'RArrowMCrate': 16, 'RArrowMCrate': (lambda ctx,x,y,s,c : draw_arrow_tile(ctx,x,y,s,c,1), MCRATE_COLOUR),
#'UArrowMCrate': 17, 'UArrowMCrate': (lambda ctx,x,y,s,c : draw_arrow_tile(ctx,x,y,s,c,0), MCRATE_COLOUR),
#'DArrowMCrate': 18, 'DArrowMCrate': (lambda ctx,x,y,s,c : draw_arrow_tile(ctx,x,y,s,c,2), MCRATE_COLOUR),
#'BombMCrate': 19, 'BombMCrate': (draw_bomb_tile, MCRATE_COLOUR),
'Boulder': (CIRCLE_DRAW_FUNCTION, (45,45,45,255)), 'Boulder': (CIRCLE_DRAW_FUNCTION, (45,45,45,255)),
#'Runner': 21, 'Runner': (TRIANGLE_DRAW_FUNCTION, (0,0,128,255)),
'Player': (REC_DRAW_FUNCTION, (255,0,255,255)), 'Player': (REC_DRAW_FUNCTION, (255,0,255,255)),
'Chest': (REC_DRAW_FUNCTION, (255,255,0,255)), 'Chest': (REC_DRAW_FUNCTION, (255,255,0,255)),
'Exit': (REC_DRAW_FUNCTION, (0,255,0,255)), 'Exit': (REC_DRAW_FUNCTION, (0,255,0,255)),
#'Urchin': 25, 'Urchin': (draw_urchin_tile, DANGER_COLOUR),
} }
# First go to tilesets and find Simple_tiles identifier, then find enumTags to identifier which tile type is what tileid # First go to tilesets and find Simple_tiles identifier, then find enumTags to identifier which tile type is what tileid
@ -126,7 +153,7 @@ converted_filename = '.'.join(fileparts)
TILE_SIZE = 8 TILE_SIZE = 8
# Each level should be packed as: [width, 2 bytes][height, 2 bytes][tile_type,entity,water,padding 1,1,1,1 bytes][tile_type,entity,water,padding 1,1,1,1 bytes]... # Each level should be packed as: [width, 2 bytes][height, 2 bytes][tile_type,entity,water,padding 1,1,1,1 bytes][tile_type,entity,water,padding 1,1,1,1 bytes]...
# Then loop the levels. Read the layerIndstances # Then loop the levels. Read the layerIndstances
for level in all_levels[17:18]: for level in all_levels[13:15]:
n_chests : int = 0 n_chests : int = 0
# Search for __identifier for the level layout # Search for __identifier for the level layout
level_name = "" level_name = ""
@ -176,27 +203,24 @@ for level in all_levels[17:18]:
print("Error on tile", x, y) print("Error on tile", x, y)
render_ctx.circle((x,y), 2,(255,0,0,255)) render_ctx.circle((x,y), 2,(255,0,0,255))
print(e) print(e)
lvl_render.show()
#for i, water_level in enumerate(water_layout["intGridCsv"]): #for i, water_level in enumerate(water_layout["intGridCsv"]):
# tiles_info[i][2] = water_level # tiles_info[i][2] = water_level
## Subject to change ## Subject to change
#for ent in entity_layout["entityInstances"]: for ent in entity_layout["entityInstances"]:
# x,y = ent["__grid"] x,y = ent["__grid"]
# tiles_info[y*width + x][0] = ENUMIDS_TILETYPE_MAPPING[ent["__identifier"]] x *= TILE_SIZE
# if ent["__identifier"] == "Urchin": y *= TILE_SIZE
# spd_encoding = 0 ent = ent["__identifier"]
# for urchin_data in ent['fieldInstances']: try:
# if urchin_data["__identifier"] == "Direction": if ent in TILETYPE_SHAPE_MAP:
# spd_encoding |= urchin_data["__value"] << 2 draw_func, colour = TILETYPE_SHAPE_MAP[ent]
# elif urchin_data["__identifier"] == "SpeedLevel": draw_func(render_ctx, x, y, TILE_SIZE, colour)
# spd_encoding |= urchin_data["__value"] else:
print(ent, "is not mapped");
# tiles_info[y*width + x][0] += spd_encoding except Exception as e:
print("Error on tile", x, y)
render_ctx.circle((x,y), 2,(255,0,0,255))
#for y in range(height): print(e)
# for x in range(width):
# print(tiles_info[y*width + x], end=" ")
# print()
lvl_render.show()