From 4a8127146eafd4fef5b478c1bf9f5a152c8c4cd5 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Thu, 22 Jun 2023 22:10:28 +0100 Subject: Import entities from the map and spawn them --- data/stage.json | 26 ++++++++++++++++++++++++-- src/game.c | 1 - src/map.c | 15 +++++++++++++++ src/map.h | 5 +++++ src/player.c | 4 ++-- src/player.h | 2 +- tools/map.py | 37 +++++++++++++++++++++++++++++++++++-- 7 files changed, 82 insertions(+), 8 deletions(-) diff --git a/data/stage.json b/data/stage.json index ca58a1d..1e348c1 100644 --- a/data/stage.json +++ b/data/stage.json @@ -25,9 +25,31 @@ "width":40, "x":0, "y":0 + }, + { + "draworder":"topdown", + "id":3, + "name":"Entities", + "objects":[ + { + "height":16, + "id":1, + "name":"Player", + "rotation":0, + "type":"", + "visible":true, + "width":16, + "x":16, + "y":128 + }], + "opacity":1, + "type":"objectgroup", + "visible":true, + "x":0, + "y":0 }], - "nextlayerid":3, - "nextobjectid":1, + "nextlayerid":4, + "nextobjectid":2, "orientation":"orthogonal", "renderorder":"right-down", "tiledversion":"1.7.2", diff --git a/src/game.c b/src/game.c index 9878871..ddd9e63 100644 --- a/src/game.c +++ b/src/game.c @@ -82,7 +82,6 @@ void run_game() map_init(binary_stage_start); map_render(); - player_init(16, 128); player_draw(); timer_start(GAME_TIME_MAX, &clock_updated); diff --git a/src/map.c b/src/map.c index 9908384..4c5390f 100644 --- a/src/map.c +++ b/src/map.c @@ -4,6 +4,8 @@ #include "vga.h" #include "data.h" +#include "player.h" + #include "map.h" /* current map; set via map_init */ @@ -24,6 +26,19 @@ void map_init(const uint8_t map[]) /* gold is not 0xff */ if (gold[i] != 0xff) total_gold++; + + /* spawn entities, 0xff is the list terminator */ + for ( + const uint8_t *ent = map + MAP_W * MAP_H * 2; + *ent != 0xff; + ent += 4 + ) + switch (*ent) + { + case Player: + player_init(ent[1] * MAP_TILE_W, ent[2] * MAP_TILE_H, ent[3] & 1); + break; + } } void map_render() diff --git a/src/map.h b/src/map.h index 42f8da7..992c982 100644 --- a/src/map.h +++ b/src/map.h @@ -14,6 +14,11 @@ #define MAP_FIRST_BLOCKED 40 #define MAP_FIRST_DEADLY 100 +typedef enum +{ + Player = 0, +} EntityType; + void map_init(const uint8_t map[]); void map_render(); diff --git a/src/player.c b/src/player.c index 52595f4..a0b840f 100644 --- a/src/player.c +++ b/src/player.c @@ -69,9 +69,9 @@ static const Rect frames[2][6] = }, }; -void player_init(uint16_t start_x, uint8_t start_y) +void player_init(uint16_t start_x, uint8_t start_y, uint8_t start_dir) { - dir = DIR_RIGHT; + dir = start_dir; frame = FRAME_STANDING; delay = 0; jump = 0; diff --git a/src/player.h b/src/player.h index 56bbd22..a2a946c 100644 --- a/src/player.h +++ b/src/player.h @@ -1,7 +1,7 @@ #ifndef _PLAYER_H #define _PLAYER_H -void player_init(uint16_t start_x, uint8_t start_y); +void player_init(uint16_t start_x, uint8_t start_y, uint8_t start_dir); void player_update(); void player_erase(); diff --git a/tools/map.py b/tools/map.py index 5522a9e..7e0210f 100755 --- a/tools/map.py +++ b/tools/map.py @@ -10,12 +10,23 @@ __version__ = "1.0" ld = os.environ.get("LD", "i586-pc-msdosdjgpp-ld") strip = os.environ.get("STRIP", "i586-pc-msdosdjgpp-strip") +entity_types = ("Player",) + def get_layer(data, name): for layer in data["layers"]: if layer["name"] == name: return layer - raise ValueError("Layer %s not found" % name) + raise ValueError("Layer %r not found" % name) + + +def get_prop(data, name, default): + if "properties" not in data: + return default + for prop in data["properties"]: + if prop["name"] == name: + return prop["value"] + return default def main(): @@ -55,7 +66,29 @@ def main(): out.extend(map(lambda x: (x - tileset["firstgid"]) & 0xFF, gold_layer["data"])) - # TODO: process map entities + entity_layer = get_layer(data, "Entities") + + for ent in entity_layer["objects"]: + try: + typ = entity_types.index(ent["name"]) + except ValueError: + parser.error("Entity in map %r not found in entity types" % ent["name"]) + + x = ent["x"] // tileset["tilewidth"] + y = ent["y"] // tileset["tileheight"] + + flags = 0 + + dir = get_prop(ent, "dir", "right") + if dir not in ("left", "right"): + parser.error("Invalid value for property dir: %r" % dir) + if dir == "left": + flags |= 1 + + out.extend([typ, x, y, flags]) + + # end of entity list + out.append(0xFF) tmp = args.output.rstrip(".o") with open(tmp, "wb") as fd: -- cgit v1.2.3