Something went wrong. Try again.
A card game engine for Magic: The Gathering inspired by Card Forge
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176#include "game.h"#include <flecs.h>#include <vector>#include <string>#include <uuid.h>#include "player.h"#include "zone.h"#include "card.h"#include <map>#include <cassert>#include <tuple>using namespace std;
Game::Game(flecs::world* w, vector<string> 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<Player>({ (*gen)(), playerName, 0, hand, library, graveyard, exile, battlefield, commander, map<string, unsigned int>(), Mana{} });}
flecs::entity Game::initZone(string name, flecs::entity owner, ZoneVisibility visibility, ZoneType zoneType) { flecs::entity entity = world->entity();
entity.set<Zone>({ (*gen)(), owner, vector<flecs::entity>(), name, visibility, zoneType });
return entity;}
tuple<flecs::entity, vector<CardDefId>> 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<Player> q = world->query<Player>();
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<Player>(); Zone& handZone = playerData.hand.get_mut<Zone>();
for (int i = 0; i < 7; i++) { flecs::entity newInstance = world->entity();
newInstance.set<CardInstance>({ (*gen)(), ids[randInt(0, 5)] });
newInstance.set<CardRect>({ 0.0f, 0.0f, 0.0f, 0.0f, });
handZone.cards.push_back(newInstance); }
Zone& libraryZone = playerData.library.get_mut<Zone>();
for (int i = 0; i < 92; i++) { flecs::entity newInstance = world->entity();
newInstance.set<CardInstance>({ (*gen)(), ids[randInt(0, 5)] });
newInstance.set<CardRect>({ 0.0f, 0.0f, 0.0f, 0.0f, });
libraryZone.cards.push_back(newInstance); }
return { player, vector<CardDefId>({ forestId, mountainId, swampId, islandId, plainsId })};}
void setupRand() { srand(time(0));}
int randInt(int a, int b) { return (rand() % (b - a)) + a;}