botgob

Character

Nov 22nd, 2019
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 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.  
  11. float Character::getXUpperLeftCorner()
  12. {
  13. return this->rectShape.getPosition().x;
  14. }
  15.  
  16. float Character::getYUpperLeftCorner()
  17. {
  18. return this->rectShape.getPosition().y;
  19. }
  20.  
  21. float Character::getWidth()
  22. {
  23. return this->rectShape.getGlobalBounds().width;
  24. }
  25.  
  26. float Character::getHeight()
  27. {
  28. return this->rectShape.getGlobalBounds().height;
  29. }
  30.  
  31. void Character::setMovable(bool moveable)
  32. {
  33. this->movable = movable;
  34. }
  35.  
  36. void Character::move()
  37. {
  38. if (this->movable)
  39. {
  40. float movementSpeed = 10;
  41. if (sf::Keyboard::isKeyPressed(sf::Keyboard::A) && getXUpperLeftCorner() >= 0)
  42. {
  43. this->rectShape.move(movementSpeed * -1, 0.f);
  44. }
  45. else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D) && getXUpperLeftCorner() + getWidth() <= 800)
  46. {
  47. this->rectShape.move(movementSpeed, 0.f);
  48. }
  49. else if (sf::Keyboard::isKeyPressed(sf::Keyboard::W) && getYUpperLeftCorner() >= 0)
  50. {
  51. this->rectShape.move(0.f, movementSpeed * -1);
  52. }
  53. else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S) && getYUpperLeftCorner() + getHeight() <= 600)
  54. {
  55. this->rectShape.move(0.f, movementSpeed);
  56. }
  57. }
  58. }
  59.  
  60. void Character::draw(sf::RenderTarget & target, sf::RenderStates states) const
  61. {
  62. target.draw(this->rectShape);
  63. }
Advertisement
Add Comment
Please, Sign In to add comment