diff --git a/config.yml b/config.yml index 6743324..2eef538 100644 --- a/config.yml +++ b/config.yml @@ -1,7 +1,8 @@ # Players config players: width: 250 - startXPosition: 0 + startXPosition: 50 + fireCooldown: 50 speed: 1 user1: color: red @@ -18,6 +19,7 @@ players: # Enemies config invaders: + fireCooldown: 20 size: 15 speed: 2 distance: 10 # distance in pixels between invaders diff --git a/headers/configData.h b/headers/configData.h index 52e8f5c..78ce285 100644 --- a/headers/configData.h +++ b/headers/configData.h @@ -11,20 +11,25 @@ struct ConfigData { invadersGrid grid; + unsigned startXPosition; + unsigned playersSpeed; + unsigned playersWidth; + unsigned playersFireCooldown; + vector playerDefs; + + + + unsigned invadersSpeed; unsigned invadersSize; unsigned invadersDistance; + unsigned invadersFireCooldown; unsigned missilesWidth; unsigned missilesLength; // auto defined from width + unsigned missilesSpeed; unsigned torpedosWidth; unsigned torpedosLength; // auto defined from width - unsigned playersWidth; - - unsigned invadersSpeed; - unsigned playersSpeed; - - vector playerDefs; - unsigned startXPosition; + unsigned torpedosSpeed; }; #endif \ No newline at end of file diff --git a/headers/game.h b/headers/game.h index 170c9e7..b8f0c7b 100644 --- a/headers/game.h +++ b/headers/game.h @@ -40,7 +40,7 @@ private: bool checkTorpedosAndInvaders(); bool invadersTouchPlayer(); - void managePlayerMoves(PlayerDef&, unsigned&, unsigned); + void manageOnePlayer(unsigned); public: // in case someone wants to mess with the code, here's a minimal API, costs nothing to us diff --git a/headers/player.h b/headers/player.h index 41e8a8a..eaf7ab8 100644 --- a/headers/player.h +++ b/headers/player.h @@ -8,6 +8,7 @@ struct Player{ unsigned score=0; unsigned deathAnimCounter=0; + unsigned fireCooldown=0; }; #endif \ No newline at end of file diff --git a/src/configManagement.cpp b/src/configManagement.cpp index ec8ece1..a3c8418 100644 --- a/src/configManagement.cpp +++ b/src/configManagement.cpp @@ -77,6 +77,7 @@ void ConfigBuilder::readConfig() { collectedData.playersWidth = getInt("players.width"); collectedData.startXPosition = getInt("players.startXPosition"); collectedData.playersSpeed = getInt("players.speed"); + collectedData.playersFireCooldown = getInt("players.fireCooldown"); // the scalability behind the vector of players is only an illusion, because we force player count to be 1 or 2 // It was done so the 2+ players implementation could be easier in the future, if wanted @@ -95,13 +96,16 @@ void ConfigBuilder::readConfig() { collectedData.invadersSize = getInt("invaders.size"); collectedData.invadersSpeed = getInt("invaders.speed"); collectedData.invadersDistance = getInt("invaders.distance"); + collectedData.invadersFireCooldown = getInt("invaders.fireCooldown"); // projectiles collectedData.missilesWidth = getInt("projectiles.missiles.width"); collectedData.missilesLength = collectedData.missilesWidth*PROJ_LENGTH_FACTOR; + collectedData.missilesSpeed = getInt("projectiles.missiles.speed"); collectedData.torpedosWidth = getInt("projectiles.torpedos.width"); collectedData.torpedosLength = collectedData.torpedosWidth*PROJ_LENGTH_FACTOR; + collectedData.torpedosSpeed = getInt("projectiles.missiles.speed"); } int ConfigBuilder::getInt(const configKey& key) { diff --git a/src/game_managers.cpp b/src/game_managers.cpp index 5b3a4e2..bbf8317 100644 --- a/src/game_managers.cpp +++ b/src/game_managers.cpp @@ -1,30 +1,28 @@ #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, unsigned playerId){ - if (ISPRESSED(pdef.keys.left)){ - if(playerX < confData.playersSpeed) playerX = 0; - else playerX = playerX - confData.playersSpeed; +void Game::manageOnePlayer(unsigned id){ +#define ISPRESSED(ID, X) window.isPressed({confData.playerDefs[ID].keys.X, false}) + if (ISPRESSED(id, left)){ + if(players[id].x < confData.playersSpeed) players[id].x = 0; + else players[id].x -= confData.playersSpeed; } - if (ISPRESSED(pdef.keys.right)){ - if(playerX + confData.playersSpeed >= pm.getScreenWidth()) playerX = pm.getScreenWidth() - 1; - else playerX = playerX + confData.playersSpeed; - } - if(ISPRESSED(pdef.keys.shoot)){ - torpedos.emplace_back(playerX + confData.playersWidth / 2, pm.getScreenHeight() - PLAYER_HEIGHT, playerId); + if (ISPRESSED(id, right)){ + if(players[id].x + confData.playersSpeed >= pm.getScreenWidth()) players[id].x = pm.getScreenWidth() - 1; + else players[id].x += confData.playersSpeed; } + if(players[id].fireCooldown==0) { + if (ISPRESSED(id, shoot)) { + torpedos.emplace_back(players[id].x + confData.playersWidth / 2, pm.getScreenHeight() - PLAYER_HEIGHT, id); + players[id].fireCooldown = confData.playersFireCooldown; + } + }else --players[id].fireCooldown; } /** Makes the players play once */ void Game::managePlayers(){ - managePlayerMoves(confData.playerDefs[0], players[0].x,0); - if(playMode==PlayMode::TWO_LOCAL)managePlayerMoves(confData.playerDefs[1], players[1].x, 1); + manageOnePlayer(0); + if(playMode==PlayMode::TWO_LOCAL)manageOnePlayer(1); else if(playMode==PlayMode::TWO_TCPIP){ // TODO TCPIP } @@ -101,7 +99,7 @@ void Game::moveMissiles() { while (miss != missiles.end()) { if (miss->getY() >= pm.getScreenHeight())missiles.erase(miss); else { - miss->setY(miss->getY()+MISSILE_SPEED); + miss->setY(miss->getY()+confData.missilesSpeed); ++miss; } } @@ -112,7 +110,7 @@ void Game::moveTorpedos() { 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->setY(tor->getY()-confData.torpedosSpeed); ++tor; } } diff --git a/src/pixel_manager.cpp b/src/pixel_manager.cpp index 0096af8..e1c861b 100644 --- a/src/pixel_manager.cpp +++ b/src/pixel_manager.cpp @@ -42,7 +42,6 @@ void PixelManager::dessinerJoueur(const nsGraphics::Vec2D& baseVector, unsigned window << nsShape::Triangle(nsGraphics::Vec2D(15+width*2, 720)+baseVector, nsGraphics::Vec2D(15+width*2, 720-PLAYER_HEIGHT/2)+baseVector, nsGraphics::Vec2D(20+width*2, 720)+baseVector, color); window << nsShape::Triangle(nsGraphics::Vec2D(5,720-PLAYER_HEIGHT/2)+baseVector, nsGraphics::Vec2D(5+width,720-PLAYER_HEIGHT/2)+baseVector, nsGraphics::Vec2D(5+width,720-PLAYER_HEIGHT*0.9)+baseVector, color); window << nsShape::Triangle(nsGraphics::Vec2D(15+width,720-PLAYER_HEIGHT/2)+baseVector, nsGraphics::Vec2D(15+width*2,720-PLAYER_HEIGHT/2)+baseVector, nsGraphics::Vec2D(15+width,720-PLAYER_HEIGHT*0.9)+baseVector, color); - } void PixelManager::dessinerSprite(const nsGraphics::Vec2D& baseVector, const std::string& spritePath){