SUPER Space invader : Turbo edition DX - VS GOD 1.0.0
A simple space invader ripoff
godManager.cpp
Go to the documentation of this file.
1
11#include "game.h"
12
13void Game::tryAwakeGod() {
14 if (baseInvPos.getY() > 100 /*lambda value*/ && god.state==GodState::NONE) {
15 god.counter = 0;
17 }
18}
19
24/* This is a really long function, but I feel like it's still readable because of the switch, and...
25 * Honestly I think splitting it into multiple small functions would be ugly*/
26bool Game::manageGod() {
27 switch (god.state) {
28 case GodState::NONE: {
29 return false;
30 }
31 case GodState::AWAKE: {
32 if (god.counter == GOD_BENCH_SIZE) {
33 god.counter = 0;
35 } else ++god.counter;
36 return false;
37 }
38 case GodState::WAIT: {
39 if (god.counter < 100) {
40 ++god.counter;
41 return false;
42 }
43
44 // init throw
45 god.counter = 0;
47 god.thrownInvPosX = grid.randomValidCol();
48 god.thrownInvPosY = grid[god.thrownInvPosX].randomValidInv();
50
51 god.thrownTransition.setX(pm->getScreenWidth() - GOD_HAND_DISTANCE - GOD_HAND_SIZE);
52 god.thrownTransition.setY(baseInvPos.getY() + INV_GET_POS(god.thrownInvPosY));
53 return false;
54 }
56 if (god.counter < 100) {
57 god.counter += 2;
58 return false;
59 }
60
61 if (grid[god.thrownInvPosX].size() > god.thrownInvPosY &&
63 god.thrownInvType = grid[god.thrownInvPosX][god.thrownInvPosY];
65 }
67 return false;
68 }
70 if (god.counter > 0) {
71 god.counter -= 2;
72 return false;
73 }
76 return false;
77 }
79
80 // compute the launch vector
81
82 Position invaderMiddlePos(pm->getScreenWidth() - GOD_HAND_DISTANCE - GOD_HAND_SIZE / 2,
83 GOD_HAND_SIZE / 2);
84
85
86 playerID target;
87 if (players.size() == 1)target = PLAYER1; // don't want to use random if not needed
88 else target = rand() % players.size();
89 /* Let's just pretend god is drunk and can fire at a player that have a death animation, because
90 * honestly at this point I want to re-code the whole game engine to allow a better handling of cases like this...*/
91
92 Position playerMiddlePos(players[target].x + confData.playersWidth / 2,
93 pm->getScreenHeight() - PLAYER_HEIGHT / 2);
94
95 god.thrownVector = playerMiddlePos - invaderMiddlePos;
96 god.thrownVector = god.thrownVector / (god.thrownVector.computeMagnitude() / 1000.0);
97 // let's normalize it, but keep it's length big so x and y and non-zero
98 // We will divide it in displayAll
99 return false;
100 }
101 case GodState::THROW: {
102 ++god.counter;
103
104 Position invaderPos = god.getRightHandPos(pm->getScreenWidth());
105 applyTransformation(invaderPos, GOD_HAND_SIZE, confData.invadersSize);
106 Position a = god.thrownVector * (god.counter / 100.0);
107 invaderPos = invaderPos + a;
108
109 bool touched = false;
110
111 // check if OOB (Out Of Bounds)
112 if (invaderPos.getY() + confData.invadersSize >= pm->getScreenWidth() ||
113 (invaderPos.getX() < 0 || invaderPos.getX() + confData.invadersSize >= pm->getScreenWidth())) {
114 touched = true;
115
116 /* there are no invaders in the grid anymore, and the one thrown just went out of bound
117 * So... return true, the player wins*/
118 if(!areThereInvadersLeft())return true;
119
120 // check player collision
121 } else if (invaderPos.getY() + confData.invadersSize >= pm->getScreenHeight() - PLAYER_HEIGHT) {
122 for (Player &p: players) {
123 if(p.isPlaying()){
125 p.x, p.x + confData.playersWidth,
126 invaderPos.getX(), invaderPos.getX() + confData.invadersSize
127 )) {
128 p.damage();
129 touched = true;
130 // do not break, the other player also deserves to be hit
131 }
132 }
133 }
134 }
135 if (touched) {
136 god.state = GodState::WAIT;
137 god.counter = 0;
138 }
139 /* we do not need to reset other members, they'll be treated as non-initialized
140 * When we cycle back between states*/
141
142
143 return false;
144 }
145
146 }
147 throw runtime_error("SHOULD NOT HAPPEN : invalid action for god : ID="+ to_string(static_cast<int>(god.state)));
148}
149
150Position God::getRightHandPos(unsigned screenWidth) const {
151 return {screenWidth - GOD_HAND_DISTANCE - GOD_HAND_SIZE, 0};
152}
unsigned counter
manage all sorts of things, gods secrets remains unknown
Definition: god.h:58
unsigned thrownInvPosY
y pixel coordinate of the invader thrown by the hand of god
Definition: god.h:71
Position thrownTransition
position of a point for bezier's curve
Definition: god.h:86
Position getRightHandPos(unsigned screenWidth) const
give initial the pixel coordinates of god's right hand
Definition: godManager.cpp:150
Position thrownVector
direction of the thrown invader movement
Definition: god.h:81
GodState state
god's current state
Definition: god.h:53
InvaderType thrownInvType
type of the invader thrown by the hand of god
Definition: god.h:76
unsigned thrownInvPosX
x pixel coordinate of the invader thrown by the hand of god
Definition: god.h:66
unsigned randomValidCol() const
full game logic and display management
#define GOD_HAND_DISTANCE
Definition: god.h:36
#define GOD_BENCH_SIZE
Definition: god.h:34
#define GOD_HAND_SIZE
Definition: god.h:35
unsigned playersWidth
player horizontal size in pixel
Definition: configData.h:57
unsigned invadersSize
invader radius size in pixel
Definition: configData.h:82
player data structure
Definition: player.h:19
unsigned playerID
Definition: utils.h:54
#define PLAYER_HEIGHT
Definition: utils.h:18
#define INV_GET_POS(i)
Definition: utils.h:24
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
void applyTransformation(Position &pos, unsigned sizeFrom, unsigned sizeTo)
change the size of a Position object
Definition: utils.cpp:8
nsGraphics::Vec2D Position
Definition: utils.h:53
#define PLAYER1
Definition: utils.h:55