SUPER Space invader : Turbo edition DX - VS GOD 1.0.0
A simple space invader ripoff
display.cpp
Go to the documentation of this file.
1
12#include "game.h"
13
14
18void Game::displayAll(unsigned fps) const {
19 pm->drawSprite(pm->gameBackground, Position(0, 0));
20 for (unsigned i = 0; i < this->grid.size(); ++i){
21 for (unsigned j = 0; j < this->grid[i].size(); ++j){
22 Position vec(
23 baseInvPos.getX() + i * confData.invadersSize + i * confData.invadersDistance,
24 baseInvPos.getY() + j * confData.invadersSize + j * confData.invadersDistance
25 );
26 displayInvader(vec, grid[i][j]);
27 }
28 }
29
30 for(const missile& miss : missiles){
31 pm->drawMissile(miss, confData.missilesWidth, confData.missilesColor);
32 }
33 for(const Torpedo& tor : torpedos){
34 pm->drawTorpedo(tor, confData.torpedosWidth, confData.torpedosColor);
35 }
36
37
38 displayGod();
39 displayScore();
40
42 pm->drawText(Position(pm->getScreenWidth()-200, 20), "FPS : "+to_string(fps), nsGraphics::KWhite, Font::BITMAP_8_BY_13);
43 )
44
45 for(unsigned i=0;i<players.size();++i){
46 if(!players[i].isEliminated()){
47 if(players[i].deathAnimCounter%2==0){
48 pm->drawPlayer(players[i].x, confData.playersWidth, confData.playerDefs[i].color);
49 }
50 }
51 // out of the condition, because we still need to display the falling heart
52 displayHearts(i);
53 }
54}
55
56void Game::displayHearts(playerID pID) const {
57
58 // As said before, the player loop is an illusion, 2 players max
59 unsigned x;
60 if(pID==PLAYER1)x = 0;
61 else x = pm->getScreenWidth()-HEART_LENGTH;
62
63 unsigned y = GOD_BENCH_SIZE+5;
64 for(unsigned i=0;i<players[pID].lives;++i){
65 pm->drawHeart(Position(x, y));
66 y+=HEART_LENGTH+5;
67 }
68 if(players[pID].hasDeathAnimation()){
69 pm->drawHeart(Position(x, y+players[pID].deathAnimCounter*5));
70 }
71}
72
73void Game::displayScore() const{
74 unsigned margin = 0;
75 unsigned playerNumber = 1;
76 for (auto& player: players){
77 pm->drawText(Position(0,10+margin),"player "+to_string(playerNumber)+" :",nsGraphics::KWhite,Font::BITMAP_8_BY_13);
78 pm->drawText(Position(100,10+margin),to_string(player.score) ,nsGraphics::KWhite,Font::BITMAP_8_BY_13);
79 ++playerNumber;
80 margin +=15;
81 }
82}
83
84void Game::displayInvader(const Position& pos, InvaderType type) const {
85 if(type==InvaderType::NONE)return;
86 const InvaderTypeDef& invDef = confData.invadersDef.at(type);
87 switch(type){
89 pm->drawInvaderA(pos, confData.invadersSize, invDef.color);
90 return;
91 }
93 pm->drawInvaderB(pos, confData.invadersSize, invDef.color);
94 return;
95 }
97 pm->drawInvaderC(pos, confData.invadersSize, invDef.color);
98 return;
99 }
100 }
101}
102
103void applyBezier(Position& pos, const Position& point, const double percent) {
104 pos += (point-pos)*percent;
105}
106
107void Game::displayGod() const {
108 switch (god.state) {
109 case GodState::NONE:
110 return;
111 case GodState::AWAKE: {
112 pm->drawGodBench(god.counter - GOD_BENCH_SIZE);
113
115 Position rightHand(pm->getScreenWidth()-GOD_HAND_DISTANCE-GOD_HAND_SIZE, god.counter-GOD_BENCH_SIZE);
116 pm->drawSprite(pm->leftHand, leftHand);
117 pm->drawSprite(pm->rightHand, rightHand);
118 pm->drawGodFace(god.counter - GOD_BENCH_SIZE);
119 break;
120 }
121 case GodState::WAIT:{
122 pm->drawGodBench(0);
123 Position leftHand(GOD_HAND_DISTANCE, 0);
124 Position rightHand(god.getRightHandPos(pm->getScreenWidth()));
125 pm->drawSprite(pm->leftHand, leftHand);
126 pm->drawSprite(pm->rightHand, rightHand);
127 pm->drawGodFace(0);
128 break;
129 }
132 // Bezier curve
133 // counter goes [0-100]
134 pm->drawGodBench(0);
135 pm->drawSprite(pm->leftHand, Position(GOD_HAND_DISTANCE, 0));
136 pm->drawGodFace(0);
137
138 Position pos(god.getRightHandPos(pm->getScreenWidth()));
139 Position endPos = invIndexToPos(god.thrownInvPosX, god.thrownInvPosY);
140
141 applyBezier(pos, god.thrownTransition, god.counter / 100.0);
142 applyBezier(pos, endPos, god.counter / 100.0);
143
144 // pos is now the position we need to draw our hand to
145 pm->drawSprite(pm->rightHand, pos);
147
149 pos-=Position(confData.invadersSize/2, confData.invadersSize/2);
150 displayInvader(pos, god.thrownInvType);
151 }
152 break;
153 }
154 case GodState::THROW:{
155 pm->drawGodBench(0);
156 pm->drawSprite(pm->leftHand, Position(GOD_HAND_DISTANCE, 0));
157 pm->drawGodFace(0);
158
159 // compute start position (not sure if we should store it or compute it each time ?)
160 Position handPos = god.getRightHandPos(pm->getScreenWidth());
161
162 Position invaderPos = handPos;
163 applyTransformation(invaderPos, GOD_HAND_SIZE, confData.invadersSize);
164 Position a = god.thrownVector * (god.counter / 100.0);
165 invaderPos = invaderPos + a;
166
167 displayInvader(invaderPos, god.thrownInvType);
168 if(god.counter<30){
169 // handling hand retraction
170 unsigned handCounter;
171 if(god.counter<15)handCounter = god.counter;
172 else handCounter = 30-god.counter;
173 handPos = handPos + god.thrownVector * (handCounter / 100.0);
174 }
175 pm->drawSprite(pm->rightHand, handPos);
176
177 break;
178 }
179 }
180}
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
player's projectiles
Definition: projectiles.h:22
void applyBezier(Position &pos, const Position &point, const double percent)
Definition: display.cpp:103
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
InvaderType
List of all invader type.
Definition: invadersGrid.h:22
#define HEART_LENGTH
Definition: pixelManager.h:150
Position missile
Definition: projectiles.h:16
unsigned missilesWidth
invaders missiles width in pixel
Definition: configData.h:102
nsGraphics::RGBAcolor torpedosColor
players torpedos color
Definition: configData.h:137
vector< PlayerDef > playerDefs
player key configuration
Definition: configData.h:72
unsigned invadersDistance
distance in pixel between two invader
Definition: configData.h:87
nsGraphics::RGBAcolor missilesColor
invaders missiles color
Definition: configData.h:117
unsigned playersWidth
player horizontal size in pixel
Definition: configData.h:57
unsigned invadersSize
invader radius size in pixel
Definition: configData.h:82
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
defines an invader type
Definition: invaderDef.h:21
nsGraphics::RGBAcolor color
color of the invader type
Definition: invaderDef.h:25
unsigned playerID
Definition: utils.h:54
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 DEBUG_INSTR(X)
Definition: utils.h:35
#define PLAYER1
Definition: utils.h:55