-
Notifications
You must be signed in to change notification settings - Fork 0
/
Entity.cpp
208 lines (186 loc) · 6.62 KB
/
Entity.cpp
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "Entity.h"
void Entity::setEntityView()
{
entityView.setCenter(lastPosition.x+10, lastPosition.y+10);
entityView.setSize(int((VideoMode::getDesktopMode().width) * 0.5), int((VideoMode::getDesktopMode().height) * 0.5));
}
Entity::Entity()
{
}
Entity::Entity(sf::Texture _texture, std::vector<std::vector<int>> options)
{
texture = _texture;
setTexture(texture);
healthBar.setHealthBar(health, maxHealth);
animations[AnimationName::Idle] = new Animation(texture, options[0], options.size() > 2 ? options[2] : vector<int>{ 0,0,0,0 });
animations[AnimationName::Walk] = new Animation(texture, options[1], options.size() > 2 ? options[2] : vector<int>{ 0,0,0,0 });
}
void Entity::setEntity(sf::Texture _texture, std::vector<std::vector<int>> options)
{
texture = _texture;
setTexture(texture);
healthBar.setHealthBar(health, maxHealth);
animations[AnimationName::Idle] = new Animation(texture, options[0], options.size() > 2 ? options[2] : vector<int>{ 0,0,0,0 });
animations[AnimationName::Walk] = new Animation(texture, options[1], options.size() > 2 ? options[2] : vector<int>{ 0,0,0,0 });
}
void Entity::Up(float elapsed) { if (!jumping) { jumping = true; jumpVelocity = -jumpSpeed; soundJump.play(); } }
void Entity::Down(float elapsed) { velocity.y += speed * elapsed; }
void Entity::Left(float elapsed) { setScale({ (float)(-1)*abs(getScale().x), getScale().y }); velocity.x += -speed * elapsed; }
void Entity::Right(float elapsed) { setScale({ (float)abs(getScale().x), getScale().y }); velocity.x += speed * elapsed; }
void Entity::Attack(float elapsed, Entity& entity)
{
if (!getIsPlayer())
{
attackTime += elapsed;
if (attackTime > holdAttackTime)
{
soundAttack.play();
state = AnimationName::Attack;
entity.setState(AnimationName::Damage);
entity.TakeDamage(10);
attackTime = 0;
}
}
else
{
soundAttack.play();
}
}
void Entity::TakeDamage(int damage)
{
health -= damage;
if (health <= 0) setState(AnimationName::Death);
soundDamage.play();
}
void Entity::CheckCollisions(const sf::FloatRect *arg)
{
FloatRect playerBounds = getGlobalBounds();
FloatRect argBounds = *arg;
nextPosition = playerBounds;
nextPosition.left += velocity.x;
nextPosition.top += velocity.y;
if (argBounds.intersects(nextPosition))
{
// Bottom collision
if (playerBounds.top < argBounds.top
and playerBounds.top + playerBounds.height < argBounds.top + argBounds.height
and playerBounds.left < argBounds.left + argBounds.width
and playerBounds.left + playerBounds.width > argBounds.left)
{
velocity.y = 0;
setPosition(playerBounds.left + origin.x, argBounds.top - playerBounds.height + origin.y);
if (jumping == true) soundLand.play();
jumping = false;
}
// Top collision
else if (playerBounds.top > argBounds.top
and playerBounds.top + playerBounds.height > argBounds.top + argBounds.height
and playerBounds.left < argBounds.left + argBounds.width
and playerBounds.left + playerBounds.width > argBounds.left)
{
jumpVelocity = 0;
velocity.y = 0;
setPosition(playerBounds.left + origin.x, argBounds.top + argBounds.height + origin.y);
}
// Right collision
if (playerBounds.left < argBounds.left
and playerBounds.left + playerBounds.width < argBounds.left + argBounds.width
and playerBounds.top < argBounds.top + argBounds.height
and playerBounds.top + playerBounds.height > argBounds.top)
{
velocity.x = 0;
setPosition(argBounds.left - playerBounds.width + origin.x, playerBounds.top + origin.y);
}
// Left collision
else if (playerBounds.left > argBounds.left
and playerBounds.left + playerBounds.width > argBounds.left + argBounds.width
and playerBounds.top < argBounds.top + argBounds.height
and playerBounds.top + playerBounds.height > argBounds.top)
{
velocity.x = 0;
setPosition(argBounds.left + argBounds.width + origin.x, playerBounds.top + origin.y);
}
}
}
void Entity::GravityUpdate(float elapsed, float gravity)
{
if (jumpVelocity < 300) jumpVelocity += gravity;
velocity.y += jumpVelocity * elapsed;
}
void Entity::Update(float elapsed)
{
int pom = 200 - playerDistance;
if (pom < 0) pom = 0;
soundStep.setVolume(pom);
soundJump.setVolume(pom);
soundLand.setVolume(pom);
soundAttack.setVolume(pom);
soundDamage.setVolume(pom);
// if (isPlayer) cout << state << endl;
if (isAlive)
{
healingTime += elapsed;
if (getIsPlayer() and healingTime >= 2)
{
healingTime = 0;
Heal(1);
}
if (state == AnimationName::Basic)
{
move(velocity);
if (getPosition() != lastPosition)
{
if (velocity.y == 0)
{
stepSoundTime += elapsed;
if (stepSoundTime >= 0.4)
{
soundStep.play();
stepSoundTime = 0;
}
}
animations[AnimationName::Walk]->Update(elapsed);
animations[AnimationName::Walk]->ApplyToSprite(this);
}
else
{
animations[AnimationName::Idle]->Update(elapsed);
animations[AnimationName::Idle]->ApplyToSprite(this);
}
lastPosition = getPosition();
}
else
{
if (animations[state] != nullptr)
{
animations[state]->Update(elapsed);
animations[state]->ApplyToSprite(this);
if (animations[state]->isEnded())
{
if (state == AnimationName::Death)
{
isAlive = false;
animations[state]->LastFrame();
animations[state]->ApplyToSprite(this);
}
else state = AnimationName::Basic;
}
}
else state = AnimationName::Basic;
}
}
else isDeadTime += elapsed;
velocity = sf::Vector2f(0, 0);
setEntityView();
}
View Entity::getEntityView()
{
return entityView;
}
void Entity::Respawn()
{
isAlive = true;
health = maxHealth;
state = AnimationName::Basic;
isDeadTime = 0;
}