Remove multiple colliders, add spatial index component

This commit is contained in:
2023-11-17 15:13:30 +01:00
parent 208cdc6b6f
commit 021df6d77a
11 changed files with 122 additions and 95 deletions

View File

@@ -24,7 +24,7 @@ typedef double f64;
#define DEBUG_MODE #define DEBUG_MODE
#ifndef DEBUG_MODE #ifndef DEBUG_MODE
#undef BZ_ASSERT #undef BZ_ASSERT
#define BZ_ASSERT(e) BZ_UNUSDE(x) #define BZ_ASSERT(e) BZ_UNUSED(x)
#endif #endif
#define BZ_UNUSED(x) (void)(x) #define BZ_UNUSED(x) (void)(x)

View File

@@ -10,19 +10,25 @@
BzTileMap BZ_TILEMAP_INVALID = {.isValid = false}; BzTileMap BZ_TILEMAP_INVALID = {.isValid = false};
void bzTileLayerSkipRender(BzTileMap *map, BzTileLayer *layer) { void bzTileLayerSkipRender(BzTileMap *map, BzTileLayer *layer) {
BZ_UNUSED(map);
BZ_UNUSED(layer);
} }
void bzTileObjectGroupSkipRender(BzTileMap *map, BzTileObjectGroup *objectGroup) { void bzTileObjectGroupSkipRender(BzTileMap *map, BzTileObjectGroup *objectGroup) {
BZ_UNUSED(map);
BZ_UNUSED(objectGroup);
} }
BzTileLayerRenderFunc BZ_TILE_LAYER_SKIP_RENDER = bzTileLayerSkipRender; BzTileLayerRenderFunc BZ_TILE_LAYER_SKIP_RENDER = bzTileLayerSkipRender;
BzTileObjectGroupRenderFunc BZ_TILE_OBJECTS_SKIP_RENDER = bzTileObjectGroupSkipRender; BzTileObjectGroupRenderFunc BZ_TILE_OBJECTS_SKIP_RENDER = bzTileObjectGroupSkipRender;
bool bzTileLayerClear(BzTileMap *map, BzTileLayer *layer) { bool bzTileLayerClear(BzTileMap *map, BzTileLayer *layer) {
BZ_UNUSED(map);
BZ_UNUSED(layer);
return true; return true;
} }
bool bzTileObjectsClear(BzTileMap *map, BzTileObjectGroup *objectGroup) { bool bzTileObjectsClear(BzTileMap *map, BzTileObjectGroup *objectGroup) {
BZ_UNUSED(map);
BZ_UNUSED(objectGroup);
return true; return true;
} }
@@ -106,12 +112,20 @@ static void handleTileObjectLayer(BzTileObjectGroup *layer, cute_tiled_layer_t *
} }
static void updateColliders(BzTileMap *map, i32 startX, i32 startY, i32 endX, i32 endY) { static void updateColliders(BzTileMap *map, i32 startX, i32 startY, i32 endX, i32 endY) {
BZ_ASSERT(map->colliderMap);
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 x = startX; x < endX; x++) {
map->colliderMap[y * map->width + x] = (BzTileShape){.type=BZ_TILE_SHAPE_NONE};
}
}
// Top-most layer takes priority // Top-most layer takes priority
for (i32 i = map->layerCount - 1; i >= 0; i--) { for (i32 i = map->layerCount - 1; i >= 0; i--) {
BzTileLayer *layer = map->layers + i; BzTileLayer *layer = map->layers + i;
if (!layer->desc.applyColliders) continue;
if (!layer->data) continue; if (!layer->data) continue;
if (layer->tilesetIdx == -1) continue; if (layer->tilesetIdx == -1) continue;
BzTileset *tileset = map->tilesets + layer->tilesetIdx; BzTileset *tileset = map->tilesets + layer->tilesetIdx;
@@ -125,15 +139,9 @@ static void updateColliders(BzTileMap *map, i32 startX, i32 startY, i32 endX, i3
tilesetShape.x += layer->offsetX; tilesetShape.x += layer->offsetX;
tilesetShape.y += layer->offsetY; tilesetShape.y += layer->offsetY;
i32 colliderIdx = y * map->width + x; BzTileShape *shape = &map->colliderMap[y * map->width + x];
BzTileCollider *collider = map->colliderMap + colliderIdx; if (shape->type == BZ_TILE_SHAPE_NONE) {
*shape = tilesetShape;
for (i32 sIdx = 0; sIdx < BZ_MAP_COLLIDER_DEPTH; sIdx++) {
BzTileShape *shape = collider->shapes + sIdx;
if (shape->type == BZ_TILE_SHAPE_NONE) {
*shape = tilesetShape;
break;
}
} }
} }
} }
@@ -141,17 +149,12 @@ static void updateColliders(BzTileMap *map, i32 startX, i32 startY, i32 endX, i3
} }
static void createColliders(BzTileMap *map) { static void createColliders(BzTileMap *map) {
map->collidersCount = map->width * map->height; map->colliderMap = bzAlloc(map->width * map->height * sizeof(*map->colliderMap));
map->colliderMap = bzAlloc(map->collidersCount * sizeof(*map->colliderMap));
for (i32 i = 0; i < map->collidersCount; i++) {
map->colliderMap[i] = (BzTileCollider) {{BZ_TILE_SHAPE_NONE}};
}
updateColliders(map, 0, 0, map->width, map->height); updateColliders(map, 0, 0, map->width, map->height);
} }
BzTileMap bzTileMapCreate(const BzTileMapDesc *desc) { BzTileMap bzTileMapCreate(const BzTileMapDesc *desc) {
BzTileMap map = {}; BzTileMap map = {.backgroundColor=BLACK};
// Auto detect tileset count. // Auto detect tileset count.
for (i32 i = 0; i < BZ_MAP_MAX_TILESETS; i++) { for (i32 i = 0; i < BZ_MAP_MAX_TILESETS; i++) {
if (!desc->tilesets[i].isValid) if (!desc->tilesets[i].isValid)
@@ -181,8 +184,8 @@ BzTileMap bzTileMapCreate(const BzTileMapDesc *desc) {
for (i32 i = 0; i < BZ_MAP_MAX_LAYERS; i++) { for (i32 i = 0; i < BZ_MAP_MAX_LAYERS; i++) {
const BzTileLayerDesc *layerDesc = desc->layers + i; const BzTileLayerDesc *layerDesc = desc->layers + i;
const BzTileObjectsDesc *objectsDesc = desc->objectGroups + i; const BzTileObjectsDesc *objectsDesc = desc->objectGroups + i;
if (layerDesc->name && strcmp(layerDesc->name, cuteLayer->name.ptr) == 0 || if ((layerDesc->name && strcmp(layerDesc->name, cuteLayer->name.ptr) == 0) ||
objectsDesc->name && strcmp(objectsDesc->name, cuteLayer->name.ptr) == 0) { (objectsDesc->name && strcmp(objectsDesc->name, cuteLayer->name.ptr) == 0)) {
slot = i; slot = i;
break; break;
} else if (!layerDesc->name && !objectsDesc->name) { } else if (!layerDesc->name && !objectsDesc->name) {
@@ -254,7 +257,8 @@ BzTileMap bzTileMapCreate(const BzTileMapDesc *desc) {
} }
cute_tiled_free_map(cuteMap); cute_tiled_free_map(cuteMap);
createColliders(&map); if (desc->generateColliderMap)
createColliders(&map);
map.isValid = true; map.isValid = true;
return map; return map;
@@ -279,7 +283,6 @@ void bzTileMapDestroy(BzTileMap *map) {
} }
bzFree(map->colliderMap); bzFree(map->colliderMap);
map->collidersCount = 0;
*map = BZ_TILEMAP_INVALID; *map = BZ_TILEMAP_INVALID;
} }
@@ -403,28 +406,25 @@ void bzTileMapDrawColliders(BzTileMap *map) {
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;
BzTileCollider collider = map->colliderMap[idx]; BzTileShape shape = map->colliderMap[idx];
for (i32 i = 0; i < BZ_MAP_COLLIDER_DEPTH; i++) { if (shape.type == BZ_TILE_SHAPE_NONE)
BzTileShape shape = collider.shapes[i]; continue;
if (shape.type == BZ_TILE_SHAPE_NONE)
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; break;
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;
}
} }
@@ -432,17 +432,18 @@ void bzTileMapDrawColliders(BzTileMap *map) {
} }
} }
void bzTileMapUpdateCollider(BzTileMap *map, i32 x, i32 y) { BzTileShape bzTileMapGetCollider(BzTileMap *map, i32 x, i32 y) {
BzTileShape invalid = {.type = BZ_TILE_SHAPE_NONE};
if (map->colliderMap == 0) return invalid;
i32 idx = y * map->width + x; i32 idx = y * map->width + x;
BZ_ASSERT(idx >= 0 && idx < map->collidersCount); if (idx < 0 || idx >= map->width * map->height) {
map->colliderMap[idx] = (BzTileCollider){}; return invalid;
updateColliders(map, x, y, x + 1, y + 1);
}
BzTileCollider bzTileMapGetCollider(BzTileMap *map, i32 x, i32 y) {
i32 idx = y * map->width + x;
if (idx < 0 || idx >= map->collidersCount) {
return (BzTileCollider) {{BZ_TILE_SHAPE_NONE}};
} }
return map->colliderMap[idx]; return map->colliderMap[idx];
} }
void bzTileMapUpdateColliders(BzTileMap *map, i32 x, i32 y, i32 sizeX, i32 sizeY) {
if (!map->colliderMap) return;
updateColliders(map, x, y, x + sizeX, y + sizeY);
}

View File

@@ -6,7 +6,6 @@
#define BZ_MAP_MAX_LAYERS 8 #define BZ_MAP_MAX_LAYERS 8
#define BZ_MAP_MAX_TILESETS 8 #define BZ_MAP_MAX_TILESETS 8
#define BZ_MAP_COLLIDER_DEPTH 2
typedef struct BzTileLayer BzTileLayer; typedef struct BzTileLayer BzTileLayer;
typedef struct BzTileObject BzTileObject; typedef struct BzTileObject BzTileObject;
@@ -22,6 +21,7 @@ extern BzTileObjectGroupRenderFunc BZ_TILE_OBJECTS_SKIP_RENDER;
typedef struct BzTileLayerDesc { typedef struct BzTileLayerDesc {
const char *name; // Matches map layer names const char *name; // Matches map layer names
BzTileLayerRenderFunc renderer; BzTileLayerRenderFunc renderer;
bool applyColliders;
} BzTileLayerDesc; } BzTileLayerDesc;
typedef struct BzTileObjectsDesc { typedef struct BzTileObjectsDesc {
const char *name; // Matches map layer names const char *name; // Matches map layer names
@@ -31,6 +31,7 @@ typedef struct BzTileObjectsDesc {
typedef struct BzTileMapDesc { typedef struct BzTileMapDesc {
const char *path; const char *path;
bool generateColliderMap;
BzTileset tilesets[BZ_MAP_MAX_TILESETS]; BzTileset tilesets[BZ_MAP_MAX_TILESETS];
BzTileLayerDesc layers[BZ_MAP_MAX_LAYERS]; BzTileLayerDesc layers[BZ_MAP_MAX_LAYERS];
@@ -76,10 +77,6 @@ typedef struct BzTileObjectGroup {
} BzTileObjectGroup; } BzTileObjectGroup;
typedef struct BzTileCollider {
BzTileShape shapes[BZ_MAP_COLLIDER_DEPTH];
} BzTileCollider;
typedef struct BzTileMap { typedef struct BzTileMap {
Color backgroundColor; Color backgroundColor;
@@ -88,8 +85,7 @@ typedef struct BzTileMap {
i32 tileWidth; i32 tileWidth;
i32 tileHeight; i32 tileHeight;
BzTileCollider *colliderMap; BzTileShape *colliderMap;
i32 collidersCount;
BzTileLayer layers[BZ_MAP_MAX_LAYERS]; BzTileLayer layers[BZ_MAP_MAX_LAYERS];
i32 layerCount; i32 layerCount;
@@ -128,8 +124,8 @@ BzTileObjectGroup *bzTileMapGetObjects(BzTileMap *map, i32 slotID);
void bzTileMapDraw(BzTileMap *map); void bzTileMapDraw(BzTileMap *map);
void bzTileMapDrawColliders(BzTileMap *map); void bzTileMapDrawColliders(BzTileMap *map);
BzTileCollider bzTileMapGetCollider(BzTileMap *map, i32 x, i32 y); BzTileShape bzTileMapGetCollider(BzTileMap *map, i32 x, i32 y);
void bzTileMapUpdateCollider(BzTileMap *map, i32 x, i32 y); void bzTileMapUpdateColliders(BzTileMap *map, i32 x, i32 y, i32 sizeX, i32 sizeY);

View File

@@ -37,30 +37,27 @@ bool canPlaceBuilding(BzTileMap *map, BuildingType type, BzTile tileX, BzTile ti
if (tile == BUILDINGS_ROAD) if (tile == BUILDINGS_ROAD)
return false; return false;
} }
BzTileCollider collider = bzTileMapGetCollider(map, x, y); 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;
for (int i = 0; i < BZ_MAP_COLLIDER_DEPTH; i++) { shape.x += posX;
BzTileShape shape = collider.shapes[i]; shape.y += posY;
shape.x += posX; switch (shape.type) {
shape.y += posY; case BZ_TILE_SHAPE_NONE:
switch (shape.type) { case BZ_TILE_SHAPE_POINT:
case BZ_TILE_SHAPE_NONE: break;
case BZ_TILE_SHAPE_POINT: case BZ_TILE_SHAPE_RECT: {
break; Rectangle shapeRec = {shape.x, shape.y, shape.sizeX, shape.sizeY};
case BZ_TILE_SHAPE_RECT: { if (CheckCollisionRecs(buildArea, shapeRec))
Rectangle shapeRec = {shape.x, shape.y, shape.sizeX, shape.sizeY}; return false;
if (CheckCollisionRecs(buildArea, shapeRec)) break;
return false; }
break; case BZ_TILE_SHAPE_ELLIPSE: {
} Vector2 pos = {shape.x, shape.y};
case BZ_TILE_SHAPE_ELLIPSE: { f32 radius = (shape.sizeX + shape.sizeY) * 0.5f;
Vector2 pos = {shape.x, shape.y}; if (CheckCollisionCircleRec(pos, radius, buildArea))
f32 radius = (shape.sizeX + shape.sizeY) * 0.5f; return false;
if (CheckCollisionCircleRec(pos, radius, buildArea)) break;
return false;
break;
}
} }
} }
} }
@@ -82,9 +79,9 @@ ecs_entity_t placeBuilding(BzTileMap *map, BuildingType type, BzTile tileX, BzTi
// Create entity // Create entity
ecs_entity_t e = ecs_new_id(ECS); ecs_entity_t e = ecs_new_id(ECS);
ecs_set(ECS, e, TilePosition, {.x=tileX, .y=tileY}); ecs_set(ECS, e, TilePosition, { .x = tileX, .y = tileY });
ecs_set(ECS, e, TileSize, {.sizeX=sizeX, .sizeY=sizeY}); ecs_set(ECS, e, TileSize, { .sizeX = sizeX, .sizeY = sizeY });
ecs_set(ECS, e, Owner, {.playerID=BUILDINGS_PLAYER_RED}); ecs_set(ECS, e, Owner, { .playerID = BUILDINGS_PLAYER_RED });
for (i32 y = tileY; y < tileY + sizeY; y++) { for (i32 y = tileY; y < tileY + sizeY; y++) {
for (i32 x = tileX; x < tileX + sizeX; x++) { for (i32 x = tileX; x < tileX + sizeX; x++) {
@@ -92,7 +89,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++;
bzTileMapUpdateCollider(map, x, y); bzTileMapUpdateColliders(map, x, y, 1, 1);
} }
buildingTile += buildingTileset->width - sizeX; buildingTile += buildingTileset->width - sizeX;
} }

View File

@@ -8,6 +8,7 @@ ECS_COMPONENT_DECLARE(Position);
ECS_COMPONENT_DECLARE(Size); ECS_COMPONENT_DECLARE(Size);
ECS_COMPONENT_DECLARE(TargetPosition); ECS_COMPONENT_DECLARE(TargetPosition);
ECS_COMPONENT_DECLARE(MoveForce); ECS_COMPONENT_DECLARE(MoveForce);
ECS_COMPONENT_DECLARE(SpatialGridID);
ECS_COMPONENT_DECLARE(Rotation); ECS_COMPONENT_DECLARE(Rotation);
ECS_COMPONENT_DECLARE(Health); ECS_COMPONENT_DECLARE(Health);
ECS_COMPONENT_DECLARE(TextureRegion); ECS_COMPONENT_DECLARE(TextureRegion);
@@ -23,6 +24,7 @@ void initComponentIDs(ecs_world_t *ecs) {
ECS_COMPONENT_DEFINE(ecs, Size); ECS_COMPONENT_DEFINE(ecs, Size);
ECS_COMPONENT_DEFINE(ecs, TargetPosition); ECS_COMPONENT_DEFINE(ecs, TargetPosition);
ECS_COMPONENT_DEFINE(ecs, MoveForce); ECS_COMPONENT_DEFINE(ecs, MoveForce);
ECS_COMPONENT_DEFINE(ecs, SpatialGridID);
ECS_COMPONENT_DEFINE(ecs, Rotation); ECS_COMPONENT_DEFINE(ecs, Rotation);
ECS_COMPONENT_DEFINE(ecs, Health); ECS_COMPONENT_DEFINE(ecs, Health);
ECS_COMPONENT_DEFINE(ecs, TextureRegion); ECS_COMPONENT_DEFINE(ecs, TextureRegion);

View File

@@ -32,6 +32,9 @@ extern ECS_COMPONENT_DECLARE(Size);
extern ECS_COMPONENT_DECLARE(TargetPosition); extern ECS_COMPONENT_DECLARE(TargetPosition);
extern ECS_COMPONENT_DECLARE(MoveForce); extern ECS_COMPONENT_DECLARE(MoveForce);
typedef BzSpatialGridID SpatialGridID;
extern ECS_COMPONENT_DECLARE(SpatialGridID);
typedef f32 Rotation; typedef f32 Rotation;
extern ECS_COMPONENT_DECLARE(Rotation); extern ECS_COMPONENT_DECLARE(Rotation);

View File

@@ -83,15 +83,16 @@ bool init(void *userData) {
game->map = bzTileMapCreate(&(BzTileMapDesc) { game->map = bzTileMapCreate(&(BzTileMapDesc) {
.path="assets/maps/test.tmj", .path="assets/maps/test.tmj",
.generateColliderMap=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,
.layers[LAYER_TERRAIN]=(BzTileLayerDesc) {"Terrain"}, .layers[LAYER_TERRAIN]=(BzTileLayerDesc) {"Terrain", .applyColliders=true},
.layers[LAYER_FOLIAGE]=(BzTileLayerDesc) {"Foliage"}, .layers[LAYER_FOLIAGE]=(BzTileLayerDesc) {"Foliage"},
.layers[LAYER_TREES]=(BzTileLayerDesc) {"Trees"}, .layers[LAYER_TREES]=(BzTileLayerDesc) {"Trees"},
.layers[LAYER_TREES2]=(BzTileLayerDesc) {"TreesS"}, .layers[LAYER_TREES2]=(BzTileLayerDesc) {"TreesS"},
.layers[LAYER_BUILDINGS]=(BzTileLayerDesc) {"Buildings"}, .layers[LAYER_BUILDINGS]=(BzTileLayerDesc) {"Buildings", .applyColliders=true},
.layers[LAYER_BUILDING_OWNER]=(BzTileLayerDesc) {"BuildingOwnership", BZ_TILE_LAYER_SKIP_RENDER}, .layers[LAYER_BUILDING_OWNER]=(BzTileLayerDesc) {"BuildingOwnership", BZ_TILE_LAYER_SKIP_RENDER},
.objectGroups[OBJECTS_GAME]=(BzTileObjectsDesc) {"Game"}, .objectGroups[OBJECTS_GAME]=(BzTileObjectsDesc) {"Game"},
@@ -100,8 +101,8 @@ bool init(void *userData) {
game->entityGrid = bzSpatialGridCreate(&(BzSpatialGridDesc) { game->entityGrid = bzSpatialGridCreate(&(BzSpatialGridDesc) {
.maxWidth=game->map.width * game->map.tileWidth, .maxWidth=game->map.width * game->map.tileWidth,
.maxHeight=game->map.height * game->map.tileHeight, .maxHeight=game->map.height * game->map.tileHeight,
.cellWidth=game->map.tileWidth * 2, .cellWidth=game->map.tileWidth * 5,
.cellHeight=game->map.tileHeight * 2, .cellHeight=game->map.tileHeight * 5,
.userDataSize=sizeof(ecs_entity_t) .userDataSize=sizeof(ecs_entity_t)
}); });
@@ -113,6 +114,7 @@ bool init(void *userData) {
ECS_SYSTEM(ECS, uiTask, EcsOnUpdate, 0); ECS_SYSTEM(ECS, uiTask, EcsOnUpdate, 0);
ECS_SYSTEM(ECS, updateAnimations, EcsOnUpdate, Animation, TextureRegion); ECS_SYSTEM(ECS, updateAnimations, EcsOnUpdate, Animation, TextureRegion);
ECS_SYSTEM(ECS, updatePos, EcsOnUpdate, Position, TargetPosition, TextureRegion); ECS_SYSTEM(ECS, updatePos, EcsOnUpdate, Position, TargetPosition, TextureRegion);
ECS_SYSTEM(ECS, entityUpdatePhysics, EcsOnUpdate, Position, Size, SpatialGridID);
ECS_SYSTEM(ECS, drawDebugPath, EcsOnUpdate, Path); ECS_SYSTEM(ECS, drawDebugPath, EcsOnUpdate, Path);
ECS_SYSTEM(ECS, renderEntities, EcsOnUpdate, Position, Size, Rotation, TextureRegion); ECS_SYSTEM(ECS, renderEntities, EcsOnUpdate, Position, Size, Rotation, TextureRegion);
ECS_OBSERVER(ECS, targetFinish, EcsOnRemove, TargetPosition); ECS_OBSERVER(ECS, targetFinish, EcsOnRemove, TargetPosition);

View File

@@ -29,6 +29,10 @@ bool initEntityObjectsLayer(BzTileMap *map, BzTileObjectGroup *objectGroup) {
game->entity = e; game->entity = e;
ecs_set(ECS, e, Position, {object.shape.x, object.shape.y}); ecs_set(ECS, e, Position, {object.shape.x, object.shape.y});
ecs_set(ECS, e, Size, {object.shape.sizeX, object.shape.sizeY}); ecs_set(ECS, e, Size, {object.shape.sizeX, object.shape.sizeY});
BzSpatialGridID spatialID = bzSpatialGridInsert(game->entityGrid, &e,
object.shape.x, object.shape.y,
object.shape.sizeX, object.shape.sizeY);
ecs_set(ECS, e, SpatialGridID, {spatialID});
ecs_set(ECS, e, Rotation, {0.0f}); ecs_set(ECS, e, Rotation, {0.0f});
ecs_set(ECS, e, TextureRegion, {objectTileset->tiles, bzTilesetGetTileRegion(objectTileset, object.gid)}); ecs_set(ECS, e, TextureRegion, {objectTileset->tiles, bzTilesetGetTileRegion(objectTileset, object.gid)});
ecs_set(ECS, e, Animation, { ecs_set(ECS, e, Animation, {

View File

@@ -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).shapes[0].type != BZ_TILE_SHAPE_NONE) if (bzTileMapGetCollider(map, x, y).type != BZ_TILE_SHAPE_NONE)
continue; continue;
Visited *curVisited = &visited[y * map->width + x]; Visited *curVisited = &visited[y * map->width + x];
if (curVisited->visited) if (curVisited->visited)
@@ -91,6 +91,7 @@ bool findPath(const PathfindingDesc *desc) {
pathData->next = NULL; pathData->next = NULL;
i32 numWaypoints = 0; i32 numWaypoints = 0;
// Write path // Write path
// TODO: Write end pos
while (pos.x != desc->start.x || pos.y != desc->start.y) { while (pos.x != desc->start.x || pos.y != desc->start.y) {
Position waypoint = { Position waypoint = {
pos.x * map->tileWidth + map->tileWidth * 0.5f, pos.x * map->tileWidth + map->tileWidth * 0.5f,
@@ -113,6 +114,10 @@ bool findPath(const PathfindingDesc *desc) {
pos.y -= visit.y; pos.y -= visit.y;
pathLen++; pathLen++;
} }
if (pathLen == 0) {
bzObjectPoolRelease(desc->pool, pathData);
pathData = NULL;
}
out->paths = pathData; out->paths = pathData;
// Reverse paths // Reverse paths

View File

@@ -22,7 +22,9 @@ void entityAdded(ecs_iter_t *it);
/* /*
* 0: Game (singleton) * 0: Game (singleton)
* 1: Position * 1: Position
* 2: MoveForce * 2: Size
* 3: MoveForce
* 4: SpatialGridID
*/ */
void entityUpdatePhysics(ecs_iter_t *it); void entityUpdatePhysics(ecs_iter_t *it);

View File

@@ -12,6 +12,19 @@ void entityAdded(ecs_iter_t *it) {
} }
void entityUpdatePhysics(ecs_iter_t *it) { void entityUpdatePhysics(ecs_iter_t *it) {
Game *game = ecs_singleton_get_mut(ECS, Game);
Position *pos = ecs_field(it, Position, 1);
Size *size = ecs_field(it, Size, 2);
//MoveForce *force = ecs_field(it, MoveForce, 3);
SpatialGridID *spatialID = ecs_field(it, SpatialGridID, 3);
for (i32 i = 0; i < it->count; i++) {
//bzSpatialGridUpdate(game->entityGrid, spatialID[i], pos[i].x, pos[i].y, size[i].x, size[i].y);
ecs_entity_t *e = bzSpatialGridGetData(game->entityGrid, spatialID[i]);
BZ_ASSERT(*e == it->entities[i]);
bzSpatialGridRemove(game->entityGrid, spatialID[i]);
spatialID[i] = bzSpatialGridInsert(game->entityGrid, &(it->entities[i]), pos[i].x, pos[i].y, size[i].x, size[i].y);
}
} }
@@ -46,6 +59,7 @@ void renderEntities(ecs_iter_t *it) {
if (t[i].flipX) src.width *= -1.0f; if (t[i].flipX) src.width *= -1.0f;
if (t[i].flipY) src.height *= -1.0f; if (t[i].flipY) src.height *= -1.0f;
DrawTexturePro(t[i].texture, src, dst, origin, r[i], WHITE); DrawTexturePro(t[i].texture, src, dst, origin, r[i], WHITE);
DrawRectangleLines(dst.x - dst.width * 0.5f, dst.y, dst.width, dst.height, RED);
} }
} }
@@ -74,6 +88,7 @@ void updatePos(ecs_iter_t *it) {
#include <stdlib.h> #include <stdlib.h>
void targetFinish(ecs_iter_t *it) { void targetFinish(ecs_iter_t *it) {
const Game *game = ecs_singleton_get(ECS, Game); const Game *game = ecs_singleton_get(ECS, Game);
if (game == NULL) return;
for (i32 i = 0; i < it->count; i++) { for (i32 i = 0; i < it->count; i++) {
ecs_entity_t e = it->entities[i]; ecs_entity_t e = it->entities[i];