-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnemyObject.cpp
90 lines (68 loc) · 2.35 KB
/
EnemyObject.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
#include "EnemyObject.h"
#include "Window.h"
#define _USE_MATH_DEFINES
#include <math.h>
/*
EnemyObject inherits from GameObject
It overrides GameObject's update method, so that you can check for input to change the velocity of the player
*/
EnemyObject::EnemyObject(glm::vec3 &entityPos, GLuint entityTexture, GLint entityNumElements, float s)
: GameObject(entityPos, entityTexture, entityNumElements) {
orientation = 0.0f;
finished = true;
speed = s;
hp = 500.0f;
currNode = nullptr;
speedMultiplier = 1.0f;
}
// Update function for moving the player object around
void EnemyObject::update(double deltaTime) {
if (finished || kill) {
return;
}
// calculate orientation
float dx = currNode->getX() - position.x;
float dy = position.y - currNode->getY();
float radians = atan2f(dy, dx);
if (radians < 0) {
radians = abs(radians);
}
else {
radians = 2 * M_PI - radians;
}
float degrees = glm::degrees(radians);
orientation = degrees;
position.x += speedMultiplier * speed * (float)deltaTime * cos(glm::radians(orientation));
position.y += speedMultiplier * speed * (float)deltaTime * sin(glm::radians(orientation));
}
// slightly modified render function so the players appear larger
void EnemyObject::render(Shader &shader) {
if (kill) return;
// Bind the entities texture
glBindTexture(GL_TEXTURE_2D, texture);
// Setup the transformation matrix for the shader
glm::mat4 translationMatrix = glm::translate(glm::mat4(1.0f), position);
glm::mat4 rotationMatrix = glm::rotate(glm::mat4(1.0f), 0.0f, glm::vec3(0.0f, 0.0f, 1.0f));
glm::mat4 scaleMatrix = glm::scale(glm::mat4(1.0f), glm::vec3(0.4f, 0.58f, 0.0f));
// Set the transformation matrix in the shader
glm::mat4 transformationMatrix = translationMatrix * rotationMatrix * scaleMatrix;
//transformationMatrix = rotationMatrix * translationMatrix * scaleMatrix;
shader.setUniformMat4("transformationMatrix", transformationMatrix);
// Draw the entity
glDrawElements(GL_TRIANGLES, numElements, GL_UNSIGNED_INT, 0);
}
// reset the player's path to the parameter
void EnemyObject::setPath(std::vector<Node*> p) {
finished = false;
path = p;
currNode = path.back();
}
// set current "target" node to the next one specified in the path, pop the previous node off
void EnemyObject::nextNode() {
path.pop_back();
if (path.empty()) {
finished = true;
return;
}
currNode = path.back();
}