Files
prog2/Game/GridSystem/WorldGridManager.cpp
Bram Verhulst 71d364d9d8 Added inheritance for the screen system
basic edge detection for tile rendering
2024-04-04 13:49:38 +02:00

67 lines
1.8 KiB
C++

#include "WorldGridManager.h"
#include <stdexcept>
#include "WorldTile.h"
WorldGridManager::WorldGridManager() {
for (size_t x { 0 }; x < WORLD_WIDTH; ++x) {
for (size_t y { 0 }; y < WORLD_HEIGHT; ++y) {
m_worldTiles[x][y] = nullptr;
}
}
}
WorldGridManager::~WorldGridManager() {
}
std::vector<WorldTile*> WorldGridManager::GetSurroundingTiles(const WorldTile* world_tile) {
std::vector<WorldTile*> tiles;
Point2f pos = world_tile->GetPosition();
Point2f gridCoords = this->GetIndexFromPosition(pos);
int x = gridCoords.x;
//TODO: Stupid fix, fix this
int y = gridCoords.y - 1;
if (x < 0 || x >= WORLD_WIDTH || y < 0 || y >= WORLD_HEIGHT) {
return tiles;
}
for (int i { -1 }; i <= 1; ++i) {
for (int j { -1 }; j <= 1; ++j) {
if (i == 0 && j == 0) {
continue;
}
if(x + i < 0 || x + i >= WORLD_WIDTH || y + j < 0 || y + j >= WORLD_HEIGHT) {
continue;
}
WorldTile* tile = GetTileAtIndex(x + i, y + j);
if (tile != nullptr) {
tiles.push_back(tile);
}
}
}
return tiles;
}
Point2f WorldGridManager::GetIndexFromPosition(Point2f position) {
int x = int(position.x / TILE_WIDTH + WORLD_WIDTH / 2);
int y = int(-position.y / TILE_HEIGHT);
return Point2f{ float(x), float(y) };
}
WorldTile * WorldGridManager::GetTileAtIndex(const int x, const int y) const {
if (x < 0 || x >= WORLD_WIDTH || y < 0 || y >= WORLD_HEIGHT) {
return nullptr;
//TODO: see if this can be Math'ed out
}
return m_worldTiles[x][y];
}
WorldTile * WorldGridManager::GetTileAtWorldPos(const Point2f& pos) const {
int x = int(pos.x / TILE_WIDTH + WORLD_WIDTH / 2);
int y = int(-pos.y / TILE_HEIGHT);
if (x < 0 || x >= WORLD_WIDTH || y < 0 || y >= WORLD_HEIGHT) {
return nullptr;
}
return m_worldTiles[x][y];
}
void WorldGridManager::SetTileAtIndex(const int x, const int y, WorldTile* tile) {
m_worldTiles[x][y] = tile;
}