Change collisionMap to boolean value
This commit is contained in:
@@ -111,14 +111,14 @@ static void handleTileObjectLayer(BzTileObjectGroup *layer, cute_tiled_layer_t *
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void updateColliders(BzTileMap *map, i32 startX, i32 startY, i32 endX, i32 endY) {
|
static void updateCollisionMap(BzTileMap *map, i32 startX, i32 startY, i32 endX, i32 endY) {
|
||||||
BZ_ASSERT(map->colliderMap);
|
BZ_ASSERT(map->collisionMap);
|
||||||
BZ_ASSERT(startX >= 0 && endX <= map->width &&
|
BZ_ASSERT(startX >= 0 && endX <= map->width &&
|
||||||
startY >= 0 && endY <= map->height);
|
startY >= 0 && endY <= map->height);
|
||||||
|
|
||||||
for (i32 y = startY; y < endY; y++) {
|
for (i32 y = startY; y < endY; y++) {
|
||||||
for (i32 x = startX; x < endX; x++) {
|
for (i32 x = startX; x < endX; x++) {
|
||||||
map->colliderMap[y * map->width + x] = (BzTileShape){.type=BZ_TILE_SHAPE_NONE};
|
map->collisionMap[y * map->width + x] = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -131,26 +131,24 @@ static void updateColliders(BzTileMap *map, i32 startX, i32 startY, i32 endX, i3
|
|||||||
BzTileset *tileset = map->tilesets + layer->tilesetIdx;
|
BzTileset *tileset = map->tilesets + layer->tilesetIdx;
|
||||||
for (i32 y = startY; y < endY; y++) {
|
for (i32 y = startY; y < endY; y++) {
|
||||||
for (i32 x = startX; x < endX; x++) {
|
for (i32 x = startX; x < endX; x++) {
|
||||||
|
i32 idx = y * map->width + x;
|
||||||
|
if (map->collisionMap[idx]) continue;
|
||||||
|
|
||||||
i32 tile = bzTileLayerGetTile(layer, x, y);
|
i32 tile = bzTileLayerGetTile(layer, x, y);
|
||||||
BzTileShape tilesetShape = bzTilesetGetTileCollider(tileset, tile);
|
BzTileShape tilesetShape = bzTilesetGetTileCollider(tileset, tile);
|
||||||
if (tilesetShape.type == BZ_TILE_SHAPE_NONE ||
|
if (tilesetShape.type == BZ_TILE_SHAPE_NONE ||
|
||||||
tilesetShape.type == BZ_TILE_SHAPE_POINT)
|
tilesetShape.type == BZ_TILE_SHAPE_POINT)
|
||||||
continue;
|
continue;
|
||||||
tilesetShape.x += layer->offsetX;
|
|
||||||
tilesetShape.y += layer->offsetY;
|
|
||||||
|
|
||||||
BzTileShape *shape = &map->colliderMap[y * map->width + x];
|
map->collisionMap[idx] = true;
|
||||||
if (shape->type == BZ_TILE_SHAPE_NONE) {
|
|
||||||
*shape = tilesetShape;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void createColliders(BzTileMap *map) {
|
static void createColliders(BzTileMap *map) {
|
||||||
map->colliderMap = bzAlloc(map->width * map->height * sizeof(*map->colliderMap));
|
map->collisionMap = bzAlloc(map->width * map->height * sizeof(*map->collisionMap));
|
||||||
updateColliders(map, 0, 0, map->width, map->height);
|
updateCollisionMap(map, 0, 0, map->width, map->height);
|
||||||
}
|
}
|
||||||
|
|
||||||
BzTileMap bzTileMapCreate(const BzTileMapDesc *desc) {
|
BzTileMap bzTileMapCreate(const BzTileMapDesc *desc) {
|
||||||
@@ -257,7 +255,7 @@ BzTileMap bzTileMapCreate(const BzTileMapDesc *desc) {
|
|||||||
}
|
}
|
||||||
cute_tiled_free_map(cuteMap);
|
cute_tiled_free_map(cuteMap);
|
||||||
|
|
||||||
if (desc->generateColliderMap)
|
if (desc->generateCollisionMap)
|
||||||
createColliders(&map);
|
createColliders(&map);
|
||||||
|
|
||||||
map.isValid = true;
|
map.isValid = true;
|
||||||
@@ -282,7 +280,7 @@ void bzTileMapDestroy(BzTileMap *map) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bzFree(map->colliderMap);
|
bzFree(map->collisionMap);
|
||||||
|
|
||||||
*map = BZ_TILEMAP_INVALID;
|
*map = BZ_TILEMAP_INVALID;
|
||||||
}
|
}
|
||||||
@@ -400,50 +398,30 @@ void bzTileMapDraw(BzTileMap *map) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void bzTileMapDrawColliders(BzTileMap *map) {
|
void bzTileMapDrawCollisions(BzTileMap *map) {
|
||||||
|
if (!map->collisionMap) return;
|
||||||
Color color = RED;
|
Color color = RED;
|
||||||
color.a = 150;
|
color.a = 150;
|
||||||
|
i32 sizeX = map->tileWidth;
|
||||||
|
i32 sizeY = map->tileHeight;
|
||||||
for (i32 y = 0; y < map->height; y++) {
|
for (i32 y = 0; y < map->height; y++) {
|
||||||
for (i32 x = 0; x < map->width; x++) {
|
for (i32 x = 0; x < map->width; x++) {
|
||||||
i32 idx = y * map->width + x;
|
i32 idx = y * map->width + x;
|
||||||
BzTileShape shape = map->colliderMap[idx];
|
i32 posX = x * sizeX;
|
||||||
if (shape.type == BZ_TILE_SHAPE_NONE)
|
i32 posY = y * sizeY;
|
||||||
continue;
|
if (map->collisionMap[idx])
|
||||||
|
DrawRectangleLines(posX, posY, sizeX, sizeY, color);
|
||||||
i32 posX = x * map->tileWidth + shape.x;
|
|
||||||
i32 posY = y * map->tileHeight + shape.y;
|
|
||||||
f32 sizeX = shape.sizeX;
|
|
||||||
f32 sizeY = shape.sizeY;
|
|
||||||
|
|
||||||
switch (shape.type) {
|
|
||||||
case BZ_TILE_SHAPE_NONE:
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
case BZ_TILE_SHAPE_RECT:
|
|
||||||
DrawRectangleLines(posX, posY, sizeX, sizeY, color);
|
|
||||||
break;
|
|
||||||
case BZ_TILE_SHAPE_ELLIPSE:
|
|
||||||
DrawEllipseLines(posX, posY, sizeX, sizeY, color);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
BzTileShape bzTileMapGetCollider(BzTileMap *map, i32 x, i32 y) {
|
bool bzTileMapHasCollision(BzTileMap *map, i32 x, i32 y) {
|
||||||
BzTileShape invalid = {.type = BZ_TILE_SHAPE_NONE};
|
if (!map->collisionMap) return false;
|
||||||
if (map->colliderMap == 0) return invalid;
|
|
||||||
i32 idx = y * map->width + x;
|
i32 idx = y * map->width + x;
|
||||||
if (idx < 0 || idx >= map->width * map->height) {
|
return map->collisionMap[idx];
|
||||||
return invalid;
|
|
||||||
}
|
|
||||||
return map->colliderMap[idx];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void bzTileMapUpdateColliders(BzTileMap *map, i32 x, i32 y, i32 sizeX, i32 sizeY) {
|
void bzTileMapUpdateCollisions(BzTileMap *map, i32 x, i32 y, i32 sizeX, i32 sizeY) {
|
||||||
if (!map->colliderMap) return;
|
if (!map->collisionMap) return;
|
||||||
updateColliders(map, x, y, x + sizeX, y + sizeY);
|
updateCollisionMap(map, x, y, sizeX, sizeY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ typedef struct BzTileObjectsDesc {
|
|||||||
|
|
||||||
typedef struct BzTileMapDesc {
|
typedef struct BzTileMapDesc {
|
||||||
const char *path;
|
const char *path;
|
||||||
bool generateColliderMap;
|
bool generateCollisionMap;
|
||||||
BzTileset tilesets[BZ_MAP_MAX_TILESETS];
|
BzTileset tilesets[BZ_MAP_MAX_TILESETS];
|
||||||
|
|
||||||
BzTileLayerDesc layers[BZ_MAP_MAX_LAYERS];
|
BzTileLayerDesc layers[BZ_MAP_MAX_LAYERS];
|
||||||
@@ -85,7 +85,7 @@ typedef struct BzTileMap {
|
|||||||
i32 tileWidth;
|
i32 tileWidth;
|
||||||
i32 tileHeight;
|
i32 tileHeight;
|
||||||
|
|
||||||
BzTileShape *colliderMap;
|
bool *collisionMap;
|
||||||
|
|
||||||
BzTileLayer layers[BZ_MAP_MAX_LAYERS];
|
BzTileLayer layers[BZ_MAP_MAX_LAYERS];
|
||||||
i32 layerCount;
|
i32 layerCount;
|
||||||
@@ -123,9 +123,9 @@ BzTileLayer *bzTileMapGetLayer(BzTileMap *map, i32 slotID);
|
|||||||
BzTileObjectGroup *bzTileMapGetObjects(BzTileMap *map, i32 slotID);
|
BzTileObjectGroup *bzTileMapGetObjects(BzTileMap *map, i32 slotID);
|
||||||
|
|
||||||
void bzTileMapDraw(BzTileMap *map);
|
void bzTileMapDraw(BzTileMap *map);
|
||||||
void bzTileMapDrawColliders(BzTileMap *map);
|
void bzTileMapDrawCollisions(BzTileMap *map);
|
||||||
BzTileShape bzTileMapGetCollider(BzTileMap *map, i32 x, i32 y);
|
bool bzTileMapHasCollision(BzTileMap *map, i32 x, i32 y);
|
||||||
void bzTileMapUpdateColliders(BzTileMap *map, i32 x, i32 y, i32 sizeX, i32 sizeY);
|
void bzTileMapUpdateCollisions(BzTileMap *map, i32 x, i32 y, i32 sizeX, i32 sizeY);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -37,28 +37,10 @@ bool canPlaceBuilding(BzTileMap *map, BuildingType type, BzTile tileX, BzTile ti
|
|||||||
if (tile == BUILDINGS_ROAD)
|
if (tile == BUILDINGS_ROAD)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
BzTileShape shape = bzTileMapGetCollider(map, x, y);
|
|
||||||
f32 posX = x * map->tileWidth;
|
f32 posX = x * map->tileWidth;
|
||||||
f32 posY = y * map->tileHeight;
|
f32 posY = y * map->tileHeight;
|
||||||
shape.x += posX;
|
if (bzTileMapHasCollision(map, posX, posY)) {
|
||||||
shape.y += posY;
|
return false;
|
||||||
switch (shape.type) {
|
|
||||||
case BZ_TILE_SHAPE_NONE:
|
|
||||||
case BZ_TILE_SHAPE_POINT:
|
|
||||||
break;
|
|
||||||
case BZ_TILE_SHAPE_RECT: {
|
|
||||||
Rectangle shapeRec = {shape.x, shape.y, shape.sizeX, shape.sizeY};
|
|
||||||
if (CheckCollisionRecs(buildArea, shapeRec))
|
|
||||||
return false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case BZ_TILE_SHAPE_ELLIPSE: {
|
|
||||||
Vector2 pos = {shape.x, shape.y};
|
|
||||||
f32 radius = (shape.sizeX + shape.sizeY) * 0.5f;
|
|
||||||
if (CheckCollisionCircleRec(pos, radius, buildArea))
|
|
||||||
return false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -89,7 +71,7 @@ ecs_entity_t placeBuilding(BzTileMap *map, BuildingType type, BzTile tileX, BzTi
|
|||||||
bzTileLayerSetTile(buildingLayer, layerTile, x, y, 1, 1);
|
bzTileLayerSetTile(buildingLayer, layerTile, x, y, 1, 1);
|
||||||
buildingTile++;
|
buildingTile++;
|
||||||
|
|
||||||
bzTileMapUpdateColliders(map, x, y, 1, 1);
|
bzTileMapUpdateCollisions(map, x, y, map->tileWidth, map->tileHeight);
|
||||||
}
|
}
|
||||||
buildingTile += buildingTileset->width - sizeX;
|
buildingTile += buildingTileset->width - sizeX;
|
||||||
}
|
}
|
||||||
|
|||||||
12
game/main.c
12
game/main.c
@@ -91,7 +91,7 @@ bool init(void *userData) {
|
|||||||
|
|
||||||
game->map = bzTileMapCreate(&(BzTileMapDesc) {
|
game->map = bzTileMapCreate(&(BzTileMapDesc) {
|
||||||
.path="assets/maps/test.tmj",
|
.path="assets/maps/test.tmj",
|
||||||
.generateColliderMap=true,
|
.generateCollisionMap=true,
|
||||||
.tilesets[0]=game->terrainTileset,
|
.tilesets[0]=game->terrainTileset,
|
||||||
.tilesets[1]=game->buildingsTileset,
|
.tilesets[1]=game->buildingsTileset,
|
||||||
.tilesets[2]=game->entitiesTileset,
|
.tilesets[2]=game->entitiesTileset,
|
||||||
@@ -140,6 +140,8 @@ bool init(void *userData) {
|
|||||||
renderDebugPathSystem = renderDebugPath;
|
renderDebugPathSystem = renderDebugPath;
|
||||||
renderCollidersSystem = renderColliders;
|
renderCollidersSystem = renderColliders;
|
||||||
|
|
||||||
|
game->debugDraw.mapColliders = true;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
void deinit(void *userData) {
|
void deinit(void *userData) {
|
||||||
@@ -151,11 +153,13 @@ void deinit(void *userData) {
|
|||||||
bzTilesetDestroy(&game->buildingsTileset);
|
bzTilesetDestroy(&game->buildingsTileset);
|
||||||
bzTilesetDestroy(&game->entitiesTileset);
|
bzTilesetDestroy(&game->entitiesTileset);
|
||||||
|
|
||||||
|
Game gameCopy = *game;
|
||||||
|
|
||||||
ecs_fini(ECS);
|
ecs_fini(ECS);
|
||||||
ECS = NULL;
|
ECS = NULL;
|
||||||
|
|
||||||
bzObjectPoolDestroy(game->pools.pathData);
|
bzObjectPoolDestroy(gameCopy.pools.pathData);
|
||||||
bzSpatialGridDestroy(game->entityGrid);
|
bzSpatialGridDestroy(gameCopy.entityGrid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -288,7 +292,7 @@ void render(float dt, void *userData) {
|
|||||||
ecs_enable(ECS, renderDebugPathSystem, game->debugDraw.path);
|
ecs_enable(ECS, renderDebugPathSystem, game->debugDraw.path);
|
||||||
ecs_enable(ECS, renderCollidersSystem, game->debugDraw.entityColliders);
|
ecs_enable(ECS, renderCollidersSystem, game->debugDraw.entityColliders);
|
||||||
if (game->debugDraw.mapColliders)
|
if (game->debugDraw.mapColliders)
|
||||||
bzTileMapDrawColliders(&game->map);
|
bzTileMapDrawCollisions(&game->map);
|
||||||
if (game->debugDraw.spatialGrid)
|
if (game->debugDraw.spatialGrid)
|
||||||
bzSpatialGridDrawDebugGrid(game->entityGrid);
|
bzSpatialGridDrawDebugGrid(game->entityGrid);
|
||||||
|
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ bool findPath(const PathfindingDesc *desc) {
|
|||||||
if (y < 0 || y >= map->height ||
|
if (y < 0 || y >= map->height ||
|
||||||
x < 0 || x >= map->width)
|
x < 0 || x >= map->width)
|
||||||
continue;
|
continue;
|
||||||
if (bzTileMapGetCollider(map, x, y).type != BZ_TILE_SHAPE_NONE)
|
if (bzTileMapHasCollision(map, x, y))
|
||||||
continue;
|
continue;
|
||||||
Visited *curVisited = &visited[y * map->width + x];
|
Visited *curVisited = &visited[y * map->width + x];
|
||||||
if (curVisited->visited)
|
if (curVisited->visited)
|
||||||
|
|||||||
Reference in New Issue
Block a user