133 lines
3.5 KiB
C++
133 lines
3.5 KiB
C++
#include "game.h"
|
|
|
|
// TODO put in config (EVERYTHING SHALL GO IN CONFIG, THE WHOLE CODE SHALL GO IN CONFIIIIIIIIG)
|
|
#define MISSILE_SPEED 5
|
|
#define TORPEDO_SPEED MISSILE_SPEED
|
|
|
|
#define ISPRESSED(X) window.isPressed({X, false})
|
|
|
|
void Game::managePlayerMoves(PlayerDef& pdef, unsigned& playerX){
|
|
if (ISPRESSED(pdef.keys.left)){
|
|
if(playerX < conf.playersSpeed) playerX = 0;
|
|
else playerX = playerX-conf.playersSpeed;
|
|
}
|
|
if (ISPRESSED(pdef.keys.right)){
|
|
if(playerX+conf.playersSpeed >= pm.getScreenWidth()) playerX = pm.getScreenWidth() - 1;
|
|
else playerX = playerX+conf.playersSpeed;
|
|
}
|
|
if(ISPRESSED(pdef.keys.shoot)){
|
|
torpedos.emplace_back(playerX+ conf.playersWidth / 2, pm.getScreenHeight() - PLAYER_HEIGHT);
|
|
}
|
|
}
|
|
|
|
/** Makes the players play once
|
|
*/
|
|
void Game::managePlayers(){
|
|
managePlayerMoves(conf.p1Def, p1.x);
|
|
if(playMode==PlayMode::TWO_LOCAL)managePlayerMoves(conf.p2Def, p2.x);
|
|
}
|
|
|
|
/** Makes the invaders play once, and check lower bounds
|
|
*
|
|
* @return true if the invaders went down from one line (and we should check lower boundary), else false
|
|
*/
|
|
bool Game::manageInvaders(){
|
|
if(direction){ // go to the right
|
|
int end = basePos.getX(); // start position
|
|
end+= grid.size()*conf.invadersSize; // add the aliens
|
|
end+= (grid.size()-1)*conf.invadersDistance; // add the invadersDistance between aliens
|
|
|
|
// you got the end position of the alien crowd !
|
|
|
|
if(end+conf.invadersSpeed < pm.getScreenWidth()){
|
|
basePos.setX(basePos.getX()+conf.invadersSpeed);
|
|
}else{
|
|
basePos.setY(basePos.getY()+conf.invadersSize);
|
|
direction = !direction;
|
|
return true;
|
|
}
|
|
}else{
|
|
if(basePos.getX()>=conf.invadersSpeed){
|
|
basePos.setX(basePos.getX()-conf.invadersSpeed);
|
|
}else{
|
|
basePos.setY(basePos.getY()+conf.invadersSize);
|
|
direction = !direction;
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
|
|
}
|
|
|
|
/** makes projectiles move, and manage collisions between them and everything
|
|
*
|
|
* @return 1 if the invaders are all dead, 2 if the player is dead, else 0
|
|
*/
|
|
void Game::remCollidingProjectiles(){
|
|
|
|
auto miss = missiles.begin();
|
|
auto tor = torpedos.begin();
|
|
|
|
while(miss != missiles.end()){
|
|
bool wasColling = false;
|
|
while(tor != torpedos.end()){
|
|
|
|
// missiles can't be right under torpedos, so that must means they are colliding in Y
|
|
if(miss->getY()+conf.missilesLength < tor->getY()){
|
|
|
|
}
|
|
if(lineCollideCheck( // now check if they collide in X
|
|
miss->getX(), miss->getX()+conf.missilesWidth,
|
|
tor->getX(), tor->getX()+conf.torpedosWidth)){
|
|
missiles.erase(miss);
|
|
torpedos.erase(tor);
|
|
wasColling = true;
|
|
break;
|
|
}
|
|
++tor;
|
|
}
|
|
/* if it was colling, it was removed and his position is now replaced by the next.
|
|
* else, go to the next
|
|
*/
|
|
if(!wasColling)++miss;
|
|
}
|
|
}
|
|
|
|
void Game::moveMissiles() {
|
|
auto miss = missiles.begin();
|
|
while (miss != missiles.end()) {
|
|
if (miss->getY() >= pm.getScreenHeight())missiles.erase(miss);
|
|
else {
|
|
miss->setY(miss->getY()+MISSILE_SPEED);
|
|
++miss;
|
|
}
|
|
}
|
|
}
|
|
|
|
void Game::moveTorpedos() {
|
|
auto tor = torpedos.begin();
|
|
while (tor != torpedos.end()) {
|
|
if (tor->getY() <= 0)torpedos.erase(tor); // TODO maybe wait for it to be 100% offscreen
|
|
else{
|
|
tor->setY(tor->getY() - TORPEDO_SPEED);
|
|
++tor;
|
|
}
|
|
}
|
|
}
|
|
|
|
bool Game::checkMissilesAndPlayers() {
|
|
for(missile& miss : missiles){
|
|
if(miss.getY()<=PLAYER_HEIGHT){ // colliding on Y
|
|
if(lineCollideCheck( // now check collision on X
|
|
miss.getX(), miss.getX()+conf.missilesWidth,
|
|
p1.x, p1.x+conf.playersWidth)){
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool Game::checkTorpedosAndInvaders() {
|
|
return false;
|
|
} |