SUPER Space invader : Turbo edition DX - VS GOD 1.0.0
A simple space invader ripoff
gameManagers.cpp
Go to the documentation of this file.
1
11#include "game.h"
12
13#define ISPRESSED(ID, X) window.isPressed({confData.playerDefs[ID].keys.X, false})
14void Game::manageOnePlayer(playerID pID){
15 Player& p = players[pID];
16 if(p.isEliminated())return;
17
18 if (ISPRESSED(pID, left)){
19 if(p.x < confData.playersSpeed) p.x = 0;
20 else p.x -= confData.playersSpeed;
21 }
22 if (ISPRESSED(pID, right)){
23 if(p.x + confData.playersWidth + confData.playersSpeed >= pm->getScreenWidth()) p.x = pm->getScreenWidth() - confData.playersWidth - 1;
24 else p.x += confData.playersSpeed;
25 }
26
27
28 if(p.hasDeathAnimation()) {
30 if (p.deathAnimCounter == 75) {
31 p.deathAnimCounter = 0;
32 }
33 }else{
34 if(p.isEliminated())return;
35
36 if(p.fireCooldown==0) {
37 if (ISPRESSED(pID, shoot)) {
38 torpedos.emplace_back(p.x + confData.playersWidth / 2, pm->getScreenHeight() - PLAYER_HEIGHT, pID);
40 }
41 }else --p.fireCooldown;
42 }
43}
44
45void Game::managePlayers(){
46 for(unsigned i=0;i<players.size();++i)manageOnePlayer(i);
47}
48
49bool Game::manageInvaders(){
50 if(!areThereInvadersLeft())return false; // If there are no more invaders we don't need to manage them
51
52 // shoot
53 if(fireCooldown==0) {
54 fireCooldown = confData.invadersFireCooldown + rand() % 25;
55
56 unsigned rdCol = grid.randomValidCol();
57 // fire !
58 missiles.push_back(basePos + Position(
59 confData.invadersSize * rdCol + confData.invadersDistance * (rdCol + 0.5),
60 confData.invadersSize * (grid[rdCol].size() - 1) + confData.invadersDistance * (grid[rdCol].size() - 1)
61 ));
62 }else --fireCooldown;
63
64 // moving
65 // TODO fix this lol
66
67 if(direction){ // go to the right
68 int end = basePos.getX(); // start Position
69 end+= grid.size() * confData.invadersSize; // add the invaders
70 end+= (grid.size()-1) * confData.invadersDistance; // add the invadersDistance between invaders
71
72 // you got the end position of the invader crowd !
73 unsigned relativeScreenWidth = pm->getScreenWidth();
74 size_t i = grid.size()-1;
75 while (grid[i].hasNoValid()){
76 relativeScreenWidth += confData.invadersSize + confData.invadersDistance;
77 --i;
78 }
79
80 if(end + confData.invadersSpeed < relativeScreenWidth){
81 basePos.setX(basePos.getX() + confData.invadersSpeed);
82 }
83 else{
84 basePos.setY(basePos.getY() + confData.invadersSize + confData.invadersDistance);
85 direction = !direction;
86 return true;
87 }
88 }
89 else{
90 size_t i = 0;
91 unsigned relativeBasePos = basePos.getX();
92 while (grid[i].hasNoValid()){
93 relativeBasePos += confData.invadersSize + confData.invadersDistance;
94 ++i;
95 }
96
97 if(relativeBasePos >= confData.invadersSpeed){
98 basePos.setX(basePos.getX() - confData.invadersSpeed);
99 }else{
100 basePos.setY(basePos.getY() + confData.invadersSize + confData.invadersDistance);
101 direction = !direction;
102 return true;
103 }
104 }
105 return false;
106
107}
108
109void Game::remCollidingProjectiles(){
110
111 auto miss = missiles.begin();
112 auto tor = torpedos.begin();
113
114 while(miss != missiles.end()){
115 bool wasColliding = false;
116 while(tor != torpedos.end()){
117
118 // missiles can't be right under torpedos, so that must means they are colliding in Y
119 if(miss->getY() + confData.missilesLength < tor->getY()){
120
121 }
122 if(areLinesColliding( // now check if they collide in X
123 miss->getX(), miss->getX() + confData.missilesWidth,
124 tor->getX(), tor->getX() + confData.torpedosWidth)){
125 missiles.erase(miss);
126 torpedos.erase(tor);
127 wasColliding = true;
128 break;
129 }
130 ++tor;
131 }
132 /* if it was colling, it was removed and his Position is now replaced by the next.
133 * else, go to the next */
134 if(!wasColliding)++miss;
135 }
136}
137
138void Game::moveMissiles() {
139 auto miss = missiles.begin();
140 while (miss != missiles.end()) {
141 if (miss->getY() >= pm->getScreenHeight())missiles.erase(miss);
142 else {
143 miss->setY(miss->getY()+confData.missilesSpeed);
144 ++miss;
145 }
146 }
147}
148
149void Game::moveTorpedos() {
150 auto tor = torpedos.begin();
151 while (tor != torpedos.end()) {
152 if (tor->getY()+confData.torpedosLength <= 0)torpedos.erase(tor);
153 else{
154 tor->setY(tor->getY()-confData.torpedosSpeed);
155 ++tor;
156 }
157 }
158}
159
160void Game::checkMissilesAndPlayers() {
161 auto miss_ite = missiles.begin();
162 while(miss_ite!=missiles.end()){
163 bool wasColliding = false;
164 if(miss_ite->getY()>=pm->getScreenHeight()-PLAYER_HEIGHT){ // check collision on Y
165 // now check collision on X (with both players)
166 for(Player& p : players){
167 if(p.isPlaying()){
169 miss_ite->getX(), miss_ite->getX() + confData.missilesWidth,
170 p.x, p.x + confData.playersWidth)){
171 wasColliding = true;
172 p.damage();
173 // do not break, the second player also deserves to be hit
174 }
175 }
176 }
177 }
178 if(wasColliding)missiles.erase(miss_ite);
179 else ++miss_ite;
180 }
181}
182
183bool Game::checkTorpedosAndInvaders() {
184 auto tor_ite = torpedos.begin();
185 while(tor_ite!=torpedos.end()){
186 unsigned i=0;
187 for(;i<grid.size();++i){
188
189 unsigned alienIndex = grid[i].getOutterInvader();
190 if(alienIndex==grid[i].size())continue;
191 // calculate top-left Position of invader
192 Position pos = basePos + Position(
193 confData.invadersSize*i+confData.invadersDistance*i,
194 confData.invadersSize*alienIndex+confData.invadersDistance*alienIndex
195 );
196 // check collision on Y (invaders can actually be "under" torpedos, so we check both lower and upper bounds
197 if(pos.getY()+confData.invadersSize>=tor_ite->getY() &&
198 pos.getY()<=tor_ite->getY()+confData.torpedosLength){
199 // now check collision on X
200 if(areLinesColliding( // now check collision on X
201 tor_ite->getX(), tor_ite->getX() + confData.torpedosWidth,
202 pos.getX(), pos.getX() + confData.invadersSize)){
203
204
205 InvaderType invType = grid[i][alienIndex];
206 players[tor_ite->owner].score += confData.invadersDef.at(invType).points;
207 torpedos.erase(tor_ite);
208
209 grid[i][alienIndex] = InvaderType::NONE;
210
211 if(!areThereInvadersLeft()) return true;
212 break;
213 }
214 }
215 }
216 if(i==grid.size()) ++tor_ite;
217 }
218 return false;
219}
220
221bool Game::invadersTouchPlayer() const {
222 return any_of(grid.begin(), grid.end(), [this](const InvadersColumn& line) -> bool {
223 unsigned outter = line.getOutterInvader();
224 return this->basePos.getY()+confData.invadersSize*(outter+1)
225 +confData.invadersDistance*outter
226 >= pm->getScreenHeight() - PLAYER_HEIGHT;
227 });
228}
Column of invader.
Definition: invadersGrid.h:33
unsigned randomValidCol() const
full game logic and display management
#define ISPRESSED(ID, X)
InvaderType
List of all invader type.
Definition: invadersGrid.h:22
unsigned invadersFireCooldown
wait time between two invader missile
Definition: configData.h:92
unsigned playersSpeed
player movement speed
Definition: configData.h:52
unsigned missilesWidth
invaders missiles width in pixel
Definition: configData.h:102
unsigned torpedosLength
players torpedos length in pixel // auto defined from width
Definition: configData.h:127
unsigned playersFireCooldown
player shooting wait time
Definition: configData.h:62
unsigned torpedosSpeed
players topedos movement speed
Definition: configData.h:132
unsigned invadersDistance
distance in pixel between two invader
Definition: configData.h:87
unsigned invadersSpeed
invader movement speed
Definition: configData.h:77
unsigned playersWidth
player horizontal size in pixel
Definition: configData.h:57
unsigned invadersSize
invader radius size in pixel
Definition: configData.h:82
unsigned missilesSpeed
invaders missiles movement speed
Definition: configData.h:112
unsigned missilesLength
invaders missiles length in pixel - auto defined from width
Definition: configData.h:107
unsigned torpedosWidth
players torpedos width in pixel
Definition: configData.h:122
map< InvaderType, InvaderTypeDef > invadersDef
link between an invader type, and its data
Definition: configData.h:97
player data structure
Definition: player.h:19
unsigned deathAnimCounter
counter used for the death animation of players undefined once the player is eliminated
Definition: player.h:45
bool hasDeathAnimation() const
Definition: player.cpp:18
unsigned fireCooldown
player's shooting cooldown
Definition: player.h:50
bool isEliminated() const
Definition: player.cpp:22
unsigned x
x coordinate of the player
Definition: player.h:29
unsigned playerID
Definition: utils.h:54
#define PLAYER_HEIGHT
Definition: utils.h:18
bool areLinesColliding(unsigned start1, unsigned end1, unsigned start2, unsigned end2)
tells if 2 lines are colliding in a 1 dimentionnal space
Definition: utils.cpp:3
nsGraphics::Vec2D Position
Definition: utils.h:53