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 uglier
26 * Each state is responsible for managing the counter*/
27bool Game::manageGod() {
28 switch (god.state) {
29 case GodState::NONE: {
30 return false;
31 }
32 case GodState::AWAKE: {
33 if (god.counter == GOD_BENCH_SIZE) {
34 god.counter = 0;
36 } else ++god.counter;
37 return false;
38 }
39 case GodState::WAIT: {
40 if (god.counter < 100) {
41 ++god.counter;
42 return false;
43 }
44
45 // init throw
46 god.counter = 0;
48 god.thrownInvPosX = grid.randomValidCol();
49 god.thrownInvPosY = grid[god.thrownInvPosX].randomValidInv();
51
52 god.thrownTransition.setX(pm->getScreenWidth() - GOD_HAND_DISTANCE - GOD_HAND_SIZE);
53 god.thrownTransition.setY(baseInvPos.getY() + INV_GET_SINGLE_POS(god.thrownInvPosY));
54 return false;
55 }
57 if (god.counter < 100) {
58 god.counter += 2;
59 return false;
60 }
61
62 if (grid[god.thrownInvPosX].size() > god.thrownInvPosY &&
64 god.thrownInvType = grid[god.thrownInvPosX][god.thrownInvPosY];
66 }
68 return false;
69 }
71 if (god.counter > 0) {
72 god.counter -= 2;
73 return false;
74 }
77 return false;
78 }
80
81 // compute the launch vector
82
83 Position invaderMiddlePos(pm->getScreenWidth() - GOD_HAND_DISTANCE - GOD_HAND_SIZE / 2,
84 GOD_HAND_SIZE / 2);
85
86
87 playerID target;
88 if (players.size() == 1)target = PLAYER1; // don't want to use random if not needed
89 else target = rand() % players.size();
90 /* Let's just pretend god is drunk and can fire at a player that have a death animation, because
91 * honestly at this point I want to re-code the whole game engine to allow a better handling of cases like this...*/
92
93 Position playerMiddlePos(players[target].x + confData.playersWidth / 2,
94 pm->getScreenHeight() - PLAYER_HEIGHT / 2);
95
96 god.thrownVector = playerMiddlePos - invaderMiddlePos;
97 god.thrownVector = god.thrownVector / (god.thrownVector.computeMagnitude() / 1000.0);
98 // let's normalize it, but keep it's length big so x and y and non-zero
99 // We will divide it in displayAll
100 return false;
101 }
102 case GodState::THROW: {
103 ++god.counter;
104
105 Position invaderPos = god.getRightHandPos(pm->getScreenWidth());
106 applyTransformation(invaderPos, GOD_HAND_SIZE, confData.invadersSize);
107 Position a = god.thrownVector * (god.counter / 100.0);
108 invaderPos = invaderPos + a;
109
110 bool touched = false;
111
112 // check if OOB (Out Of Bounds)
113 if (invaderPos.getY() + confData.invadersSize >= pm->getScreenWidth() ||
114 (invaderPos.getX() < 0 || invaderPos.getX() + confData.invadersSize >= pm->getScreenWidth())) {
115 touched = true;
116
117 /* there are no invaders in the grid anymore, and the one thrown just went out of bound
118 * So... return true, the player wins*/
119 if(!areThereInvadersLeft())return true;
120
121 // check player collision
122 } else if (invaderPos.getY() + confData.invadersSize >= pm->getScreenHeight() - PLAYER_HEIGHT) {
123 for (Player &p: players) {
124 if(p.isPlaying()){
126 p.x, p.x + confData.playersWidth,
127 invaderPos.getX(), invaderPos.getX() + confData.invadersSize
128 )) {
129 p.damage();
130 touched = true;
131 // do not break, the other player also deserves to be hit
132 }
133 }
134 }
135 }
136 if (touched) {
137 god.state = GodState::WAIT;
138 god.counter = 0;
139 }
140 /* we do not need to reset other members, they'll be treated as non-initialized
141 * When we cycle back between states*/
142
143
144 return false;
145 }
146
147 }
148 throw runtime_error("SHOULD NOT HAPPEN : invalid action for god : ID="+ to_string(static_cast<int>(god.state)));
149}
150
151Position God::getRightHandPos(unsigned screenWidth) const {
152 return {screenWidth - GOD_HAND_DISTANCE - GOD_HAND_SIZE, 0};
153}
unsigned counter
timer used differently in all states
Definition: god.h:58
unsigned thrownInvPosY
y index (invader in the column) of the invader thrown by the hand of god
Definition: god.h:70
Position thrownTransition
position of a point for bezier's curve (used in RETRIEVE1 and RETRIEVE2 states, for the hand animatio...
Definition: god.h:85
Position getRightHandPos(unsigned screenWidth) const
give initial the pixel coordinates of god's right hand
Definition: godManager.cpp:151
Position thrownVector
direction of the thrown invader movement
Definition: god.h:80
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:75
unsigned thrownInvPosX
x index (column in the grid) of the invader thrown by the hand of god
Definition: god.h:65
unsigned randomValidCol() const
Returns a random valid column in the grid.
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
#define INV_GET_SINGLE_POS(i)
Definition: utils.h:22
unsigned playerID
Definition: utils.h:52
#define PLAYER_HEIGHT
Definition: utils.h:19
bool areLinesColliding(unsigned start1, unsigned end1, unsigned start2, unsigned end2)
tells if 2 lines are colliding in a 1 dimensional space. Didn't want to use Position because of the s...
Definition: utils.cpp:3
void applyTransformation(Position &pos, unsigned sizeFrom, unsigned sizeTo)
change the position object to reflect the top-right position of a "sizeTo"-sized entity,...
Definition: utils.cpp:8
nsGraphics::Vec2D Position
Definition: utils.h:51
#define PLAYER1
Definition: utils.h:54