Add static keyword to ease inout sine function
This commit is contained in:
41
game/systems/s_ai.c
Normal file
41
game/systems/s_ai.c
Normal file
@@ -0,0 +1,41 @@
|
||||
#include "systems.h"
|
||||
|
||||
#include "game_state.h"
|
||||
|
||||
#include "unit_ai.h"
|
||||
#include "unit_actions.h"
|
||||
|
||||
void handleUnitActionsSystem(ecs_iter_t *it) {
|
||||
Game *game = ecs_singleton_get_mut(ECS, Game);
|
||||
UnitAction *action = ecs_field(it, UnitAction, 1);
|
||||
|
||||
for (i32 i = 0; i < it->count; i++) {
|
||||
ecs_entity_t entity = it->entities[i];
|
||||
handleAction(entity, &action[i], game);
|
||||
}
|
||||
}
|
||||
|
||||
void updateUnitAISystem(ecs_iter_t *it) {
|
||||
Game *game = ecs_singleton_get_mut(ECS, Game);
|
||||
UnitAI *unitAI = ecs_field(it, UnitAI, 1);
|
||||
UnitAction *action = ecs_field(it, UnitAction, 2);
|
||||
|
||||
for (i32 i = 0; i < it->count; i++) {
|
||||
ecs_entity_t entity = it->entities[i];
|
||||
|
||||
Action *firstAction = action[i].first;
|
||||
unitAI[i].action = firstAction;
|
||||
|
||||
updateUnitAI(entity, &unitAI[i], game);
|
||||
}
|
||||
}
|
||||
|
||||
void updateUnitActionsSystem(ecs_iter_t *it) {
|
||||
Game *game = ecs_singleton_get_mut(ECS, Game);
|
||||
UnitAction *action = ecs_field(it, UnitAction, 1);
|
||||
|
||||
for (i32 i = 0; i < it->count; i++) {
|
||||
ecs_entity_t entity = it->entities[i];
|
||||
updateAction(&action[i], game);
|
||||
}
|
||||
}
|
||||
258
game/systems/s_entity.c
Normal file
258
game/systems/s_entity.c
Normal file
@@ -0,0 +1,258 @@
|
||||
#include "systems.h"
|
||||
|
||||
|
||||
#include "game_state.h"
|
||||
#include "input.h"
|
||||
#include "pathfinding.h"
|
||||
|
||||
#include <math.h>
|
||||
#include <raymath.h>
|
||||
|
||||
bool entitySetPath(const ecs_entity_t entity, const Vector2 target, Game *game) {
|
||||
const Vector2 *pPath = ecs_get(ECS, entity, Position);
|
||||
BZ_ASSERT(pPath);
|
||||
const Vector2 start = *pPath;
|
||||
|
||||
Path path = {NULL, 0};
|
||||
const bool foundPath = pathfindAStar(&(PathfindingDesc) {
|
||||
.start = start,
|
||||
.target = target,
|
||||
.map = &game->map,
|
||||
.outPath = &path,
|
||||
.pool = game->pools.pathData,
|
||||
.alloc = &game->stackAlloc,
|
||||
});
|
||||
if (foundPath) {
|
||||
BZ_ASSERT(path.paths);
|
||||
ecs_set_ptr(ECS, entity, Path, &path);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
static Position getBottomLeftPos(Position pos, Size size) {
|
||||
return (Position) {pos.x - size.x * 0.5f, pos.y - size.y * 0.5f};
|
||||
}
|
||||
|
||||
|
||||
|
||||
void entityPathRemove(ecs_iter_t *it) {
|
||||
Game *game = ecs_singleton_get_mut(ECS, Game);
|
||||
for (i32 i = 0; i < it->count; i++) {
|
||||
ecs_entity_t entity = it->entities[i];
|
||||
ecs_remove(ECS, entity, TargetPosition);
|
||||
}
|
||||
}
|
||||
|
||||
void entityUpdateSpatialID(ecs_iter_t *it) {
|
||||
Game *game = ecs_singleton_get_mut(ECS, Game);
|
||||
Position *position = ecs_field(it, Position, 1);
|
||||
Position *size = ecs_field(it, Position, 2);
|
||||
//Velocity *velocity = ecs_field(it, Velocity, 3);
|
||||
SpatialGridID *id = ecs_field(it, SpatialGridID, 4);
|
||||
|
||||
for (i32 i = 0; i < it->count; i++) {
|
||||
Position pos = getBottomLeftPos(position[i], size[i]);
|
||||
bzSpatialGridUpdate(game->entityGrid, id[i], pos.x, pos.y, size[i].x, size[i].y);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void entityUpdateKinematic(ecs_iter_t *it) {
|
||||
Position *position = ecs_field(it, Position, 1);
|
||||
Rotation *rotation = ecs_field(it, Rotation, 2);
|
||||
Velocity *velocity = ecs_field(it, Velocity, 3);
|
||||
Steering *steering = ecs_field(it, Steering, 4);
|
||||
|
||||
f32 dt = it->delta_time;
|
||||
|
||||
for (i32 i = 0; i < it->count; i++) {
|
||||
// Update position and rotation
|
||||
// position += velocity * dt
|
||||
position[i] = Vector2Add(position[i], Vector2Scale(velocity[i], dt));
|
||||
|
||||
// Update velocity and angular velocity
|
||||
// velocity += steering.liner * dt
|
||||
velocity[i] = Vector2Add(velocity[i], Vector2Scale(steering[i], dt * 10));
|
||||
|
||||
if (Vector2LengthSqr(steering[i]) == 0) {
|
||||
// Decay velocity
|
||||
velocity[i] = Vector2Scale(velocity[i], 1 - (dt * 5.0f));
|
||||
}
|
||||
// Reset steering
|
||||
steering[i] = Vector2Zero();
|
||||
|
||||
{
|
||||
const InputState *input = ecs_singleton_get(ECS, InputState);
|
||||
|
||||
Vector2 mouse = input->mouseDownWorld;
|
||||
f32 rot = Vector2Angle(position[i], mouse) + 270 * DEG2RAD;
|
||||
rotation[i] = rot;
|
||||
|
||||
}
|
||||
|
||||
// Check for speeding and clip
|
||||
const f32 maxSpeed = 15.0f;
|
||||
if (Vector2Length(velocity[i]) > maxSpeed) {
|
||||
velocity[i] = Vector2Normalize(velocity[i]);
|
||||
velocity[i] = Vector2Scale(velocity[i], maxSpeed);
|
||||
}
|
||||
|
||||
// Update flipX
|
||||
ecs_entity_t entity = it->entities[i];
|
||||
if (ecs_has(it->world, entity, TextureRegion)) {
|
||||
TextureRegion *text = ecs_get_mut(it->world, entity, TextureRegion);
|
||||
text->flipX = rotation[i] >= 0.0f * RAD2DEG && rotation[i] <= 180.0f * RAD2DEG;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void entityMoveToTarget(ecs_iter_t *it) {
|
||||
Position *position = ecs_field(it, Position, 1);
|
||||
Rotation *rotation = ecs_field(it, Rotation, 2);
|
||||
Velocity *velocity = ecs_field(it, Velocity, 3);
|
||||
|
||||
TargetPosition *targetPos = ecs_field(it, TargetPosition, 4);
|
||||
Steering *steering = ecs_field(it, Steering, 5);
|
||||
|
||||
for (i32 i = 0; i < it->count; i++) {
|
||||
Position target = targetPos[i];
|
||||
steering[i] = Vector2Subtract(target, position[i]);
|
||||
f32 dst = Vector2LengthSqr(steering[i]);
|
||||
f32 maxAccel = 10.0f;
|
||||
steering[i] = Vector2Normalize(steering[i]);
|
||||
steering[i] = Vector2Scale(steering[i], maxAccel);
|
||||
|
||||
if (Vector2Length(velocity[i]) > 10.0f) {
|
||||
f32 rot = Vector2Angle(position[i], target);
|
||||
rotation[i] = rot;
|
||||
}
|
||||
|
||||
if (dst < 8.0f) {
|
||||
// Arrived
|
||||
ecs_remove(ECS, it->entities[i], TargetPosition);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void entityFollowPath(ecs_iter_t *it) {
|
||||
const Game *game = ecs_singleton_get(ECS, Game);
|
||||
|
||||
Path *path = ecs_field(it, Path, 1);
|
||||
|
||||
for (i32 i = 0; i < it->count; i++) {
|
||||
const ecs_entity_t entity = it->entities[i];
|
||||
|
||||
if (!ecs_has(ECS, entity, TargetPosition)) {
|
||||
if (path[i].curWaypoint >= path[i].paths->numWaypoints) {
|
||||
path[i].curWaypoint = 0;
|
||||
PathData *oldPath = path[i].paths;
|
||||
bzObjectPoolRelease(game->pools.pathData, oldPath);
|
||||
path[i].paths = path[i].paths->next;
|
||||
if (!path[i].paths) ecs_remove(ECS, it->entities[i], Path);
|
||||
}
|
||||
if (path[i].paths) {
|
||||
TargetPosition target = path[i].paths->waypoints[path[i].curWaypoint];
|
||||
path[i].curWaypoint++;
|
||||
ecs_set_ptr(ECS, entity, TargetPosition, &target);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void entityUpdateAnimationState(ecs_iter_t *it) {
|
||||
Animation *anim = ecs_field(it, Animation, 1);
|
||||
TextureRegion *text = ecs_field(it, TextureRegion, 2);
|
||||
for (i32 i = 0; i < it->count; i++) {
|
||||
ecs_entity_t entity = it->entities[i];
|
||||
AnimType type = ANIM_IDLE;
|
||||
if (ecs_has(ECS, entity, Velocity)) {
|
||||
Velocity vel = *ecs_get(ECS, entity, Velocity);
|
||||
f32 len = Vector2Length(vel);
|
||||
if (len > 1.0f) {
|
||||
type = ANIM_WALK;
|
||||
text[i].flipX = vel.x < 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (type != anim[i].animType) {
|
||||
anim[i].animType = type;
|
||||
anim[i].sequence = entityGetAnimationSequence(anim[i].entityType, type);
|
||||
anim[i].curFrame = 0;
|
||||
anim[i].elapsed = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
void entityUpdateAnimation(ecs_iter_t *it) {
|
||||
Game *game = ecs_singleton_get_mut(ECS, Game);
|
||||
Animation *anim = ecs_field(it, Animation, 1);
|
||||
TextureRegion *texture = ecs_field(it, TextureRegion, 2);
|
||||
|
||||
float dt = GetFrameTime();
|
||||
|
||||
for (i32 i = 0; i < it->count; i++) {
|
||||
AnimationFrame frame = anim[i].frame;
|
||||
AnimationSequence seq = anim[i].sequence;
|
||||
|
||||
anim[i].elapsed += dt;
|
||||
if (anim[i].elapsed < frame.duration) continue;
|
||||
|
||||
i32 nextFrame = (anim[i].curFrame + 1) % seq.frameCount;
|
||||
anim[i].curFrame = nextFrame;
|
||||
anim[i].frame = entityGetAnimationFrame(anim[i].entityType, anim[i].animType, nextFrame);
|
||||
anim[i].elapsed = 0.0f;
|
||||
|
||||
texture[i].rec = bzTilesetGetTileRegion(anim[i].tileset, anim[i].frame.frame);
|
||||
}
|
||||
}
|
||||
|
||||
void renderColliders(ecs_iter_t *it) {
|
||||
Position *pos = ecs_field(it, Position, 1);
|
||||
Size *size = ecs_field(it, Size, 2);
|
||||
|
||||
for (i32 i = 0; i < it->count; i++) {
|
||||
f32 posX = pos[i].x - size[i].x * 0.5f;
|
||||
f32 posY = pos[i].y - size[i].y * 0.5f;
|
||||
DrawRectangleLines(posX, posY, size[i].x, size[i].y, RED);
|
||||
}
|
||||
}
|
||||
|
||||
void renderRotationDirection(ecs_iter_t *it) {
|
||||
Position *pos = ecs_field(it, Position, 1);
|
||||
Rotation *rot = ecs_field(it, Rotation, 2);
|
||||
|
||||
for (i32 i = 0; i < it->count; i++) {
|
||||
Vector2 v = {10.0f, 0.0f};
|
||||
v = Vector2Rotate(v, rot[i]);
|
||||
v = Vector2Add(v, pos[i]);
|
||||
DrawCircle(v.x, v.y, 1.0f, RED);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void renderDebugPath(ecs_iter_t *it) {
|
||||
Path *path = ecs_field(it, Path, 1);
|
||||
|
||||
for (i32 i = 0; i < it->count; i++) {
|
||||
PathData *pathData = path[i].paths;
|
||||
bool first = true;
|
||||
while (pathData) {
|
||||
for (i32 iPath = 0; iPath < pathData->numWaypoints; iPath++) {
|
||||
Color color = RED;
|
||||
if (first && iPath < path[i].curWaypoint)
|
||||
color = GREEN;
|
||||
else if (first && iPath == path[i].curWaypoint)
|
||||
color = ORANGE;
|
||||
color.a = 180;
|
||||
|
||||
Position pos = pathData->waypoints[iPath];
|
||||
DrawCircle(pos.x, pos.y, 3, color);
|
||||
}
|
||||
first = false;
|
||||
pathData = pathData->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
499
game/systems/s_input.c
Normal file
499
game/systems/s_input.c
Normal file
@@ -0,0 +1,499 @@
|
||||
#include "systems.h"
|
||||
#include "game_state.h"
|
||||
#include "input.h"
|
||||
#include "buildings.h"
|
||||
#include "pathfinding.h"
|
||||
#include "unit_ai.h"
|
||||
#include "unit_actions.h"
|
||||
|
||||
#include <rlImGui.h>
|
||||
#include <raymath.h>
|
||||
#include <rlgl.h>
|
||||
|
||||
Rectangle calculateEntityBounds(Position pos, Size size);
|
||||
bool getEntityBounds(ecs_entity_t entity, Position *outPos, Size *outSize, Rectangle *outBounds);
|
||||
|
||||
ecs_entity_t queryEntity(BzSpatialGrid *entityGrid, Vector2 point, ecs_entity_t tag);
|
||||
|
||||
bool pickEntity(BzSpatialGrid *entityGrid, Vector2 point, ecs_entity_t tag);
|
||||
void pickUnits(BzSpatialGrid *entityGrid, Rectangle area);
|
||||
|
||||
static void iterateSelectedUnits(ecs_query_t *query, void (*fn)(ecs_entity_t entity, Position *pos, Size *size));
|
||||
static void iterRemovePaths(ecs_entity_t entity, Position *pos, Size *size);
|
||||
|
||||
void placeUnits(i32 numUnits, f32 unitSpacing, Vector2 start, Vector2 end, BzTileMap *map, Vector2 **outPlaces);
|
||||
|
||||
void resetInputState(InputState *input) {
|
||||
input->cursor = CURSOR_NONE;
|
||||
input->state = INPUT_NONE;
|
||||
input->building = 0;
|
||||
}
|
||||
|
||||
void inputPrimaryAction(Game *game, InputState *input) {
|
||||
const MouseButton primaryBtn = input->mapping.primaryBtn;
|
||||
if (isInputBtnDragged(input, primaryBtn)) {
|
||||
Vector2 start = input->mouseDownWorld;
|
||||
Vector2 end = input->mouseWorld;
|
||||
if (start.x > end.x) {
|
||||
f32 tmp = start.x;
|
||||
start.x = end.x;
|
||||
end.x = tmp;
|
||||
}
|
||||
if (start.y > end.y) {
|
||||
f32 tmp = start.y;
|
||||
start.y = end.y;
|
||||
end.y = tmp;
|
||||
}
|
||||
input->pickArea = (Rectangle) {start.x, start.y, end.x - start.x, end.y - start.y};
|
||||
pickUnits(game->entityGrid, input->pickArea);
|
||||
}
|
||||
i32 selectedCount = ecs_query_entity_count(input->queries.selected);
|
||||
if (isInputBtnJustDragged(input, primaryBtn) && selectedCount > 0) {
|
||||
resetInputState(input);
|
||||
input->state = INPUT_SELECTED_UNITS;
|
||||
} else if (isInputBtnJustUp(input, primaryBtn)) {
|
||||
if (pickEntity(game->entityGrid, input->mouseDownWorld, Unit)) {
|
||||
resetInputState(input);
|
||||
input->state = INPUT_SELECTED_UNITS;
|
||||
} else if (pickEntity(game->entityGrid, input->mouseDownWorld, Harvestable)) {
|
||||
resetInputState(input);
|
||||
input->state = INPUT_SELECTED_OBJECT;
|
||||
} else if (pickEntity(game->entityGrid, input->mouseDownWorld, Buildable)) {
|
||||
resetInputState(input);
|
||||
input->state = INPUT_SELECTED_BUILDING;
|
||||
}
|
||||
selectedCount = ecs_query_entity_count(input->queries.selected);
|
||||
}
|
||||
|
||||
if (selectedCount == 0)
|
||||
resetInputState(input);
|
||||
}
|
||||
void inputUnitAction(Game *game, InputState *input) {
|
||||
ecs_query_t *query = input->queries.selected;
|
||||
BzTileMap *map = &game->map;
|
||||
const i32 numUnits = ecs_query_entity_count(query);
|
||||
BZ_ASSERT(numUnits > 0);
|
||||
|
||||
const MouseButton actionBtn = input->mapping.secondaryBtn;
|
||||
|
||||
input->cursor = CURSOR_NONE;
|
||||
ecs_entity_t taskEntity;
|
||||
if ((taskEntity = queryEntity(game->entityGrid, input->mouseWorld, Harvestable))) {
|
||||
input->cursor = CURSOR_COLLECT_WOOD;
|
||||
if (isInputBtnJustUp(input, actionBtn)) {
|
||||
ecs_defer_begin(ECS);
|
||||
ecs_iter_t it = ecs_query_iter(ECS, query);
|
||||
while (ecs_query_next(&it)) {
|
||||
for (i32 i = 0; i < it.count; i++) {
|
||||
const ecs_entity_t entity = it.entities[i];
|
||||
const Position target = *ecs_get(ECS, taskEntity, Position);
|
||||
setUnitAI(entity, game, &(const UnitAI) {
|
||||
.type = AI_WORKER_HARVEST,
|
||||
.as.workerHarvest.resource = RES_WOOD,
|
||||
.as.workerHarvest.target = taskEntity,
|
||||
.as.workerHarvest.targetPosition = target
|
||||
});
|
||||
//addAction(entity, game, &(const Action) {
|
||||
// .type = ACTION_MOVE_TO,
|
||||
// .as.moveTo.target = target,
|
||||
// .as.moveTo.proximityThreshold = 10.0f,
|
||||
//});
|
||||
//ecs_set(ECS, entity, HarvestTask, {taskEntity});
|
||||
goto while_break;
|
||||
}
|
||||
}
|
||||
while_break:
|
||||
ecs_iter_fini(&it);
|
||||
ecs_defer_end(ECS);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (isInputBtnJustUp(input, actionBtn)) {
|
||||
// Note: We mustn't use ecs ecs_remove_all since this will also
|
||||
// remove ongoing paths that are not part of this query.
|
||||
ecs_defer_begin(ECS);
|
||||
iterateSelectedUnits(query, iterRemovePaths);
|
||||
ecs_defer_end(ECS);
|
||||
|
||||
const Position target = input->mouseWorld;
|
||||
|
||||
ecs_iter_t it = ecs_query_iter(ECS, query);
|
||||
ecs_defer_begin(ECS);
|
||||
while (ecs_iter_next(&it)) {
|
||||
for (i32 i = 0; i < it.count; i++) {
|
||||
const ecs_entity_t entity = it.entities[i];
|
||||
entitySetPath(entity, target, game);
|
||||
}
|
||||
}
|
||||
ecs_defer_end(ECS);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void updatePlayerInput() {
|
||||
f32 dt = GetFrameTime();
|
||||
ImGuiIO *io = igGetIO();
|
||||
if (io->WantCaptureMouse || io->WantCaptureKeyboard)
|
||||
return;
|
||||
|
||||
Game *game = ecs_singleton_get_mut(ECS, Game);
|
||||
InputState *input = ecs_singleton_get_mut(ECS, InputState);
|
||||
if (IsKeyDown(KEY_W)) game->camera.target.y -= 5;
|
||||
if (IsKeyDown(KEY_S)) game->camera.target.y += 5;
|
||||
if (IsKeyDown(KEY_A)) game->camera.target.x -= 5;
|
||||
if (IsKeyDown(KEY_D)) game->camera.target.x += 5;
|
||||
|
||||
if (IsKeyDown(KEY_Q)) game->camera.rotation--;
|
||||
if (IsKeyDown(KEY_E)) game->camera.rotation++;
|
||||
|
||||
|
||||
game->camera.zoom += GetMouseWheelMove() * 0.05f;
|
||||
BzTileMap *map = &game->map;
|
||||
|
||||
BzTile tileX = 0, tileY = 0;
|
||||
bzTileMapPosToTile(map, input->mouseWorld, &tileX, &tileY);
|
||||
|
||||
if (IsKeyPressed(input->mapping.backBtn)) {
|
||||
ecs_remove_all(ECS, Selected);
|
||||
resetInputState(input);
|
||||
return;
|
||||
}
|
||||
|
||||
const MouseButton primaryBtn = input->mapping.primaryBtn;
|
||||
const MouseButton secondaryBtn = input->mapping.secondaryBtn;
|
||||
|
||||
switch (input->state) {
|
||||
case INPUT_NONE: {
|
||||
inputPrimaryAction(game, input);
|
||||
break;
|
||||
}
|
||||
case INPUT_BUILDING: {
|
||||
BZ_ASSERT(input->building);
|
||||
BzTile sizeX = 0, sizeY = 0;
|
||||
getBuildingSize(input->building, &sizeX, &sizeY);
|
||||
bool canPlace = canPlaceBuilding(&game->map, input->building, tileX, tileY);
|
||||
if (canPlace && isInputBtnDown(input, primaryBtn)) {
|
||||
placeBuilding(&game->map, input->building, tileX, tileY);
|
||||
}
|
||||
input->buildingCanPlace = canPlace;
|
||||
input->buildingPos = (TilePosition) {tileX, tileY};
|
||||
input->buildingSize = (TileSize) {sizeX, sizeY};
|
||||
break;
|
||||
}
|
||||
case INPUT_SELECTED_UNITS: {
|
||||
inputPrimaryAction(game, input);
|
||||
if (input->state != INPUT_SELECTED_UNITS) break;
|
||||
inputUnitAction(game, input);
|
||||
|
||||
/*
|
||||
|
||||
i32 selectedCount = ecs_query_entity_count(input->queries.selected);
|
||||
if (selectedCount > 1 && isInputBtnJustDragged(input, secondaryBtn)) {
|
||||
// TODO: For click it should just move them
|
||||
i32 numUnits = selectedCount;
|
||||
f32 unitSpacing = 3.5f;
|
||||
bzArrayClear(input->unitPositions);
|
||||
placeUnits(numUnits, unitSpacing,
|
||||
input->mouseDownWorld, input->mouseWorld,
|
||||
map, &input->unitPositions);
|
||||
|
||||
BZ_ASSERT(bzArraySize(input->unitPositions) == numUnits);
|
||||
i32 unitPosIdx = 0;
|
||||
|
||||
ecs_defer_begin(ECS);
|
||||
ecs_defer_end(ECS);
|
||||
|
||||
ecs_iter_t it = ecs_query_iter(ECS, input->queries.selected);
|
||||
ecs_defer_begin(ECS);
|
||||
while (ecs_iter_next(&it)) {
|
||||
Position *pos = ecs_field(&it, Position, 1);
|
||||
for (i32 i = 0; i < it.count; i++) {
|
||||
ecs_entity_t entity = it.entities[i];
|
||||
|
||||
Position target = bzArrayGet(input->unitPositions, unitPosIdx);
|
||||
unitPosIdx++;
|
||||
|
||||
Path path = {NULL, 0};
|
||||
pathfindAStar(&(PathfindingDesc) {
|
||||
.start=pos[i],
|
||||
.target=target,
|
||||
.map=map,
|
||||
.outPath=&path,
|
||||
.pool=game->pools.pathData,
|
||||
.alloc=&game->stackAlloc
|
||||
});
|
||||
if (!path.paths) continue;
|
||||
ecs_set_ptr(ECS, entity, Path, &path);
|
||||
|
||||
}
|
||||
}
|
||||
ecs_defer_end(ECS);
|
||||
} else if (isInputBtnJustDown(input, secondaryBtn)) {
|
||||
ecs_defer_begin(ECS);
|
||||
iterateSelectedUnits(input->queries.selected, iterRemovePaths);
|
||||
ecs_defer_end(ECS);
|
||||
|
||||
ecs_iter_t it = ecs_query_iter(ECS, input->queries.selected);
|
||||
|
||||
ecs_defer_begin(ECS);
|
||||
while (ecs_iter_next(&it)) {
|
||||
Position *pos = ecs_field(&it, Position, 1);
|
||||
for (i32 i = 0; i < it.count; i++) {
|
||||
ecs_entity_t entity = it.entities[i];
|
||||
ecs_remove(ECS, entity, Path);
|
||||
|
||||
Path path = {NULL, 0};
|
||||
pathfindAStar(&(PathfindingDesc) {
|
||||
.start=pos[i],
|
||||
.target=input->mouseWorld,
|
||||
.map=map,
|
||||
.outPath=&path,
|
||||
.pool=game->pools.pathData,
|
||||
.alloc=&game->stackAlloc
|
||||
});
|
||||
if (!path.paths) continue;
|
||||
ecs_set_ptr(ECS, entity, Path, &path);
|
||||
|
||||
}
|
||||
}
|
||||
ecs_defer_end(ECS);
|
||||
}
|
||||
*/
|
||||
break;
|
||||
}
|
||||
case INPUT_SELECTED_OBJECT: {
|
||||
inputPrimaryAction(game, input);
|
||||
if (input->state != INPUT_SELECTED_OBJECT) break;
|
||||
break;
|
||||
}
|
||||
case INPUT_SELECTED_BUILDING: {
|
||||
inputPrimaryAction(game, input);
|
||||
if (input->state != INPUT_SELECTED_BUILDING) break;
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void drawPlayerInputUIGround() {
|
||||
const Game *game = ecs_singleton_get_mut(ECS, Game);
|
||||
const InputState *input = ecs_singleton_get_mut(ECS, InputState);
|
||||
|
||||
const MouseButton primaryBtn = input->mapping.primaryBtn;
|
||||
|
||||
const i32 selectedUnitCount = ecs_count_id(ECS, Selected);
|
||||
switch (input->state) {
|
||||
case INPUT_BUILDING: {
|
||||
const Color placeColor = input->buildingCanPlace ?
|
||||
(Color) {0, 255, 0, 200} :
|
||||
(Color) {255, 0, 0, 200};
|
||||
|
||||
const BzTile width = game->map.tileWidth;
|
||||
const BzTile height = game->map.tileHeight;
|
||||
DrawRectangleLines(input->buildingPos.x * width,
|
||||
input->buildingPos.y * height,
|
||||
input->buildingSize.sizeX * width,
|
||||
input->buildingSize.sizeY * height, placeColor);
|
||||
break;
|
||||
}
|
||||
case INPUT_SELECTED_UNITS: {
|
||||
BZ_ASSERT(selectedUnitCount);
|
||||
/*
|
||||
if (selectedUnitCount > 1 && isInputBtnDragged(input, primaryBtn)) {
|
||||
i32 numUnits = selectedUnitCount;
|
||||
f32 unitSpacing = 3.5f;
|
||||
bzArrayClear(input->unitPositions);
|
||||
placeUnits(numUnits, unitSpacing, input->mouseDownWorld, input->mouseWorld,
|
||||
map, &input->unitPositions);
|
||||
|
||||
BZ_ASSERT(bzArraySize(input->unitPositions) == numUnits);
|
||||
bzArrayFor(input->unitPositions, i) {
|
||||
Position pos = bzArrayGet(input->unitPositions, i);
|
||||
DrawCircle(pos.x, pos.y, 2.0f, ORANGE);
|
||||
}
|
||||
}
|
||||
*/
|
||||
break;
|
||||
}
|
||||
default: break;
|
||||
}
|
||||
ecs_iter_t it = ecs_query_iter(ECS, input->queries.selected);
|
||||
rlSetLineWidth(2.0f);
|
||||
while (ecs_query_next(&it)) {
|
||||
Position *pos = ecs_field(&it, Position, 1);
|
||||
Size *size = ecs_field(&it, Size, 2);
|
||||
for (i32 i = 0; i < it.count; i++) {
|
||||
f32 radius = size[i].x;
|
||||
if (size[i].y > radius)
|
||||
radius = size[i].y;
|
||||
radius *= 0.5f;
|
||||
DrawCircleLines(pos[i].x, pos[i].y, radius, GREEN);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void drawPlayerInputUI() {
|
||||
const Game *game = ecs_singleton_get_mut(ECS, Game);
|
||||
const InputState *input = ecs_singleton_get_mut(ECS, InputState);
|
||||
|
||||
const MouseButton primaryBtn = input->mapping.primaryBtn;
|
||||
|
||||
if (isInputBtnDragged(input, primaryBtn)) {
|
||||
const Rectangle area = input->pickArea;
|
||||
DrawRectangleLines(area.x, area.y, area.width, area.height, RED);
|
||||
}
|
||||
|
||||
switch (input->cursor) {
|
||||
case CURSOR_COLLECT_WOOD: {
|
||||
const Vector2 point = input->mouseWorld;
|
||||
DrawCircle(point.x, point.y, 2.0f, RED);
|
||||
DrawText("Collect wood", point.x, point.y, 10.0f, RED);
|
||||
break;
|
||||
}
|
||||
default: break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Rectangle calculateEntityBounds(Position pos, Size size) {
|
||||
return (Rectangle) {
|
||||
pos.x - size.x * 0.5f,
|
||||
pos.y - size.x * 0.5f,
|
||||
size.x, size.y
|
||||
};
|
||||
}
|
||||
|
||||
bool getEntityBounds(ecs_entity_t entity, Position *outPos, Size *outSize, Rectangle *outBounds) {
|
||||
if (!ecs_is_alive(ECS, entity))
|
||||
return false;
|
||||
const Position *pos = ecs_get(ECS, entity, Position);
|
||||
if (!pos)
|
||||
return false;
|
||||
const Size *size = ecs_get(ECS, entity, Size);
|
||||
if (!size)
|
||||
return false;
|
||||
|
||||
if (outPos) {
|
||||
*outPos = *pos;
|
||||
}
|
||||
if (outSize) {
|
||||
*outSize = *size;
|
||||
}
|
||||
if (outBounds) {
|
||||
*outBounds = (Rectangle) {
|
||||
pos->x - size->x * 0.5f,
|
||||
pos->y - size->y * 0.5f,
|
||||
size->x, size->y
|
||||
};
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
ecs_entity_t queryEntity(BzSpatialGrid *entityGrid, Vector2 point, ecs_entity_t tag) {
|
||||
BzSpatialGridIter it = bzSpatialGridIter(entityGrid, point.x, point.y, 0.0f, 0.0f);
|
||||
f32 closestDst = INFINITY;
|
||||
ecs_entity_t closest = 0;
|
||||
while (bzSpatialGridQueryNext(&it)) {
|
||||
ecs_entity_t entity = *(ecs_entity_t *) it.data;
|
||||
if (!ecs_is_alive(ECS, entity)) continue;
|
||||
if (!ecs_has_id(ECS, entity, Selectable)) continue;
|
||||
if (!ecs_has_id(ECS, entity, tag)) continue;
|
||||
Vector2 pos;
|
||||
Rectangle bounds;
|
||||
if (!getEntityBounds(entity, &pos, NULL, &bounds)) continue;
|
||||
|
||||
if (!CheckCollisionPointRec(point, bounds)) continue;
|
||||
|
||||
f32 curDst = Vector2Distance(point, pos);
|
||||
if (closestDst > curDst) {
|
||||
closestDst = curDst;
|
||||
closest = entity;
|
||||
}
|
||||
}
|
||||
return closest;
|
||||
}
|
||||
|
||||
bool pickEntity(BzSpatialGrid *entityGrid, Vector2 point, ecs_entity_t tag) {
|
||||
ecs_remove_all(ECS, Selected);
|
||||
const ecs_entity_t entity = queryEntity(entityGrid, point, tag);
|
||||
if (entity) {
|
||||
ecs_add(ECS, entity, Selected);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void pickUnits(BzSpatialGrid *entityGrid, Rectangle area) {
|
||||
ecs_remove_all(ECS, Selected);
|
||||
BzSpatialGridIter it = bzSpatialGridIter(entityGrid, area.x, area.y, area.width, area.height);
|
||||
while (bzSpatialGridQueryNext(&it)) {
|
||||
ecs_entity_t entity = *(ecs_entity_t *) it.data;
|
||||
if (!ecs_has_id(ECS, entity, Unit)) continue;
|
||||
Rectangle bounds;
|
||||
if (!getEntityBounds(entity, NULL, NULL, &bounds)) continue;
|
||||
|
||||
if (!CheckCollisionRecs(area, bounds)) continue;
|
||||
ecs_add(ECS, entity, Selected);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
static bool isUnitObstructed(f32 x, f32 y, BzTileMap *map) {
|
||||
return bzTileMapHasCollision(map, x / map->tileWidth, y / map->tileHeight);
|
||||
}
|
||||
|
||||
static bool canPlaceUnit(Vector2 pos, f32 space, BzTileMap *map) {
|
||||
for (i32 y = -1; y <= 1; y++) {
|
||||
for (i32 x = -1; x <= 1; x++) {
|
||||
if (isUnitObstructed(pos.x + x * space, pos.y + y * space, map))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void placeUnits(i32 numUnits, f32 unitSpacing, Vector2 start, Vector2 end, BzTileMap *map, Vector2 **outPlaces) {
|
||||
f32 angle = Vector2Angle(start, end);
|
||||
|
||||
f32 lineLength = Vector2Distance(start, end);
|
||||
|
||||
Vector2 pos = Vector2Zero();
|
||||
pos.x = unitSpacing;
|
||||
|
||||
for (i32 i = 0; i < numUnits; i++) {
|
||||
if (pos.x + unitSpacing * 2.0f > lineLength) {
|
||||
pos.x = unitSpacing;
|
||||
pos.y += unitSpacing * 2.0f;
|
||||
}
|
||||
Vector2 unitPos = Vector2Add(start, Vector2Rotate(pos, angle));
|
||||
if (!canPlaceUnit(unitPos, 4.0f, map)) {
|
||||
i--;
|
||||
} else {
|
||||
bzArrayPush(*outPlaces, unitPos);
|
||||
}
|
||||
pos.x += unitSpacing * 2.0f;
|
||||
}
|
||||
}
|
||||
|
||||
static void iterateSelectedUnits(ecs_query_t *query, void (*fn)(ecs_entity_t entity, Position *pos, Size *size)) {
|
||||
ecs_iter_t it = ecs_query_iter(ECS, query);
|
||||
while (ecs_iter_next(&it)) {
|
||||
Position *pos = ecs_field(&it, Position, 1);
|
||||
Size *size = ecs_field(&it, Size, 2);
|
||||
|
||||
for (i32 i = 0; i < it.count; i++) {
|
||||
ecs_entity_t entity = it.entities[i];
|
||||
|
||||
fn(entity, pos + i, size + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
static void iterRemovePaths(ecs_entity_t entity, Position *pos, Size *size) {
|
||||
ecs_remove(ECS, entity, Path);
|
||||
}
|
||||
|
||||
15
game/systems/s_ui.c
Normal file
15
game/systems/s_ui.c
Normal file
@@ -0,0 +1,15 @@
|
||||
#include "systems.h"
|
||||
|
||||
#include "game_state.h"
|
||||
|
||||
void uiTask(ecs_iter_t *it) {
|
||||
const Game *game = ecs_singleton_get(ECS, Game);
|
||||
Vector2 mousePos = GetMousePosition();
|
||||
Vector2 worldPos = GetScreenToWorld2D(mousePos, game->camera);
|
||||
|
||||
const BzTileMap *map = &game->map;
|
||||
|
||||
i32 tileX = (i32) worldPos.x / map->tileWidth;
|
||||
i32 tileY = (i32) worldPos.y / map->tileHeight;
|
||||
|
||||
}
|
||||
150
game/systems/systems.h
Normal file
150
game/systems/systems.h
Normal file
@@ -0,0 +1,150 @@
|
||||
#ifndef PIXELDEFENSE_SYSTEMS_H
|
||||
#define PIXELDEFENSE_SYSTEMS_H
|
||||
|
||||
#include <flecs.h>
|
||||
|
||||
#include "components.h"
|
||||
|
||||
typedef struct Game Game;
|
||||
|
||||
/**********************************
|
||||
* Utils
|
||||
**********************************/
|
||||
|
||||
bool entitySetPath(const ecs_entity_t entity, const Vector2 target, Game *game);
|
||||
|
||||
/**********************************
|
||||
* AI systems
|
||||
**********************************/
|
||||
|
||||
/*
|
||||
* 0: Game (singleton)
|
||||
* 1: UnitAction
|
||||
*/
|
||||
void handleUnitActionsSystem(ecs_iter_t *it);
|
||||
|
||||
/*
|
||||
* 0: Game (singleton)
|
||||
* 1: UnitAI
|
||||
* 2: UnitAction
|
||||
*/
|
||||
void updateUnitAISystem(ecs_iter_t *it);
|
||||
|
||||
/*
|
||||
* 0: Game (singleton)
|
||||
* 1: UnitAction
|
||||
*/
|
||||
void updateUnitActionsSystem(ecs_iter_t *it);
|
||||
|
||||
|
||||
/**********************************
|
||||
* Entity Systems
|
||||
**********************************/
|
||||
|
||||
/* Observer (for removing TargetPosition)
|
||||
* 0: Game (singleton) for object pool
|
||||
* 1: Path
|
||||
*/
|
||||
void entityPathRemove(ecs_iter_t *it);
|
||||
|
||||
/*
|
||||
* 0: Game (singleton) for entity map
|
||||
* 1: Position
|
||||
* 2: Size
|
||||
* 3: Velocity (only entities with velocity change position)
|
||||
* 4: SpatialGridID
|
||||
*/
|
||||
void entityUpdateSpatialID(ecs_iter_t *it);
|
||||
|
||||
/*
|
||||
* 0: Game (singleton) for collisions
|
||||
* 1: Position
|
||||
* 2: Rotation
|
||||
* 3: Velocity
|
||||
* 4: Steering
|
||||
*/
|
||||
void entityUpdateKinematic(ecs_iter_t *it);
|
||||
|
||||
/*
|
||||
* 1: Position
|
||||
* 2: Rotation
|
||||
* 3: Velocity
|
||||
* 4: TargetPosition
|
||||
* 5: Steering
|
||||
*/
|
||||
void entityMoveToTarget(ecs_iter_t *it);
|
||||
|
||||
/*
|
||||
* 0: Game (singleton) for object pool
|
||||
* 1: Path
|
||||
*/
|
||||
void entityFollowPath(ecs_iter_t *it);
|
||||
|
||||
|
||||
/*
|
||||
* 1: Animation
|
||||
* 2: TextureRegion
|
||||
*/
|
||||
void entityUpdateAnimationState(ecs_iter_t *it);
|
||||
/*
|
||||
* 0:
|
||||
* 1: Animation
|
||||
* 2: TextureRegion
|
||||
*/
|
||||
void entityUpdateAnimation(ecs_iter_t *it);
|
||||
|
||||
|
||||
/*
|
||||
* 1: Position
|
||||
* 2: Size
|
||||
*/
|
||||
void renderColliders(ecs_iter_t *it);
|
||||
|
||||
/*
|
||||
* 1: Position
|
||||
* 2: Rotation
|
||||
*/
|
||||
void renderRotationDirection(ecs_iter_t *it);
|
||||
|
||||
/*
|
||||
* 1: Path
|
||||
*/
|
||||
void renderDebugPath(ecs_iter_t *it);
|
||||
|
||||
/**********************************
|
||||
* Event Systems
|
||||
**********************************/
|
||||
|
||||
i32 harvestEvent(ecs_entity_t entity, HarvestEvent event);
|
||||
|
||||
|
||||
/**********************************
|
||||
* Input systems
|
||||
**********************************/
|
||||
|
||||
/*
|
||||
* Task:
|
||||
* 0: Game (singleton)
|
||||
* 0: InputState (singleton)
|
||||
*/
|
||||
void updatePlayerInput();
|
||||
|
||||
/*
|
||||
* Task (rendering above ground):
|
||||
* 0: Game (singleton)
|
||||
* 0: InputState (singleton)
|
||||
*/
|
||||
void drawPlayerInputUIGround();
|
||||
|
||||
/*
|
||||
* Task (rendering on top):
|
||||
* 0: Game (singleton)
|
||||
* 0: InputState (singleton)
|
||||
*/
|
||||
void drawPlayerInputUI();
|
||||
|
||||
/**********************************
|
||||
* UI systems
|
||||
**********************************/
|
||||
|
||||
#endif //PIXELDEFENSE_SYSTEMS_H
|
||||
Reference in New Issue
Block a user