aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--assets/maps/debug.tmx13
-rw-r--r--assets/tiled/dungeon crawler.tiled-session4
-rw-r--r--include/map.h9
-rw-r--r--include/marker.h10
-rw-r--r--src/map.c38
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;
+
+
diff --git a/src/map.c b/src/map.c
index 398b721..468024f 100644
--- a/src/map.c
+++ b/src/map.c
@@ -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;