forked from WenlinMao/SleepWalking
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMovingGameObject.cpp
More file actions
95 lines (68 loc) · 2.75 KB
/
Copy pathMovingGameObject.cpp
File metadata and controls
95 lines (68 loc) · 2.75 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
#include "MovingGameObject.hpp"
#include "load_save_png.hpp"
#include "AudioSystem.hpp"
#include <cstdlib>
#include <ctime>
MovingGameObject::MovingGameObject() {
}
MovingGameObject::MovingGameObject(float mass, vector<string> properties, const glm::vec3& pos, float w, float h,
const glm::vec3& vel, bool isFixed, const std::string& filename, const std::string& box_name,
float l) :
GameObject(mass, pos, vel, isFixed, filename, l), width(w), height(h) {
start_position = pos;
if (properties[2] == "H") moving_dir = glm::vec3(1.f, 0.f, 0.f);
else moving_dir = glm::vec3(0.f, 1.f, 0.f);
moving_speed = std::stof(properties[3]);
moving_range = std::stof(properties[4]);
std::srand((unsigned int)std::time(nullptr));
int offset = (std::rand() % (2 * (int)moving_range)) - (int) moving_range;
position = moving_dir * (float)offset + start_position;
if (glm::length(position - start_position) > moving_range) {
moving_dir *= -1.f;
position += 2.f * moving_dir * (glm::length(position - start_position) - moving_range);
}
if (box != nullptr) {
//cout << "box is not nullptr\n\n" << endl;
box->SetPos(position);
}
createVerts();
prepareDraw();
//box = CollisionSystem::Instance().AddOneThornBlock(glm::vec2(position.x, position.y),
// glm::vec2(width * 2, height * 2),
// box_name);
////box = CollisionSystem::Instance().AddOneSceneBlock(glm::vec2(position.x, position.y),
//// glm::vec2(width * 2, height * 2),
//// box_name);
//box->owner = this;
}
MovingGameObject::~MovingGameObject() {
}
void MovingGameObject::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 MovingGameObject::reset() {
}
void MovingGameObject::update(float elapsed) {
//cout << "updating moving object" << endl;
position += moving_dir * moving_speed * elapsed;
if (glm::length(position - start_position) > moving_range) {
moving_dir *= -1.f;
position += 2.f * moving_dir * (glm::length(position - start_position) - moving_range);
}
if (box != nullptr) {
//cout << "box is not nullptr\n\n" << endl;
box->SetPos(position);
}
}