Implement farming

This commit is contained in:
2024-01-28 17:33:55 +01:00
parent 6ee46da8ab
commit 205fb57f07
10 changed files with 85 additions and 73 deletions

View File

@@ -88,17 +88,26 @@ ecs_entity_t placeBuilding(Game *game, BuildingType type,
region.rec.height *= sizeY;
ecs_set_ptr(ECS, building, TextureRegion, &region);
bzTileMapSetCollisions(&game->map, true, COLL_LAYER_BUILDINGS, posX, posY, sizeX, sizeY);
bool hasCollision = true;
switch (type) {
case BUILDING_KEEP:
ecs_set(ECS, building, AddPopCapacity, {10});
ecs_add_id(ECS, building, Storage);
break;
case BUILDING_WHEAT_0:
case BUILDING_WHEAT_1:
hasCollision = false;
ecs_add_id(ECS, building, Harvestable);
ecs_set(ECS, building, Resource, {RES_FOOD, INFINITY});
break;
default:
break;
}
if (hasCollision)
bzTileMapSetCollisions(&game->map, true, COLL_LAYER_BUILDINGS, posX, posY, sizeX, sizeY);
return building;
}
@@ -166,12 +175,14 @@ Vector2 getPositionNearBuilding(ecs_entity_t building, Vector2 fromPos) {
Vector2 pos = *ecs_get(ECS, building, Position);
HitBox hitbox = *ecs_get(ECS, building, HitBox);
Vector2 center = entityGetCenter(pos, hitbox);
Vector2 size = {hitbox.width, hitbox.height};
size = Vector2SubtractValue(size, 10.0f);
Vector2 dir = Vector2Normalize(Vector2Subtract(fromPos, pos));
Vector2 dir = Vector2Normalize(Vector2Subtract(fromPos, center));
dir = Vector2Multiply(dir, size);
pos = Vector2Add(pos, dir);
return pos;
center = Vector2Add(center, dir);
return center;
}

View File

@@ -208,11 +208,10 @@ typedef struct Building {
Vec2i pos;
Vec2i size;
} Building;
extern ECS_COMPONENT_DECLARE(Building);
extern ECS_TAG_DECLARE(Storage);
extern ECS_COMPONENT_DECLARE(Building);
extern ECS_TAG_DECLARE(Harvestable);
extern ECS_TAG_DECLARE(Buildable);
extern ECS_TAG_DECLARE(Workable);
extern ECS_TAG_DECLARE(Attackable);
void initComponentIDs(ecs_world_t *ecs);

View File

@@ -37,8 +37,6 @@ static OwnerType getOwnerType(BzTileID tile) {
typedef enum TerrainType {
TERRAIN_NONE = -1,
TERRAIN_GOLD_ORE,
TERRAIN_WHEAT_0,
TERRAIN_WHEAT_1,
TERRAIN_COUNT,
} TerrainType;
@@ -295,6 +293,8 @@ typedef enum BuildingType {
BUILDING_MARKET,
BUILDING_MILL,
BUILDING_WAREHOUSE,
BUILDING_WHEAT_0,
BUILDING_WHEAT_1,
BUILDING_COUNT,
} BuildingType;
@@ -307,6 +307,10 @@ static BuildingType getTileBuilding(BzTileID tile) {
case 5910:
case 5911:
return BUILDING_KEEP;
case 6400:
return BUILDING_WHEAT_1;
case 6401:
return BUILDING_WHEAT_0;
case 6405:
case 6406:
return BUILDING_ARCHERY_RANGE;
@@ -345,6 +349,8 @@ static BzTileID getBuildingTile(BuildingType type) {
switch (type) {
case BUILDING_BARRACKS: return 5638;
case BUILDING_KEEP: return 5654;
case BUILDING_WHEAT_1: return 6400;
case BUILDING_WHEAT_0: return 6401;
case BUILDING_ARCHERY_RANGE: return 6405;
case BUILDING_WAREHOUSE: return 6433;
case BUILDING_HOUSE_01: return 6666;
@@ -367,6 +373,8 @@ static const char *getBuildingStr(BuildingType type) {
switch (type) {
case BUILDING_BARRACKS: return "barracks";
case BUILDING_KEEP: return "keep";
case BUILDING_WHEAT_1: return "wheat_1";
case BUILDING_WHEAT_0: return "wheat_0";
case BUILDING_ARCHERY_RANGE: return "archery_range";
case BUILDING_WAREHOUSE: return "warehouse";
case BUILDING_HOUSE_01: return "house_01";
@@ -395,6 +403,14 @@ static BuildingType getBuildingSize(BuildingType type, i32 *outWidth, i32 *outHe
if (outWidth) *outWidth = 2;
if (outHeight) *outHeight = 2;
break;
case BUILDING_WHEAT_1:
if (outWidth) *outWidth = 1;
if (outHeight) *outHeight = 1;
break;
case BUILDING_WHEAT_0:
if (outWidth) *outWidth = 1;
if (outHeight) *outHeight = 1;
break;
case BUILDING_ARCHERY_RANGE:
if (outWidth) *outWidth = 2;
if (outHeight) *outHeight = 1;
@@ -1707,8 +1723,6 @@ static bool hasEntityHitBoxRec(BzTile tile) {
case 5891:
case 6146:
case 6147:
case 6400:
case 6401:
case 6402:
case 6403:
case 6656:
@@ -1734,8 +1748,6 @@ static Rectangle getEntityHitBoxRec(BzTile tile) {
case 5891: return (Rectangle) {6, 2, 4, 10};
case 6146: return (Rectangle) {6, 2, 4, 10};
case 6147: return (Rectangle) {6, 2, 4, 10};
case 6400: return (Rectangle) {4, 2, 8, 10};
case 6401: return (Rectangle) {4, 2, 8, 10};
case 6402: return (Rectangle) {6, 2, 4, 10};
case 6403: return (Rectangle) {6, 2, 4, 10};
case 6656: return (Rectangle) {6, 2, 4, 10};

View File

@@ -553,7 +553,6 @@ void igInspectWindow(ecs_entity_t entity, bool *open) {
igTagCheckbox("Selected", ECS, entity, Selected);
igTagCheckbox("Storage", ECS, entity, Storage);
igTagCheckbox("Harvestable", ECS, entity, Harvestable);
igTagCheckbox("Workable", ECS, entity, Workable);
igTagCheckbox("Attackable", ECS, entity, Attackable);
}
if (ecs_has(ECS, entity, BzBTState) &&

View File

@@ -106,13 +106,20 @@ void inputUnitAction(Game *game, InputState *input) {
ecs_entity_t entity = it.entities[i];
Position target = *ecs_get(ECS, taskEntity, Position);
f32 proximity = 6.0f;
if (resource.type == RES_FOOD)
proximity = 2.0f;
Worker *worker = ecs_get_mut(ECS, entity, Worker);
worker->carryRes = resource.type;
setAIBehaviour(entity, game->BTs.workerHarvest, &(AIBlackboard) {
.as.worker = {
.harvestType = resource.type,
.harvestTarget = taskEntity,
.harvestPos = target,
},
.proximity = 6.0f,
.proximity = proximity,
});
}
}

View File

@@ -58,7 +58,7 @@ void drawGameUI(Game *game, f32 dt) {
BUILDING_WAREHOUSE,
BUILDING_MARKET,
BUILDING_MILL,
BUILDING_HOUSE_02, // placeholder for farm
BUILDING_WHEAT_0,
BUILDING_BARRACKS,
BUILDING_ARCHERY_RANGE
};