Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. #include "Character.h"
  2.  
  3. Character::Character(float xPos, float yPos, float width, float height, sf::Color color)
  4. :movable(true)
  5. {
  6. this->rectShape.setPosition(xPos, yPos);
  7. this->rectShape.setSize(sf::Vector2f(width, height));
  8. this->rectShape.setFillColor(color);
  9. }
  10. float Character::getXUpperLeftCorner()
  11. {
  12. return this->rectShape.getPosition().x;
  13. }
  14. float Character::getYUpperLeftCorner()
  15. {
  16. return this->rectShape.getPosition().y;
  17. }
  18. float Character::getWidth()
  19. {
  20. return this->rectShape.getGlobalBounds().width;
  21. }
  22. float Character::getHeight()
  23. {
  24. return this->rectShape.getGlobalBounds().height;
  25. }
  26. void Character::setMovable(bool moveable)
  27. {
  28. this->movable = movable;
  29. }
  30. void Character::move()
  31. {
  32. if (this->movable)
  33. {
  34. if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
  35. {
  36. if (rectShape.getPosition().x > 0)
  37. this->rectShape.move(-10.f, 0.f);
  38. }
  39. else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
  40. {
  41. if (rectShape.getPosition().x < 720)
  42. this->rectShape.move(10.f, 0.f);
  43. }
  44. else if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
  45. {
  46. if (rectShape.getPosition().y > 0)
  47. this->rectShape.move(0.f, -10.f);
  48. }
  49. else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
  50. {
  51. if (rectShape.getPosition().y < 480)
  52. this->rectShape.move(0.f, 10.f);
  53. }
  54. }
  55. }
  56.  
  57. void Character::draw(sf::RenderTarget& target, sf::RenderStates states) const
  58. {
  59. target.draw(this->rectShape);
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement