-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity.h
165 lines (126 loc) · 3.21 KB
/
entity.h
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#pragma once
#include <SFML/Graphics.hpp>
#include <math.h>
// Entity is the base element of the gameplay. It is a Sprite on steroids with defined behaviour for collisons, and the flow of time.
class Entity {
protected:
bool dead = false;
bool active = true;
int score = 0;
std::string type;
float maxLife, health, damage, rotation, scale, mass = 1;
std::vector<Entity*> *renderQueue;
sf::Sprite sprite;
sf::Texture texture;
sf::Vector2f position, velocity;
sf::Clock animationTimer;
sf::Time animationTime;
sf::Clock cooldownTimer;
sf::Time cooldownTime;
sf::Clock lifeTimer;
sf::Time pauseTime;
public:
Entity(std::string _type, float _health, float _damage, sf::Vector2f _position, float _rotation, std::string _texture, float _scale, std::vector<Entity*> *_renderQueue) {
type = _type;
maxLife = _health;
health = _health;
damage = _damage;
position = _position;
renderQueue = _renderQueue;
scale = _scale;
rotation = _rotation;
lifeTimer.restart();
texture.loadFromFile(_texture);
sprite.setTexture(texture);
sprite.setOrigin(texture.getSize().x /2, texture.getSize().y /2);
sprite.setScale(_scale, _scale);
sprite.setPosition(_position);
sprite.setRotation(_rotation);
}
virtual ~Entity() {
}
virtual void animate() {};
virtual void destroy() {};
virtual void collide(Entity *entity) {};
void draw(sf::RenderTarget* target) {
if(active == true) {
cooldownTime = cooldownTimer.getElapsedTime() - pauseTime;
animationTime = animationTimer.getElapsedTime() - pauseTime;
pauseTime = pauseTime.Zero;
animate();
} else {
animationTimer.restart();
}
target->draw(sprite);
}
void resume() {
pauseTime = animationTimer.getElapsedTime();
active = true;
}
void pause() {
active = false;
}
void stop() {
dead = true;
active = false;
}
float distanceTo(sf::Vector2f distant) {
position = sprite.getPosition();
return sqrt(pow(distant.x - position.x, 2) + pow(position.y - distant.x, 2));
}
void attack(Entity *enemy) {
health -= enemy->getDamage();
//std::cout << health << " - " << _damage << " = " << health - _damage << std::endl;
if (health <= 0) {
enemy->addPoint(maxLife);
destroy();
stop();
}
}
void heal(float heal) {
health += heal;
}
float getDamage() {
return damage;
}
float getLife() {
return health;
}
float getTime() {
return lifeTimer.getElapsedTime().asMilliseconds();
}
bool isDead() {
return dead;
}
sf::Vector2f getPosition() {
return sprite.getPosition();
}
sf::Vector2f getVelocity() {
return velocity;
}
void addVelocity(sf::Vector2f _velocity) {
velocity += _velocity;
}
float getRotation() {
return sprite.getRotation();
}
float getMass() {
return mass;
}
sf::Rect<float> getHitbox() {
return sprite.getGlobalBounds();
}
std::string getType() {
return type;
}
void addPoint(int points) {
score += points;
}
int getScore() {
return score;
}
void lookAt(sf::Vector2f point) {
position = sprite.getPosition();
rotation = (atan2(point.y - position.y, point.x - position.x)) * 180 / 3.14159265;
}
};