-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntity.cpp
More file actions
144 lines (122 loc) · 3.75 KB
/
Copy pathEntity.cpp
File metadata and controls
144 lines (122 loc) · 3.75 KB
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
#include "Entity.h"
void Entity::initVariables()
{
this->hitboxComponent = nullptr;
this->movementComponent = nullptr;
this->animationComponent = nullptr;
this->death = false;
this->invincible = false;
}
Entity::Entity()
{
this->initVariables();
}
Entity::~Entity()
{
delete this->hitboxComponent;
delete this->movementComponent;
delete this->animationComponent;
delete this->HPbar;
}
//Component function
//===============================================================================================
void Entity::setTexture(sf::Texture& texture)
{
this->sprite.setTexture(texture);
}
void Entity::createHitboxComponent(sf::Sprite& sprite,float offset_x,float offset_y,float width,float height)
{
this->hitboxComponent = new HitboxComponent(sprite, offset_x, offset_y, width, height);
}
void Entity::createMovementComponent(const float maxVelocity,const float acceleration,const float deceleration)
{
this->movementComponent = new MovementComponent(this->sprite, this->hitboxComponent->getHitbox(),maxVelocity,acceleration,deceleration);
}
void Entity::createAnimationComponent( sf::Texture& textureSheet, sf::Vector2f scale, int imgx, int imgy)
{
this->animationComponent = new AnimationComponent(this->sprite, textureSheet, scale,imgx,imgy);
}
void Entity::initfont()
{
if (!this->font.loadFromFile("Fonts/Minecraft.ttf")) {
throw("ERROR::MAINMENUSTATE::COULD NOT LOAD FONT");
}
}
void Entity::createHpbar(float &x,float &y,sf::Vector2f scale)
{
this->HPbar = new gui::bar(x, y, 10*scale.x, 10, this->stat["HP"], this->stat["MAXHP"], &this->font, 26, sf::Color::Black, sf::Color::Red, sf::Color::White);
}
//========================================================================================
const sf::Vector2f &Entity::getPosition() const
{
return this->sprite.getPosition();
}
const sf::Vector2f Entity::getCenter() const
{
if (this->hitboxComponent)
return this->hitboxComponent->getHitbox()->getPosition() +
sf::Vector2f(
this->hitboxComponent->getHitbox()->getGlobalBounds().width / 2.f,
this->hitboxComponent->getHitbox()->getGlobalBounds().height / 2.f);
return this->sprite.getPosition() +
sf::Vector2f(
this->sprite.getGlobalBounds().width / 2.f,
this->sprite.getGlobalBounds().height / 2.f);
}
const float Entity::getstat(std::string key)
{
return this->stat[key];
}
const bool Entity::checkDeath()
{
if (this->stat["HP"]<=0) {
this->death = true;
return true;
}
return false;
}
const bool Entity::Invincible()
{
return this->invincible;
}
const sf::FloatRect Entity::getHitboxGlobalbound() const
{
if (this->hitboxComponent) return this->hitboxComponent->getHitbox()->getGlobalBounds();
else return sf::FloatRect(0, 0, 0, 0);
}
bool Entity::intersect(const sf::FloatRect& frect)
{
if(this->hitboxComponent) return this->hitboxComponent->checkIntersect(frect);
return false;
}
//=======================================================================================
void Entity::setstat(std::string key, float value)
{
this->stat[key] = value;
if(!key.compare("HP")) this->HPbar->setValue(value);
else if(!key.compare("MAXHP")) this->HPbar->setMaxValue(value);
if (this->stat[key] <= 0) this->stat[key] = 0;
}
void Entity::spriteSetPosition(const float x, const float y)
{
this->sprite.setPosition(x,y);
}
//================================================================================
//function
void Entity::move(const float x, const float y,const float &deltaTime)
{
if (this->movementComponent) {
this->movementComponent->move(x, y, deltaTime);
}
}
void Entity::Update(const float& deltaTime)
{
}
void Entity::Render(sf::RenderTarget& target, sf::Shader* shader, bool showhitbox)
{
target.draw(this->sprite);
if (this->hitboxComponent && showhitbox)
hitboxComponent->Render(target);
if (this->HPbar)
HPbar->Render(target);
}