Improve unit recruiting
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
#include "../game_state.h"
|
||||
#include "../input.h"
|
||||
#include "../pathfinding.h"
|
||||
#include "../entity_factory.h"
|
||||
|
||||
#include <math.h>
|
||||
#include <raymath.h>
|
||||
@@ -236,6 +237,38 @@ void entityFollowPath(ecs_iter_t *it) {
|
||||
}
|
||||
}
|
||||
|
||||
void updateBuildingRecruitment(ecs_iter_t *it) {
|
||||
Game *game = ecs_singleton_get_mut(ECS, Game);
|
||||
Owner *owner = ecs_field(it, Owner, 1);
|
||||
Building *building = ecs_field(it, Building, 2);
|
||||
BuildingRecruitInfo *info = ecs_field(it, BuildingRecruitInfo, 3);
|
||||
|
||||
f32 dt = GetFrameTime();
|
||||
for (i32 i = 0; i < it->count; i++) {
|
||||
Player player = owner[i].player;
|
||||
Vector2 placePos = {
|
||||
(building[i].pos.x + building[i].size.x * 0.5f) * 16.0f,
|
||||
(building[i].pos.y + building->size.y + 0.5f) * 16.0f
|
||||
};
|
||||
placePos.x += GetRandomValue(-building[i].size.x, building[i].size.x);
|
||||
placePos.y += GetRandomValue(-4, 4);
|
||||
|
||||
for (i32 slotIdx = 0; slotIdx < info[i].numSlots; i++) {
|
||||
BuildingRecruitSlot *slot = &info[i].slots[slotIdx];
|
||||
|
||||
if (slot->numRecruiting <= 0) continue;
|
||||
slot->elapsed += dt;
|
||||
if (slot->elapsed >= slot->recruitTime) {
|
||||
slot->elapsed = 0;
|
||||
bzLogInfo("spawned");
|
||||
entityRecruit(slot->entityType, placePos, player, game);
|
||||
slot->numRecruiting--;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void renderColliders(ecs_iter_t *it) {
|
||||
Position *pos = ecs_field(it, Position, 1);
|
||||
HitBox *hitbox = ecs_field(it, HitBox , 2);
|
||||
|
||||
@@ -191,29 +191,30 @@ void drawGameUI(Game *game, f32 dt) {
|
||||
};
|
||||
placePos.x += GetRandomValue(-16, 16);
|
||||
placePos.y += GetRandomValue(-4, 4);
|
||||
PlayerResources playerRes = game->playerResources[game->player];
|
||||
switch (building.type) {
|
||||
case BUILDING_KEEP: {
|
||||
Rectangle rec = getTextureRect(getEntityTile(ENTITY_WORKER));
|
||||
PlayerResources *playerRes = &game->playerResources[game->player];
|
||||
|
||||
bool canRecruit = ecs_has(ECS, buildingEntity, BuildingRecruitInfo);
|
||||
if (canRecruit) {
|
||||
BuildingRecruitInfo *info = ecs_get_mut(ECS, buildingEntity, BuildingRecruitInfo);
|
||||
for (i32 i = 0; i < info->numSlots; i++) {
|
||||
BuildingRecruitSlot *slot = &info->slots[i];
|
||||
const char *label = getEntityStr(slot->entityType);
|
||||
Rectangle rec = getTextureRect(getEntityTile(slot->entityType));
|
||||
bool selected = false;
|
||||
uiGameBuild("Worker", rec, tex, canAffordEntity(ENTITY_WORKER, playerRes), &selected);
|
||||
if (selected)
|
||||
entityHire(ENTITY_WORKER, placePos, game->player, game);
|
||||
break;
|
||||
bool canAfford = canAffordEntity(slot->entityType, *playerRes);
|
||||
canAfford &= playerRes->pop < playerRes->popCapacity;
|
||||
uiGameBuild(label, rec, tex, canAfford, &selected);
|
||||
if (selected) {
|
||||
i32 res[RES_COUNT] = {0,};
|
||||
getEntityCost(slot->entityType, res);
|
||||
playerRes->food -= res[RES_FOOD];
|
||||
playerRes->wood -= res[RES_WOOD];
|
||||
playerRes->gold -= res[RES_GOLD];
|
||||
|
||||
slot->numRecruiting++;
|
||||
}
|
||||
|
||||
}
|
||||
case BUILDING_BARRACKS: {
|
||||
Rectangle soldierRec = getTextureRect(getEntityTile(ENTITY_SOLDIER));
|
||||
Rectangle warriorRec = getTextureRect(getEntityTile(ENTITY_WARRIOR));
|
||||
bool selected = false;
|
||||
uiGameBuild("Soldier", soldierRec, tex, canAffordEntity(ENTITY_SOLDIER, playerRes), &selected);
|
||||
if (selected)
|
||||
entityHire(ENTITY_SOLDIER, placePos, game->player, game);
|
||||
selected = false;
|
||||
uiGameBuild("Warrior", warriorRec, tex, canAffordEntity(ENTITY_WARRIOR, playerRes), &selected);
|
||||
if (selected)
|
||||
entityHire(ENTITY_WARRIOR, placePos, game->player, game);
|
||||
}
|
||||
default:;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -104,6 +104,8 @@ void setupSystems() {
|
||||
ECS_SYSTEM(ECS, entityMoveToTarget, EcsOnUpdate, Position, Velocity, TargetPosition, Steering);
|
||||
ECS_SYSTEM(ECS, entityFollowPath, EcsOnUpdate, Path);
|
||||
|
||||
ECS_SYSTEM(ECS, updateBuildingRecruitment, EcsOnUpdate, Owner, Building, BuildingRecruitInfo);
|
||||
|
||||
ECS_SYSTEM(ECS, resetHarvestCount, EcsOnUpdate, Harvestable);
|
||||
ECS_SYSTEM(ECS, updateAISystem, EcsOnUpdate, BzBTState);
|
||||
|
||||
|
||||
@@ -141,6 +141,14 @@ void entityMoveToTarget(ecs_iter_t *it);
|
||||
*/
|
||||
void entityFollowPath(ecs_iter_t *it);
|
||||
|
||||
/*
|
||||
* 0: Game (singleton for spawning entities)
|
||||
* 1: Owner
|
||||
* 2: Building
|
||||
* 3: BuildingRecruitInfo
|
||||
*/
|
||||
void updateBuildingRecruitment(ecs_iter_t *it);
|
||||
|
||||
/*
|
||||
* 1: Position
|
||||
* 2: HitBox
|
||||
@@ -158,6 +166,8 @@ void renderOrientationDirection(ecs_iter_t *it);
|
||||
*/
|
||||
void renderDebugPath(ecs_iter_t *it);
|
||||
|
||||
|
||||
|
||||
/**********************************
|
||||
* Event Systems
|
||||
**********************************/
|
||||
|
||||
Reference in New Issue
Block a user