parent
ea9f6bf383
commit
3a745d492a
@ -1,22 +0,0 @@ |
|||||||
#include <catch2/catch_test_macros.hpp> |
|
||||||
#include <fmt/core.h> |
|
||||||
#include "map.hpp" |
|
||||||
#include "levelmanager.hpp" |
|
||||||
#include "tilemap.hpp" |
|
||||||
#include "config.hpp" |
|
||||||
#include "rand.hpp" |
|
||||||
|
|
||||||
using namespace fmt; |
|
||||||
using std::string; |
|
||||||
|
|
||||||
TEST_CASE("tilemap can load tiles and make a map", "[tilemap]") { |
|
||||||
LevelManager levels; |
|
||||||
GameLevel level = levels.current(); |
|
||||||
auto &map = *level.map; |
|
||||||
|
|
||||||
TileMap tiles(map.width(), map.height()); |
|
||||||
auto& walls = map.walls(); |
|
||||||
tiles.load(walls); |
|
||||||
tiles.dump(); |
|
||||||
REQUIRE(tiles.INVARIANT()); |
|
||||||
} |
|
@ -1,78 +0,0 @@ |
|||||||
#include "tilemap.hpp" |
|
||||||
#include "dbc.hpp" |
|
||||||
#include "constants.hpp" |
|
||||||
#include <iostream> |
|
||||||
|
|
||||||
using nlohmann::json; |
|
||||||
using components::Tile; |
|
||||||
using std::string; |
|
||||||
|
|
||||||
TileMap::TileMap(size_t width, size_t height) : |
|
||||||
$config("./assets/tiles.json"), |
|
||||||
$width(width), |
|
||||||
$height(height), |
|
||||||
$tile_ids(height, matrix::Row(width, SPACE_VALUE)), |
|
||||||
$display(height, TileRow(width, {L'#', {0,0,0}, {0,0,0}})) |
|
||||||
{ |
|
||||||
} |
|
||||||
|
|
||||||
std::string TileMap::to_string(int show_x, int show_y) { |
|
||||||
std::string result; |
|
||||||
|
|
||||||
for(matrix::each_row it{$tile_ids}; it.next();) { |
|
||||||
const Tile &cell = $display[it.y][it.x]; |
|
||||||
|
|
||||||
if(int(it.x) == show_x && int(it.y) == show_y) { |
|
||||||
result += "@"; |
|
||||||
} else { |
|
||||||
result += cell.display; |
|
||||||
} |
|
||||||
|
|
||||||
if(it.row) result += "\n"; |
|
||||||
} |
|
||||||
|
|
||||||
return result; |
|
||||||
} |
|
||||||
|
|
||||||
void TileMap::dump(int show_x, int show_y) { |
|
||||||
std::cout << to_string(show_x, show_y) << std::endl; |
|
||||||
} |
|
||||||
|
|
||||||
void TileMap::set_tile(size_t x, size_t y, const string& tile_name) { |
|
||||||
json& tile_conf = $config[tile_name]; |
|
||||||
|
|
||||||
auto tile = components::convert<Tile>(tile_conf); |
|
||||||
$tile_ids[y][x] = tile.display; |
|
||||||
$display[y][x] = tile; |
|
||||||
} |
|
||||||
|
|
||||||
void TileMap::load(matrix::Matrix &walls) { |
|
||||||
for(matrix::each_cell it{walls}; it.next();) { |
|
||||||
string tile_name = walls[it.y][it.x] == SPACE_VALUE ? "FLOOR_TILE" : "WALL_PLAIN"; |
|
||||||
set_tile(it.x, it.y, tile_name); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
const Tile &TileMap::at(size_t x, size_t y) { |
|
||||||
return $display[y][x]; |
|
||||||
} |
|
||||||
|
|
||||||
std::vector<std::string> TileMap::tile_names(bool collision) { |
|
||||||
const auto &json = $config.json(); |
|
||||||
std::vector<std::string> keys; |
|
||||||
|
|
||||||
for(const auto& el : json.items()) { |
|
||||||
const auto &val = el.value(); |
|
||||||
if(val["collision"] == collision) { |
|
||||||
keys.push_back(el.key()); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
return keys; |
|
||||||
} |
|
||||||
|
|
||||||
bool TileMap::INVARIANT() { |
|
||||||
dbc::check(matrix::height($tile_ids) == $height, "$tile_ids has wrong height"); |
|
||||||
dbc::check(matrix::width($tile_ids) == $width, "$tile_ids has wrong width"); |
|
||||||
return true; |
|
||||||
} |
|
@ -1,38 +0,0 @@ |
|||||||
#pragma once |
|
||||||
#include <vector> |
|
||||||
#include <utility> |
|
||||||
#include <string> |
|
||||||
#include <algorithm> |
|
||||||
#include <fmt/core.h> |
|
||||||
#include "point.hpp" |
|
||||||
#include "matrix.hpp" |
|
||||||
#include "config.hpp" |
|
||||||
#include "components.hpp" |
|
||||||
|
|
||||||
typedef std::vector<components::Tile> TileRow; |
|
||||||
typedef std::vector<TileRow> TileGrid; |
|
||||||
|
|
||||||
class TileMap { |
|
||||||
public: |
|
||||||
Config $config; |
|
||||||
size_t $width; |
|
||||||
size_t $height; |
|
||||||
matrix::Matrix $tile_ids; |
|
||||||
TileGrid $display; |
|
||||||
|
|
||||||
TileMap(size_t width, size_t height); |
|
||||||
|
|
||||||
// disable copying
|
|
||||||
TileMap(TileMap &map) = delete; |
|
||||||
|
|
||||||
size_t width() { return $width; } |
|
||||||
size_t height() { return $height; } |
|
||||||
void load(matrix::Matrix &walls); |
|
||||||
const components::Tile& at(size_t x, size_t y); |
|
||||||
void set_tile(size_t x, size_t y, const std::string& tile_name); |
|
||||||
std::vector<std::string> tile_names(bool collision); |
|
||||||
|
|
||||||
std::string to_string(int show_x, int show_y); |
|
||||||
void dump(int show_x=-1, int show_y=-1); |
|
||||||
bool INVARIANT(); |
|
||||||
}; |
|
Loading…
Reference in new issue