2025-02-13 19:40:37 +03:00
|
|
|
#include "dinkyecs.hpp"
|
|
|
|
#include "point.hpp"
|
|
|
|
#include <cassert>
|
2025-02-13 22:25:18 +03:00
|
|
|
#include <fmt/base.h>
|
2025-02-13 19:40:37 +03:00
|
|
|
|
|
|
|
#define REQUIRE(X) assert(X);
|
|
|
|
|
|
|
|
using DinkyECS::Entity;
|
|
|
|
|
|
|
|
using namespace fmt;
|
|
|
|
using DinkyECS::Entity;
|
|
|
|
using std::string;
|
|
|
|
|
2025-02-13 19:47:52 +03:00
|
|
|
struct Player
|
|
|
|
{
|
|
|
|
string name;
|
|
|
|
Entity eid;
|
2025-02-13 19:40:37 +03:00
|
|
|
};
|
|
|
|
|
2025-02-13 19:47:52 +03:00
|
|
|
struct Position
|
|
|
|
{
|
|
|
|
Point location;
|
2025-02-13 19:40:37 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// DINKY_HAS_COMPONENT(Point, x, y);
|
|
|
|
// DINKY_HAS_COMPONENT(Position, location);
|
|
|
|
|
2025-02-13 19:47:52 +03:00
|
|
|
struct Motion
|
|
|
|
{
|
|
|
|
int dx;
|
|
|
|
int dy;
|
|
|
|
bool random = false;
|
2025-02-13 19:40:37 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// DINKY_HAS_COMPONENT(Motion, dx, dy, random);
|
|
|
|
|
2025-02-13 19:47:52 +03:00
|
|
|
struct Velocity
|
|
|
|
{
|
|
|
|
double x, y;
|
2025-02-13 19:40:37 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// DINKY_HAS_COMPONENT(Velocity, x, y);
|
|
|
|
|
2025-02-13 19:47:52 +03:00
|
|
|
struct Gravity
|
|
|
|
{
|
|
|
|
double level;
|
2025-02-13 19:40:37 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// DINKY_HAS_COMPONENT(Gravity, level);
|
|
|
|
|
2025-02-13 19:47:52 +03:00
|
|
|
struct DaGUI
|
|
|
|
{
|
|
|
|
int event;
|
2025-02-13 19:40:37 +03:00
|
|
|
};
|
|
|
|
|
2025-02-13 19:47:52 +03:00
|
|
|
void configure(DinkyECS::World &world, Entity &test)
|
|
|
|
{
|
|
|
|
println("---Configuring the base system.");
|
|
|
|
Entity test2 = world.entity();
|
2025-02-13 19:40:37 +03:00
|
|
|
|
2025-02-13 19:47:52 +03:00
|
|
|
world.set<Position>(test, {10, 20});
|
|
|
|
world.set<Velocity>(test, {1, 2});
|
2025-02-13 19:40:37 +03:00
|
|
|
|
2025-02-13 19:47:52 +03:00
|
|
|
world.set<Position>(test2, {1, 1});
|
|
|
|
world.set<Velocity>(test2, {9, 19});
|
2025-02-13 19:40:37 +03:00
|
|
|
|
2025-02-13 19:47:52 +03:00
|
|
|
println("---- Setting up the player as a fact in the system.");
|
2025-02-13 19:40:37 +03:00
|
|
|
|
2025-02-13 19:47:52 +03:00
|
|
|
auto player_eid = world.entity();
|
|
|
|
Player player_info{"Zed", player_eid};
|
|
|
|
// just set some player info as a fact with the entity id
|
|
|
|
world.set_the<Player>(player_info);
|
2025-02-13 19:40:37 +03:00
|
|
|
|
2025-02-13 19:47:52 +03:00
|
|
|
world.set<Velocity>(player_eid, {0, 0});
|
|
|
|
world.set<Position>(player_eid, {0, 0});
|
2025-02-13 19:40:37 +03:00
|
|
|
|
2025-02-13 19:47:52 +03:00
|
|
|
auto enemy = world.entity();
|
|
|
|
world.set<Velocity>(enemy, {0, 0});
|
|
|
|
world.set<Position>(enemy, {0, 0});
|
2025-02-13 19:40:37 +03:00
|
|
|
|
2025-02-13 19:47:52 +03:00
|
|
|
println("--- Creating facts (singletons)");
|
|
|
|
world.set_the<Gravity>({0.9});
|
2025-02-13 19:40:37 +03:00
|
|
|
}
|
|
|
|
|
2025-02-13 19:47:52 +03:00
|
|
|
int main()
|
|
|
|
{
|
|
|
|
DinkyECS::World world;
|
|
|
|
Entity test = world.entity();
|
|
|
|
|
|
|
|
configure(world, test);
|
|
|
|
|
|
|
|
Position &pos = world.get<Position>(test);
|
2025-02-14 19:14:15 +03:00
|
|
|
Position &pos2 = world.get<Position>(test);
|
2025-02-13 19:47:52 +03:00
|
|
|
REQUIRE(pos.location.x == 10);
|
|
|
|
REQUIRE(pos.location.y == 20);
|
|
|
|
|
2025-02-14 19:14:15 +03:00
|
|
|
REQUIRE(pos2.location.x == 10);
|
|
|
|
REQUIRE(pos2.location.y == 20);
|
|
|
|
|
2025-02-13 19:47:52 +03:00
|
|
|
Velocity &vel = world.get<Velocity>(test);
|
|
|
|
REQUIRE(vel.x == 1);
|
|
|
|
REQUIRE(vel.y == 2);
|
|
|
|
|
|
|
|
world.query<Position>(
|
|
|
|
[](const auto &ent, auto &pos)
|
|
|
|
{
|
|
|
|
REQUIRE(ent > 0);
|
|
|
|
REQUIRE(pos.location.x >= 0);
|
|
|
|
REQUIRE(pos.location.y >= 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
world.query<Velocity>(
|
|
|
|
[](const auto &ent, auto &vel)
|
|
|
|
{
|
|
|
|
REQUIRE(ent > 0);
|
|
|
|
REQUIRE(vel.x >= 0);
|
|
|
|
REQUIRE(vel.y >= 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
println("--- Manually get the velocity in position system:");
|
|
|
|
world.query<Position>(
|
|
|
|
[&](const auto &ent, auto &pos)
|
|
|
|
{
|
|
|
|
Velocity &vel = world.get<Velocity>(ent);
|
|
|
|
|
|
|
|
REQUIRE(ent > 0);
|
|
|
|
REQUIRE(pos.location.x >= 0);
|
|
|
|
REQUIRE(pos.location.y >= 0);
|
|
|
|
REQUIRE(ent > 0);
|
|
|
|
REQUIRE(vel.x >= 0);
|
|
|
|
REQUIRE(vel.y >= 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
println("--- Query only entities with Position and Velocity:");
|
|
|
|
world.query<Position, Velocity>(
|
|
|
|
[&](const auto &ent, auto &pos, auto &vel)
|
|
|
|
{
|
|
|
|
Gravity &grav = world.get_the<Gravity>();
|
|
|
|
REQUIRE(grav.level <= 1.0f);
|
|
|
|
REQUIRE(grav.level > 0.5f);
|
|
|
|
REQUIRE(ent > 0);
|
|
|
|
REQUIRE(pos.location.x >= 0);
|
|
|
|
REQUIRE(pos.location.y >= 0);
|
|
|
|
REQUIRE(ent > 0);
|
|
|
|
REQUIRE(vel.x >= 0);
|
|
|
|
REQUIRE(vel.y >= 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
// now remove Velocity
|
|
|
|
REQUIRE(world.has<Velocity>(test));
|
|
|
|
world.remove<Velocity>(test);
|
|
|
|
// REQUIRE_THROWS(world.get<Velocity>(test));
|
|
|
|
REQUIRE(!world.has<Velocity>(test));
|
|
|
|
|
|
|
|
println("--- After remove test, should only result in test2:");
|
|
|
|
world.query<Position, Velocity>(
|
|
|
|
[&](const auto &ent, auto &pos, auto &vel)
|
|
|
|
{
|
|
|
|
auto &in_position = world.get<Position>(ent);
|
|
|
|
auto &in_velocity = world.get<Velocity>(ent);
|
|
|
|
REQUIRE(pos.location.x >= 0);
|
|
|
|
REQUIRE(pos.location.y >= 0);
|
|
|
|
REQUIRE(in_position.location.x == pos.location.x);
|
|
|
|
REQUIRE(in_position.location.y == pos.location.y);
|
|
|
|
REQUIRE(in_velocity.x == vel.x);
|
|
|
|
REQUIRE(in_velocity.y == vel.y);
|
|
|
|
});
|
|
|
|
return 0;
|
2025-02-13 19:40:37 +03:00
|
|
|
}
|