-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.cpp
154 lines (131 loc) · 4.07 KB
/
Player.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "Player.hpp"
#include <glm/gtc/matrix_transform.hpp>
#include <iostream>
Player::Player(HeightMap &height, int x, int y) :
m_Hit(false),
m_HitBox(3.0f),
m_Map(height),
m_Position(glm::vec3(x, y, m_Map.getHeight(x, y))),
m_NextPosition(m_Position) {
createPlayerMesh(m_Model,20,20);
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");
}
void
Player::updatePosition() {
m_ModelMatrix=glm::mat4(1.0f);
m_Position += (m_NextPosition - m_Position) * 0.01f;
m_Position.z = m_Map.getHeight(m_Position.x, m_Position.y) + 2.5f; // Half size of the cube
m_ModelMatrix = glm::translate(m_ModelMatrix, m_Position);
m_ModelMatrix = glm::rotate(m_ModelMatrix,0.01f,glm::vec3(0.0f,0.0f,1.0f));
switch(m_PowerUp) {
case PowerUpAttribute::GROW :
m_ModelMatrix = glm::scale(m_ModelMatrix, glm::vec3(2.0f));
}
}
void
Player::drawShadow(Shader &shader, RenderContext &rctx) {
if (m_Hit) return;
shader["model"]=m_ModelMatrix;
m_VertexArray.bindVertexArray();
rctx.draw(m_VertexArray, PrimitiveType::Triangles);
for(auto & bullet : m_Bullets) {
bullet.updatePosition();
bullet.drawShadow(shader,rctx);
}
}
void
Player::draw(Camera const & camera, RenderContext & rctx) {
if (m_Hit) return;
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);
for(auto & bullet : m_Bullets) {
bullet.updatePosition();
bullet.draw(camera, rctx);
}
clearBullets();
}
void
Player::shoot(const glm::vec3 &shootDirection) {
m_Bullets.emplace_back(shootDirection, m_Position);
}
bool const
Player::isHit() {
return m_Hit;
}
void
Player::hit() {
m_Hit = true;
}
glm::vec3 const &
Player::getPosition() const {
return m_Position;
}
float const
Player::getHitBox() {
return m_HitBox;
}
glm::vec3
Player::setNextPosition(glm::vec3 const & nextPosition) {
m_NextPosition = nextPosition;
}
std::list<Bullet> const &
Player::getBullets() {
return m_Bullets;
}
void
Player::setPowerUp(PowerUpAttribute powerUp) {
m_PowerUp = powerUp;
}
void
Player::clearBullets() {
if (m_Bullets.empty())
return;
if ( m_Bullets.front().canBeRemoved())
m_Bullets.pop_front();
}
void
Player::createPlayerMesh(std::vector<glm::vec3> &model, int uline, int vline) {
int startU=0;
int startV=0;
float endU=3.14f*2.0f;
float endV=3.14f;
float stepU=(endU-startU)/uline;
float stepV=(endV-startV)/vline;
for (int i=0;i<uline;i++) {
for (int j=0;j<vline;j++) {
float u=i*stepU+startU;
float v=j*stepV+startV;
float un=(i+1==uline) ? endU : (i+1)*stepU+startU;
float vn=(j+1==vline) ? endV : (j+1)*stepV+startV;
glm::vec3 p0=sphereEquation(u,v, 1.5f);
glm::vec3 p1=sphereEquation(u, vn, 1.5f);
glm::vec3 p2=sphereEquation(un, v, 1.5f);
glm::vec3 p3=sphereEquation(un, vn, 1.5f);
// Like a donkey....
model.emplace_back(p0);
model.emplace_back(glm::normalize(p0));
model.emplace_back(p2);
model.emplace_back(glm::normalize(p2));
model.emplace_back(p1);
model.emplace_back(glm::normalize(p1));
model.emplace_back(p3);
model.emplace_back(glm::normalize(p3));
model.emplace_back(p1);
model.emplace_back(glm::normalize(p1));
model.emplace_back(p2);
model.emplace_back(glm::normalize(p2));
}
}
}
glm::vec3
Player::sphereEquation(float u, float v, float r){
return glm::vec3(std::cos(u)*std::sin(v)*r, std::cos(v)*r, std::sin(u)*std::sin(v)*r);
}