Advertisement
Guest User

Untitled

a guest
Oct 14th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.83 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <SFML/System.hpp>
  3. #include <cmath>
  4. #include "player.h"
  5.  
  6. class Player{
  7.     private:
  8.         sf::RenderWindow& window;
  9.         sf::Texture playerTexture;
  10.     sf::Sprite playerSprite;
  11.     public:
  12.         float x = 0, y = 0;
  13.         float velocityX = 0, velocityY = 0;
  14.         float currentVelocity = 0;
  15.         float maxVelocity = 8;
  16.    
  17.     //functions
  18.     Player(sf::RenderWindow& w, int x, int y): window(w)
  19.     {
  20.         this->x = x;
  21.         this->y = y;
  22.  
  23.         this->playerTexture.loadFromFile("Sprites/ship.png");        
  24.         this->playerSprite.setTexture(playerTexture);
  25.         this->playerSprite.setPosition(this->x,this->y);
  26.         this->playerSprite.setOrigin(this->playerSprite.getLocalBounds().width / 2, this->playerSprite.getLocalBounds().height / 2);
  27.     }
  28.  
  29.     ~Player(){
  30.        
  31.     }
  32.  
  33.     void AddXVelocity(float v){
  34.         velocityX += v;
  35.     };
  36.  
  37.     void AddYVelocity(float v){
  38.         velocityY += v;
  39.     };
  40.  
  41.     void NormalizeVelocity(){
  42.         currentVelocity = fabs(velocityX) + fabs(velocityY);
  43.     if(currentVelocity > maxVelocity){
  44.         //percentage that each axis is the of the total velocity
  45.         int xp = fabs(velocityX) / (fabs(velocityX) + fabs(velocityY));
  46.         int yp = fabs(velocityY) / (fabs(velocityX) + fabs(velocityY));
  47.        
  48.         //different normalization if value is negative
  49.         if(velocityX > 0){
  50.             velocityX = maxVelocity * xp;
  51.         }
  52.         else if(velocityX < 0){
  53.             velocityX = -maxVelocity * xp;
  54.         }
  55.         if(velocityY < 0){
  56.             velocityY = -maxVelocity * yp;
  57.         }
  58.         else if(velocityY > 0){
  59.             velocityY = maxVelocity * yp;
  60.         }
  61.     }
  62.         x += velocityX;
  63.     y -= velocityY;
  64.     }
  65.  
  66.     void GetInput(){
  67.         //get keyboard input
  68.         if(sf::Keyboard::isKeyPressed(sf::Keyboard::W)){
  69.             AddYVelocity(.1);
  70.         }
  71.         if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)){
  72.             AddYVelocity(-.1);
  73.         }
  74.         if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)){
  75.             AddXVelocity(-.1);
  76.         }
  77.         if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)){
  78.             AddXVelocity(.1);
  79.         }
  80.     }
  81.  
  82.     void DrawPlayer(){
  83.         window.draw(playerSprite);
  84.     }
  85.  
  86.     void LookAtMouse(){
  87.     //get mouse coords
  88.         sf::Vector2f mouseCoords;
  89.         mouseCoords = window.mapPixelToCoords(sf::Mouse::getPosition(window));
  90.         int mouseX = mouseCoords.x;
  91.         int mouseY = mouseCoords.y;
  92.  
  93.     //keep the player looking at the mouse
  94.         float dx = mouseX - x;
  95.         float dy = mouseY - y;
  96.         float rotation = (atan2(dy, dx) * 180 / 3.14159265);
  97.         playerSprite.setRotation(rotation + 90);
  98.     }
  99.  
  100.     void Update(){
  101.         GetInput();
  102.     NormalizeVelocity();
  103.     LookAtMouse();
  104.         playerSprite.setPosition(x,y);
  105.     DrawPlayer();
  106.     }
  107. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement