-
Notifications
You must be signed in to change notification settings - Fork 0
/
Enemy.cpp
103 lines (89 loc) · 2.99 KB
/
Enemy.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
#include "Enemy.hpp"
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/component_wise.hpp>
Enemy::Enemy(HeightMap &height, glm::vec2 position) :
m_MovementRadius(2.5f),
m_Speed(0.005f),
m_DeltaPosition(0.0f),
m_Map(height),
m_AggroRange(30.0f){
m_VertexArray.createVertexArray(m_Model);
m_VertexArray.describeVertexArray(0,3,GlTypes::Float, 6, GlBool::False,0);
m_VertexArray.describeVertexArray(1,3,GlTypes::Float, 6, GlBool::False,3);
m_Shader.bindShader("./shader/VertexShader.vert");
m_Shader.bindShader("./shader/FragmentShader.frag");
m_ModelMatrix=glm::mat4(1.0f);
m_Position = glm::vec3(position.x, position.y,height.getHeight(position.x, position.y)+2.5f);
m_ModelMatrix = glm::translate(m_ModelMatrix, m_Position);
m_ModelMatrix = glm::scale(m_ModelMatrix, glm::vec3(1.5,1.0,2.0f));
m_Hit = false;
}
void
Enemy::intersectWithBullet(std::list<Bullet> const & bullets) {
for (auto & bullet : bullets) {
if (std::abs(bullet.getPosition().x - m_Position.x)< 1.0f && std::abs(bullet.getPosition().y - m_Position.y)< 1.0f
&& std::abs(bullet.getPosition().z - m_Position.z)< 2.0f) {
m_Hit=true;
}
}
}
void
Enemy::drawShadow(Shader &shader, RenderContext &rctx) {
if(m_Hit) return; // placeholder
movement();
shader["model"]=m_ModelMatrix;
m_VertexArray.bindVertexArray();
rctx.draw(m_VertexArray, PrimitiveType::Triangles);
}
void
Enemy::draw(Camera const & camera, RenderContext & rctx) {
if (m_Hit) return;
movement();
m_Shader.activate();
m_Shader["model"] = m_ModelMatrix;
m_Shader["view"] = camera.View;
m_Shader["projection"] = camera.Projection;
m_VertexArray.bindVertexArray();
rctx.draw(m_VertexArray, PrimitiveType::Triangles);
}
void
Enemy::lookForPlayer(Player & player) {
if (m_Hit) return;
float disToPlayer = glm::distance(player.getPosition(), m_Position);
if (disToPlayer < player.getHitBox()) {
player.hit();
return;
}
else if (disToPlayer < m_AggroRange) {
m_PlayerDirection = glm::normalize(player.getPosition() - m_Position);
}
else {
m_PlayerDirection = glm::vec3(0.0f);
}
}
void
Enemy::movement() {
if (glm::compAdd(m_PlayerDirection) != 0.0f)
huntPlayer();
else {
m_Position.x += m_Speed;
m_Position.z = m_Map.getHeight(m_Position.x, m_Position.y)+0.5f;
m_DeltaPosition += m_Speed;
glm::mat4 mat(1.0f);
m_ModelMatrix = glm::translate(mat, m_Position);
if (std::abs(m_DeltaPosition) > m_MovementRadius )
m_Speed = -m_Speed;
}
}
void
Enemy::huntPlayer() {
m_Position.x += 3 * std::abs(m_Speed) * m_PlayerDirection.x;
m_Position.y += 3 * std::abs(m_Speed) * m_PlayerDirection.y;
m_Position.z = m_Map.getHeight(m_Position.x, m_Position.y)+0.5f;
glm::mat4 mat(1.0f);
m_ModelMatrix = glm::translate(mat, m_Position);
}
bool
Enemy::isHit() {
return m_Hit;
}