Example #1
0
void Demo4Object::DoUpdate( int iCurrentTime )
{
	m_iPreviousScreenX = m_iCurrentScreenX;
	m_iPreviousScreenY = m_iCurrentScreenY;

/********** EXTRA ********/

// Iterate through the objects
// We are looking for one which is too close to us
DisplayableObject* pObject;
for ( int iObjectId = 0 ; 
	 (pObject = m_pMainEngine->GetDisplayableObject( iObjectId )
			) != NULL ;
	iObjectId++ )
{
	if ( pObject == this ) // This is us, skip it
		continue;
	// If you need to cast to the sub-class type, you must use dynamic_cast, see lecture 19
	// We are just using base class parts
	int iXDiff = pObject->GetXCentre() - m_iCurrentScreenX;
	int iYDiff = pObject->GetYCentre() - m_iCurrentScreenY;

	// Estimate the size - by re-calculating it
	int iTick = iCurrentTime/20; // 1 per 20ms
	int iFrame = iTick % 30;
	int iSize = 10 + iFrame;
	if ( iFrame > 15 )
		iSize = 10 + (30-iFrame);
	int iSizeOther = iSize; // Assume both the same size

	// Pythagorus' theorum:
	if ( ((iXDiff*iXDiff)+(iYDiff*iYDiff)) 
			< ((iSizeOther+iSize)*(iSizeOther+iSize)) )
	{
		// Move us to 1,1 and set direction right
		m_iMapX = 1+rand()%13;
		m_iMapY = 1+(rand()%2)*8;
		m_iDir = 1; // Face right
		m_oMover.Setup( 
			m_iMapX *50 + 25 + 25, //m_iCurrentScreenX,
			m_iMapY *50 + 25 + 40, //m_iCurrentScreenY,
			m_iMapX *50 + 25 + 25,
			m_iMapY *50 + 25 + 40,
			iCurrentTime,
			iCurrentTime+400+rand()%200 );
		// Ask the mover where the object should be
		m_oMover.Calculate( iCurrentTime );
		m_iCurrentScreenX = m_oMover.GetX();
		m_iCurrentScreenY = m_oMover.GetY();
		// Ensure that the object gets redrawn on the display, if something changed
		RedrawObjects();
		return;
	}
}
/********** END EXTRA ********/

	// If movement has finished
	if ( m_oMover.HasMovementFinished(iCurrentTime) )
	{
		Demo4TileManager& tm = m_pMainEngine->GetTileManager();
 
		// Handle any tile that we just moved onto
		switch ( tm.GetValue( m_iMapX, m_iMapY ) )
		{
		case 2:
		case 3:
		case 4:
		case 5:
		case 6:
		case 7:
			tm.UpdateTile( m_pMainEngine, m_iMapX, m_iMapY, 
			tm.GetValue( m_iMapX, m_iMapY ) + 1 );
			break;
		case 8:
			tm.UpdateTile( m_pMainEngine, m_iMapX, m_iMapY, 0 );
			break;
		}

		// Set off a new movement
		switch( rand() % 10 )
		{
		case 0: // Increase dir by 1
			m_iDir = ( m_iDir + 1 )%4;
			break;
		case 1: // Reduce dir by 1
			m_iDir = ( m_iDir + 3 )%4;
			break;
		}

		// Allow some control over the object by the player
		if ( m_pMainEngine->IsKeyPressed( SDLK_UP ) )
			m_iDir = 0;
		if ( m_pMainEngine->IsKeyPressed( SDLK_RIGHT ) )
			m_iDir = 1;
		if ( m_pMainEngine->IsKeyPressed( SDLK_DOWN ) )
			m_iDir = 2;
		if ( m_pMainEngine->IsKeyPressed( SDLK_LEFT ) )
			m_iDir = 3;


		switch ( tm.GetValue( 
				m_iMapX + GetXDiffForDirection(m_iDir),
				m_iMapY + GetYDiffForDirection(m_iDir) ) )
		{
		case 0: // Passageway
		case 2: // Pellet
		case 3: // Pellet
		case 4: // Pellet
		case 5: // Pellet
		case 6: // Pellet
		case 7: // Pellet
		case 8: // Pellet
			// Allow move - set up new movement now
			m_iMapX += GetXDiffForDirection(m_iDir);
			m_iMapY += GetYDiffForDirection(m_iDir);

			m_oMover.Setup( 
				m_iCurrentScreenX,
				m_iCurrentScreenY,
				m_iMapX *50 + 25 + 25,
				m_iMapY *50 + 25 + 40,
				iCurrentTime,
				iCurrentTime+400+rand()%200 );
			break;	
		case 1: // Wall
			m_iDir = rand()%4; // Rotate randomly
			break;
		}
	}

	// If making a move then do the move
	if ( !m_oMover.HasMovementFinished(iCurrentTime) )
	{
		// Ask the mover where the object should be
		m_oMover.Calculate( iCurrentTime );
		m_iCurrentScreenX = m_oMover.GetX();
		m_iCurrentScreenY = m_oMover.GetY();
	}

	// Ensure that the object gets redrawn on the display, if something changed
	RedrawObjects();
}
void Nxs13uEnemyObject::DoUpdate(int currentTime)
{
	//keep the objects in the playing field 
	//so the dimensions of the player field is (0,50), (600,50), (0,550), (600,550)
	int fieldXmin = 0;
	int fieldXmax = 600;
	int fieldYmin = 50;
	int fieldYmax = 550;

	if (id == 3){ //for start page, enemy must be able to travel anywhere
		fieldXmin = 0;
		fieldXmax = GetEngine()->GetScreenWidth();
		fieldYmin = 0;
		fieldYmax = GetEngine()->GetScreenHeight();
	}

	// Alter position for speed - this makes the ball appear to vibrate 
	// remember that speed is basically pixels travelled per tick
	m_iCurrentScreenX += (speedX * rand() % (difficulty + 1));
	m_iCurrentScreenY += (speedY * rand() % (difficulty + 1));

	// Check for bounce off the edge
	if (m_iCurrentScreenX < fieldXmin + radius)
	{
		m_iCurrentScreenX = fieldXmin + radius;
		if (speedX < 0)
			speedX = -speedX;
	}
	if (m_iCurrentScreenX > fieldXmax - radius)
	{
		m_iCurrentScreenX = fieldXmax - radius;
		if (speedX > 0)
			speedX = -speedX;
	}
	if (m_iCurrentScreenY < fieldYmin + radius)
	{
		m_iCurrentScreenY = fieldYmin + radius;
		if (speedY < 0)
			speedY = -speedY;
	}
	if (m_iCurrentScreenY > fieldYmax - radius)
	{
		m_iCurrentScreenY = fieldYmax - radius;
		if (speedY > 0)
			speedY = -speedY;
	}

	// Set current position 
	m_iCurrentScreenX = (int)(m_iCurrentScreenX + speedX);
	m_iCurrentScreenY = (int)(m_iCurrentScreenY + speedY);

	//dynamic cast
	DisplayableObject* playerObject;

	//COLLISIONS
	if (id == 2){ //FOR GAME MODE ENEMY (not start-up page enemy)
		for (int objectId = 0; (playerObject = m_pMainEngine->GetDisplayableObject(objectId)) != NULL; objectId++)
		{
			if (playerObject == this) // This is us, skip it
				continue;
	
			//get the difference between the player and the enemy
			int playerSize = 40;
			int xDifference = playerObject->GetXCentre() - m_iCurrentScreenX;
			int yDifference = playerObject->GetYCentre() - m_iCurrentScreenY;

			// Pythagorus' theorum, if the distance is smaller than the size (radius size of both objects), then it is a collission
			if (((xDifference*xDifference) + (yDifference*yDifference)) < ((playerSize + radius)*(playerSize + radius)))
			{

				//move 70 pixels away from user and go in opposite direction (prevents re-collision)
				speedX = -speedX;
				speedY = -speedY;

				if (speedX < 0 || m_iCurrentScreenX > 530)
					m_iCurrentScreenX = m_iCurrentScreenX - 70;
				else 
					m_iCurrentScreenX = m_iCurrentScreenX + 70;

				if (speedY < 0 || m_iCurrentScreenY > 480)
					m_iCurrentScreenY = m_iCurrentScreenY - 70;
				else
					m_iCurrentScreenY = m_iCurrentScreenY + 70;  

				collision = true;

				//check which player it collided with - lives are passed back to Nxs13uMain
				if (objectId == 0)
				player1lives--;
				else if (objectId == 1)
				player2lives--; 
				
			}
			// end of collissions 
		}
	}

	RedrawObjects();
}