Properly draw selected rings and target formation position
This commit is contained in:
@@ -9,8 +9,13 @@ bool getEntityBounds(ecs_entity_t entity, Position *outPos, Size *outSize, Recta
|
|||||||
void pickEntity(BzSpatialGrid *entityGrid, Vector2 point, ecs_entity_t **outEntities);
|
void pickEntity(BzSpatialGrid *entityGrid, Vector2 point, ecs_entity_t **outEntities);
|
||||||
void pickEntities(BzSpatialGrid *entityGrid, Rectangle area, ecs_entity_t **outEntities);
|
void pickEntities(BzSpatialGrid *entityGrid, Rectangle area, ecs_entity_t **outEntities);
|
||||||
|
|
||||||
static void placeUnits(i32 numUnits, f32 unitSpacing, Vector2 start, Vector2 end, BzTileMap *map, Vector2 **outPlaces);
|
void placeUnits(i32 numUnits, f32 unitSpacing, Vector2 start, Vector2 end, BzTileMap *map, Vector2 **outPlaces);
|
||||||
|
|
||||||
|
void drawSelectionCircles(ecs_entity_t **selected);
|
||||||
|
|
||||||
|
static bool isInputDragged(InputState *input) {
|
||||||
|
return IsMouseButtonDown(input->LMB && input->mouseDownElapsed > input->CLICK_LIMIT);
|
||||||
|
}
|
||||||
static bool wasInputDragged(InputState *input) {
|
static bool wasInputDragged(InputState *input) {
|
||||||
return IsMouseButtonReleased(input->LMB) && input->mouseDownElapsed > input->CLICK_LIMIT;
|
return IsMouseButtonReleased(input->LMB) && input->mouseDownElapsed > input->CLICK_LIMIT;
|
||||||
}
|
}
|
||||||
@@ -52,23 +57,6 @@ void updatePlayerInput(ecs_iter_t *it) {
|
|||||||
switch (input->state) {
|
switch (input->state) {
|
||||||
case INPUT_NONE:
|
case INPUT_NONE:
|
||||||
if (wasInputDragged(input)) {
|
if (wasInputDragged(input)) {
|
||||||
// Dragging
|
|
||||||
|
|
||||||
Vector2 start = input->mouseDownWorld;
|
|
||||||
Vector2 end = worldPos;
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
Rectangle pickArea = {start.x, start.y, end.x - start.x, end.y - start.y};
|
|
||||||
|
|
||||||
pickEntities(game->entityGrid, pickArea, &input->entities);
|
|
||||||
if (bzArraySize(input->entities) > 0) {
|
if (bzArraySize(input->entities) > 0) {
|
||||||
input->state = INPUT_SELECTED_UNITS;
|
input->state = INPUT_SELECTED_UNITS;
|
||||||
break;
|
break;
|
||||||
@@ -78,7 +66,6 @@ void updatePlayerInput(ecs_iter_t *it) {
|
|||||||
// Click
|
// Click
|
||||||
|
|
||||||
// 1. Entity
|
// 1. Entity
|
||||||
pickEntity(game->entityGrid, worldPos, &input->entities);
|
|
||||||
if (bzArraySize(input->entities) > 0) {
|
if (bzArraySize(input->entities) > 0) {
|
||||||
input->state = INPUT_SELECTED_UNITS;
|
input->state = INPUT_SELECTED_UNITS;
|
||||||
break;
|
break;
|
||||||
@@ -109,7 +96,8 @@ void updatePlayerInput(ecs_iter_t *it) {
|
|||||||
input->buildingSize = (TileSize) {sizeX, sizeY};
|
input->buildingSize = (TileSize) {sizeX, sizeY};
|
||||||
break;
|
break;
|
||||||
case INPUT_SELECTED_UNITS:
|
case INPUT_SELECTED_UNITS:
|
||||||
if (IsKeyPressed(input->ESC)) {
|
if (IsKeyPressed(input->ESC) || IsMouseButtonPressed(input->RMB)) {
|
||||||
|
bzArrayClear(input->entities);
|
||||||
input->state = INPUT_NONE;
|
input->state = INPUT_NONE;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -138,7 +126,6 @@ void updatePlayerInput(ecs_iter_t *it) {
|
|||||||
});
|
});
|
||||||
if (!path.paths) continue;
|
if (!path.paths) continue;
|
||||||
ecs_set_ptr(ECS, entity, Path, &path);
|
ecs_set_ptr(ECS, entity, Path, &path);
|
||||||
input->state = INPUT_NONE;
|
|
||||||
}
|
}
|
||||||
} else if (wasInputClicked(input)) {
|
} else if (wasInputClicked(input)) {
|
||||||
bzArrayFor(input->entities, i) {
|
bzArrayFor(input->entities, i) {
|
||||||
@@ -156,7 +143,6 @@ void updatePlayerInput(ecs_iter_t *it) {
|
|||||||
});
|
});
|
||||||
if (!path.paths) continue;
|
if (!path.paths) continue;
|
||||||
ecs_set_ptr(ECS, entity, Path, &path);
|
ecs_set_ptr(ECS, entity, Path, &path);
|
||||||
input->state = INPUT_NONE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -171,11 +157,12 @@ void updatePlayerInput(ecs_iter_t *it) {
|
|||||||
void drawPlayerInputUI(ecs_iter_t *it) {
|
void drawPlayerInputUI(ecs_iter_t *it) {
|
||||||
Game *game = ecs_singleton_get_mut(ECS, Game);
|
Game *game = ecs_singleton_get_mut(ECS, Game);
|
||||||
InputState *input = ecs_singleton_get_mut(ECS, InputState);
|
InputState *input = ecs_singleton_get_mut(ECS, InputState);
|
||||||
|
BzTileMap *map = &game->map;
|
||||||
Vector2 worldPos = GetScreenToWorld2D(GetMousePosition(), game->camera);
|
Vector2 worldPos = GetScreenToWorld2D(GetMousePosition(), game->camera);
|
||||||
|
|
||||||
switch (input->state) {
|
switch (input->state) {
|
||||||
case INPUT_NONE:
|
case INPUT_NONE:
|
||||||
if (IsMouseButtonDown(input->LMB) && input->mouseDownElapsed > input->CLICK_LIMIT) {
|
if (isInputDragged(input)) {
|
||||||
Vector2 start = input->mouseDownWorld;
|
Vector2 start = input->mouseDownWorld;
|
||||||
Vector2 end = worldPos;
|
Vector2 end = worldPos;
|
||||||
if (start.x > end.x) {
|
if (start.x > end.x) {
|
||||||
@@ -190,6 +177,8 @@ void drawPlayerInputUI(ecs_iter_t *it) {
|
|||||||
}
|
}
|
||||||
Rectangle area = {start.x, start.y, end.x - start.x, end.y - start.y};
|
Rectangle area = {start.x, start.y, end.x - start.x, end.y - start.y};
|
||||||
DrawRectangleLines(area.x, area.y, area.width, area.height, RED);
|
DrawRectangleLines(area.x, area.y, area.width, area.height, RED);
|
||||||
|
|
||||||
|
pickEntities(game->entityGrid, area, &input->entities);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case INPUT_BUILDING: {
|
case INPUT_BUILDING: {
|
||||||
@@ -206,16 +195,20 @@ void drawPlayerInputUI(ecs_iter_t *it) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case INPUT_SELECTED_UNITS: {
|
case INPUT_SELECTED_UNITS: {
|
||||||
bzArrayFor(input->entities, i) {
|
if (bzArraySize(input->entities) > 1 && isInputDragged(input)) {
|
||||||
ecs_entity_t entity = bzArrayGet(input->entities, i);
|
i32 numUnits = bzArraySize(input->entities);
|
||||||
Position pos = {0};
|
f32 unitSpacing = 3.5f;
|
||||||
if (!getEntityBounds(entity, &pos, NULL, NULL))
|
bzArrayClear(input->unitPositions);
|
||||||
continue;
|
placeUnits(numUnits, unitSpacing, input->mouseDownWorld, worldPos,
|
||||||
DrawCircleLines(pos.x, pos.y, 4.5f, GREEN);
|
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;
|
break;
|
||||||
}
|
}
|
||||||
case INPUT_SELECTED_OBJECT:
|
case INPUT_SELECTED_OBJECT:
|
||||||
@@ -223,10 +216,8 @@ void drawPlayerInputUI(ecs_iter_t *it) {
|
|||||||
case INPUT_SELECTED_BUILDING:
|
case INPUT_SELECTED_BUILDING:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
bzArrayFor(input->unitPositions, i) {
|
drawSelectionCircles(&input->entities);
|
||||||
Position pos = bzArrayGet(input->unitPositions, i);
|
|
||||||
DrawCircle(pos.x, pos.y, 2.0f, ORANGE);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -336,3 +327,15 @@ void placeUnits(i32 numUnits, f32 unitSpacing, Vector2 start, Vector2 end, BzTil
|
|||||||
pos.x += unitSpacing * 2.0f;
|
pos.x += unitSpacing * 2.0f;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void drawSelectionCircles(ecs_entity_t **selected) {
|
||||||
|
bzArrayFor(*selected, i) {
|
||||||
|
ecs_entity_t entity = bzArrayGet(*selected, i);
|
||||||
|
Position pos = {0};
|
||||||
|
if (!getEntityBounds(entity, &pos, NULL, NULL))
|
||||||
|
continue;
|
||||||
|
DrawCircleLines(pos.x, pos.y, 4.5f, GREEN);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user