summaryrefslogtreecommitdiff
path: root/src/player.cpp
blob: e99ea43c7c7987255d9d58e77a76f60cba0fc468 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "object.h"
#include "player.h"
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <bits/stdc++.h>
#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()));
}