-
Notifications
You must be signed in to change notification settings - Fork 0
/
Zombie.cpp
66 lines (65 loc) · 1.86 KB
/
Zombie.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
#include "Zombie.h"
#include "TextureHolder.h"
#include <cstdlib>
#include <ctime>
using namespace std;
void Zombie::spawn(float startX, float startY, int type, int seed) {
switch (type) {
case 0:
m_Sprite = Sprite(TextureHolder::GetTexture("graphics/bloater.png"));
m_Speed = BLOATER_SPEED;
m_Health = BLOATER_HEALTH;
break;
case 1:
m_Sprite = Sprite(TextureHolder::GetTexture("graphics/chaser.png"));
m_Speed = CHASER_SPEED;
m_Health = CHASER_HEALTH;
break;
case 2:
m_Sprite = Sprite(TextureHolder::GetTexture("graphics/crawler.png"));
m_Speed = CRAWLER_SPEED;
m_Health = CRAWLER_HEALTH;
break;
}
srand((int)time(0) * seed);
float modifier = (rand() % MAX_VARRIANCE) + OFFSET;
modifier /= 100;
m_Speed *= modifier;
m_Position.x = startX;
m_Position.y = startY;
m_Sprite.setOrigin(12, 12);
m_Sprite.setPosition(m_Position);
}
bool Zombie::hit() {
m_Health--;
if (m_Health < 0) {
m_Alive = false;
m_Sprite.setTexture(TextureHolder::GetTexture("graphics/blood.png"));
return true;
}
return false;
}
bool Zombie::isAlive() {
return m_Alive;
}
FloatRect Zombie::getPosition() {
return m_Sprite.getGlobalBounds();
}
Sprite Zombie::getSprite() {
return m_Sprite;
}
void Zombie::update(float elapsedTime, Vector2f playerLocation) {
float playerX = playerLocation.x;
float playerY = playerLocation.y;
if (playerX > m_Position.x)
m_Position.x = m_Position.x + m_Speed * elapsedTime;
if (playerY > m_Position.y)
m_Position.y = m_Position.y + m_Speed * elapsedTime;
if (playerX < m_Position.x)
m_Position.x = m_Position.x - m_Speed * elapsedTime;
if (playerY < m_Position.y)
m_Position.y = m_Position.y - m_Speed * elapsedTime;
m_Sprite.setPosition(m_Position);
float angle = (atan2(playerY - m_Position.y, playerX - m_Position.x) * 180) / 3.141;
m_Sprite.setRotation(angle);
}