forked from WenlinMao/SleepWalking
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerObject.cpp
More file actions
161 lines (126 loc) · 5.67 KB
/
Copy pathPlayerObject.cpp
File metadata and controls
161 lines (126 loc) · 5.67 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
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
154
155
156
157
158
159
160
161
#include "PlayerObject.hpp"
#include "load_save_png.hpp"
#include "AudioSystem.hpp"
PlayerObject::PlayerObject() {
}
PlayerObject::PlayerObject(float mass, const glm::vec3& pos, float w, float h,
const glm::vec3& vel, bool isFixed, const std::string& filename, float l) :
GameObject(mass, pos, vel, isFixed, filename, l), width(w), height(h) {
PlayerStats::Instance().player1SavedPos = pos;
PlayerStats::Instance().player1SavedVel = vel;
PlayerStats::Instance().player1Size = glm::vec2(w * 2, h * 2);
createVerts();
prepareDraw();
box = std::shared_ptr<PlayerCollisionBox>(new PlayerCollisionBox(position, glm::vec2(width, height), false, "player1"));
CollisionSystem::Instance().player1_collision = box;
box->owner = this;
}
PlayerObject::~PlayerObject() {
}
void PlayerObject::createVerts() {
vertex_positions = vector<glm::vec4>({
glm::vec4(width, height, 0.0f, 1.0f), // top right
glm::vec4(width, -height, 0.0f, 1.0f), // bottom right
glm::vec4(-width, -height, 0.0f, 1.0f), // bottom left
glm::vec4(-width, height, 0.0f, 1.0f) // top left
});
vertex_texcoords = vector<glm::vec2>({
glm::vec2(1.f,1.f),
glm::vec2(1.f,0.f),
glm::vec2(0.f,0.f),
glm::vec2(0.f,1.f)
});
indices = std::vector<unsigned int>({ 0, 1, 2, 2, 3, 0 });
}
void PlayerObject::reset() {
std::cout << "player 1 reset" << std::endl;
model = glm::mat4(1.0f);
GameObject::reset();
PlayerStats::Instance().reset();
position = PlayerStats::Instance().player1SavedPos;
velocity = PlayerStats::Instance().player1SavedVel;
box->SetPos(glm::vec2{ position.x, position.y });
//std::cout << "Before beng" << std::endl;
vertex_positions.clear();
createVerts();
prepareDraw();
}
void PlayerObject::update(float elapsed) {
//std::cout << "update player\n";
if (left.pressed && !right.pressed) {
type = "Run";
// move left
glm::vec2 new_pos = glm::vec2{ position.x, position.y };
new_pos += elapsed * speed * glm::vec2(-1.f, 0.f) * glm::mat2(PlayerStats::Instance().rotMat);
PlayerStats::Instance().isFacingLeft = false;
//cout << "checking collision\n";
if (!CollisionSystem::Instance().PlayerCheckCollision(new_pos,
glm::abs(glm::vec2{ width * 2, height * 2} * glm::mat2(PlayerStats::Instance().rotMat)))) {
//cout << "Not colliding\n";
position.x = new_pos.x;
position.y = new_pos.y;
}
if (PlayerStats::Instance().canJump) AudioSystem::Instance().PlayLongAudio(AudioSourceList::Footsteps);
}
else if (!left.pressed && right.pressed) {
type = "Run";
// move right
glm::vec2 new_pos = glm::vec2{ position.x, position.y };
new_pos += elapsed * speed * glm::vec2(1.f, 0.f) * glm::mat2(PlayerStats::Instance().rotMat);
PlayerStats::Instance().isFacingLeft = true;
if (!CollisionSystem::Instance().PlayerCheckCollision(new_pos,
glm::abs(glm::vec2{width * 2, height * 2} * glm::mat2(PlayerStats::Instance().rotMat)))) {
position.x = new_pos.x;
position.y = new_pos.y;
}
if (PlayerStats::Instance().canJump) AudioSystem::Instance().PlayLongAudio(AudioSourceList::Footsteps);
}
else if (!left.pressed && !right.pressed) {
AudioSystem::Instance().StopLongAudio(AudioSourceList::Footsteps);
}
if (!PlayerStats::Instance().canJump) PlayerStats::Instance().jumpElapsed += elapsed;
if (space.pressed && PlayerStats::Instance().canJump) {
//cout << "jump\n";
type = "Jump";
force += mass * jump_power * glm::vec3(0.f, 1.f, 0.f) * PlayerStats::Instance().rotMat;
AudioSystem::Instance().PlayShortAudio(AudioSourceList::Jump);
AudioSystem::Instance().StopLongAudio(AudioSourceList::Footsteps);
space.pressed = false;
PlayerStats::Instance().canJump = false;
}
if (!isFixed) {
glm::vec3 accel = force / mass;
//cout << "Force: " << glm::to_string(force) << endl;
velocity += accel * elapsed;
glm::vec3 new_pos = position + velocity * elapsed;
//cout << "new_pos: " << glm::to_string(new_pos) << endl;
//cout << "\n";
if (!CollisionSystem::Instance().PlayerCheckCollision(glm::vec2{ new_pos.x, new_pos.y },
glm::abs(glm::vec2{ width * 2, height * 2} * glm::mat2(PlayerStats::Instance().rotMat)))) {
//cout << "Graivity Not colliding\n";
position.x = new_pos.x;
position.y = new_pos.y;
}
else {
//cout << " Gravity collided\n";
if (PlayerStats::Instance().jumpElapsed > 0.5f) {
PlayerStats::Instance().canJump = true;
PlayerStats::Instance().jumpElapsed = 0.f;
}
velocity = glm::vec3(0.f);
}
}
PlayerStats::Instance().player1Pos = position;
CollisionSystem::Instance().PlayerCheckTrigger(glm::vec2{ position.x, position.y }, glm::vec2{ width, height });
//std::cout << "Triggers checked" << std::endl;
CollisionSystem::Instance().PlayerCheckCollectables(glm::vec2{ position.x, position.y }, glm::vec2{ width, height });
box->SetPos(glm::vec2{ position.x, position.y });
//if (position.x <= 0.f || position.y <= 0.f || PlayerStats::Instance().health <= 0.f) {
// reset();
//}
// std::cout << "player position: x: " << transform->position.x << "; y: " << transform->position.y << "\n";
// update texcoords every update
if(anims.count(type) && anims[type])
anims[type]->play(this->VAO, this->VBO_texcoords, sz, elapsed);
type = "Idle";
}