SUPER Space invader : Turbo edition DX - VS GOD 1.0.0
A simple space invader ripoff
gameBasics.cpp
Go to the documentation of this file.
1
12#include <chrono>
13#include <memory>
14#include <thread>
15#include "game.h"
16#include "playMode.h"
18
19#define WININIT window("SUPER Space Invader : Turbo Apocalypse DX - VS GOD", Position(1280, 720), Position(128, 128), nsGraphics::KBlack)
20
22
23 if(!reloadConfig()){ // Config
24 throw runtime_error("Initial config loading failed. Please check the error above and fix the configuration");
25 }
26
27 // Pixel Manager
28 if(confData.theme=="good"){
29 pm = std::make_unique<GoodPixelManager>(window);
30 }else if(confData.theme=="bad"){
31 pm = std::make_unique<PixelManager>(window);
32 }else throw runtime_error("Invalid theme value : "+confData.theme+
33 "\nValid values are : good,bad");
34
35 cout << "Loading sprites..." << endl;
36 vector<Task> tasks;
37 chrono::high_resolution_clock::time_point start = chrono::high_resolution_clock::now();
38
39 pm->loadSprites(tasks);
40 for(future<void>& f : tasks)f.wait();
41
42 // We just do that for one sprite, so I didn't bother creating an 'API' for that
43 pm->leftHand.mirror(pm->rightHand);
44
45 chrono::high_resolution_clock::duration elapsed = chrono::high_resolution_clock::now()-start;
46 cout << "Done ! Time : " << chrono::duration_cast<chrono::milliseconds>(elapsed).count() << "ms" << endl;
47
48 sm.readFile(); // Score manager
49}
50
51bool Game::areThereInvadersLeft(){
52 return grid.validColsNumber() > 0;
53}
54
55void Game::handleScoreSaving(){
56 for(unsigned i=0;i<players.size();++i){
57 string pName;
58 pm->askPlayerNameMenu(i, players[i].score, pName);
59 sm.inputScore(move(pName), players[i].score);
60 }
61 sm.writeFile();
62}
63
65
66 playMode = PlayMode::NONE;
67 WinValue whoWon;
68
69 while(playMode!=PlayMode::EXIT){
70 if(playMode==PlayMode::NONE){
71 playMode = pm->showInitialMenu();
72 }else{
73 DEBUG_MSG("Starting game")
74 initGame();
75 whoWon = enterGameLoop(); // will read the playMode
76 DEBUG_MSG("END End of game")
77 handleScoreSaving();
78 if(!pm->showDeathMenu(sm.scores,whoWon))playMode = PlayMode::NONE;
79 }
80 }
81}
82
83// we assume the game has been played before, and so we need to clean used members
84void Game::initGame(){
85 grid = confData.grid; // will copy the grid
86
87 // we re-construct players objects, we don't have to clear all members and can rely on the construction value (set in .h file)
88 players.clear();
89
90 missiles.clear();
92 torpedos.clear();
93
94 if(playMode==PlayMode::SINGLE){
95 players.resize(1);
96 }else{
97 players.resize(2);
98 // mirror the start X Position for the other
99 players[1].x = pm->getScreenWidth() - confData.startXPosition - confData.playersWidth;
100 }
101 players[0].x = confData.startXPosition;
102
103 for(unsigned i=0;i<players.size();++i){
104 players[i].id = i;
105 players[i].lives = confData.playersLives;
106 }
107
108 baseInvPos = Position(200, 0);
109 direction = true;
110
111}
112
113#define START_TIMER() DEBUG_INSTR(debugTime = chrono::high_resolution_clock::now())
114#define PRINT_TIMER(X) DEBUG_MSG((X) << ": " << chrono::duration_cast<chrono::nanoseconds>(chrono::high_resolution_clock::now()-debugTime).count())
115
116WinValue Game::enterGameLoop(){ // returns when game is finished
117 // computed in advance for performance reasons
118 chrono::milliseconds maxFrameTime = chrono::milliseconds(1000/confData.maxFPS);
119
120 unsigned tmpFps = 0;
121 unsigned fps = 0;
122 typedef chrono::high_resolution_clock::time_point MyTimePoint;
123 MyTimePoint fpsStartTime = {};
124
125 while(window.isOpen()){
126
127 MyTimePoint startTime = chrono::high_resolution_clock::now();
128 if(fpsStartTime.time_since_epoch()==chrono::seconds(0)){
129 fpsStartTime = startTime;
130 }
131
132 pm->startFrame();
133
134 managePlayers();
135 if(manageInvaders()) { // if they went down
136 if (invadersTouchPlayer())return WinValue::INVADERS;
137 tryAwakeGod();
138 }
139
140 if(manageGod())return WinValue::PLAYERS;
141 if(arePlayersDead())return WinValue::GOD;
142
143 moveMissiles();
144 remCollidingProjectiles();
145 moveTorpedos();
146 remCollidingProjectiles();
147
148 checkMissilesAndPlayers();
149 if(checkTorpedosAndInvaders())return WinValue::PLAYERS;
150
152
153 displayAll(fps);
154
155 pm->endFrame();
156
157 MyTimePoint endTime = chrono::high_resolution_clock::now();
158
159 // This code is counted as part of frames, but that's not really something we can control
160 if(fpsStartTime+chrono::seconds(1) < endTime){
161 fps = tmpFps;
162 tmpFps = 0;
163 fpsStartTime = {};
164 }else ++tmpFps;
165
166 this_thread::sleep_until(startTime+maxFrameTime);
167 }
168
169 return WinValue::NOBODY;
170}
171
172Position Game::invIndexToPos(unsigned x, unsigned y) const {
173 return baseInvPos+Position(INV_GET_POS(x), INV_GET_POS(y));
174}
175
177 return all_of(players.begin(), players.end(), [](Player& p) -> bool {return p.isEliminated();});
178}
void managedGames()
manages and changes the states of the game
Definition: gameBasics.cpp:64
WinValue enterGameLoop()
enter the main gameplay game loop
Definition: gameBasics.cpp:116
Game()
constructor for the game class
Definition: gameBasics.cpp:21
bool arePlayersDead()
tells if all players are dead
Definition: gameBasics.cpp:176
GodState state
god's current state
Definition: god.h:53
unsigned validColsNumber() const
vector< ScoreLink > scores
list of pairs of player names and their score
Definition: scoresManager.h:57
void writeFile() const
write the score list into the score file
void inputScore(string name, unsigned score)
add player name and their score in the list of scores
full game logic and display management
#define WININIT
Definition: gameBasics.cpp:19
game mode options
unsigned startXPosition
players horizontal start position
Definition: configData.h:47
unsigned playersLives
player life points
Definition: configData.h:67
InvadersGrid grid
Invader type matrix.
Definition: configData.h:42
unsigned playersWidth
player horizontal size in pixel
Definition: configData.h:57
unsigned maxFPS
maximum framerate at which the game will run
Definition: configData.h:37
player data structure
Definition: player.h:19
#define INV_GET_POS(i)
Definition: utils.h:24
#define DEBUG_MSG(X)
Definition: utils.h:34
nsGraphics::Vec2D Position
Definition: utils.h:53
WinValue
list of win values
Definition: utils.h:45