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
41 pm->drawText(Position(pm->getScreenWidth()-200, 20), "FPS : "+to_string(fps), nsGraphics::KWhite, Font::BITMAP_8_BY_13);
42 )
43
44 unsigned margin = 0;
45 for(unsigned i=0;i<players.size();++i){
46 pm->drawText(Position(0,10+margin),"player "+to_string(i+1)+" :",nsGraphics::KWhite,Font::BITMAP_8_BY_13);
47 pm->drawText(Position(100,10+margin),to_string(players[i].score) ,nsGraphics::KWhite,Font::BITMAP_8_BY_13);
48 margin +=15;
49
50 if(!players[i].isEliminated()){
51 if(players[i].deathAnimCounter%2==0){
52 pm->drawPlayer(i, players[i].x, confData.playersWidth, confData.playerDefs[i].color);
53 }
54 }
55 // out of the condition, because we still need to display the falling heart
56 displayHearts(i);
57 }
58}
59
60void Game::displayHearts(playerID pID) const {
61
62 // As said before, the player loop is an illusion, 2 players max
63 unsigned x;
64 if(pID==PLAYER1)x = 0;
65 else x = pm->getScreenWidth()-HEART_LENGTH;
66
67 unsigned y = GOD_BENCH_SIZE+5;
68 for(unsigned i=0;i<players[pID].lives;++i){
69 pm->drawHeart(Position(x, y));
70 y+=HEART_LENGTH+5;
71 }
72 if(players[pID].hasDeathAnimation()){
73 pm->drawHeart(Position(x, y+players[pID].deathAnimCounter*5));
74 }
75}
76
77void Game::displayInvader(const Position& pos, InvaderType type) const {
78 if(type==InvaderType::NONE)return;
79 const InvaderTypeDef& invDef = confData.invadersDef.at(type);
80 switch(type){
82 pm->drawInvaderA(pos, confData.invadersSize, invDef.color);
83 return;
84 }
86 pm->drawInvaderB(pos, confData.invadersSize, invDef.color);
87 return;
88 }
90 pm->drawInvaderC(pos, confData.invadersSize, invDef.color);
91 return;
92 }
93 }
94}
95
96void applyBezierCurbe(Position& pos, const Position& point, const double percent) {
97 pos += (point-pos)*percent;
98}
99
100void Game::displayGod() const {
101 switch (god.state) {
102 case GodState::NONE:
103 return;
104 case GodState::AWAKE: {
105 pm->drawGodBench(god.counter - GOD_BENCH_SIZE);
106
108 Position rightHand(pm->getScreenWidth()-GOD_HAND_DISTANCE-GOD_HAND_SIZE, god.counter-GOD_BENCH_SIZE);
109 pm->drawSprite(pm->leftHand, leftHand);
110 pm->drawSprite(pm->rightHand, rightHand);
111 pm->drawGodFace(god.counter - GOD_BENCH_SIZE);
112 break;
113 }
114 case GodState::WAIT:{
115 pm->drawGodBench(0);
116 Position leftHand(GOD_HAND_DISTANCE, 0);
117 Position rightHand(god.getRightHandPos(pm->getScreenWidth()));
118 pm->drawSprite(pm->leftHand, leftHand);
119 pm->drawSprite(pm->rightHand, rightHand);
120 pm->drawGodFace(0);
121 break;
122 }
125 // Bezier curve
126 // counter goes [0-100]
127 pm->drawGodBench(0);
128 pm->drawSprite(pm->leftHand, Position(GOD_HAND_DISTANCE, 0));
129 pm->drawGodFace(0);
130
131 Position pos(god.getRightHandPos(pm->getScreenWidth()));
132 Position endPos = invIndexToPos(god.thrownInvPosX, god.thrownInvPosY);
133
134 applyBezierCurbe(pos, god.thrownTransition, god.counter / 100.0);
135 applyBezierCurbe(pos, endPos, god.counter / 100.0);
136
137 // pos is now the position we need to draw our hand to
138 pm->drawSprite(pm->rightHand, pos);
140
142 pos-=Position(confData.invadersSize/2, confData.invadersSize/2);
143 displayInvader(pos, god.thrownInvType);
144 }
145 break;
146 }
147 case GodState::THROW:{
148 pm->drawGodBench(0);
149 pm->drawSprite(pm->leftHand, Position(GOD_HAND_DISTANCE, 0));
150 pm->drawGodFace(0);
151
152 // compute start position (not sure if we should store it or compute it each time ?)
153 Position handPos = god.getRightHandPos(pm->getScreenWidth());
154
155 Position invaderPos = handPos;
156 applyTransformation(invaderPos, GOD_HAND_SIZE, confData.invadersSize);
157 Position a = god.thrownVector * (god.counter / 100.0);
158 invaderPos = invaderPos + a;
159
160 displayInvader(invaderPos, god.thrownInvType);
161 if(god.counter<30){
162 // handling hand retraction
163 unsigned handCounter;
164 if(god.counter<15)handCounter = god.counter;
165 else handCounter = 30-god.counter;
166 handPos = handPos + god.thrownVector * (handCounter / 100.0);
167 }
168 pm->drawSprite(pm->rightHand, handPos);
169
170 break;
171 }
172 }
173}
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
player's projectiles
Definition: projectiles.h:22
void applyBezierCurbe(Position &pos, const Position &point, const double percent)
Definition: display.cpp:96
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:147
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
players 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:52
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 DEBUG_INSTR(X)
Definition: utils.h:33
#define PLAYER1
Definition: utils.h:54