Move map implementation to map.cpp
This commit is contained in:
parent
4419e9bac9
commit
70a9e6a80f
@ -3,7 +3,7 @@ all: test pathfinding
|
|||||||
# TODO linter?
|
# TODO linter?
|
||||||
|
|
||||||
pathfinding:
|
pathfinding:
|
||||||
g++ -Wall -ggdb3 -lSDL3 -lSDL3_image -std=c++23 -lGLEW -lGL -o pathfinding src/main.cpp src/sprite.cpp src/entities.cpp src/gameloop.cpp
|
g++ -Wall -ggdb3 -lSDL3 -lSDL3_image -std=c++23 -lGLEW -lGL -o pathfinding src/main.cpp src/sprite.cpp src/entities.cpp src/gameloop.cpp src/map.cpp
|
||||||
|
|
||||||
test: src/test.cpp src/array.hpp
|
test: src/test.cpp src/array.hpp
|
||||||
g++ -Wall -Wextra -Wpedantic -ggdb3 -std=c++23 -lgtest -o test src/test.cpp
|
g++ -Wall -Wextra -Wpedantic -ggdb3 -std=c++23 -lgtest -o test src/test.cpp
|
||||||
|
@ -12,8 +12,7 @@ int main(int argc, char **argv) {
|
|||||||
* Initialize the input/output system
|
* Initialize the input/output system
|
||||||
*/
|
*/
|
||||||
|
|
||||||
auto window = std::make_unique<Window>(640, 480); // the holy resolution
|
auto window = std::make_unique<Window>(640, 480);
|
||||||
// auto window_init = window->Init();
|
|
||||||
if (auto initialized = window->Init(); !initialized) {
|
if (auto initialized = window->Init(); !initialized) {
|
||||||
LOG_ERROR(initialized.error());
|
LOG_ERROR(initialized.error());
|
||||||
return error;
|
return error;
|
||||||
@ -37,4 +36,5 @@ int main(int argc, char **argv) {
|
|||||||
game_loop.SetUserInput(std::move(user_input));
|
game_loop.SetUserInput(std::move(user_input));
|
||||||
game_loop.SetGame(std::move(demo));
|
game_loop.SetGame(std::move(demo));
|
||||||
game_loop.Run();
|
game_loop.Run();
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,52 @@
|
|||||||
|
#include <cassert>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "log.hpp"
|
||||||
|
#include "map.hpp"
|
||||||
|
#include "tile.hpp"
|
||||||
|
|
||||||
|
Map::Map(int rows, int cols) : m_Cols(cols), m_Rows(rows) {
|
||||||
|
bool sw = true;
|
||||||
|
LOG_DEBUG("cols = ", cols, " rows = ", rows);
|
||||||
|
m_Tiles = std::vector<std::vector<const Tile *>>{};
|
||||||
|
for (size_t row = 0; row < m_Rows; row++) {
|
||||||
|
m_Tiles.push_back(std::vector<const Tile *>{});
|
||||||
|
for (size_t col = 0; col < m_Cols; col++) {
|
||||||
|
if (sw)
|
||||||
|
m_Tiles[row].push_back(&tile_types.at("Grass"));
|
||||||
|
else
|
||||||
|
m_Tiles[row].push_back(&tile_types.at("Road"));
|
||||||
|
sw = !sw;
|
||||||
|
}
|
||||||
|
sw = !sw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
WorldPos Map::TileToWorld(TilePos p) const {
|
||||||
|
return WorldPos{p.x * TILE_SIZE, p.y * TILE_SIZE};
|
||||||
|
}
|
||||||
|
|
||||||
|
TilePos Map::WorldToTile(WorldPos p) const {
|
||||||
|
return TilePos{p.x / TILE_SIZE, p.y / TILE_SIZE};
|
||||||
|
}
|
||||||
|
|
||||||
|
WorldPos Map::GetTileSize() const { return WorldPos{TILE_SIZE, TILE_SIZE}; }
|
||||||
|
|
||||||
|
const Tile *Map::GetTileAt(TilePos p) const {
|
||||||
|
assert(IsTilePosValid(p));
|
||||||
|
size_t row = p.x;
|
||||||
|
size_t col = p.y;
|
||||||
|
|
||||||
|
return m_Tiles[row][col];
|
||||||
|
}
|
||||||
|
|
||||||
|
const Tile *Map::GetTileAt(WorldPos p) const {
|
||||||
|
return GetTileAt(WorldToTile(p));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Map::IsTilePosValid(TilePos p) const {
|
||||||
|
size_t row = p.x;
|
||||||
|
size_t col = p.y;
|
||||||
|
|
||||||
|
return row < m_Tiles.size() && col < m_Tiles[0].size();
|
||||||
|
}
|
@ -1,80 +1,36 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <cassert>
|
|
||||||
|
|
||||||
#include "math.hpp"
|
#include "math.hpp"
|
||||||
#include "tile.hpp"
|
#include "tile.hpp"
|
||||||
|
|
||||||
|
using TileGrid = std::vector<std::vector<const Tile *>>;
|
||||||
|
|
||||||
class Map {
|
class Map {
|
||||||
|
|
||||||
// TODO using = ... for tile vector
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static constexpr float TILE_SIZE = 100.0f; // tile size in world
|
static constexpr float TILE_SIZE = 100.0f; // tile size in world
|
||||||
|
|
||||||
Map(int rows, int cols) : m_Cols(cols), m_Rows(rows) {
|
Map(int rows, int cols);
|
||||||
bool sw = true;
|
|
||||||
LOG_DEBUG("cols = ", cols, " rows = ", rows);
|
|
||||||
m_Tiles = std::vector<std::vector<const Tile *>>{};
|
|
||||||
for (size_t row = 0; row < m_Rows; row++) {
|
|
||||||
m_Tiles.push_back(std::vector<const Tile *>{});
|
|
||||||
for (size_t col = 0; col < m_Cols; col++) {
|
|
||||||
if (sw)
|
|
||||||
m_Tiles[row].push_back(&tile_types.at("Grass"));
|
|
||||||
else
|
|
||||||
m_Tiles[row].push_back(&tile_types.at("Road"));
|
|
||||||
sw = !sw;
|
|
||||||
}
|
|
||||||
sw = !sw;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Map() : Map(0, 0) {}
|
Map() : Map(0, 0) {}
|
||||||
|
|
||||||
const std::vector<std::vector<const Tile *>> &GetMapTiles() const {
|
const TileGrid &GetMapTiles() const { return m_Tiles; }
|
||||||
return m_Tiles;
|
|
||||||
}
|
|
||||||
|
|
||||||
WorldPos TileToWorld(TilePos p) const {
|
WorldPos TileToWorld(TilePos p) const;
|
||||||
return WorldPos{p.x * TILE_SIZE, p.y * TILE_SIZE};
|
TilePos WorldToTile(WorldPos p) const;
|
||||||
}
|
|
||||||
|
|
||||||
TilePos WorldToTile(WorldPos p) const {
|
WorldPos GetTileSize() const;
|
||||||
return TilePos{p.x / TILE_SIZE, p.y / TILE_SIZE};
|
const Tile *GetTileAt(TilePos p) const;
|
||||||
}
|
const Tile *GetTileAt(WorldPos p) const;
|
||||||
|
|
||||||
Vec2D<float> GetTileSize() const {
|
bool IsTilePosValid(TilePos p) const;
|
||||||
return Vec2D<float>{TILE_SIZE, TILE_SIZE};
|
|
||||||
}
|
|
||||||
|
|
||||||
const Tile* GetTileAt(TilePos p) const {
|
template <typename T> double GetTileVelocityCoeff(T p) const {
|
||||||
assert(IsTilePosValid(p));
|
|
||||||
size_t row = p.x;
|
|
||||||
size_t col = p.y;
|
|
||||||
|
|
||||||
return m_Tiles[row][col];
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
const Tile* GetTileAt(WorldPos p) const {
|
|
||||||
return GetTileAt(WorldToTile(p));
|
|
||||||
}
|
|
||||||
|
|
||||||
bool IsTilePosValid(TilePos p) const {
|
|
||||||
size_t row = p.x;
|
|
||||||
size_t col = p.y;
|
|
||||||
|
|
||||||
return row < m_Tiles.size() && col < m_Tiles[0].size();
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
double GetTileVelocityCoeff(T p) const {
|
|
||||||
return 1.0 / GetTileAt(p)->cost;
|
return 1.0 / GetTileAt(p)->cost;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// std::vector<std::vector<const Tile*>> m_Tiles;
|
TileGrid m_Tiles;
|
||||||
std::vector<std::vector<const Tile *>> m_Tiles;
|
|
||||||
size_t m_Cols = 0;
|
size_t m_Cols = 0;
|
||||||
size_t m_Rows = 0;
|
size_t m_Rows = 0;
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user