Browse Source

Merge branch 'enemy_num_bugfix' of matteozeccolimarazzini/invaders into master

Fix enemy number reset between games (#7)

Who has two thumbs and hates merge commits?
blue 4 years ago
parent
commit
ae168b4069
5 changed files with 22 additions and 18 deletions
  1. 2 0
      bossrush.cpp
  2. 1 1
      definitions.hpp
  3. 10 12
      functions.cpp
  4. 1 1
      functions.hpp
  5. 8 4
      invaders.cpp

+ 2 - 0
bossrush.cpp

@@ -23,6 +23,8 @@ using std::endl;
 using std::min;
 using std::max;
 
+int ENEMY_NUM=1;	// This variable isn't used, but we have to define it since it is decleared in definitions.hpp"
+
 int main(int argc,char** argv)
 {
 

+ 1 - 1
definitions.hpp

@@ -79,7 +79,7 @@
 #define backcyan "\033[46m"
 #define backgreen "\033[1;42m" 
 
-static int ENEMY_NUM=1;
+extern int ENEMY_NUM;
 static int WALLS_NUM;
 
 extern std::mutex m_player;

+ 10 - 12
functions.cpp

@@ -222,7 +222,7 @@ void print_info(){
 }
 
 
-int choose_level(double& shootr, int& refresh_t, boss& boss1, char& level,int* commands)	//returns ENEMY_NUM
+void choose_level(double& shootr, int& refresh_t, boss& boss1, char& level, int& enemy_num, int* commands)
 {
 	std::string phrase;
 	phrase=choose_phrase();
@@ -296,8 +296,8 @@ int choose_level(double& shootr, int& refresh_t, boss& boss1, char& level,int* c
 			std::string bossname=getenv("HOME");
 			bossname=bossname+RECORD_DIR+BOSS_FILE1+".dat";
 			boss1.loadpicture(bossname.c_str());
-			ENEMY_NUM=30;
-			return ENEMY_NUM;
+			enemy_num=30;
+			break;
 		}
 		case 2:
 		{
@@ -308,8 +308,8 @@ int choose_level(double& shootr, int& refresh_t, boss& boss1, char& level,int* c
 			std::string bossname=getenv("HOME");
 			bossname=bossname+RECORD_DIR+BOSS_FILE2+".dat";
 			boss1.loadpicture(bossname.c_str());
-			ENEMY_NUM=40;
-			return ENEMY_NUM;
+			enemy_num=40;
+			break;
 		}
 		case 3:
 		{
@@ -320,8 +320,8 @@ int choose_level(double& shootr, int& refresh_t, boss& boss1, char& level,int* c
 			std::string bossname=getenv("HOME");
 			bossname=bossname+RECORD_DIR+BOSS_FILE3+".dat";
 			boss1.loadpicture(bossname.c_str());
-			ENEMY_NUM=45;
-			return ENEMY_NUM;
+			enemy_num=45;
+			break;
 		}
 		case 4:
 		{
@@ -332,13 +332,10 @@ int choose_level(double& shootr, int& refresh_t, boss& boss1, char& level,int* c
 			std::string bossname=getenv("HOME");
 			bossname=bossname+RECORD_DIR+BOSS_FILE4+".dat";
 			boss1.loadpicture(bossname.c_str());
-			ENEMY_NUM=50;
-			return ENEMY_NUM;
+			enemy_num=50;
+			break;
 		}
 	}
-
-	return 0;
-	
 }
 
 void change_commands(int* commands){
@@ -408,6 +405,7 @@ void change_commands(int* commands){
 	
 void load_enemies(e_list& enemies,int enemy_num)
 {
+	enemies.clear();
 	for(int i=0; i<enemy_num; ++i)     				//loading enemies
 	{
 		static int c=0, r=0;

+ 1 - 1
functions.hpp

@@ -49,7 +49,7 @@ void print_info();
 
 void print_scores();
 
-int choose_level(double& shootr, int& refresh_t, boss &boss1, char& level,int* commands);
+void choose_level(double& shootr, int& refresh_t, boss &boss1, char& level, int& enemy_num,int* commands);
 
 void change_commands(int* commands);
 

+ 8 - 4
invaders.cpp

@@ -11,6 +11,8 @@ using std::cout;
 using std::endl; 
 using std::min;
 
+int ENEMY_NUM=1;
+
 int main(int argc,char** argv)
 {
 	if(argc>1){
@@ -95,7 +97,8 @@ int main(int argc,char** argv)
 	
 	WALLS_NUM=2;
 	
-	ENEMY_NUM=choose_level(shootrate, refresh_time, boss1, level, commands);     	//choose difficulty level and set game parameters and boss
+	choose_level(shootrate, refresh_time, boss1, level, ENEMY_NUM, commands);     	//choose difficulty level and set game parameters and boss
+	load_enemies(enemies,ENEMY_NUM);
 	player1.set_commands(commands);
 	erase();
 	
@@ -106,7 +109,6 @@ int main(int argc,char** argv)
 	
 	Score=newwin(3,10,R/3,C+3);
 	BossHP=newwin(3,15,R/3-3,C+3);
-	load_enemies(enemies,ENEMY_NUM);
 	
 	int i=0;
 	for(w_vec::iterator it=walls.begin(); it!=walls.end(); ++it, ++i)				//creating walls (in a quite symmetric pattern)
@@ -259,7 +261,8 @@ int main(int argc,char** argv)
 				score=0;
 				erase();
 				refresh();
-				choose_level(shootrate, refresh_time, boss1, level, commands);
+				choose_level(shootrate, refresh_time, boss1, level, ENEMY_NUM, commands);
+				load_enemies(enemies,ENEMY_NUM);
 				reset(player1, enemies, boss1, bullets, bombs, walls, powerups,rockets, level,chflag); // reset box, player, enemy and deletes all bullets and bombs
 				erase();
 				refresh();
@@ -289,7 +292,8 @@ int main(int argc,char** argv)
 				score=0;
 				erase();
 				refresh();
-				choose_level(shootrate, refresh_time, boss1, level, commands);
+				choose_level(shootrate, refresh_time, boss1, level, ENEMY_NUM, commands);
+				load_enemies(enemies,ENEMY_NUM);
 				reset(player1, enemies, boss1, bullets, bombs, walls, powerups,rockets,level,chflag);       // reset box, player, enemy and delete all bullets and bombs
 				erase();
 				refresh();