Add support for objects with tiles
This commit is contained in:
BIN
assets/entities.png
Normal file
BIN
assets/entities.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 19 KiB |
14
assets/entities.tsj
Normal file
14
assets/entities.tsj
Normal file
@@ -0,0 +1,14 @@
|
||||
{ "columns":64,
|
||||
"image":"entities.png",
|
||||
"imageheight":1024,
|
||||
"imagewidth":1024,
|
||||
"margin":0,
|
||||
"name":"entities",
|
||||
"spacing":0,
|
||||
"tilecount":4096,
|
||||
"tiledversion":"1.10.2",
|
||||
"tileheight":16,
|
||||
"tilewidth":16,
|
||||
"type":"tileset",
|
||||
"version":"1.10"
|
||||
}
|
||||
@@ -374,7 +374,19 @@
|
||||
"draworder":"topdown",
|
||||
"id":31,
|
||||
"name":"Entities",
|
||||
"objects":[],
|
||||
"objects":[
|
||||
{
|
||||
"gid":1322,
|
||||
"height":10,
|
||||
"id":19728,
|
||||
"name":"",
|
||||
"rotation":0,
|
||||
"type":"",
|
||||
"visible":true,
|
||||
"width":10,
|
||||
"x":1095.25,
|
||||
"y":405.75
|
||||
}],
|
||||
"opacity":1,
|
||||
"type":"objectgroup",
|
||||
"visible":true,
|
||||
@@ -405,7 +417,7 @@
|
||||
"y":0
|
||||
}],
|
||||
"nextlayerid":33,
|
||||
"nextobjectid":19728,
|
||||
"nextobjectid":19729,
|
||||
"orientation":"orthogonal",
|
||||
"renderorder":"right-down",
|
||||
"tiledversion":"1.10.2",
|
||||
@@ -418,6 +430,10 @@
|
||||
{
|
||||
"firstgid":298,
|
||||
"source":"..\/..\/tiled\/buildings.tsx"
|
||||
},
|
||||
{
|
||||
"firstgid":1322,
|
||||
"source":"..\/..\/tiled\/entities.tsx"
|
||||
}],
|
||||
"tilewidth":16,
|
||||
"type":"map",
|
||||
|
||||
@@ -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;
|
||||
|
||||
11
game/main.c
11
game/main.c
@@ -27,6 +27,7 @@ typedef struct Game {
|
||||
Camera2D camera;
|
||||
BzTileset terrainTileset;
|
||||
BzTileset buildingsTileset;
|
||||
BzTileset entitiesTileset;
|
||||
BzTileMap map;
|
||||
} Game;
|
||||
|
||||
@@ -204,11 +205,16 @@ bool init(Game *game) {
|
||||
.path="assets/buildings.tsj",
|
||||
.texturePath="assets/buildings.png"
|
||||
});
|
||||
game->entitiesTileset = bzTilesetCreate(&(BzTilesetDesc) {
|
||||
.path="assets/entities.tsj",
|
||||
.texturePath="assets/entities.png"
|
||||
});
|
||||
|
||||
game->map = bzTileMapCreate(&(BzTileMapDesc) {
|
||||
.path="assets/maps/test.tmj",
|
||||
.tilesets[0]=game->terrainTileset,
|
||||
.tilesets[1]=game->buildingsTileset,
|
||||
.tilesets[2]=game->entitiesTileset,
|
||||
|
||||
.layers[LAYER_TERRAIN]=(BzTileLayerDesc) {"Terrain"},
|
||||
.layers[LAYER_FOLIAGE]=(BzTileLayerDesc) {"Foliage"},
|
||||
@@ -224,15 +230,16 @@ bool init(Game *game) {
|
||||
bzTileMapOverrideLayer(&game->map, LAYER_BUILDING_OWNER, prepareBuildings);
|
||||
|
||||
bzTileMapOverrideObjectGroup(&game->map, OBJECTS_GAME, handleGameObjects);
|
||||
bzTileMapOverrideObjectGroup(&game->map, OBJECTS_ENTITIES, BZ_TILE_OBJECTS_CLEAR);
|
||||
//bzTileMapOverrideObjectGroup(&game->map, OBJECTS_ENTITIES, BZ_TILE_OBJECTS_CLEAR);
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
void deinit(Game *game) {
|
||||
bzTileMapDestroy(&game->map);
|
||||
bzTilesetDestroy(&game->terrainTileset);
|
||||
bzTilesetDestroy(&game->buildingsTileset);
|
||||
bzTileMapDestroy(&game->map);
|
||||
bzTilesetDestroy(&game->entitiesTileset);
|
||||
rlImGuiShutdown();
|
||||
}
|
||||
|
||||
|
||||
BIN
rawAssets/entities.kra
Normal file
BIN
rawAssets/entities.kra
Normal file
Binary file not shown.
BIN
rawAssets/entities.png
Normal file
BIN
rawAssets/entities.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 19 KiB |
4
tiled/entities.tsx
Normal file
4
tiled/entities.tsx
Normal file
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<tileset version="1.10" tiledversion="1.10.2" name="entities" tilewidth="16" tileheight="16" tilecount="4096" columns="64">
|
||||
<image source="../assets/entities.png" width="1024" height="1024"/>
|
||||
</tileset>
|
||||
@@ -1,10 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<map version="1.10" tiledversion="1.10.2" orientation="orthogonal" renderorder="right-down" width="80" height="50" tilewidth="16" tileheight="16" infinite="0" nextlayerid="33" nextobjectid="19728">
|
||||
<map version="1.10" tiledversion="1.10.2" orientation="orthogonal" renderorder="right-down" width="80" height="50" tilewidth="16" tileheight="16" infinite="0" nextlayerid="33" nextobjectid="19729">
|
||||
<editorsettings>
|
||||
<export target="../assets/maps/test.tmj" format="json"/>
|
||||
</editorsettings>
|
||||
<tileset firstgid="1" source="terrain.tsx"/>
|
||||
<tileset firstgid="298" source="buildings.tsx"/>
|
||||
<tileset firstgid="1322" source="entities.tsx"/>
|
||||
<layer id="1" name="Terrain" width="80" height="50">
|
||||
<data encoding="csv">
|
||||
1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,
|
||||
@@ -329,7 +330,9 @@
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
|
||||
</data>
|
||||
</layer>
|
||||
<objectgroup id="31" name="Entities"/>
|
||||
<objectgroup id="31" name="Entities">
|
||||
<object id="19728" gid="1322" x="1095.25" y="405.75" width="10" height="10"/>
|
||||
</objectgroup>
|
||||
<objectgroup id="28" name="Game">
|
||||
<object id="19726" name="camera" x="1048" y="476">
|
||||
<point/>
|
||||
|
||||
Reference in New Issue
Block a user