Mercurial > hg > beaglert
comparison projects/tank_wars/game.cpp @ 22:fbfeb5895efd matrix_gpio
Updated tank wars demo for new API
author | andrewm |
---|---|
date | Sun, 03 May 2015 01:10:17 +0100 |
parents | 49f22e1246b2 |
children | 59edd5780fef |
comparison
equal
deleted
inserted
replaced
20:58eb99dac921 | 22:fbfeb5895efd |
---|---|
31 // Location of the projectile | 31 // Location of the projectile |
32 bool projectileInMotion = false; | 32 bool projectileInMotion = false; |
33 float projectilePositionX, projectilePositionY; | 33 float projectilePositionX, projectilePositionY; |
34 float projectileVelocityX, projectileVelocityY; | 34 float projectileVelocityX, projectileVelocityY; |
35 | 35 |
36 // Infor needed for sound rendering | |
37 bool collisionJustOccurred = false; | |
38 | |
36 // Useful utility function for generating random floating-point values | 39 // Useful utility function for generating random floating-point values |
37 float randomFloat(float low, float hi) | 40 float randomFloat(float low, float hi) |
38 { | 41 { |
39 float r = (float)random() / (float)RAND_MAX; | 42 float r = (float)random() / (float)RAND_MAX; |
40 return map(r, 0, 1, low, hi); | 43 return map(r, 0, 1, low, hi); |
99 if((tank1X - projectilePositionX)*(tank1X - projectilePositionX) + | 102 if((tank1X - projectilePositionX)*(tank1X - projectilePositionX) + |
100 (tank1Y - projectilePositionY)*(tank1Y - projectilePositionY) | 103 (tank1Y - projectilePositionY)*(tank1Y - projectilePositionY) |
101 <= tankRadius * tankRadius) | 104 <= tankRadius * tankRadius) |
102 { | 105 { |
103 projectileInMotion = false; | 106 projectileInMotion = false; |
107 collisionJustOccurred = true; | |
104 playerHasWon = 2; | 108 playerHasWon = 2; |
105 } | 109 } |
106 else if((tank2X - projectilePositionX)*(tank2X - projectilePositionX) + | 110 else if((tank2X - projectilePositionX)*(tank2X - projectilePositionX) + |
107 (tank2Y - projectilePositionY)*(tank2Y - projectilePositionY) | 111 (tank2Y - projectilePositionY)*(tank2Y - projectilePositionY) |
108 <= tankRadius * tankRadius) | 112 <= tankRadius * tankRadius) |
109 { | 113 { |
110 projectileInMotion = false; | 114 projectileInMotion = false; |
115 collisionJustOccurred = true; | |
111 playerHasWon = 1; | 116 playerHasWon = 1; |
112 } | 117 } |
113 else if(projectilePositionX < 0 || projectilePositionX >= screenWidth) { | 118 else if(projectilePositionX < 0 || projectilePositionX >= screenWidth) { |
114 // Check collision whether projectile has exited the screen to the left or right | 119 // Check collision whether projectile has exited the screen to the left or right |
115 projectileInMotion = false; | 120 projectileInMotion = false; |
121 collisionJustOccurred = true; | |
116 nextPlayersTurn(); | 122 nextPlayersTurn(); |
117 } | 123 } |
118 else if(projectilePositionY >= groundLevel[(int)floorf(projectilePositionX)]) { | 124 else if(projectilePositionY >= groundLevel[(int)floorf(projectilePositionX)]) { |
119 // Check for projectile collision with ground | 125 // Check for projectile collision with ground |
120 projectileInMotion = false; | 126 projectileInMotion = false; |
127 collisionJustOccurred = true; | |
121 nextPlayersTurn(); | 128 nextPlayersTurn(); |
122 } | 129 } |
123 } | 130 } |
124 | 131 |
125 // Updates for game state | 132 // Updates for game state |
181 } | 188 } |
182 | 189 |
183 int gameStatusWinner() | 190 int gameStatusWinner() |
184 { | 191 { |
185 return playerHasWon; | 192 return playerHasWon; |
193 } | |
194 | |
195 bool gameStatusCollisionOccurred() | |
196 { | |
197 if(collisionJustOccurred) { | |
198 collisionJustOccurred = false; | |
199 return true; | |
200 } | |
201 return false; | |
202 } | |
203 | |
204 float gameStatusProjectileHeight() | |
205 { | |
206 return projectilePositionY / (float)screenHeight; | |
186 } | 207 } |
187 | 208 |
188 // Clean up any allocated memory for the game | 209 // Clean up any allocated memory for the game |
189 void cleanupGame() | 210 void cleanupGame() |
190 { | 211 { |