Facebook
From valva-ro, 1 Month ago, written in C++.
Embed
Download Paste or View Raw
Hits: 44
  1. #include <SFML/System.hpp>
  2. #include <SFML/Graphics.hpp>
  3. #include "Maze.hpp"
  4.  
  5. class Player {
  6.  
  7. private:
  8.     sf::Sprite player;
  9.     sf::Vector2f position;
  10.     sf::Texture texture;
  11.     float speed;
  12.  
  13. public:
  14.     Player(sf::Color color, const std::string &texturePath, float speed, float scale, sf::Vector2f position = sf::Vector2f(0,0));
  15.     sf::Rect<float> getGlobalBounds();
  16.     sf::Vector2f getPosition();
  17.     void setPosition(sf::Vector2f position);
  18.     void draw(sf::RenderWindow *window);
  19.     void move(Maze *maze, float deltaSpeed = 1);
  20.     bool locationAllowed(Maze *maze, sf::Vector2f position, const char &direction);
  21. };
  22.  
  23. const char UP = 'U';
  24. const char DOWN = 'D';
  25. const char LEFT = 'L';
  26. const char RIGHT = 'R';
  27.  
  28. Player:: Player(sf::Color color, const std::string &texturePath, float speed, float scale, sf::Vector2f position) {
  29.     if (this->texture.loadFromFile(texturePath)) {
  30.         this->player.setTexture(texture, true);
  31.     }
  32.     this->player.setColor(color);
  33.     this->player.setScale(sf::Vector2<float>(scale, scale));
  34.     this->position = position;
  35.     this->speed = speed;
  36. }
  37.  
  38. sf::Vector2f Player::getPosition() {
  39.     return this->player.getPosition();
  40. }
  41.  
  42. void Player::setPosition(sf::Vector2f position) {
  43.     this->player.setPosition(position);
  44.     this->position = position;
  45. }
  46.  
  47. sf::Rect<float> Player::getGlobalBounds() {
  48.     return this->player.getGlobalBounds();
  49. }
  50.  
  51. void Player::draw(sf::RenderWindow *window) {
  52.     window->draw(this->player);
  53. }
  54.  
  55. void Player::move(Maze *maze, float deltaTime) {
  56.  
  57.     if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && locationAllowed(maze, position, UP)) {
  58.         this->position += sf::Vector2f(0, -speed * deltaTime);
  59.         this->player.move(0, -speed * deltaTime);
  60.     }
  61.  
  62.     else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) && locationAllowed(maze, position, DOWN)) {
  63.         this->position += sf::Vector2f(0, speed * deltaTime);
  64.         this->player.move(0, speed * deltaTime);
  65.     }
  66.  
  67.     else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && locationAllowed(maze, position, LEFT)) {
  68.         this->position += sf::Vector2f(-speed * deltaTime, 0);
  69.         this->player.move(-speed * deltaTime, 0);
  70.     }
  71.  
  72.     else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) && locationAllowed(maze, position, RIGHT)) {
  73.         this->position += sf::Vector2f(speed * deltaTime, 0);
  74.         this->player.move(speed * deltaTime, 0);
  75.     }
  76. }
  77.  
  78. bool Player:: locationAllowed(Maze *maze, sf::Vector2f position, const char &direction) {
  79.  
  80.     bool allowed = false;
  81.     Cell *aux;
  82.  
  83.     switch (direction) {
  84.         case UP:
  85.             position += sf::Vector2f(0.f, 1.f);
  86.             aux = maze->getCell(position);
  87.             if (aux)
  88.                 allowed = (aux->getActiveWalls() != UP_WALL);
  89.             break;
  90.         case DOWN:
  91.             position += sf::Vector2f(0.f, -1.f);
  92.             aux = maze->getCell(position);
  93.             if (aux)
  94.                 allowed = (aux->getActiveWalls() != DOWN_WALL);
  95.             break;
  96.         case LEFT:
  97.             position += sf::Vector2f(-1.f, 0.f);
  98.             aux = maze->getCell(position);
  99.             if (aux)
  100.                 allowed = (aux->getActiveWalls() != LEFT_WALL);
  101.             break;
  102.         case RIGHT:
  103.             position += sf::Vector2f(1.f, 0.f);
  104.             aux = maze->getCell(position);
  105.             if (aux)
  106.                 allowed = (aux->getActiveWalls() != RIGHT_WALL);
  107.             break;
  108.     }
  109.     return allowed;
  110. }
  111.