Advertisement
Guest User

player.h

a guest
Apr 23rd, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.03 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include "game.h"
  4. #include "entity.h"
  5.  
  6. class Player : public sf::RectangleShape, public Entity {
  7. public:
  8.     Player(const sf::Vector2f &size = sf::Vector2f(0, 0)) : RectangleShape(size), Entity("", 100.0f, 10.0f, 5.0f)
  9.     {
  10.         std::cout << "Inside constructor for Player" << std::endl;
  11.  
  12.         m_Score = 0;
  13.     }
  14.  
  15.     ~Player()
  16.     {
  17.         std::cout << "Inside destructor for Player" << std::endl;
  18.     }
  19.  
  20.    
  21.     sf::Vector2f getMoveDirection() const   { return m_MoveDirection; }
  22.     int getScore() const                    { return m_Score; }
  23.  
  24.     void setMoveDirection(const sf::Vector2f &moveDirection)    { m_MoveDirection = moveDirection; }
  25.     void setMoveDirection(float x, float y)                     { m_MoveDirection.x = x; m_MoveDirection.y = y; }
  26.     void setScore(int score)                                    { m_Score = score; }
  27.  
  28.  
  29.     void checkKeys();
  30.  
  31.     void update();
  32.  
  33. private:
  34.  
  35.     sf::Vector2f m_MoveDirection;
  36.     int m_Score;
  37.  
  38. };
  39.  
  40. void Player::checkKeys() {
  41.     if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
  42.         // up
  43.         setMoveDirection(0, -1.0f);
  44.     }
  45.     else if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
  46.         // left
  47.         setMoveDirection(-1.0f, 0);
  48.     }
  49.     else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
  50.         // down
  51.         setMoveDirection(0, 1.0f);
  52.     }
  53.     else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
  54.         // right
  55.         setMoveDirection(1.0f, 0);
  56.     }
  57.     else {
  58.         setMoveDirection(0, 0);
  59.     }
  60.  
  61.     if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)
  62.         && sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
  63.         // up left
  64.         setMoveDirection(-1.0f, -1.0f);
  65.     }
  66.     else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)
  67.         && sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
  68.         // down left
  69.         setMoveDirection(-1.0f, 1.0f);
  70.     }
  71.     else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)
  72.         && sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
  73.         // down right
  74.         setMoveDirection(1.0f, 1.0f);
  75.     }
  76.     else if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)
  77.         && sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
  78.         // up right
  79.         setMoveDirection(1.0f, -1.0f);
  80.     }
  81. }
  82.  
  83. void Player::update() {
  84.     move(m_MoveDirection * getSpeed());
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement