#include "object.h" #include "player.h" #include #include #include #include "gun.h" #define PI 3.14159265 using namespace std; Player :: Player (const Point &point, const Velocity &velocity, sf::Color &color, const Gun &gun) : Object(point, velocity) { setWidth(100); setHeight(150); setHealth(100); setColor(color); setGun(gun); sf::Texture playerTexture; playerTexture.loadFromFile("sprites/player.png"); setTexture(playerTexture); this->sprite.setTexture(this->texture); this->sprite.setColor(color); this->sprite.setTextureRect(sf::IntRect(0,0,100,150)); // 1st frame this->sprite.setOrigin(sf::Vector2f(50,75)); setSprite(this->sprite); } void Player :: setGun(const Gun &gun) { this->gun = gun; } void Player :: setHealth(const float health) { this->health = health; } void Player :: setColor(const sf::Color &color) { this->color = color; } sf::Color Player :: getColor() { return this->color; } float Player :: getHealth() { return this->health; } Gun Player :: getGun() { return this->gun; } void Player :: draw(sf::RenderWindow &window) { // Draw a player to the window this->sprite.setPosition(sf::Vector2f(this->point.getX(),this->point.getY())); float ang = -180.0f / PI * this->getAngle(); int frame; if (ang > -30 && ang <= 30) { frame = 4; } else if (ang > 30 && ang <= 60) { frame = 0; } else if (ang > 60 && ang <= 120) { frame = 1; } else if (ang > 120 && ang <= 150) { frame = 2; } else if (ang > 150 || ang <= -150) { frame = 3; } else if (ang > -150 && ang <= -120) { frame = 7; } else if (ang > -120 && ang <= -60) { frame = 6; } else if (ang > -60 && ang <= -30) { frame = 5; } window.draw(this->sprite); sf::Sprite gunSprite = this->gun.getSprite(); gunSprite.setRotation(-ang); gunSprite.setPosition(sf::Vector2f(this->point.getX(),this->point.getY() + 40)); this->gun.setSprite(gunSprite); window.draw(this->gun.getSprite()); this->sprite.setTextureRect(sf::IntRect((frame % 3) * this->getWidth(), (frame / 3) * this->getHeight(), this->getWidth(), this->getHeight())); }