Add support for objects with tiles
This commit is contained in:
@@ -88,6 +88,11 @@ static void handleTileObjectLayer(BzTileObjectGroup *layer, cute_tiled_layer_t *
|
||||
object = cuteLayer->objects;
|
||||
for (i32 i = 0; i < layer->objectCount; i++) {
|
||||
layer->objects[i].id = hashFunc(object->name.ptr);
|
||||
|
||||
layer->objects[i].gid = object->gid;
|
||||
if (layer->minGID > object->gid) layer->minGID = object->gid;
|
||||
else if (layer->maxGID < object->gid) layer->maxGID = object->gid;
|
||||
|
||||
layer->objects[i].shape = bzCuteObjectToTileShape(object);
|
||||
object = object->next;
|
||||
}
|
||||
@@ -142,7 +147,7 @@ static void createColliders(BzTileMap *map) {
|
||||
BzTileMap bzTileMapCreate(const BzTileMapDesc *desc) {
|
||||
BzTileMap map = {};
|
||||
// Auto detect tileset count.
|
||||
for (i32 i = 0; i < BZ_MAX_MAP_TILESETS; i++) {
|
||||
for (i32 i = 0; i < BZ_MAP_MAX_TILESETS; i++) {
|
||||
if (!desc->tilesets[i].isValid)
|
||||
break;
|
||||
map.tilesetCount++;
|
||||
@@ -162,12 +167,12 @@ BzTileMap bzTileMapCreate(const BzTileMapDesc *desc) {
|
||||
|
||||
cute_tiled_layer_t *cuteLayer = cuteMap->layers;
|
||||
while (cuteLayer) {
|
||||
BZ_ASSERT(map.layerCount < BZ_MAX_MAP_LAYERS);
|
||||
BZ_ASSERT(map.objectGroupCount < BZ_MAX_MAP_LAYERS);
|
||||
BZ_ASSERT(map.layerCount < BZ_MAP_MAX_LAYERS);
|
||||
BZ_ASSERT(map.objectGroupCount < BZ_MAP_MAX_LAYERS);
|
||||
|
||||
// Find slot
|
||||
i32 slot = -1;
|
||||
for (i32 i = 0; i < BZ_MAX_MAP_LAYERS; i++) {
|
||||
for (i32 i = 0; i < BZ_MAP_MAX_LAYERS; i++) {
|
||||
const BzTileLayerDesc *layerDesc = desc->layers + i;
|
||||
const BzTileObjectsDesc *objectsDesc = desc->objectGroups + i;
|
||||
if (layerDesc->name && strcmp(layerDesc->name, cuteLayer->name.ptr) == 0 ||
|
||||
@@ -216,17 +221,29 @@ BzTileMap bzTileMapCreate(const BzTileMapDesc *desc) {
|
||||
}
|
||||
|
||||
// Assign tilesets to layers
|
||||
for (i32 i = 0; i < map.layerCount; i++) {
|
||||
for (i32 i = 0; i < BZ_MAP_MAX_LAYERS; i++) {
|
||||
BzTileLayer *layer = map.layers + i;
|
||||
layer->tilesetIdx = -1;
|
||||
BzTileObjectGroup *objects = map.objectGroups + i;
|
||||
objects->tilesetIdx = -1;
|
||||
|
||||
bool hasLayer = i < map.layerCount;
|
||||
bool hasObjects = i < map.objectGroupCount;
|
||||
|
||||
for (i32 j = map.tilesetCount - 1; j >= 0; j--) {
|
||||
BzTileset *tileset = map.tilesets + j;
|
||||
|
||||
if (tileset->startID >= layer->minData &&
|
||||
if (hasLayer && tileset->startID >= layer->minData &&
|
||||
tileset->startID <= layer->maxData) {
|
||||
layer->tilesetIdx = j;
|
||||
break;
|
||||
hasLayer = false;
|
||||
}
|
||||
if (hasObjects && tileset->startID >= objects->minGID &&
|
||||
tileset->startID <= objects->maxGID) {
|
||||
objects->tilesetIdx = j;
|
||||
hasObjects = false;
|
||||
}
|
||||
if (!hasLayer && !hasObjects) break;
|
||||
}
|
||||
}
|
||||
cute_tiled_free_map(cuteMap);
|
||||
@@ -316,10 +333,11 @@ static void drawLayer(BzTileLayer *layer, BzTileset *tileset) {
|
||||
|
||||
}
|
||||
|
||||
static void drawObjectLayer(BzTileObjectGroup *objectLayer) {
|
||||
static void drawObjectLayer(BzTileObjectGroup *objectLayer, BzTileset *tileset) {
|
||||
Color color = ORANGE;
|
||||
for (int i = 0; i < objectLayer->objectCount; i++) {
|
||||
BzTileShape shape = objectLayer->objects[i].shape;
|
||||
BzTileObject object = objectLayer->objects[i];
|
||||
BzTileShape shape = object.shape;
|
||||
switch (shape.type) {
|
||||
case BZ_TILE_SHAPE_NONE:
|
||||
break;
|
||||
@@ -327,7 +345,13 @@ static void drawObjectLayer(BzTileObjectGroup *objectLayer) {
|
||||
DrawCircle(shape.x, shape.y, 2.0f, color);
|
||||
break;
|
||||
case BZ_TILE_SHAPE_RECT:
|
||||
DrawRectangle(shape.x, shape.y, shape.sizeX, shape.sizeY, color);
|
||||
if (object.gid) {
|
||||
Rectangle rec = bzTilesetGetTileRegion(tileset, object.gid);
|
||||
Rectangle dest = {shape.x, shape.y, shape.sizeX, shape.sizeY};
|
||||
DrawTexturePro(tileset->tiles, rec, dest, (Vector2) {0.0f, 0.0f}, 0.0f, WHITE);
|
||||
} else {
|
||||
DrawRectangle(shape.x, shape.y, shape.sizeX, shape.sizeY, color);
|
||||
}
|
||||
break;
|
||||
case BZ_TILE_SHAPE_ELLIPSE:
|
||||
DrawEllipse(shape.x, shape.y, shape.sizeX, shape.sizeY, color);
|
||||
@@ -358,8 +382,12 @@ void bzTileMapDraw(BzTileMap *map) {
|
||||
|
||||
for (i32 i = 0; i < map->objectGroupCount; i++) {
|
||||
BzTileObjectGroup *objectLayer = map->objectGroups + i;
|
||||
BzTileset *tileset = NULL;
|
||||
if (objectLayer->tilesetIdx != -1) {
|
||||
tileset = map->tilesets + objectLayer->tilesetIdx;
|
||||
}
|
||||
if (!objectLayer->objects) continue;
|
||||
drawObjectLayer(objectLayer);
|
||||
drawObjectLayer(objectLayer, tileset);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,8 +4,8 @@
|
||||
#include "tileset.h"
|
||||
#include "../utils/string.h"
|
||||
|
||||
#define BZ_MAX_MAP_LAYERS 8
|
||||
#define BZ_MAX_MAP_TILESETS 8
|
||||
#define BZ_MAP_MAX_LAYERS 8
|
||||
#define BZ_MAP_MAX_TILESETS 8
|
||||
#define BZ_MAP_COLLIDER_DEPTH 2
|
||||
|
||||
typedef struct BzTileLayer BzTileLayer;
|
||||
@@ -30,10 +30,10 @@ typedef struct BzTileObjectsDesc {
|
||||
|
||||
typedef struct BzTileMapDesc {
|
||||
const char *path;
|
||||
BzTileset tilesets[BZ_MAX_MAP_TILESETS];
|
||||
BzTileset tilesets[BZ_MAP_MAX_TILESETS];
|
||||
|
||||
BzTileLayerDesc layers[BZ_MAX_MAP_LAYERS];
|
||||
BzTileObjectsDesc objectGroups[BZ_MAX_MAP_LAYERS];
|
||||
BzTileLayerDesc layers[BZ_MAP_MAX_LAYERS];
|
||||
BzTileObjectsDesc objectGroups[BZ_MAP_MAX_LAYERS];
|
||||
} BzTileMapDesc;
|
||||
|
||||
typedef struct BzTileLayer {
|
||||
@@ -59,12 +59,16 @@ typedef struct BzTileLayer {
|
||||
|
||||
typedef struct BzTileObject {
|
||||
u32 id;
|
||||
BzTile gid;
|
||||
BzTileShape shape;
|
||||
} BzTileObject;
|
||||
|
||||
typedef struct BzTileObjectGroup {
|
||||
BzTileObject *objects;
|
||||
i32 objectCount;
|
||||
BzTile minGID;
|
||||
BzTile maxGID;
|
||||
i32 tilesetIdx;
|
||||
|
||||
BzTileObjectsDesc desc;
|
||||
bool hasOwnership;
|
||||
@@ -86,13 +90,13 @@ typedef struct BzTileMap {
|
||||
BzTileCollider *colliderMap;
|
||||
i32 collidersCount;
|
||||
|
||||
BzTileLayer layers[BZ_MAX_MAP_LAYERS];
|
||||
BzTileLayer layers[BZ_MAP_MAX_LAYERS];
|
||||
i32 layerCount;
|
||||
|
||||
BzTileObjectGroup objectGroups[BZ_MAX_MAP_LAYERS];
|
||||
BzTileObjectGroup objectGroups[BZ_MAP_MAX_LAYERS];
|
||||
i32 objectGroupCount;
|
||||
|
||||
BzTileset tilesets[BZ_MAX_MAP_TILESETS];
|
||||
BzTileset tilesets[BZ_MAP_MAX_TILESETS];
|
||||
i32 tilesetCount;
|
||||
|
||||
bool isValid;
|
||||
|
||||
Reference in New Issue
Block a user