diff options
| author | Leander Scherer <leander@schererleander.de> | 2026-03-12 01:34:36 +0100 |
|---|---|---|
| committer | Leander Scherer <leander@schererleander.de> | 2026-03-12 01:34:36 +0100 |
| commit | ae6ac413632b427e31a26020a94864669af1ba10 (patch) | |
| tree | a726873ee5ab9a6a96b9d1736e178d23d3fe96c2 | |
| parent | e1a8ca7396d0931bcd10d8f136502f9843e30ff3 (diff) | |
feat(marker): implement map markers
| -rw-r--r-- | assets/maps/debug.tmx | 13 | ||||
| -rw-r--r-- | assets/tiled/dungeon crawler.tiled-session | 4 | ||||
| -rw-r--r-- | include/map.h | 9 | ||||
| -rw-r--r-- | include/marker.h | 10 | ||||
| -rw-r--r-- | src/map.c | 38 |
5 files changed, 60 insertions, 14 deletions
diff --git a/assets/maps/debug.tmx b/assets/maps/debug.tmx index be4885e..9b5af3b 100644 --- a/assets/maps/debug.tmx +++ b/assets/maps/debug.tmx @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="UTF-8"?> -<map version="1.10" tiledversion="1.11.2" orientation="orthogonal" renderorder="right-down" width="30" height="30" tilewidth="16" tileheight="16" infinite="0" nextlayerid="14" nextobjectid="24"> +<map version="1.10" tiledversion="1.11.2" orientation="orthogonal" renderorder="right-down" width="30" height="30" tilewidth="16" tileheight="16" infinite="0" nextlayerid="15" nextobjectid="25"> <tileset firstgid="1" source="../tilesets/grounds.tsx"/> <tileset firstgid="209" source="../tilesets/walls.tsx"/> <tileset firstgid="433" source="../tilesets/animated_props.tsx"/> @@ -109,15 +109,16 @@ </layer> <objectgroup id="11" name="Props"/> <objectgroup id="12" name="Items"> - <object id="23" gid="525" x="192" y="208" width="16" height="16"> - <properties> - <property name="unique_id" value="skeleton_key"/> - </properties> - </object> + <object id="23" name="skeleton_key" gid="525" x="192" y="208" width="16" height="16"/> </objectgroup> <objectgroup id="13" name="Pickups"> <object id="20" gid="521" x="192" y="240" width="16" height="16"/> <object id="21" gid="537" x="256" y="240" width="16" height="16"/> <object id="22" gid="529" x="224" y="272" width="16" height="16"/> </objectgroup> + <objectgroup id="14" name="Markers"> + <object id="24" name="player_spawn" x="240" y="208"> + <point/> + </object> + </objectgroup> </map> diff --git a/assets/tiled/dungeon crawler.tiled-session b/assets/tiled/dungeon crawler.tiled-session index feb12b6..2e8d537 100644 --- a/assets/tiled/dungeon crawler.tiled-session +++ b/assets/tiled/dungeon crawler.tiled-session @@ -44,9 +44,9 @@ }, "/home/schererleander/Developer/dungeon/assets/maps/debug.tmx": { "scale": 8, - "selectedLayer": 5, + "selectedLayer": 4, "viewCenter": { - "x": 241.625, + "x": 241.875, "y": 233.1875 } }, diff --git a/include/map.h b/include/map.h index 6106dc1..d1587fa 100644 --- a/include/map.h +++ b/include/map.h @@ -3,11 +3,18 @@ #include "raylib.h" #include "raytmx.h" +#include "marker.h" + +#define MAX_MARKERS 25 + typedef struct { TmxMap *map; TmxLayer *wallLayer; TmxLayer *groundLayer; TmxLayer *decorLayer; + + Marker markers[MAX_MARKERS]; + int markersCount; } Map; typedef struct Entities Entities; @@ -20,4 +27,6 @@ void DrawMap(Map *map); void DrawPickups(Entities *entities, Map *map); void DrawKeys(Entities *entities, Map *map); +Marker *FindMarker(Map *map, const char *name); + void UnloadMap(Map *map); diff --git a/include/marker.h b/include/marker.h new file mode 100644 index 0000000..5a6e302 --- /dev/null +++ b/include/marker.h @@ -0,0 +1,10 @@ +#pragma once + +#include "raylib.h" + +typedef struct { + Vector2 position; + const char* name; +} Marker; + + @@ -35,8 +35,8 @@ static PickupType GetPickupTypeByTileId(int tileId) { return PICKUP_COIN; } -static Rectangle GetTileBounds(TmxTileset *tileset, uint32_t tileId, float worldX, - float worldY) { +static Rectangle GetTileBounds(TmxTileset *tileset, uint32_t tileId, + float worldX, float worldY) { for (uint32_t i = 0; i < tileset->tilesLength; i++) { if (tileset->tiles[i].id == tileId) { TmxObjectGroup *og = &tileset->tiles[i].objectGroup; @@ -97,12 +97,24 @@ static void LoadKeys(TmxMap *tmxMap, TmxLayer *layer, Entities *entities) { Key *k = &entities->keys[entities->keysCount++]; k->position = (Vector2){x, y}; k->bounds = GetTileBounds(tileset, tileId, x, y); - k->uniqueId = (int)object->id; + k->uniqueId = object->name; k->active = true; k->gid = (uint32_t)object->gid; } } +static void LoadMarkers(Map *map, TmxLayer *layer) { + TmxObjectGroup *group = &layer->exact.objectGroup; + for (uint32_t i = 0; i < group->objectsLength; i++) { + TmxObject *object = &group->objects[i]; + if (object->type != OBJECT_TYPE_POINT || map->markersCount >= MAX_MARKERS) + continue; + + map->markers[map->markersCount++] = + (Marker){.position = {object->x, object->y}, .name = object->name}; + } +} + Map *LoadMap(const char *filename, Entities *entities) { Map *map = (Map *)malloc(sizeof(Map)); if (!map) @@ -128,6 +140,10 @@ Map *LoadMap(const char *filename, Entities *entities) { LoadKeys(map->map, itemsLayer, entities); } + TmxLayer *markersLayer = FindLayerByName(map->map, "Markers"); + if (markersLayer) + LoadMarkers(map, markersLayer); + return map; } @@ -147,9 +163,12 @@ void DrawMap(Map *map) { TmxLayer layers[3]; uint32_t count = 0; - if (map->groundLayer) layers[count++] = *map->groundLayer; - if (map->wallLayer) layers[count++] = *map->wallLayer; - if (map->decorLayer) layers[count++] = *map->decorLayer; + if (map->groundLayer) + layers[count++] = *map->groundLayer; + if (map->wallLayer) + layers[count++] = *map->wallLayer; + if (map->decorLayer) + layers[count++] = *map->decorLayer; DrawTMXLayers(map->map, NULL, NULL, layers, count, 0, 0, WHITE); } @@ -189,6 +208,13 @@ void DrawKeys(Entities *entities, Map *map) { } } +Marker *FindMarker(Map *map, const char *name) { + for (int i = 0; i < map->markersCount; i++) + if (strcmp(map->markers[i].name, name) == 0) + return &map->markers[i]; + return NULL; +} + void UnloadMap(Map *map) { if (!map) return; |
