forked from Barnold1953/GraphicsTutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZombie.cpp
More file actions
60 lines (48 loc) · 1.55 KB
/
Copy pathZombie.cpp
File metadata and controls
60 lines (48 loc) · 1.55 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
#include "Zombie.h"
#include <Bengine/ResourceManager.h>
#include "Human.h"
Zombie::Zombie()
{
}
Zombie::~Zombie()
{
}
void Zombie::init(float speed, glm::vec2 pos, Grid* grid) {
_speed = speed;
_position = pos;
m_grid = grid;
_health = 50;
// Set Green Color
_color = Bengine::ColorRGBA8(255, 255, 255, 255);
m_textureID = Bengine::ResourceManager::getTexture("Textures/zombie.png").id;
}
void Zombie::update(const std::vector<std::string>& levelData,
float deltaTime) {
// Find the closest human
//Human* closestHuman = getNearestHuman(humans);
Human* closestHuman = nullptr;
// If we found a human, move towards him
if (closestHuman != nullptr) {
// Get the direction vector towards the player
m_direction = glm::normalize(closestHuman->getPosition() - _position);
_position += m_direction * _speed * deltaTime;
}
// Do collision
collideWithLevel(levelData);
}
Human* Zombie::getNearestHuman(std::vector<Human*>& humans) {
Human* closestHuman = nullptr;
float smallestDistance = 9999999.0f;
for (int i = 0; i < humans.size(); i++) {
// Get the direction vector
glm::vec2 distVec = humans[i]->getPosition() - _position;
// Get distance
float distance = glm::length(distVec);
// If this person is closer than the closest person, this is the new closest
if (distance < smallestDistance) {
smallestDistance = distance;
closestHuman = humans[i];
}
}
return closestHuman;
}