Example #1
0
//-----------------------------------------------------------------------------
// Name : DrawObjects () (Private)
// Desc : Draws the game objects
//-----------------------------------------------------------------------------
void CGameApp::DrawObjects()
{
	m_pBBuffer->reset();

	HDC hdc = m_pBBuffer->getDC();

	m_imgBackground.Paint(hdc, 0, 0);

	if(GameOver == false && level.Winner == false && LevelChange == false)
	{
		DrawFunctor drawFn;
		std::for_each(m_vGameObjects.begin(), m_vGameObjects.end(), drawFn);
		std::for_each(m_vGameObjectsGift.begin(), m_vGameObjectsGift.end(), drawFn);
		std::for_each(m_vGameObjectsAnimate.begin(), m_vGameObjectsAnimate.end(), drawFn);
	}
	else
	{
		if(LevelChange == true)
			DrawGame(hdc,"level");
		if(level.Winner == true)
		{
			DrawGame(hdc,"winner");
			if(countGameOver == 0)
			{
				SaveScore();
				ScoreTable();
			}
			ShowScoreTable(hdc);
			countGameOver++;
		}
		if(GameOver == true)
		{
			DrawGame(hdc,"gameover");
			if(countGameOver == 0)
			{
				SaveScore();
				ScoreTable();
			}
			ShowScoreTable(hdc);
			countGameOver++;
		}
	}

	DrawScore(hdc);
	DrawLife(hdc);

	m_pBBuffer->present();
}
Example #2
0
//-----------------------------------------------------------------------------
// Name : CollisionDetection () (Private)
// Desc : Detects and handles collision
//-----------------------------------------------------------------------------
void CGameApp::CollisionDetection()
{
	HDC hdc = m_pBBuffer->getDC();

	// collision detection with the main frame
	for(auto it = m_vGameObjects.begin(); it != m_vGameObjects.end(); ++it)
	{
		CGameObject * pGameObj = it->get();
		Vec2 pos = pGameObj->myPosition;

		if(pGameObj->GetObjectTypeSub() == GOT_BrickNormal || pGameObj->GetObjectTypeSub() == GOT_BrickDouble || pGameObj->GetObjectTypeSub() == GOT_BrickGift)
		{
			BricksExist = true;
		}

		pGameObj->myCollisionSide = CS_None;

		// check collision for ball and player with wall
		if(pGameObj->GetObjectType() == GOT_Ball || pGameObj->GetObjectType() == GOT_Player)
		{
			int dx = (int)pos.x - pGameObj->GetWidth() / 2;
			if( dx < 0 )
			{
				pGameObj->myCollisionSide |= CS_Left;

				if(pGameObj->GetObjectType() == GOT_Ball)
				{
					m_pBall.lock()->myVelocity.x = -m_pBall.lock()->myVelocity.x;
				}

				if(pGameObj->GetObjectType() == GOT_Player)
				{
					pGameObj->myPosition.x = pGameObj->GetWidth()/2;
					if(FollowPlayer == true)
					{
						m_pBall.lock()->myPosition.x = ballPos.x + pGameObj->myPosition.x;
					}
				}
			}

			dx = (int)pos.x - (m_nViewWidth - pGameObj->GetWidth() / 2);
			if( dx > 0 )
			{
				pGameObj->myCollisionSide |= CS_Right;

				if(pGameObj->GetObjectType() == GOT_Ball)
				{
					m_pBall.lock()->myVelocity.x = -m_pBall.lock()->myVelocity.x;
				}

				if(pGameObj->GetObjectType() == GOT_Player)
				{
					pGameObj->myPosition.x = m_nViewWidth - pGameObj->GetWidth() / 2;
					if(FollowPlayer == true)
					{
						m_pBall.lock()->myPosition.x = ballPos.x + pGameObj->myPosition.x;
					}
				}
			}

			int dy = (int)pos.y - pGameObj->GetHeight() / 2;
			if( dy < 0 )
			{
				pGameObj->myCollisionSide |= CS_Top;
				if(pGameObj->GetObjectType() == GOT_Ball)
				{
					m_pBall.lock()->myVelocity.y = -m_pBall.lock()->myVelocity.y;
				}
			}

			dy = (int)pos.y - (m_nViewHeight - pGameObj->GetHeight() / 2);
			if( dy > 0 )
			{
				pGameObj->myCollisionSide |= CS_Bottom;
				if(pGameObj->GetObjectType() == GOT_Ball)
				{
					m_pBall.lock()->myVelocity.y = -m_pBall.lock()->myVelocity.y;
				}
			}

		}

		// check ball collision with game objects
		if(pGameObj->GetObjectType() == GOT_Ball)
			for(auto it2 = m_vGameObjects.begin(); it2 != m_vGameObjects.end(); ++it2)
			{
				CGameObject * pGameObj2 = it2->get();
				Vec2 pos2 = pGameObj2->myPosition;


				if(pGameObj->GetObjectType() == pGameObj2->GetObjectType())
					continue;
				if(pGameObj2->GetObjectType() == GOT_Player || pGameObj2->GetObjectType() == GOT_Brick)
				{

					if(pGameObj2->GetObjectType() == GOT_Player)
					{
						if(pGameObj->myPosition.y > m_nViewHeight - m_pBall.lock()->GetHeight() - m_pPlayer.lock()->GetHeight() + ONE)
						{
							if(!(abs(pos.y - pos2.y) < pGameObj->GetHeight() / 2 + pGameObj2->GetHeight() / 2 && abs(pos.x - pos2.x) < pGameObj->GetWidth() / 2 + pGameObj2->GetWidth() / 2))
							{
								Decrease_Life();
								UnstoppableBall = false;
								StickyBar = false;
								ballPos.x = 0;
								ShrinkBar = false;
								MoveBall = false;
								m_pPlayer.lock()->Normal_Bar();
								SystemParametersInfo(SPI_SETMOUSESPEED, NULL, (void*)10, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE );
								if(countLife == NO_LIFE)
								{
									GameOver = true;
								}
							}
						}
					}

					if(abs(pos.y - pos2.y) < pGameObj->GetHeight() / 2 + pGameObj2->GetHeight() / 2 && abs(pos.x - pos2.x) < pGameObj->GetWidth() / 2 + pGameObj2->GetWidth() / 2)
					{		

						if(pGameObj2->GetObjectType() == GOT_Player)
						{
							if(StickyBar == true)
							{
								pGameObj->myVelocity.y = 0;
								pGameObj->myVelocity.x = 0;
								pGameObj->myPosition.y = (int)m_nViewHeight - m_pPlayer.lock()->GetHeight() - m_pBall.lock()->GetHeight() - ONE;
								ballPos.x = pGameObj->myPosition.x - pGameObj2->myPosition.x;
								FollowPlayer = true;
							}
							else
							{
								pGameObj->myVelocity.y = -pGameObj->myVelocity.y;
							}
						}
						else
						{
							if(UnstoppableBall != true)
								if(abs(pos.x - pos2.x) > pGameObj->GetHeight() / 2 + pGameObj2->GetHeight() / 2)
								{// left or right collosion
									pGameObj->myVelocity.x = -pGameObj->myVelocity.x;
								}
								else
								{// top or bottom collision
									pGameObj->myVelocity.y = -pGameObj->myVelocity.y;
								}
						}

						if(pGameObj2->GetObjectTypeSub() == GOT_BrickNormal || pGameObj2->GetObjectTypeSub() == GOT_BrickDouble || pGameObj2->GetObjectTypeSub() == GOT_BrickGift)
						{
							if(pGameObj2->GetObjectTypeSub() == GOT_BrickDouble)
							{						
								int CheckDouble=pGameObj2->DecreaseDouble();
								if(CheckDouble == 1)
								{
									Increase_Score(BRICK_SCORE);
									pGameObj2->ChangeSprite();
								}
								if(CheckDouble == 0)
								{
									Increase_Score(BRICK_SCORE);
									DrawScore(hdc);
									auto pAnimate = std::make_shared<Brick>('k');
									pAnimate->Init(Vec2(pos2.x,pos2.y));
									m_vGameObjectsAnimate.push_back(pAnimate);
									m_vGameObjects.erase(it2);
									break;
								}
							}
							else
							{
								if(pGameObj2->GetObjectTypeSub() == GOT_BrickGift)
								{
									auto pGift = std::make_shared<Gift>(pGameObj2->GetBrickType());
									pGift->Init(Vec2(pos2.x,pos2.y));
									m_vGameObjectsGift.push_back(pGift);
								}
								Increase_Score(BRICK_SCORE);
								DrawScore(hdc);
								auto pAnimate = std::make_shared<Brick>('k');
								pAnimate->Init(Vec2(pos2.x,pos2.y));
								m_vGameObjectsAnimate.push_back(pAnimate);
								m_vGameObjects.erase(it2);
								break;
							}
						}
					}
				}

			}

			// check gift collision with player
			if(pGameObj->GetObjectType() == GOT_Player)
				for(auto it2 = m_vGameObjectsGift.begin(); it2 != m_vGameObjectsGift.end(); ++it2)
				{
					CGameObject * pGameObj2 = it2->get();
					Vec2 pos2 = pGameObj2->myPosition;

					if(abs(pos.y - pos2.y) < pGameObj->GetHeight() / 2 + pGameObj2->GetHeight() / 2 && abs(pos.x - pos2.x) < pGameObj->GetWidth() / 2 + pGameObj2->GetWidth() / 2)
					{
						if(pGameObj2->GetObjectTypeSub() == GOT_Gift100)
						{
							Increase_Score(BRICK_GIFT_SCORE_1);
							DrawScore(hdc);
							m_vGameObjectsGift.erase(it2);
							break;
						}
						if(pGameObj2->GetObjectTypeSub() == GOT_Gift200)
						{
							Increase_Score(BRICK_GIFT_SCORE_2);
							DrawScore(hdc);
							m_vGameObjectsGift.erase(it2);
							break;
						}
						if(pGameObj2->GetObjectTypeSub() == GOT_GiftUpLife)
						{
							Increase_Life();
							DrawLife(hdc);
							m_vGameObjectsGift.erase(it2);
							break;
						}
						if(pGameObj2->GetObjectTypeSub() == GOT_GiftDownLife)
						{
							Decrease_Life2();
							DrawLife(hdc);
							m_vGameObjectsGift.erase(it2);
							break;
						}
						if(pGameObj2->GetObjectTypeSub() == GOT_GiftIncreaseSpeed)
						{
							m_pBall.lock()->Increase_Speed();
							DrawLife(hdc);
							m_vGameObjectsGift.erase(it2);
							break;
						}
						if(pGameObj2->GetObjectTypeSub() == GOT_GiftDecreaseSpeed)
						{
							m_pBall.lock()->Decrease_Speed();
							DrawLife(hdc);
							m_vGameObjectsGift.erase(it2);
							break;
						}
						if(pGameObj2->GetObjectTypeSub() == GOT_GiftUnstoppableBall)
						{
							UnstoppableBall = true;
							m_vGameObjectsGift.erase(it2);
							break;
						}
						if(pGameObj2->GetObjectTypeSub() == GOT_GiftStickyBar)
						{
							StickyBar = true;
							m_vGameObjectsGift.erase(it2);
							break;
						}
						if(pGameObj2->GetObjectTypeSub() == GOT_GiftShrinkBar)
						{
							if(ShrinkBar == false)
							{
								ShrinkBar = true;
								m_pPlayer.lock()->Shrink_Bar();
							}
							m_vGameObjectsGift.erase(it2);
							break;
						}
						if(pGameObj2->GetObjectTypeSub() == GOT_GiftSlowMouse)
						{
							SlowMouse = true;
							break;
						}
					}

					int dy = (int)pos2.y - (m_nViewHeight - pGameObj2->GetHeight() / 2);
					if( dy > 0 )
					{
						m_vGameObjectsGift.erase(it2);
						break;
					}
				}
	}
}
Example #3
0
void Space::Draw()
{
    Vec2i offset = -cam;
    Tree::ClearWindow( Tree::Color( 0xFF000000 ) );

    Vec2i chunk = CurrentChunkIndex();

    DrawChunk( chunk, offset );

    // Draw surrounding chunks
    DrawChunk( chunk.x - 1, chunk.y, offset );
    DrawChunk( chunk.x + 1, chunk.y, offset );
    DrawChunk( chunk.x, chunk.y - 1, offset );
    DrawChunk( chunk.x, chunk.y + 1, offset );

    DrawChunk( chunk.x - 1, chunk.y - 1, offset );
    DrawChunk( chunk.x - 1, chunk.y + 1, offset );
    DrawChunk( chunk.x + 1, chunk.y - 1, offset );
    DrawChunk( chunk.x + 1, chunk.y + 1, offset );

    box.Draw( offset );

    // Draw satellite
    satellite.Draw( offset );

    if( satellite.SeesWayHome() ) {
        arrow_home_spr.Move( offset );
        Tree::Draw( arrow_home_spr );
    }

    if( SETTINGS->GetValue<bool>( "bounding_box_show" ) ) {
        draw_outline( satellite.BoundingBox(), -cam );
    }

    // Draw GUI elements
    DrawCollected();
    DrawLife();
    DrawFuel();

    if( victory ) {
        Vec2i pos = satellite.GetPos() + offset;

        friend_spr.SetPosition( pos.x + 15, pos.y - 10 );
        Tree::Draw( friend_spr );

        talk_str.SetPosition( pos.x + 5, pos.y - 35 );
        talk_str.SetColor( Tree::Color( 0xffeeeeee ) );
        Tree::Draw( talk_str );

        sf::Shape black = sf::Shape::Rectangle( 0, 0, Tree::GetWindowWidth(), Tree::GetWindowHeight(),
            sf::Color( 0, 0, 0, fade ) );
        Tree::Draw( black );
    }
    else if( game_over ) {
        sf::Shape black = sf::Shape::Rectangle( 0, 0, Tree::GetWindowWidth(), Tree::GetWindowHeight(),
            sf::Color( 0, 0, 0, fade ) );
        Tree::Draw( black );
    }

    // Draw docking on top, if we can ofc
    if( dock.IsActive() ) { dock.Draw(); }
}