Ejemplo n.º 1
0
void Dot::move( Tile *tiles[] )
{
    //Move the dot left or right
    mBox.x += mVelX;

    //If the dot went too far to the left or right or touched a wall
    if( ( mBox.x < 0 ) || ( mBox.x + DOT_WIDTH > LEVEL_WIDTH ) || touchesWall( mBox, tiles ) )
    {
        //move back
        mBox.x -= mVelX;
    }

    //Move the dot up or down
    mBox.y += mVelY;

    //If the dot went too far up or down or touched a wall
    if( ( mBox.y < 0 ) || ( mBox.y + DOT_HEIGHT > LEVEL_HEIGHT ) || touchesWall( mBox, tiles ) )
    {
        //move back
        mBox.y -= mVelY;
    }
}
Ejemplo n.º 2
0
//Apply physics from key presses and set animation
void Player::ApplyPhysics(Tile *tiles[], std::list <LevelObject> levelObjects, std::list <DestroyableObject> objects)
{	
	//Set frames for animations
	frameTime++;
	if (60 / frameTime <= 6)
	{
		frameTime = 0;
		currentFrame += 1;
		if (currentFrame >= 4)
			currentFrame = 0;
	}

	//Reduce hit cooldown so player can take damage again
	hitCooldown--;
	if (hitCooldown <= 0)
		hitCooldown = 0;
	flashTimer--;
	if (flashTimer <= 0)
		flashTimer = 0;
	landingTimer--;
	if (landingTimer <= 0)
		landingTimer = 0;

	if (!attacking)
		attackBox = { 0, 0, 0, 0 };

	if (whipLevel == 1 || whipLevel == 2)
		whipLength = 44;
	else if (whipLevel == 3)
		whipLength = 76;

	//Set flip for animations
	if (!onStairs && hitCooldown <= 60)
	{
		if (velX > 0)
			flip = false;
		if (velX < 0)
			flip = true;
	}

	//Apply physics and set animations for when not on stairs
	if (!goToStairs && !onStairs)
	{
		if (jumping)
		{

			jumpTime--;
			if (jumpTime <= 30)
			{				
				velY += 0.7f;

				if (!attacking && !damaged)
					currentAnimation = IDLE;

				else if (!attacking && damaged)
					currentAnimation = DAMAGED;
			}			
		}

		if (attacking && !throwing)
		{
			attackTimer--;
			if (attackTimer <= 36)
			{
				attacking = false;
				currentAnimation = IDLE;
			}

			if (attackTimer <= 48)
			{
				if (!flip)
				{
					if (!crouching || jumping)
					{
						attackBox.x = playerBox.x + PLAYER_WIDTH + 12;
						attackBox.y = playerBox.y + (PLAYER_HEIGHT / 2) - 16;
						attackBox.w = whipLength;
						attackBox.h = 16;
					}
					else
					{
						attackBox.x = playerBox.x + PLAYER_WIDTH + 12;
						attackBox.y = playerBox.y + (PLAYER_HEIGHT / 2);
						attackBox.w = whipLength;
						attackBox.h = 16;
					}
				}
				else
				{
					if (!crouching || jumping)
					{
						attackBox.x = playerBox.x - (12 + whipLength);
						attackBox.y = playerBox.y + (PLAYER_HEIGHT / 2) - 16;
						attackBox.w = whipLength;
						attackBox.h = 16;
					}
					else
					{
						attackBox.x = playerBox.x - (12 + whipLength);
						attackBox.y = playerBox.y + (PLAYER_HEIGHT / 2);
						attackBox.w = whipLength;
						attackBox.h = 16;
					}
				}
			}
		}	
		else if (throwing && attacking)
		{
			attackTimer--;
			if (attackTimer <= 30)
			{				
				throwing = false;
				attacking = false;
				currentAnimation = IDLE;
			}
		}

		if (!jumping && !attacking && !crouching)
		{
			velY += 0.7f;

			if (rightPress && !leftPress)
			{
				velX = MoveSpeed;
				currentAnimation = MOVING;
			}

			if (!rightPress && leftPress)
			{
				velX = 0 - MoveSpeed;
				currentAnimation = MOVING;
			}

			if (!rightPress && !leftPress)
			{
				velX = 0;
				currentAnimation = IDLE;
			}

			if (rightPress && leftPress)
			{
				velX = 0;
				currentAnimation = IDLE;
			}
		}

		if (crouching && !attacking && !jumping)
		{
			velX = 0;
			currentAnimation = CROUCHING;
		}
		velY += 0.3f;

		if (autoWalk)
		{
			velX = 0;
			playerBox.x += 1;
			currentAnimation = MOVING;
		}

		playerBox.x += velX;	

		//If the player reaches a wall
		if ((playerBox.x < 0) || (playerBox.x + PLAYER_WIDTH > levelWidth) || touchesWall(playerBox, tiles) || touchesDestroyableObject(playerBox, objects))
		{
			//Move back
			playerBox.x -= velX;
		}

		//Move the player up or down
		playerBox.y += (int)round(velY);

		//If the player touches floor or roof
		if ((playerBox.y < 0) || (playerBox.y + PLAYER_HEIGHT > levelHeight) || touchesWall(playerBox, tiles) || 
			touchesLevelObject(playerBox, levelObjects) || touchesDestroyableObject(playerBox, objects))
		{			

			//Move back
			playerBox.y -= (int)round(velY);			

			if (velY > 15 && !damaged && !attacking && !jumping)
			{							
				velX = 0;
				landingTimer = 20;
			}

			jumping = false;
			onGround = true;

			if (landingTimer <= 18 && landingTimer > 0)
				crouching = true;

			velY = 0;
			if (attacking)
				velX = 0;

			if (damaged && !playerDead)
			{
				crouching = true;
				flashTimer = 80;
				hitCooldown = 80;
				damaged = false;
			}

			if (health <= 0)			
				playerDeath();			
				
			if (flashTimer <= 60 && landingTimer <= 0)
				if (!downPress)
					crouching = false;			


		}
		else
			onGround = false;
	}
	
	//Apply physics and set animatons for when on stairs
	if (onStairs)
		goToStairs = false;

	if (goToStairs)
	{		
		moveToStairs(playerBox, tiles);
	}	

	//Check to move into position to go down stairs
	if (crouching && touchesStairs(playerBox, tiles, true))
	{
		currentAnimation = MOVING;
		moveToStairsDown(playerBox, tiles);
	}

	//Movement for when on stairs, different for right and left stairs
	if (onStairs && !attacking)
	{
		secondStep--;

		if (rightStair)
		{
			if (secondStep == 0 && stepUp)
			{				
				takeStep(true, true);
				stepFrame = 0;				
			}

			if (secondStep == 0 && !stepUp)
			{				
				takeStep(false, false);
				stepFrame = 0;				
			}

			if (upPress && !downPress && !leftPress || rightPress && !leftPress && !downPress)
			{
				currentAnimation = STAIRS_UP;
				flip = false;				
				stepTime++;		

				if (stepTime % 16 == 0)
				{
					secondStep = 8;
					stepUp = true;
					takeStep(true, true);
					stepFrame = 1;	
				}					
			}		
			if (!upPress && downPress && !rightPress || leftPress && !rightPress && !upPress)
			{				
				currentAnimation = STAIRS_DOWN;
				flip = true;
				stepTime++;

				if (stepTime % 16 == 0)
				{
					secondStep = 8;
					stepUp = false;
					takeStep(false, false);
					stepFrame = 1;
				}			
			}						
		}
		if (leftStair)
		{
			if (secondStep == 0 && stepUp)
			{
				stepFrame++;
				takeStep(true, false);
				if (stepFrame >= 2)
					stepFrame = 0;
			}

			if (secondStep == 0 && !stepUp)
			{
				stepFrame++;
				takeStep(false, true);
				if (stepFrame >= 2)
					stepFrame = 0;
			}

			if (upPress && !downPress && !rightPress || leftPress && !rightPress && !downPress)
			{
				currentAnimation = STAIRS_UP;
				flip = true;
				stepTime++;		
				stepUp = true;

				if (stepTime % 16 == 0)
				{
					secondStep = 8;
					stepFrame++;
					takeStep(true, false);
					if (stepFrame >= 2)
						stepFrame = 0;
				}			
			}
			if (!upPress && downPress && !leftPress || rightPress && !leftPress && !upPress)
			{
				currentAnimation = STAIRS_DOWN;
				flip = false;
				stepTime++;
				stepUp = false;

				if (stepTime % 16 == 0)
				{		
					secondStep = 8;
					stepFrame++;
					takeStep(false, true);
					if (stepFrame >= 2)
						stepFrame = 0;
				}		
			}
		}		
	}

	if (onStairs && attacking && !throwing)
	{
		attackTimer--;

		if (rightStair)
		{			
			if (attackTimer <= 36)
			{
				attacking = false;
				
				if (!flip)
					currentAnimation = STAIRS_UP;
				else if (flip)
					currentAnimation = STAIRS_DOWN;
			}

			if (attackTimer <= 48)
			{
				if (!flip)
				{
					attackBox.x = playerBox.x + PLAYER_WIDTH + 12;
					attackBox.y = playerBox.y + (PLAYER_HEIGHT / 2) - 16;
					attackBox.w = 44;
					attackBox.h = 16;
				}
				else if (flip)
				{
					attackBox.x = playerBox.x - 56;
					attackBox.y = playerBox.y + (PLAYER_HEIGHT / 2) - 16;
					attackBox.w = 44;
					attackBox.h = 16;
				}				
			}
		}	
		if (leftStair)
		{
			if (attackTimer <= 36)
			{
				attacking = false;

				if (!flip)
					currentAnimation = STAIRS_DOWN;
				else if (flip)
					currentAnimation = STAIRS_UP;
			}

			if (attackTimer <= 48)
			{
				if (!flip)
				{
					attackBox.x = playerBox.x + PLAYER_WIDTH + 12;
					attackBox.y = playerBox.y + (PLAYER_HEIGHT / 2) - 16;
					attackBox.w = 44;
					attackBox.h = 16;
				}
				else if (flip)
				{
					attackBox.x = playerBox.x - 56;
					attackBox.y = playerBox.y + (PLAYER_HEIGHT / 2) - 16;
					attackBox.w = 44;
					attackBox.h = 16;
				}
			}
		}
	}
	else if (onStairs && throwing && attacking)
	{
		attackTimer--;
		if (attackTimer <= 36)
		{
			throwing = false;
			attacking = false;
			currentAnimation = STAIRS_UP;
		}
	}
	
	//Stop stair movement if player reaches top of stairs
	if (onStairs && !touchesStairs(playerBox, tiles, false))
	{
		if (rightStair)
		{
			onStairs = false;				
			playerBox.x -= 8;
		}
		if (leftStair)
		{
			onStairs = false;				
			playerBox.x += 8;
		}
		
	}

	//Stop stair movement when player reaches floor
	if (onStairs && touchesWall(playerBox, tiles))
	{
		if (rightStair)
		{
			if (downPress || leftPress)
			{
				onStairs = false;				
				playerBox.y -= 8;	
				playerBox.x += 8;				
			}
		}	
		if (leftStair)
		{
			if (downPress || rightPress)
			{
				onStairs = false;				
				playerBox.y -= 8;	
				playerBox.x -= 8;				
			}
		}
	}
	if (onStairs)
	{
		if (cameraStart + cameraStep < playerBox.x)
		{
			cameraStep++;
		}
		else if (cameraStart + cameraStep > playerBox.x)
		{
			cameraStep--;
		}

		if (touchesExit(playerBox, tiles, true))
		{
			reachedExitStair = true;
		}

		if (touchesExit(playerBox, tiles, false))
		{
			reachedExitStair2 = true;
		}

		if (touchesEntrance(playerBox, tiles, true))
		{
			reachedEntrance = true;
		}

		if (touchesEntrance(playerBox, tiles, false))
		{
			reachedEntrance2 = true;
		}
	}
}