From f4e392912a3812b602f787c6db30296cea14962e Mon Sep 17 00:00:00 2001 From: Simponic Date: Fri, 6 Nov 2020 11:48:27 -0700 Subject: Added files --- src/player.cpp | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 src/player.cpp (limited to 'src/player.cpp') diff --git a/src/player.cpp b/src/player.cpp new file mode 100644 index 0000000..e99ea43 --- /dev/null +++ b/src/player.cpp @@ -0,0 +1,88 @@ +#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())); +} -- cgit v1.2.3-70-g09d2