コード例 #1
0
ファイル: CPlayer.cpp プロジェクト: SweetAlmighty/SFML-Game
void CPlayer::Update(void)
{
	// Jump, if capable.
	if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && onGround && !isJumping)
	{
		// higher value = higher jump
		playerPos = playerVel;
		isJumping = true;
	}

	// Move player up, if they are jumping.
	if (isJumping && onGround)
	{
		playerVel.y -= jumpSpeed;

		if (playerVel.y <= (playerPos.y - 50.0f))
			onGround = false;
	}

	// move left/right
	if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) // Left
	{
		playerVel.x -= moveSpeed;  // higher value = faster accelleration

		if (playerVel.x < -topSpeed)  // higher value = higher top speed
			playerVel.x = -topSpeed;
	}
	if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) // Right
	{
		playerVel.x += moveSpeed;

		if (playerVel.x > topSpeed)
			playerVel.x = topSpeed;
	}

	// Apply gravitational forces to player.
	if (!onGround)
		playerVel.y += playerGravity;  // higher value = more gravity

	// Slow the player down if they are not moving.
	if (!sf::Keyboard::isKeyPressed(sf::Keyboard::A) && !sf::Keyboard::isKeyPressed(sf::Keyboard::D) && !isJumping)
	{
		if (playerVel.x < 0)
		{
			playerVel.x += slowDown;  // higher value = quicker slowdown

			if (playerVel.x > 0)  // stop
				playerVel.x = 0;
		}
		else
		{
			playerVel.x -= slowDown;

			if (playerVel.x < 0)
				playerVel.x = 0;
		}
	}

	// Apply terminal velocity on player.
	if (playerVel.y > terminalVel)
		playerVel.y = terminalVel;  // the highest speed at which you can fall

	// Move Player.
	Move(playerVel.x, playerVel.y);

	KeepInBounds(sf::Vector2f(800, 600));
}
コード例 #2
0
ファイル: Player.cpp プロジェクト: JonnyIrl/AI
void Player::Update(float time, sf::Time animationTime)
{
	accelertation = 0;
	if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
	{
		// left key is pressed: move our character
		Rotate(-2, time);
	}
	if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
	{
		// left key is pressed: move our character
		Rotate(2, time);
	}
	if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
	{
		if (m_upPressedOnce)
			m_upPressedOnce = true;

		//If the player has the speed powerup
		if (speedPowerUpActive)
		{
			if (speed < max_Speed + 150)
			{
				accelertation = accerationRate + 50;
			}

			else
			{
				speed = max_Speed + 150;
			}

			if (m_playerAnimation.getAnimation() != &m_pMoveAnimation)
				m_playerAnimation.setAnimation(m_pMoveAnimation);
			//Used to keep the image on the last thruster update
			if (m_playerAnimation.m_currentFrame == lastframe)
			{
				m_playerAnimation.setFrame(lastframe);
				if (playerDecelleration)
					playerDecelleration = false;
			}

			else
			{
				m_playerAnimation.update(animationTime);
			}

		}

		else
		{

			if (speed < max_Speed)
			{
				accelertation = accerationRate;
			}
			else
			{
				speed = max_Speed;
			}

			if (m_playerAnimation.getAnimation() != &m_pMoveAnimation)
				m_playerAnimation.setAnimation(m_pMoveAnimation);
			//Used to keep the image on the last thruster update
			if (m_playerAnimation.m_currentFrame == lastframe)
			{
				m_playerAnimation.setFrame(lastframe);
				if (playerDecelleration)
					playerDecelleration = false;
			}

			else
			{
				m_playerAnimation.update(animationTime);
			}
		}
		
	}

	//Check for when the up key is not pressed then play the same animation but in reverse essentially.
	if (!sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && !playerDecelleration)
	{
		PlayDecellerationAnimation(animationTime);
	}


	if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
	{
		if (speed > 0)
		{
			accelertation = -accerationRate;
		}
	}
	if (readyToFire)
	{
		if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
		{
			if (speedPowerUpActive)
			{
				BulletManager::GetInstance()->AddPlayerBullet(m_Position + m_Direction*35.0f, rotation, bulletSpeed + 800, 3);
				readyToFire = false;
				timeSinceFire = 0;
			}

			else
			{
				BulletManager::GetInstance()->AddPlayerBullet(m_Position + m_Direction*35.0f, rotation, bulletSpeed, 3);
				readyToFire = false;
				timeSinceFire = 0;
			}
		}
		
	}
	else 
	{
		timeSinceFire += time;
		if (timeSinceFire >= fireDelay)
		{
			readyToFire = true;
		}
	}
	


	m_Position += m_Direction * speed * time;
	KeepInBounds();
	//s=ut+(1/2)a(t(t))
	m_Position += m_Direction * speed * time + m_Direction * 0.5f* accelertation * time *time;
	//v=u+at
	speed += accelertation* time;
	if (speed > 0)
	{
		speed += friction* time;
	}
	//KeepInBounds();
	sprite.setPosition(m_Position);
	playerRect.setPosition(sf::Vector2f(m_Position.x, m_Position.y));
	playerRect.setRotation(rotation);
	m_playerAnimation.setPosition(m_Position);
	//set the camera
	Camera::GetInstance()->setViewPosition(m_Position);
	HUD::GetInstance()->setViewPosition(sf::Vector2f(250, 400));
	//healthRectangle.setPosition(sf::Vector2f(Camera::GetInstance()->getViewPosition().x - 700, Camera::GetInstance()->getViewPosition().y - 700));
	//MiniMap::GetInstance()->setViewPosition(m_Position);


	//Checks for the powerups.
	if (speedPowerUpActive)
	{
		speedTimer = speedClock.getElapsedTime().asSeconds();

		if (speedTimer >= 10)
		{
			speedPowerUpActive = false;
		}
	}
	if (playerHealth <= 0)
	{
		alive = false;

	}
}