Untitled


SUBMITTED BY: Guest

DATE: Sept. 15, 2013, 8:06 p.m.

FORMAT: C++

SIZE: 2.0 kB

HITS: 1089

  1. void Player::tick()
  2. {
  3. // Collision Detection
  4. bool TouchingBottom = false;
  5. for (int i = 0;i < m_scene->GetEntityAmount();i++)
  6. {
  7. if (m_scene->GetEntity(i) != this)
  8. {
  9. //Bottom Check
  10. if (m_char.getGlobalBounds().top + m_char.getGlobalBounds().height + getPosition().y >= m_scene->GetEntity(i)->GetGlobalBounds().top
  11. && m_char.getGlobalBounds().left + m_char.getGlobalBounds().width + getPosition().x >= m_scene->GetEntity(i)->GetGlobalBounds().left
  12. && m_char.getGlobalBounds().left + getPosition().x <= m_scene->GetEntity(i)->GetGlobalBounds().left + m_scene->GetEntity(i)->GetGlobalBounds().width
  13. && m_char.getGlobalBounds().top + m_char.getGlobalBounds().height + getPosition().y <= m_scene->GetEntity(i)->GetGlobalBounds().top + m_scene->GetEntity(i)->GetGlobalBounds().height
  14. )
  15. {
  16. TouchingBottom = true;
  17. }
  18. }
  19. }
  20. // Player Input
  21. if (Game::Get()->GetInputManager()->IsKeyDown(sf::Keyboard::Key::Up) && TouchingBottom)
  22. SetVelocity(GetVelocity().x, -50);
  23. if (Game::Get()->GetInputManager()->IsKeyPressed(sf::Keyboard::Key::Left))
  24. SetVelocity(-20, GetVelocity().y);
  25. else if (Game::Get()->GetInputManager()->IsKeyPressed(sf::Keyboard::Key::Right))
  26. SetVelocity(20, GetVelocity().y);
  27. else
  28. SetVelocity(0, GetVelocity().y);
  29. // Apply collision precautions
  30. if (!TouchingBottom)
  31. SetVelocity(GetVelocity().x, GetVelocity().y+10*Game::Get()->GetFrameTime()->asSeconds()*10); // Apply gravity
  32. else if (TouchingBottom && GetVelocity().y > 0)
  33. SetVelocity(GetVelocity().x, 0);
  34. // Apply frame movement
  35. move(GetVelocity().x * Game::Get()->GetFrameTime()->asSeconds()*10, GetVelocity().y * Game::Get()->GetFrameTime()->asSeconds()*10);
  36. }

comments powered by Disqus