/*! * * @file gameBasics.cpp * @author RUBINI Thomas * @author SIMAILA Djalim * @date January 2022 * @version 1.0 * @brief game basic mechanisms * */ #include #include #include "game.h" #include "playMode.h" #define WININIT window("SUPER Space Invader : Turbo Apocalypse DX - VS GOD", Position(1280, 720), Position(128, 128), nsGraphics::KBlack) Game::Game() : WININIT, pm(window) { if(!reloadConfig()){ throw runtime_error("Initial config loading failed. Please check the error above and fix the configuration"); } sm.readFile(); } bool Game::areThereInvadersLeft(){ return grid.validColsNumber() > 0; } void Game::handleScoreSaving(){ string pName; pm.askPlayerNameMenu(PLAYER1, pName); sm.inputScore(move(pName), players[0].score); if(playMode==PlayMode::TWO_LOCAL){ string pName2; pm.askPlayerNameMenu(PLAYER2, pName2); sm.inputScore(move(pName2), players[1].score); } sm.writeFile(); } void Game::managedGames() { playMode = PlayMode::NONE; while(playMode!=PlayMode::EXIT){ if(playMode==PlayMode::NONE){ playMode = pm.showInitialMenu(); }else{ initGame(); enterGameLoop(); // will read the playMode handleScoreSaving(); cout << "END OF GAME" << endl; if(!pm.showDeathMenu())playMode = PlayMode::NONE; } } } // we assume the game has been played before, and so we need to clean used members void Game::initGame(){ grid = confData.grid; // will copy the grid // we re-construct players objects, we don't have to clear all members and can rely on the construction value (set in .h file) players.clear(); missiles.clear(); god.state = GodState::NONE; torpedos.clear(); if(playMode==PlayMode::SINGLE){ players.resize(1); }else{ players.resize(2); // mirror the start X Position for the other players[1].x = pm.getScreenWidth() - confData.startXPosition - confData.playersWidth; } players[0].x = confData.startXPosition; for(unsigned i=0;i 100 /*lambda value*/ && god.state==GodState::NONE) { awakeGod(); } } if(manageGod())return WinValue::PLAYERS; if(arePlayersDead())return WinValue::GOD; moveMissiles(); remCollidingProjectiles(); moveTorpedos(); remCollidingProjectiles(); checkMissilesAndPlayers(); if(checkTorpedosAndInvaders())return WinValue::PLAYERS; if(arePlayersDead())return WinValue::INVADERS; displayAll(fps); pm.endFrame(); MyTimePoint endTime = chrono::high_resolution_clock::now(); // This code is counted as part of frames, but that's not really something we can control if(fpsStartTime+chrono::seconds(1) < endTime){ fps = tmpFps; tmpFps = 0; fpsStartTime = {}; }else ++tmpFps; this_thread::sleep_until(startTime+maxFrameTime); } return WinValue::NOBODY; } Position Game::invIndexToPos(unsigned x, unsigned y) const { return basePos+Position(INV_GET_POS(x), INV_GET_POS(y)); } bool Game::arePlayersDead() { for(Player& p : players){ if(!p.isEliminated())return false; } return true; }