138 lines
3 KiB
C++
138 lines
3 KiB
C++
#include <catch2/catch_test_macros.hpp>
|
|
#include <fmt/core.h>
|
|
#include "game/systems.hpp"
|
|
#include <cmath>
|
|
#include <numbers>
|
|
|
|
|
|
TEST_CASE("figure out best rotation direction", "[systems-rotate]") {
|
|
Matrix map = matrix::make(3, 3);
|
|
|
|
Point player_at{1, 1};
|
|
map[player_at.y][player_at.x] = 2;
|
|
|
|
|
|
for(matrix::box target{map, player_at.x, player_at.y, 1}; target.next();) {
|
|
for(matrix::box aiming_at{map, player_at.x, player_at.y, 1}; aiming_at.next();) {
|
|
map[aiming_at.y][aiming_at.x] = 10;
|
|
|
|
float target_dx = float(player_at.x) - float(target.x);
|
|
float target_dy = float(player_at.y) - float(target.y);
|
|
float aiming_dx = float(player_at.x) - float(aiming_at.x);
|
|
float aiming_dy = float(player_at.y) - float(aiming_at.y);
|
|
|
|
float target_angle = atan2(-target_dy, target_dx) * (180.0 / std::numbers::pi);
|
|
float aiming_angle = atan2(-aiming_dy, aiming_dx) * (180.0 / std::numbers::pi);
|
|
|
|
float diff = target_angle - aiming_angle;
|
|
double normalized = fmod(diff + 360.0, 360.0);
|
|
|
|
REQUIRE(normalized >= 0);
|
|
REQUIRE(normalized <= 360);
|
|
|
|
map[aiming_at.y][aiming_at.x] = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
using MovingFunc = std::function<void()>;
|
|
using CombatFunc = std::function<void(bool)>;
|
|
using RenderFunc = std::function<void()>;
|
|
using UpdateFunc = std::function<void()>;
|
|
using UseFunc = std::function<void()>;
|
|
using PickupFunc = std::function<void()>;
|
|
|
|
struct Engine {
|
|
std::vector<MovingFunc> $moving;
|
|
std::vector<CombatFunc> $combat;
|
|
std::vector<RenderFunc> $render;
|
|
std::vector<UpdateFunc> $update;
|
|
std::vector<UseFunc> $use;
|
|
std::vector<PickupFunc> $pickup;
|
|
|
|
void addMoving(MovingFunc action) {
|
|
$moving.emplace_back(action);
|
|
}
|
|
|
|
void addCombat(CombatFunc action) {
|
|
$combat.emplace_back(action);
|
|
}
|
|
|
|
void addRender(RenderFunc action) {
|
|
$render.emplace_back(action);
|
|
}
|
|
|
|
void addUpdate(UpdateFunc action) {
|
|
$update.emplace_back(action);
|
|
}
|
|
|
|
void addUse(UseFunc action) {
|
|
$use.emplace_back(action);
|
|
}
|
|
|
|
void addPickup(PickupFunc action) {
|
|
$pickup.emplace_back(action);
|
|
}
|
|
|
|
void runMoving() {
|
|
for(auto func : $moving) {
|
|
func();
|
|
}
|
|
}
|
|
|
|
void runCombat(bool attr) {
|
|
for(auto func : $combat) {
|
|
func(attr);
|
|
}
|
|
}
|
|
|
|
void runRender() {
|
|
for(auto func : $render) {
|
|
func();
|
|
}
|
|
}
|
|
|
|
void runUpdate() {
|
|
for(auto func : $update) {
|
|
func();
|
|
}
|
|
}
|
|
|
|
void runUse() {
|
|
for(auto func : $use) {
|
|
func();
|
|
}
|
|
}
|
|
|
|
void runPickup() {
|
|
for(auto func : $pickup) {
|
|
func();
|
|
}
|
|
}
|
|
};
|
|
|
|
void test_system_1() {
|
|
fmt::println("TEST 1");
|
|
}
|
|
|
|
void test_combat(bool what) {
|
|
fmt::println("TEST 2: {}", what);
|
|
}
|
|
|
|
TEST_CASE("new system running engine thing", "[systems-engine]") {
|
|
Engine systems;
|
|
|
|
systems.addMoving(test_system_1);
|
|
systems.addCombat(test_combat);
|
|
systems.addRender(test_system_1);
|
|
systems.addUpdate(test_system_1);
|
|
systems.addUse(test_system_1);
|
|
systems.addPickup(test_system_1);
|
|
|
|
systems.runCombat(false);
|
|
systems.runMoving();
|
|
systems.runRender();
|
|
systems.runUpdate();
|
|
systems.runUse();
|
|
systems.runPickup();
|
|
}
|