#include "game.h" #include #include #include #include #include "player.h" #include "zone.h" #include "card.h" #include #include #include using namespace std; Game::Game(flecs::world* w, vector playerNames, uuids::uuid_random_generator* g) { if ((w == nullptr) || (g == nullptr)) { return; } world = w; gen = g; for (string playerName : playerNames) { initPlayer(playerName, world->entity()); } } void Game::initPlayer(string playerName, flecs::entity entity) { flecs::entity hand = initZone("Hand", entity, ZoneVisibility::Owner, ZoneType::Hand); flecs::entity library = initZone("Library", entity, ZoneVisibility::Hidden, ZoneType::Library); flecs::entity graveyard = initZone("Graveyard", entity, ZoneVisibility::All, ZoneType::Graveyard); flecs::entity exile = initZone("Exile", entity, ZoneVisibility::All, ZoneType::Exile); flecs::entity battlefield = initZone("Battlefield", entity, ZoneVisibility::All, ZoneType::Battlefield); flecs::entity commander = initZone("Commander", entity, ZoneVisibility::All, ZoneType::Command); entity.set({ (*gen)(), playerName, 0, hand, library, graveyard, exile, battlefield, commander, map(), Mana{} }); } flecs::entity Game::initZone(string name, flecs::entity owner, ZoneVisibility visibility, ZoneType zoneType) { flecs::entity entity = world->entity(); entity.set({ (*gen)(), owner, vector(), name, visibility, zoneType }); return entity; } tuple> Game::setupTestData() { setupRand(); CardDefId forestId = (*gen)(); CardDefId mountainId = (*gen)(); CardDefId islandId = (*gen)(); CardDefId plainsId = (*gen)(); CardDefId swampId = (*gen)(); CardDefId ids[5] = { forestId, mountainId, islandId, plainsId, swampId }; flecs::query q = world->query(); flecs::entity player = q.find([](Player& p) { return p.name == "Player 1"; }); assert(player); cards.insert(pair{forestId, Card{ forestId, "Forest", Mana{} }}); cards.insert(pair{mountainId, Card{ mountainId, "Mountain", Mana{} }}); cards.insert(pair{islandId, Card{ islandId, "Island", Mana{} }}); cards.insert(pair{plainsId, Card{ plainsId, "Plains", Mana{} }}); cards.insert(pair{swampId, Card{ swampId, "Swamp", Mana{} }}); Player& playerData = player.get_mut(); Zone& handZone = playerData.hand.get_mut(); for (int i = 0; i < 7; i++) { flecs::entity newInstance = world->entity(); newInstance.set({ (*gen)(), ids[randInt(0, 5)] }); newInstance.set({ 0.0f, 0.0f, 0.0f, 0.0f, }); handZone.cards.push_back(newInstance); } Zone& libraryZone = playerData.library.get_mut(); for (int i = 0; i < 92; i++) { flecs::entity newInstance = world->entity(); newInstance.set({ (*gen)(), ids[randInt(0, 5)] }); newInstance.set({ 0.0f, 0.0f, 0.0f, 0.0f, }); libraryZone.cards.push_back(newInstance); } return { player, vector({ forestId, mountainId, swampId, islandId, plainsId })}; } void setupRand() { srand(time(0)); } int randInt(int a, int b) { return (rand() % (b - a)) + a; }