#include "app.h" #include "raylib.h" #include #include #include "assets.h" #include #include #include #include #include "ecs.h" #include #include using namespace std; App::App(uuids::uuid_random_generator* gen) { if (gen == nullptr) { return; } assets = getAssets(); game = Game(&ecs, vector({ "Player 1", "Player 2", "Player 3", "Player 4" }), gen); } void App::run() { InitWindow(1920, 1080, "ManaGrove"); SetTargetFPS(60); initGame(); while (!WindowShouldClose()) { updateGame(); BeginDrawing(); ClearBackground(BLACK); drawGame(); EndDrawing(); } cleanGame(); CloseWindow(); } void App::initGame() { Image noCardLoadedImage = LoadImageFromMemory(".png", &assets["no_card.png"][0], (int)assets["no_card.png"].size()); cardWidth = (1080.0f / 6.0f) * ((float)(noCardLoadedImage.width) / (float)(noCardLoadedImage.height)); noCardImage = LoadTextureFromImage(noCardLoadedImage); UnloadImage(noCardLoadedImage); TraceLog(LOG_INFO, "Card width is %.2f", cardWidth); font = LoadFontFromMemory(".ttf", &assets["Carlito-Regular.ttf"][0], (int)assets["Carlito-Regular.ttf"].size(), 24, NULL, 0); tuple> data = game.setupTestData(); mainPlayerEntity = get(data); vector cards = get>(data); vector cardName = vector({ "forest.png", "mountain.png", "swamp.png", "island.png", "plains.png" }); for (unsigned int i = 0; i < 5; i++) { Image cardImage = LoadImageFromMemory(".png", &assets[cardName[i]][0], (int)assets[cardName[i]].size()); Texture cardTexture = LoadTextureFromImage(cardImage); UnloadImage(cardImage); textures.insert(pair{cards[i], cardTexture}); } zones.insert(pair{"topRight", map()}); zones.insert(pair{"topLeft", map()}); zones.insert(pair{"bottomLeft", map()}); map bottomRight; bottomRight.insert(pair{"battlefield", Rectangle{ (1920.0f - 361.0f) / 2.0f, 1080.0f / 2.0f, (1920.0f - 361.0f) / 2.0f, 1080.0f / 2.0f * (2.0f / 3.0f) }}); bottomRight.insert(pair{"command", Rectangle{ 1920.0f - 361.0f - cardWidth, 1080.0f * (5.0f / 6.0f), cardWidth, 1080.0f / 6.0f }}); bottomRight.insert(pair{"exile", Rectangle{ 1920.0f - 361.0f - (cardWidth * 2.0f), 1080.0f * (5.0f / 6.0f), cardWidth, 1080.0f / 6.0f }}); bottomRight.insert(pair{"graveyard", Rectangle{ 1920.0f - 361.0f - (cardWidth * 3.0f), 1080.0f * (5.0f / 6.0f), cardWidth, 1080.0f / 6.0 }}); bottomRight.insert(pair{"library", Rectangle{ 1920.0f - 361.0f - (cardWidth * 4.0f), 1080.0f * (5.0f / 6.0f), cardWidth, 1080.0f / 6.0 }}); bottomRight.insert(pair{"hand", Rectangle{ (1920.0f - 361.0f) / 2.0f, 1080.0f * (5.0f / 6.0f), ((1920.0f - 361.0f) / 2.0f) - (cardWidth * 4.0f), 1080.0f / 6.0f }}); zones.insert(pair{"bottomRight", bottomRight}); } void App::updateGame() { updateHandCards(); } void App::updateHandCards() { float cardAreaWidth = ((1920.0f - 361.0f) / 2.0f) - (cardWidth * 4.0f); Player& playerData = mainPlayerEntity.get_mut(); Zone& handZone = playerData.hand.get_mut(); unsigned int cardNum = (unsigned int)handZone.cards.size(); float spacing; if (cardNum == 1) { spacing = cardAreaWidth - cardWidth; } else { spacing = (cardAreaWidth - cardWidth) / ((float)cardNum - 1.0f); } float startX = (1920.0f - 361.0f) / 2.0f; float yCoord = 1080.0f * (5.0f / 6.0f); // calculate each card's x/y coord for (unsigned int i = 0; i < cardNum; i++) { flecs::entity entity = handZone.cards[i]; if (!entity.has()) { throw "Not a card instance"; } if (!entity.has()) { throw "Entity does not have rectangle"; } const CardInstance card = entity.get(); CardRect& rec = entity.get_mut(); if (entity.has()) { continue; } rec.x = startX + ((float)i * spacing); rec.y = yCoord; rec.h = 1080.0f / 6.0f; rec.w = cardWidth; entity.add(); } } void App::drawGame() { renderDebugAreas(); flecs::query mousedQuery = ecs.query(); flecs::entity mousedEntity = mousedQuery.first(); if (mousedEntity.is_alive()) { const CardInstance& instance = mousedEntity.get(); float calcHeight = 509.0f * (textures[instance.source].height / textures[instance.source].width); DrawTextureEx(textures[instance.source], Vector2{ 1920.0f - 361.0f, 1080.0f - calcHeight }, 0.0f, (361.0f / textures[instance.source].width), WHITE); } else { float calcHeight = 509.0f * (noCardImage.height / noCardImage.width); DrawTextureEx(noCardImage, Vector2{ 1920.0f - 361.0f, 1080.0f - calcHeight }, 0.0f, (361.0f / noCardImage.width), WHITE); } flecs::query cardQuery = ecs.query(); cardQuery.each([this](flecs::entity entity, const CardInstance& instance, const CardRect& rect, const RenderFront& rf) { DrawTextureEx(textures[instance.source], Vector2{ rect.x, rect.y }, 0.0f, rect.h / textures[instance.source].height, WHITE); }); } void App::renderDebugAreas() { // Player 2 stuff DrawRectangleRec(Rectangle{ 0.0f, 0.0f, (1920.0f - 361.0f) / 2.0f, 1080.0f / 2.0f}, Color{ 0x7f, 0x00, 0x00, 0xff }); DrawTextEx(font, "Player 2", Vector2{ (1920.0f - 361.0f) / 4.0f, 1080.0f / 4.0f }, 24.0f, 1.0f, WHITE); // Player 3 stuff DrawRectangleRec(Rectangle{ 0.0f, 1080.0f / 2.0f, (1920.0f - 361.0f) / 2.0f, 1080.0f / 2.0f }, Color{ 0x55, 0x00, 0x00, 0xff }); DrawTextEx(font, "Player 3", Vector2{ (1920.0f - 361.0f) / 4.0f, 1080.0f * (3.0f / 4.0f) }, 24.0f, 1.0f, WHITE); // Player 4 stuff DrawRectangleRec(Rectangle{ (1920.0f - 361.0f) / 2.0f, 0.0f, (1920.0f - 361.0f) / 2.0f, 1080.0f / 2.0f }, Color{ 0x00, 0x7f, 0x00, 0xff }); DrawTextEx(font, "Player 4", Vector2{ (1920.0f - 361.0f) * (3.0f / 4.0f), 1080.0f / 4.0f }, 24.0f, 1.0f, WHITE); // Player 1 stuff DrawRectangleRec(Rectangle{ (1920.0f - 361.0f) / 2.0f, 1080.0f / 2.0f, (1920.0f - 361.0f) / 2.0f, 1080.0f / 2.0f }, Color{ 0x00, 0x55, 0x00, 0xff }); // // Command Zone DrawRectangleRec(zones["bottomRight"]["command"], Color{ 0x00, 0xff, 0x00, 0xff }); DrawTextEx(font, "Command", Vector2{ 1920.0f - 361.0f - cardWidth, 1080.0f * (11.0f / 12.0f) }, 24.0f, 1.0f, BLACK); // // Exile Zone DrawRectangleRec(zones["bottomRight"]["exile"], Color{ 0x3f, 0x3f, 0xff, 0xff }); DrawTextEx(font, "Exile", Vector2{ 1920.0f - 361.0f - (cardWidth * 2.0f), 1080.0f * (11.0f / 12.0f) }, 24.0f, 1.0f, Color{ 0xee, 0xee, 0xee, 0xff }); // // Graveyard Zone DrawRectangleRec(zones["bottomRight"]["graveyard"], Color{ 0xff, 0x3f, 0x3f, 0xff }); DrawTextEx(font, "Graveyard", Vector2{ 1920.0f - 361.0f - (cardWidth * 3.0f), 1080.0f * (11.0f / 12.0f) }, 24.0f, 1.0f, BLACK); // // Library Zone DrawRectangleRec(zones["bottomRight"]["library"], Color{ 0xff, 0xff, 0x00, 0xff }); DrawTextEx(font, "Library", Vector2{ 1920.0f - 361.0f - (cardWidth * 4.0f), 1080.0f * (11.0f / 12.0f) }, 24.0f, 1.0f, BLACK); // // Hand Zone DrawRectangleRec(zones["bottomRight"]["hand"], Color{ 0x00, 0xff, 0xff, 0xff }); DrawTextEx(font, "Hand", Vector2{ (1920.0f - 361.0f) / 2.0f, 1080.0f * (11.0f / 12.0f) }, 24.0f, 1.0f, BLACK); } void App::cleanGame() { UnloadTexture(noCardImage); for (const auto& [key, value] : textures) { UnloadTexture(value); } }