forked from Barnold1953/GraphicsTutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHuman.cpp
More file actions
62 lines (47 loc) · 1.52 KB
/
Copy pathHuman.cpp
File metadata and controls
62 lines (47 loc) · 1.52 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
#include "Human.h"
#include <ctime>
#include <random>
#include <glm/gtx/rotate_vector.hpp>
#include <Bengine/ResourceManager.h>
Human::Human() :
_frames(0)
{
}
Human::~Human()
{
}
void Human::init(float speed, glm::vec2 pos) {
static std::mt19937 randomEngine(time(nullptr));
static std::uniform_real_distribution<float> randDir(-1.0f, 1.0f);
_health = 20;
_color.r = 255;
_color.g = 255;
_color.b = 255;
_color.a = 255;
_speed = speed;
_position = pos;
// Get random direction
m_direction = glm::vec2(randDir(randomEngine), randDir(randomEngine));
// Make sure direction isn't zero
if (m_direction.length() == 0) m_direction = glm::vec2(1.0f, 0.0f);
m_direction = glm::normalize(m_direction);
m_textureID = Bengine::ResourceManager::getTexture("Textures/human.png").id;
}
void Human::update(const std::vector<std::string>& levelData,
std::vector<Human*>& humans,
std::vector<Zombie*>& zombies,
float deltaTime) {
static std::mt19937 randomEngine(time(nullptr));
static std::uniform_real_distribution<float> randRotate(-40.0f, 40.0f);
_position += m_direction * _speed * deltaTime;
// Randomly change direction every 20 frames
if (_frames == 20) {
m_direction = glm::rotate(m_direction, randRotate(randomEngine));
_frames = 0;
} else {
_frames++;
}
if (collideWithLevel(levelData)) {
m_direction = glm::rotate(m_direction, randRotate(randomEngine));
}
}