95 lines
2.9 KiB
C
95 lines
2.9 KiB
C
#include "buildings.h"
|
|
|
|
#include "components.h"
|
|
#include "game_state.h"
|
|
#include "map_layers.h"
|
|
|
|
#include <raymath.h>
|
|
|
|
bool canPlaceBuilding(BzTileMap *map, BuildingType type, BzTile tileX, BzTile tileY) {
|
|
i32 sizeX, sizeY;
|
|
getBuildingSize(type, &sizeX, &sizeY);
|
|
if (sizeX == 0 || sizeY == 0) return false;
|
|
|
|
// Ensure that it is within the map
|
|
if (tileX < 0 || tileX + sizeX > map->width ||
|
|
tileY < 0 || tileY + sizeY > map->height)
|
|
return false;
|
|
|
|
Rectangle buildArea = {tileX * map->tileWidth, tileY * map->tileHeight,
|
|
sizeX * map->tileWidth, sizeY * map->tileHeight};
|
|
|
|
// Need to check neighbour tiles
|
|
//tileX -= 1;
|
|
//tileY -= 1;
|
|
//sizeX += 2;
|
|
//sizeY += 2;
|
|
|
|
BzTileLayer *buildLayer = bzTileMapGetLayer(map, LAYER_BUILDINGS);
|
|
BzTileset *tileset = bzTileLayerGetTileset(map, buildLayer);
|
|
|
|
for (i32 y = tileY; y < tileY + sizeY; y++) {
|
|
for (i32 x = tileX; x < tileX + sizeX; x++) {
|
|
BzTile tile = bzTileLayerGetTile(buildLayer, x, y);
|
|
tile = bzTilesetGetTileID(tileset, tile);
|
|
tile = getTileBuilding(tile);
|
|
//if (tile == BUILDINGS_ROAD)
|
|
// return false;
|
|
if (bzTileMapHasCollision(map, x, y)) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
ecs_entity_t placeBuilding(BzTileMap *map, BuildingType type, BzTile tileX, BzTile tileY) {
|
|
Game *game = ecs_singleton_get_mut(ECS, Game);
|
|
i32 sizeX, sizeY;
|
|
getBuildingSize(type, &sizeX, &sizeY);
|
|
|
|
BzTileLayer *buildingLayer = bzTileMapGetLayer(map, LAYER_BUILDINGS);
|
|
BzTileset *buildingTileset = bzTileLayerGetTileset(map, buildingLayer);
|
|
BzTile buildingTile = getBuildingTile(type);
|
|
BZ_ASSERT(buildingTile != -1);
|
|
|
|
// Create entity
|
|
ecs_entity_t e = entityCreate(ECS);
|
|
|
|
ecs_set(ECS, e, TilePosition, { .x = tileX, .y = tileY });
|
|
ecs_set(ECS, e, TileSize, { .sizeX = sizeX, .sizeY = sizeY });
|
|
//ecs_set(ECS, e, Owner, { .playerID = BUILDINGS_PLAYER_RED });
|
|
|
|
for (i32 y = tileY; y < tileY + sizeY; y++) {
|
|
for (i32 x = tileX; x < tileX + sizeX; x++) {
|
|
BzTile layerTile = buildingTile + buildingTileset->startID;
|
|
bzTileLayerSetTile(buildingLayer, layerTile, x, y, 1, 1);
|
|
buildingTile++;
|
|
|
|
bzTileMapUpdateCollisions(map, x, y, 1, 1);
|
|
}
|
|
buildingTile += buildingTileset->width - sizeX;
|
|
}
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
Vector2 getPositionNearBuilding(ecs_entity_t building, Vector2 fromPos) {
|
|
BZ_ASSERT(ecs_is_alive(ECS, building));
|
|
BZ_ASSERT(ecs_has(ECS, building, Position));
|
|
BZ_ASSERT(ecs_has(ECS, building, Size));
|
|
|
|
Vector2 pos = *ecs_get(ECS, building, Position);
|
|
Vector2 size = *ecs_get(ECS, building, Size);
|
|
|
|
size = Vector2SubtractValue(size, 10.0f);
|
|
|
|
Vector2 dir = Vector2Normalize(Vector2Subtract(fromPos, pos));
|
|
dir = Vector2Multiply(dir, size);
|
|
|
|
pos = Vector2Add(pos, dir);
|
|
return pos;
|
|
}
|