Add trees as entities
This commit is contained in:
@@ -1,5 +1,8 @@
|
|||||||
#include "components.h"
|
#include "components.h"
|
||||||
|
|
||||||
|
ECS_TAG_DECLARE(TextureTerrain);
|
||||||
|
ECS_TAG_DECLARE(TextureBuildings);
|
||||||
|
ECS_TAG_DECLARE(TextureEntities);
|
||||||
|
|
||||||
ECS_COMPONENT_DECLARE(TilePosition);
|
ECS_COMPONENT_DECLARE(TilePosition);
|
||||||
ECS_COMPONENT_DECLARE(TileSize);
|
ECS_COMPONENT_DECLARE(TileSize);
|
||||||
@@ -8,6 +11,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(Resource);
|
||||||
ECS_COMPONENT_DECLARE(SpatialGridID);
|
ECS_COMPONENT_DECLARE(SpatialGridID);
|
||||||
ECS_COMPONENT_DECLARE(Rotation);
|
ECS_COMPONENT_DECLARE(Rotation);
|
||||||
ECS_COMPONENT_DECLARE(Health);
|
ECS_COMPONENT_DECLARE(Health);
|
||||||
@@ -17,6 +21,10 @@ ECS_COMPONENT_DECLARE(Animation);
|
|||||||
ECS_COMPONENT_DECLARE(Path);
|
ECS_COMPONENT_DECLARE(Path);
|
||||||
|
|
||||||
void initComponentIDs(ecs_world_t *ecs) {
|
void initComponentIDs(ecs_world_t *ecs) {
|
||||||
|
ECS_TAG_DEFINE(ecs, TextureTerrain);
|
||||||
|
ECS_TAG_DEFINE(ecs, TextureBuildings);
|
||||||
|
ECS_TAG_DEFINE(ecs, TextureEntities);
|
||||||
|
|
||||||
ECS_COMPONENT_DEFINE(ecs, TilePosition);
|
ECS_COMPONENT_DEFINE(ecs, TilePosition);
|
||||||
ECS_COMPONENT_DEFINE(ecs, TileSize);
|
ECS_COMPONENT_DEFINE(ecs, TileSize);
|
||||||
ECS_COMPONENT_DEFINE(ecs, Owner);
|
ECS_COMPONENT_DEFINE(ecs, Owner);
|
||||||
@@ -24,6 +32,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, Resource);
|
||||||
ECS_COMPONENT_DEFINE(ecs, SpatialGridID);
|
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);
|
||||||
|
|||||||
@@ -6,8 +6,9 @@
|
|||||||
|
|
||||||
#include "utils/building_types.h"
|
#include "utils/building_types.h"
|
||||||
|
|
||||||
|
extern ECS_TAG_DECLARE(TextureTerrain);
|
||||||
#define ecs_set_p(world, e, T, ...) ecs_set_id(world, e, ecs_id(T), sizeof(T), (T*)__VA_ARGS__)
|
extern ECS_TAG_DECLARE(TextureBuildings);
|
||||||
|
extern ECS_TAG_DECLARE(TextureEntities);
|
||||||
|
|
||||||
typedef struct TilePosition {
|
typedef struct TilePosition {
|
||||||
BzTile x;
|
BzTile x;
|
||||||
@@ -32,6 +33,20 @@ 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 enum ResourceType {
|
||||||
|
RES_IRON,
|
||||||
|
RES_WOOD,
|
||||||
|
RES_GOLD,
|
||||||
|
RES_FOOD,
|
||||||
|
RES_COUNT,
|
||||||
|
} ResourceType;
|
||||||
|
|
||||||
|
typedef struct Resource {
|
||||||
|
ResourceType type;
|
||||||
|
i32 amount;
|
||||||
|
} Resource;
|
||||||
|
extern ECS_COMPONENT_DECLARE(Resource);
|
||||||
|
|
||||||
typedef BzSpatialGridID SpatialGridID;
|
typedef BzSpatialGridID SpatialGridID;
|
||||||
extern ECS_COMPONENT_DECLARE(SpatialGridID);
|
extern ECS_COMPONENT_DECLARE(SpatialGridID);
|
||||||
|
|
||||||
|
|||||||
@@ -106,6 +106,9 @@ bool init(void *userData) {
|
|||||||
.userDataSize=sizeof(ecs_entity_t)
|
.userDataSize=sizeof(ecs_entity_t)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
bzTileMapOverrideLayer(&game->map, LAYER_TREES, initTreesLayer);
|
||||||
|
bzTileMapOverrideLayer(&game->map, LAYER_TREES2, initTreesLayer);
|
||||||
|
|
||||||
bzTileMapOverrideLayer(&game->map, LAYER_BUILDING_OWNER, initBuildingsLayer);
|
bzTileMapOverrideLayer(&game->map, LAYER_BUILDING_OWNER, initBuildingsLayer);
|
||||||
|
|
||||||
bzTileMapOverrideObjectGroup(&game->map, OBJECTS_GAME, initGameObjectsLayer);
|
bzTileMapOverrideObjectGroup(&game->map, OBJECTS_GAME, initGameObjectsLayer);
|
||||||
@@ -116,7 +119,11 @@ bool init(void *userData) {
|
|||||||
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, 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, renderTerrain, EcsOnUpdate, Position, Size, Rotation, TextureRegion, TextureTerrain);
|
||||||
|
ECS_SYSTEM(ECS, renderBuildings, EcsOnUpdate, Position, Size, Rotation, TextureRegion, TextureBuildings);
|
||||||
|
ECS_SYSTEM(ECS, renderEntities, EcsOnUpdate, Position, Size, Rotation, TextureRegion, TextureEntities);
|
||||||
|
|
||||||
ECS_OBSERVER(ECS, targetFinish, EcsOnRemove, TargetPosition);
|
ECS_OBSERVER(ECS, targetFinish, EcsOnRemove, TargetPosition);
|
||||||
ECS_OBSERVER(ECS, startPath, EcsOnSet, Path);
|
ECS_OBSERVER(ECS, startPath, EcsOnSet, Path);
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ bool initEntityObjectsLayer(BzTileMap *map, BzTileObjectGroup *objectGroup) {
|
|||||||
BzTileObject object = objectGroup->objects[i];
|
BzTileObject object = objectGroup->objects[i];
|
||||||
ecs_entity_t e = ecs_new_id(ECS);
|
ecs_entity_t e = ecs_new_id(ECS);
|
||||||
game->entity = e;
|
game->entity = e;
|
||||||
|
ecs_add(ECS, e, TextureEntities);
|
||||||
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,
|
BzSpatialGridID spatialID = bzSpatialGridInsert(game->entityGrid, &e,
|
||||||
@@ -70,7 +71,6 @@ bool initBuildingsLayer(BzTileMap *map, BzTileLayer *layer) {
|
|||||||
TileSize size = {};
|
TileSize size = {};
|
||||||
getBuildingSize(buildingTile, &size.sizeX, &size.sizeY);
|
getBuildingSize(buildingTile, &size.sizeX, &size.sizeY);
|
||||||
bzTileLayerSetTile(ownershipLayer, 0, x, y, size.sizeX, size.sizeY);
|
bzTileLayerSetTile(ownershipLayer, 0, x, y, size.sizeX, size.sizeY);
|
||||||
bzLogInfo("Got size: %2d %2d", size.sizeX, size.sizeY);
|
|
||||||
|
|
||||||
ecs_entity_t e = ecs_new_id(ECS);
|
ecs_entity_t e = ecs_new_id(ECS);
|
||||||
ecs_set(ECS, e, TilePosition, {.x=x, .y=y});
|
ecs_set(ECS, e, TilePosition, {.x=x, .y=y});
|
||||||
@@ -85,3 +85,32 @@ bool initBuildingsLayer(BzTileMap *map, BzTileLayer *layer) {
|
|||||||
return true;
|
return true;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool initTreesLayer(BzTileMap *map, BzTileLayer *layer) {
|
||||||
|
Game *game = ecs_singleton_get_mut(ECS, Game);
|
||||||
|
|
||||||
|
BzTileset *tileset = bzTileLayerGetTileset(map, layer);
|
||||||
|
|
||||||
|
for (i32 y = 0; y < layer->height; y++) {
|
||||||
|
for (i32 x = 0; x < layer->width; x++) {
|
||||||
|
BzTile tile = layer->data[y * layer->width + x];
|
||||||
|
BzTile layerTile = tile;
|
||||||
|
tile = bzTilesetGetTile(tileset, tile);
|
||||||
|
if (tile != 185) continue; // Not a tree
|
||||||
|
|
||||||
|
f32 sizeX = tileset->tileWidth;
|
||||||
|
f32 sizeY = tileset->tileHeight;
|
||||||
|
f32 posX = layer->offsetX + x * sizeX + sizeX * 0.5f;
|
||||||
|
f32 posY = layer->offsetY + y * sizeY + sizeY * 0.5f;
|
||||||
|
ecs_entity_t e = ecs_new_id(ECS);
|
||||||
|
ecs_add(ECS, e, TextureTerrain);
|
||||||
|
ecs_set(ECS, e, Position, {posX, posY});
|
||||||
|
ecs_set(ECS, e, Size, {sizeX, sizeY});
|
||||||
|
ecs_set(ECS, e, Rotation, {0});
|
||||||
|
ecs_set(ECS, e, TextureRegion, {tileset->tiles, bzTilesetGetTileRegion(tileset, layerTile)});
|
||||||
|
ecs_set(ECS, e, Resource, {RES_WOOD, 20});
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|||||||
@@ -9,4 +9,6 @@ bool initEntityObjectsLayer(BzTileMap *map, BzTileObjectGroup *objectGroup);
|
|||||||
|
|
||||||
bool initBuildingsLayer(BzTileMap *map, BzTileLayer *layer);
|
bool initBuildingsLayer(BzTileMap *map, BzTileLayer *layer);
|
||||||
|
|
||||||
|
bool initTreesLayer(BzTileMap *map, BzTileLayer *layer);
|
||||||
|
|
||||||
#endif //PIXELDEFENSE_MAP_INITIALIZATION_H
|
#endif //PIXELDEFENSE_MAP_INITIALIZATION_H
|
||||||
|
|||||||
@@ -28,8 +28,14 @@ void entityAdded(ecs_iter_t *it);
|
|||||||
*/
|
*/
|
||||||
void entityUpdatePhysics(ecs_iter_t *it);
|
void entityUpdatePhysics(ecs_iter_t *it);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 0:
|
||||||
|
*/
|
||||||
|
void renderTerrain(ecs_iter_t *it);
|
||||||
|
void renderBuildings(ecs_iter_t *it);
|
||||||
void renderEntities(ecs_iter_t *it);
|
void renderEntities(ecs_iter_t *it);
|
||||||
|
|
||||||
|
//void renderEntities(ecs_iter_t *it);
|
||||||
void updateAnimations(ecs_iter_t *it);
|
void updateAnimations(ecs_iter_t *it);
|
||||||
void updatePos(ecs_iter_t *it);
|
void updatePos(ecs_iter_t *it);
|
||||||
void targetFinish(ecs_iter_t *it);
|
void targetFinish(ecs_iter_t *it);
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ void updateAnimations(ecs_iter_t *it) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void renderEntities(ecs_iter_t *it) {
|
static void render(ecs_iter_t *it) {
|
||||||
Position *p = ecs_field(it, Position, 1);
|
Position *p = ecs_field(it, Position, 1);
|
||||||
Size *s = ecs_field(it, Size, 2);
|
Size *s = ecs_field(it, Size, 2);
|
||||||
Rotation *r = ecs_field(it, Rotation, 3);
|
Rotation *r = ecs_field(it, Rotation, 3);
|
||||||
@@ -59,10 +59,20 @@ 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);
|
//DrawRectangleLines(dst.x - dst.width * 0.5f, dst.y - dst.height * 0.5f, dst.width, dst.height, RED);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void renderTerrain(ecs_iter_t *it) {
|
||||||
|
render(it);
|
||||||
|
}
|
||||||
|
void renderBuildings(ecs_iter_t *it) {
|
||||||
|
render(it);
|
||||||
|
}
|
||||||
|
void renderEntities(ecs_iter_t *it) {
|
||||||
|
render(it);
|
||||||
|
}
|
||||||
|
|
||||||
void updatePos(ecs_iter_t *it) {
|
void updatePos(ecs_iter_t *it) {
|
||||||
Position *pos = ecs_field(it, Position, 1);
|
Position *pos = ecs_field(it, Position, 1);
|
||||||
TargetPosition *target = ecs_field(it, TargetPosition, 2);
|
TargetPosition *target = ecs_field(it, TargetPosition, 2);
|
||||||
|
|||||||
Reference in New Issue
Block a user