We can finally pew pew
This commit is contained in:
		
							parent
							
								
									a753e7a7b9
								
							
						
					
					
						commit
						b123c9a3f3
					
				| @ -21,16 +21,18 @@ players: | |||||||
| invaders: | invaders: | ||||||
|  fireCooldown: 20 |  fireCooldown: 20 | ||||||
|  size: 15 |  size: 15 | ||||||
|  speed: 2 |  speed: 0 | ||||||
|  distance: 10 # distance in pixels between invaders |  distance: 10 # distance in pixels between invaders | ||||||
| 
 | 
 | ||||||
| # Projectiles config | # Projectiles config | ||||||
| projectiles: | projectiles: | ||||||
|  missiles: |  missiles: | ||||||
|  |   color: yellow | ||||||
|   speed: 2 |   speed: 2 | ||||||
|   width: 10 |   width: 10 | ||||||
|  torpedos: |  torpedos: | ||||||
|   speed: 2 |   color: green | ||||||
|  |   speed: 10 | ||||||
|   width: 10 |   width: 10 | ||||||
| 
 | 
 | ||||||
| points: | points: | ||||||
|  | |||||||
| @ -27,9 +27,12 @@ struct ConfigData { | |||||||
| 	unsigned missilesWidth; | 	unsigned missilesWidth; | ||||||
| 	unsigned missilesLength; // auto defined from width
 | 	unsigned missilesLength; // auto defined from width
 | ||||||
| 	unsigned missilesSpeed; | 	unsigned missilesSpeed; | ||||||
|  | 	nsGraphics::RGBAcolor missilesColor; | ||||||
|  | 
 | ||||||
| 	unsigned torpedosWidth; | 	unsigned torpedosWidth; | ||||||
| 	unsigned torpedosLength; // auto defined from width
 | 	unsigned torpedosLength; // auto defined from width
 | ||||||
| 	unsigned torpedosSpeed; | 	unsigned torpedosSpeed; | ||||||
|  | 	nsGraphics::RGBAcolor torpedosColor; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| #endif | #endif | ||||||
| @ -12,11 +12,13 @@ class PixelManager{ | |||||||
| public: | public: | ||||||
| 	MinGL& window; | 	MinGL& window; | ||||||
| 	explicit PixelManager(MinGL&); | 	explicit PixelManager(MinGL&); | ||||||
| 	void dessinerInvader1(const nsGraphics::Vec2D& baseVector, unsigned size); | 	void drawInvader1(const nsGraphics::Vec2D& baseVector, unsigned size); | ||||||
| 	void dessinerInvader2(const nsGraphics::Vec2D& baseVector, unsigned size); | 	void drawInvader2(const nsGraphics::Vec2D& baseVector, unsigned size); | ||||||
| 	void dessinerInvader3(const nsGraphics::Vec2D& baseVector, unsigned size); | 	void drawInvader3(const nsGraphics::Vec2D& baseVector, unsigned size); | ||||||
| 	void dessinerJoueur(const nsGraphics::Vec2D& baseVector, unsigned size, const nsGraphics::RGBAcolor&); | 	void drawPlayer(const nsGraphics::Vec2D& baseVector, unsigned width, const nsGraphics::RGBAcolor &color); | ||||||
| 	void dessinerSprite(const nsGraphics::Vec2D& baseVector,const std::string& spritePath); | 	void drawTorpedo(const nsGraphics::Vec2D& baseVector, unsigned width, const nsGraphics::RGBAcolor& color); | ||||||
|  | 	void drawMissile(const nsGraphics::Vec2D& baseVector, unsigned width, const nsGraphics::RGBAcolor& color); | ||||||
|  | 	void drawSprite(const nsGraphics::Vec2D& baseVector, const std::string& spritePath); | ||||||
| 
 | 
 | ||||||
| 	unsigned showInitialMenu(); | 	unsigned showInitialMenu(); | ||||||
| 	unsigned showDeathMenu(); | 	unsigned showDeathMenu(); | ||||||
| @ -24,6 +26,7 @@ public: | |||||||
| 	unsigned getScreenWidth(); | 	unsigned getScreenWidth(); | ||||||
| 	void startFrame(); | 	void startFrame(); | ||||||
| 	void endFrame(); | 	void endFrame(); | ||||||
|  | 
 | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -23,6 +23,6 @@ typedef nsGraphics::Vec2D position; | |||||||
| typedef unsigned playerID; | typedef unsigned playerID; | ||||||
| 
 | 
 | ||||||
| // didn't want to use position because of the semantic with x and y
 | // didn't want to use position because of the semantic with x and y
 | ||||||
| bool lineCollideCheck(unsigned start1, unsigned end1, unsigned start2, unsigned end2); | bool areLinesColliding(unsigned start1, unsigned end1, unsigned start2, unsigned end2); | ||||||
| 
 | 
 | ||||||
| #endif | #endif | ||||||
| @ -102,10 +102,12 @@ void ConfigBuilder::readConfig() { | |||||||
| 	collectedData.missilesWidth = getInt("projectiles.missiles.width"); | 	collectedData.missilesWidth = getInt("projectiles.missiles.width"); | ||||||
| 	collectedData.missilesLength = collectedData.missilesWidth*PROJ_LENGTH_FACTOR; | 	collectedData.missilesLength = collectedData.missilesWidth*PROJ_LENGTH_FACTOR; | ||||||
| 	collectedData.missilesSpeed = getInt("projectiles.missiles.speed"); | 	collectedData.missilesSpeed = getInt("projectiles.missiles.speed"); | ||||||
|  | 	collectedData.missilesColor = getColor("projectiles.missiles.color"); | ||||||
| 
 | 
 | ||||||
| 	collectedData.torpedosWidth = getInt("projectiles.torpedos.width"); | 	collectedData.torpedosWidth = getInt("projectiles.torpedos.width"); | ||||||
| 	collectedData.torpedosLength = collectedData.torpedosWidth*PROJ_LENGTH_FACTOR; | 	collectedData.torpedosLength = collectedData.torpedosWidth*PROJ_LENGTH_FACTOR; | ||||||
| 	collectedData.torpedosSpeed = getInt("projectiles.missiles.speed"); | 	collectedData.torpedosSpeed = getInt("projectiles.missiles.speed"); | ||||||
|  | 	collectedData.torpedosColor = getColor("projectiles.torpedos.color"); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| int ConfigBuilder::getInt(const configKey& key) { | int ConfigBuilder::getInt(const configKey& key) { | ||||||
|  | |||||||
| @ -128,22 +128,29 @@ void Game::display() { | |||||||
| 			); | 			); | ||||||
| 			switch(grid[i][j]){ | 			switch(grid[i][j]){ | ||||||
| 				case 0:{ | 				case 0:{ | ||||||
| 					pm.dessinerInvader1(vec, confData.invadersSize); | 					pm.drawInvader1(vec, confData.invadersSize); | ||||||
| 					break; | 					break; | ||||||
| 				} | 				} | ||||||
| 				case 1:{ | 				case 1:{ | ||||||
| 					pm.dessinerInvader2(vec, confData.invadersSize); | 					pm.drawInvader2(vec, confData.invadersSize); | ||||||
| 					break; | 					break; | ||||||
| 				} | 				} | ||||||
| 				case 2:{ | 				case 2:{ | ||||||
| 					pm.dessinerInvader3(vec, confData.invadersSize); | 					pm.drawInvader3(vec, confData.invadersSize); | ||||||
| 					break; | 					break; | ||||||
| 				} | 				} | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	for(missile& miss : missiles){ | ||||||
|  | 		pm.drawTorpedo(miss, confData.missilesWidth, confData.missilesColor); | ||||||
|  | 	} | ||||||
|  | 	for(torpedo& tor : torpedos){ | ||||||
|  | 		pm.drawTorpedo(tor, confData.torpedosWidth, confData.torpedosColor); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	for(size_t i=0;i<players.size();++i){ | 	for(size_t i=0;i<players.size();++i){ | ||||||
| 		pm.dessinerJoueur(position(players[i].x, 0), confData.playersWidth, confData.playerDefs[i].color); | 		pm.drawPlayer(position(players[i].x, 0), confData.playersWidth, confData.playerDefs[i].color); | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| @ -77,7 +77,7 @@ void Game::remCollidingProjectiles(){ | |||||||
| 			if(miss->getY() + confData.missilesLength < tor->getY()){ | 			if(miss->getY() + confData.missilesLength < tor->getY()){ | ||||||
| 
 | 
 | ||||||
| 			} | 			} | ||||||
| 			if(lineCollideCheck( // now check if they collide in X
 | 			if(areLinesColliding( // now check if they collide in X
 | ||||||
| 					miss->getX(), miss->getX() + confData.missilesWidth, | 					miss->getX(), miss->getX() + confData.missilesWidth, | ||||||
| 					tor->getX(), tor->getX() + confData.torpedosWidth)){ | 					tor->getX(), tor->getX() + confData.torpedosWidth)){ | ||||||
| 				missiles.erase(miss); | 				missiles.erase(miss); | ||||||
| @ -123,7 +123,7 @@ bool Game::checkMissilesAndPlayers() { | |||||||
| 			// now check collision on X (with both players)
 | 			// now check collision on X (with both players)
 | ||||||
| 			bool wasColliding = false; | 			bool wasColliding = false; | ||||||
| 			for(Player& p : players){ | 			for(Player& p : players){ | ||||||
| 				if(lineCollideCheck( | 				if(areLinesColliding( | ||||||
| 						miss_ite->getX(), miss_ite->getX() + confData.missilesWidth, | 						miss_ite->getX(), miss_ite->getX() + confData.missilesWidth, | ||||||
| 						p.x, p.x + confData.playersWidth)){ | 						p.x, p.x + confData.playersWidth)){ | ||||||
| 					wasColliding = true; | 					wasColliding = true; | ||||||
| @ -145,13 +145,14 @@ bool Game::checkTorpedosAndInvaders() { | |||||||
| 		for(;i<grid.size();++i){ | 		for(;i<grid.size();++i){ | ||||||
| 			// calculate top-left position of invader
 | 			// calculate top-left position of invader
 | ||||||
| 			position pos = basePos+position( | 			position pos = basePos+position( | ||||||
| 					confData.invadersSize*(grid[i].size()-1)+confData.invadersDistance*(grid[i].size()-1), | 					confData.invadersSize*i+confData.invadersDistance*i, | ||||||
| 					confData.invadersSize*(grid[i].size()-1)+confData.invadersDistance*(grid[i].size()-1)); | 					confData.invadersSize*(grid[i].size()-1)+confData.invadersDistance*(grid[i].size()-1) | ||||||
|  | 			); | ||||||
| 			// check collision on Y (invaders can actually be "under" torpedos, so we check both lower and upper bounds
 | 			// check collision on Y (invaders can actually be "under" torpedos, so we check both lower and upper bounds
 | ||||||
| 			if(pos.getY()+confData.invadersSize>=tor_ite->getY() && | 			if(pos.getY()+confData.invadersSize>=tor_ite->getY() && | ||||||
| 					pos.getY()<=tor_ite->getY()+confData.torpedosLength){ | 					pos.getY()<=tor_ite->getY()+confData.torpedosLength){ | ||||||
| 				// now check collision on X
 | 				// now check collision on X
 | ||||||
| 				if(lineCollideCheck( // now check collision on X
 | 				if(areLinesColliding( // now check collision on X
 | ||||||
| 						tor_ite->getX(), tor_ite->getX() + confData.torpedosWidth, | 						tor_ite->getX(), tor_ite->getX() + confData.torpedosWidth, | ||||||
| 						pos.getX(), pos.getX() + confData.invadersSize)){ | 						pos.getX(), pos.getX() + confData.invadersSize)){ | ||||||
| 					break; | 					break; | ||||||
|  | |||||||
| @ -5,7 +5,7 @@ PixelManager::PixelManager(MinGL& a) : window(a) { | |||||||
| 	window.initGlut(); | 	window.initGlut(); | ||||||
| 	window.initGraphic(); | 	window.initGraphic(); | ||||||
| } | } | ||||||
| void PixelManager::dessinerInvader1(const nsGraphics::Vec2D& baseVector, unsigned size){ | void PixelManager::drawInvader1(const nsGraphics::Vec2D& baseVector, unsigned size){ | ||||||
| 	float scale = size/(float)100; | 	float scale = size/(float)100; | ||||||
| 	window << nsShape::Circle(nsGraphics::Vec2D(50*scale, 50*scale)+baseVector, 50*scale, nsGraphics::KGray); | 	window << nsShape::Circle(nsGraphics::Vec2D(50*scale, 50*scale)+baseVector, 50*scale, nsGraphics::KGray); | ||||||
| 	window << nsShape::Triangle(nsGraphics::Vec2D(35*scale, 50*scale)+baseVector, nsGraphics::Vec2D(15*scale, 25*scale)+baseVector, nsGraphics::Vec2D(15*scale, 75*scale)+baseVector, nsGraphics::KBlack); | 	window << nsShape::Triangle(nsGraphics::Vec2D(35*scale, 50*scale)+baseVector, nsGraphics::Vec2D(15*scale, 25*scale)+baseVector, nsGraphics::Vec2D(15*scale, 75*scale)+baseVector, nsGraphics::KBlack); | ||||||
| @ -16,7 +16,7 @@ void PixelManager::dessinerInvader1(const nsGraphics::Vec2D& baseVector, unsigne | |||||||
| 
 | 
 | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void PixelManager::dessinerInvader2(const nsGraphics::Vec2D& baseVector, unsigned size){ | void PixelManager::drawInvader2(const nsGraphics::Vec2D& baseVector, unsigned size){ | ||||||
| 	float scale = size/(float)100; | 	float scale = size/(float)100; | ||||||
| 	window << nsShape::Circle(nsGraphics::Vec2D(50*scale, 50*scale)+baseVector, 50*scale, nsGraphics::KRed); | 	window << nsShape::Circle(nsGraphics::Vec2D(50*scale, 50*scale)+baseVector, 50*scale, nsGraphics::KRed); | ||||||
| 	window << nsShape::Rectangle(nsGraphics::Vec2D(25*scale, 30*scale)+baseVector, nsGraphics::Vec2D(45*scale, 40*scale)+baseVector, nsGraphics::KBlack); | 	window << nsShape::Rectangle(nsGraphics::Vec2D(25*scale, 30*scale)+baseVector, nsGraphics::Vec2D(45*scale, 40*scale)+baseVector, nsGraphics::KBlack); | ||||||
| @ -24,7 +24,7 @@ void PixelManager::dessinerInvader2(const nsGraphics::Vec2D& baseVector, unsigne | |||||||
| 	window << nsShape::Rectangle(nsGraphics::Vec2D(35*scale, 65*scale)+baseVector, nsGraphics::Vec2D(65*scale, 72*scale)+baseVector, nsGraphics::KBlack); | 	window << nsShape::Rectangle(nsGraphics::Vec2D(35*scale, 65*scale)+baseVector, nsGraphics::Vec2D(65*scale, 72*scale)+baseVector, nsGraphics::KBlack); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void PixelManager::dessinerInvader3(const nsGraphics::Vec2D& baseVector, unsigned size){ | void PixelManager::drawInvader3(const nsGraphics::Vec2D& baseVector, unsigned size){ | ||||||
| 	float scale = size/(float)100; | 	float scale = size/(float)100; | ||||||
| 	window << nsShape::Circle(nsGraphics::Vec2D(50*scale, 50*scale)+baseVector, 50*scale, nsGraphics::KGreen); | 	window << nsShape::Circle(nsGraphics::Vec2D(50*scale, 50*scale)+baseVector, 50*scale, nsGraphics::KGreen); | ||||||
| 	window << nsShape::Circle(nsGraphics::Vec2D(35*scale, 35*scale)+baseVector, 10*scale, nsGraphics::KBlack); | 	window << nsShape::Circle(nsGraphics::Vec2D(35*scale, 35*scale)+baseVector, 10*scale, nsGraphics::KBlack); | ||||||
| @ -32,7 +32,7 @@ void PixelManager::dessinerInvader3(const nsGraphics::Vec2D& baseVector, unsigne | |||||||
| 	window << nsShape::Rectangle(nsGraphics::Vec2D(35*scale, 65*scale)+baseVector, nsGraphics::Vec2D(65*scale, 72*scale)+baseVector, nsGraphics::KBlack); | 	window << nsShape::Rectangle(nsGraphics::Vec2D(35*scale, 65*scale)+baseVector, nsGraphics::Vec2D(65*scale, 72*scale)+baseVector, nsGraphics::KBlack); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void PixelManager::dessinerJoueur(const nsGraphics::Vec2D& baseVector, unsigned width, const nsGraphics::RGBAcolor& color){ | void PixelManager::drawPlayer(const nsGraphics::Vec2D& baseVector, unsigned width, const nsGraphics::RGBAcolor& color){ | ||||||
| 	width = width-10-10; | 	width = width-10-10; | ||||||
| 	width = width/2; | 	width = width/2; | ||||||
| 	window << nsShape::Triangle(nsGraphics::Vec2D(0, 720)+baseVector, nsGraphics::Vec2D(5, 720)+baseVector, nsGraphics::Vec2D(5, 720-PLAYER_HEIGHT/2)+baseVector, color); | 	window << nsShape::Triangle(nsGraphics::Vec2D(0, 720)+baseVector, nsGraphics::Vec2D(5, 720)+baseVector, nsGraphics::Vec2D(5, 720-PLAYER_HEIGHT/2)+baseVector, color); | ||||||
| @ -44,7 +44,15 @@ void PixelManager::dessinerJoueur(const nsGraphics::Vec2D& baseVector, unsigned | |||||||
| 	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); | 	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){ | void PixelManager::drawMissile(const nsGraphics::Vec2D& baseVector, unsigned width, const nsGraphics::RGBAcolor& color){ | ||||||
|  | 	window << nsShape::Rectangle(baseVector, baseVector+position(width, width*PROJ_LENGTH_FACTOR), color); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void PixelManager::drawTorpedo(const nsGraphics::Vec2D& baseVector, unsigned width, const nsGraphics::RGBAcolor& color){ | ||||||
|  | 	window << nsShape::Rectangle(baseVector, baseVector+position(width, width*PROJ_LENGTH_FACTOR), color); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void PixelManager::drawSprite(const nsGraphics::Vec2D& baseVector, const std::string& spritePath){ | ||||||
| 	nsGui::Sprite sprite(spritePath,baseVector); | 	nsGui::Sprite sprite(spritePath,baseVector); | ||||||
| 	sprite.draw(window); | 	sprite.draw(window); | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,6 +1,6 @@ | |||||||
| #include "utils.h" | #include "utils.h" | ||||||
| 
 | 
 | ||||||
| bool lineCollideCheck(unsigned start1, unsigned end1, unsigned start2, unsigned end2){ | bool areLinesColliding(unsigned start1, unsigned end1, unsigned start2, unsigned end2){ | ||||||
| 	return start1 < end2 != start2 < end1; | 	return start1 < end2 == start2 < end1; | ||||||
| 	// if true, are colliding. I like truth tables
 | 	// if it returns true, lines are colliding. I like truth tables
 | ||||||
| } | } | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user