SuperSpaceInvaderTurboApoca.../src/game/gameBasics.cpp
2022-01-06 09:58:06 +01:00

141 lines
3.6 KiB
C++

#include <chrono>
#include <thread>
#include "game.h"
#include "playMode.h"
#define WININIT window("space invader du turfu", 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();
}
void Game::updateColumns(){
while(grid.at(0).getOutterInvader()==grid.at(0).size()){
grid.erase(grid.begin());
basePos+=confData.invadersSize+confData.invadersDistance;
}
while(grid.at(grid.size()-1).getOutterInvader()==grid.at(grid.size()-1).size()){
grid.erase(grid.end()-1);
}
}
void Game::handleScoreSaving(){
cout << players[0].score << endl; // will remove
string pName;
pm.askPlayerNameMenu(0, pName);
sm.inputScore(pName, players[0].score);
if(playMode==PlayMode::TWO_LOCAL){
string pName2;
pm.askPlayerNameMenu(1, pName2);
sm.inputScore(pName, players[1].score);
}
sm.writeFile();
}
void Game::managedGames() {
playMode = PlayMode::NONE;
god.state = GodState::NONE;
while(playMode!=PlayMode::EXIT){
if(playMode==PlayMode::NONE){
playMode = pm.showInitialMenu();
}else{
playGame(); // will read the playMode
handleScoreSaving();
cout << "END OF GAME" << endl;
break; // TODO remove
if(!pm.showDeathMenu()) playMode = PlayMode::NONE; // back to the main menu
}
}
}
// TODO maybe directly call theses from pm and do not use getters ?
/**
* Plays the game, and returns once the game is finished
*
* @return @WinValue::PLAYERS if the players won, @WinValue::INVADERS is the invaders won, WinValue::NOBODY else (also in case of error)
*/
WinValue Game::playGame(){ // returns when game is finished
// INIT
// we assume the game has been played before, and so we need to clean used members
grid = confData.grid; // will copy the grid
updateColumns(); // Would have liked to to that in the "config grid", but.. I'm lazy
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;
basePos = Position(200, 0);
// GAMELOOP
const int MAX_FPS = 60;
chrono::milliseconds fpsTime;
unsigned tmpFps = 0;
unsigned fps = 0;
while(window.isOpen()){
chrono::time_point startTime = chrono::high_resolution_clock::now();
pm.startFrame();
managePlayers();
if(manageInvaders()) { // if they went down
if (invadersTouchPlayer())return WinValue::INVADERS;
// TODO abstract this ?
if (basePos.getY() > 100 /*lambda value*/ && god.state==GodState::NONE) {
awakeGod();
}
}
manageGod();
moveMissiles();
remCollidingProjectiles();
moveTorpedos();
remCollidingProjectiles();
if(checkMissilesAndPlayers())return WinValue::INVADERS;
if(checkTorpedosAndInvaders())return WinValue::PLAYERS;
display(fps);
pm.endFrame();
chrono::time_point endTime = chrono::high_resolution_clock::now();
chrono::milliseconds frameTime = chrono::duration_cast<chrono::milliseconds>(endTime-startTime);
fpsTime = fpsTime + frameTime;
if(fpsTime>chrono::seconds(1)){
fps = tmpFps;
tmpFps = 0;
fpsTime = chrono::milliseconds(0);
}else ++tmpFps;
// old implementation, too volatile
// cout << "FPS : " << chrono::milliseconds(1000)/frameTime << endl;
this_thread::sleep_until(startTime+chrono::duration<double, ratio<1, MAX_FPS>>(1));
}
return WinValue::NOBODY;
}
Position Game::invIndexToPos(unsigned x, unsigned y) const {
return basePos+Position(INV_GET_POS(x), INV_GET_POS(y));
}