85 lines
2.2 KiB
C++
85 lines
2.2 KiB
C++
#include <catch2/catch_test_macros.hpp>
|
|
#include <fmt/core.h>
|
|
#include <nlohmann/json.hpp>
|
|
#include <fstream>
|
|
#include "game/map.hpp"
|
|
#include "game/level.hpp"
|
|
#include "game/systems.hpp"
|
|
#include <cmath>
|
|
#include "graphics/textures.hpp"
|
|
#include "algos/rand.hpp"
|
|
|
|
using namespace fmt;
|
|
using namespace nlohmann;
|
|
using std::string;
|
|
|
|
json load_test_data(const string &fname) {
|
|
std::ifstream infile(fname);
|
|
return json::parse(infile);
|
|
}
|
|
|
|
TEST_CASE("camera control", "[map]") {
|
|
GameDB::init();
|
|
|
|
auto& level = GameDB::current_level();
|
|
auto& map = *level.map;
|
|
|
|
Point center = map.center_camera({10,10}, 5, 5);
|
|
|
|
// map.dump(center.x, center.y);
|
|
REQUIRE(center.x == 8);
|
|
REQUIRE(center.y == 8);
|
|
|
|
Point translation = map.map_to_camera({10,10}, center);
|
|
|
|
REQUIRE(translation.x == 2);
|
|
REQUIRE(translation.y == 2);
|
|
}
|
|
|
|
TEST_CASE("map placement test", "[map-fail]") {
|
|
GameDB::init();
|
|
|
|
for(int i = 0; i < 10; i++) {
|
|
auto& level = GameDB::create_level();
|
|
|
|
for(size_t rnum = 0; rnum < level.map->room_count(); rnum++) {
|
|
Point pos;
|
|
|
|
REQUIRE(level.map->place_entity(rnum, pos));
|
|
|
|
REQUIRE(!level.map->iswall(pos.x, pos.y));
|
|
REQUIRE(level.map->inmap(pos.x, pos.y));
|
|
}
|
|
}
|
|
}
|
|
|
|
TEST_CASE("map image test", "[map]") {
|
|
GameDB::init();
|
|
|
|
auto& level = GameDB::current_level();
|
|
Matrix map_tiles = matrix::make(7,7);
|
|
EntityGrid entity_map;
|
|
|
|
auto render = std::make_shared<sf::RenderTexture>();
|
|
sf::Sprite sprite{render->getTexture()};
|
|
auto player = level.world->get_the<components::Player>();
|
|
auto& player_pos = level.world->get<components::Position>(player.entity);
|
|
auto player_display = level.world->get<components::Tile>(player.entity).display;
|
|
|
|
for(matrix::each_row it{level.map->walls()}; it.next();) {
|
|
player_pos.location.x = it.x;
|
|
player_pos.location.y = it.y;
|
|
System::draw_map(map_tiles, entity_map);
|
|
System::render_map(map_tiles, entity_map, *render, 2, player_display);
|
|
|
|
// randomly test about 80% of them
|
|
if(Random::uniform(0, 100) < 20) break;
|
|
|
|
#ifdef TEST_RENDER
|
|
// confirm we get two different maps
|
|
auto out_img = render->getTexture().copyToImage();
|
|
bool worked = out_img.saveToFile(fmt::format("tmp/map_render{}{}.png", it.x, it.y));
|
|
REQUIRE(worked);
|
|
#endif
|
|
}
|
|
}
|