-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.cpp
More file actions
executable file
·218 lines (184 loc) · 5.09 KB
/
Model.cpp
File metadata and controls
executable file
·218 lines (184 loc) · 5.09 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
Made with ❤ by srinSkit.
Created on 10 March 2018.
*/
/*
* A model has a local origin (x,y) and list of elements to be rendered around this origin
* An element has a shape and vertex array representing that shape
* A vertex contains the location of the vertex
*/
#include "Model.h"
#include <GL/gl.h>
#include <vector>
#include <algorithm>
#include <cstdio>
#include "Common.h"
#include "Graphics.h"
#define RADIAN(x) ((x)*M_PI/180.0)
using namespace std;
Vertex::Vertex() = default;
void Vertex::set(GLint x, GLint y) {
this->x = x;
this->y = y;
}
vector<Element *> Element::elementStore;
Element::Element(int numberOfVertices, Vertex *vertexArray, GLenum elementShape) {
glShape = elementShape;
v = vertexArray;
nV = numberOfVertices;
layer = 0;
r = g = b = 255;
elementStore.push_back(this);
}
void Element::setColor(GLubyte r, GLubyte g, GLubyte b) {
this->r = r;
this->g = g;
this->b = b;
}
void Element::loadColor() {
glColor3ub(r, g, b);
}
void Element::render(int x, int y) {
myPushMatrix();
myTranslatef(x, y, 0);
loadColor();
myVertexPointer(2, GL_INT, sizeof(Vertex), v);
myDrawArrays(glShape, 0, nV);
myPopMatrix();
}
Element *Element::softCopy() {
auto element = new Element(nV, v, glShape);
element->setColor(r, g, b);
return element;
}
void Element::destroyAll() {
for (auto &it : elementStore) {
delete (it->v);
delete it;
}
}
int Model::deadCount = 0, Model::maxDeadCount = 20;
vector<Model *> Model::staticModels, Model::dynamicModels;
Model::CollisionManager Model::coolMan(&Model::staticModels, &Model::dynamicModels);
Model::Model(int localOriginX, int localOriginY, bool isStaticModel, bool hide) {
x = localOriginX, y = localOriginY, static_model = isStaticModel;
elements = nullptr;
nE = 0;
hl = hr = 0;
ht = hb = 0;
this->active = true;
this->hide = hide;
health = 0;
hCode = 0;
if (static_model)
staticModels.push_back(this);
else
dynamicModels.push_back(this);
coolMan.login(this);
coolMan.push(this);
}
void Model::adjustHitbox(Element *element) {
for (auto v = element->v; v < element->v + element->nV; ++v) {
hl = min(hl, v->x);
hr = max(hr, v->x);
hb = min(hb, v->y);
ht = max(ht, v->y);
}
}
Model *Model::softCopy() {
auto clone = new Model(x, y, static_model, hide);
clone->elements = elements;
clone->nE = nE;
return clone;
}
void Model::destroy() {
coolMan.logout(this);
active = false;
if (elements != nullptr)
delete (elements);
elements = nullptr;
++deadCount;
}
void Model::addElement(Element *element) {
if (elements == nullptr)
elements = new vector<Element *>();
elements->push_back(element);
sort(elements->begin(), elements->end(), [](const Element *e1, const Element *e2) {
return e1->layer < e2->layer;
});
coolMan.pop(this);
adjustHitbox(element);
coolMan.push(this);
}
void Model::update() {
}
void Model::render() {
if (elements == nullptr || elements->empty()) return;
for (auto it = elements->begin(); it < elements->end(); ++it) {
(*it)->render(x, y);
}
}
void Model::renderStatic() {
for (auto &staticModel : staticModels) {
if (!staticModel->active) continue;
if (staticModel->hide) continue;
staticModel->render();
}
}
void Model::renderDynamic() {
for (auto &dynamicModel : dynamicModels) {
if (dynamicModel == nullptr) continue;
if (!dynamicModel->active) continue;
coolMan.pop(dynamicModel);
dynamicModel->update();
coolMan.push(dynamicModel);
if (dynamicModel->hide) continue;
dynamicModel->render();
}
}
void Model::cleanRenderQueue() {
for (auto &staticModel : staticModels)
if (!staticModel->active) {
delete staticModel;
staticModel = nullptr;
}
for (auto &dynamicModel : dynamicModels)
if (!dynamicModel->active) {
delete dynamicModel;
dynamicModel = nullptr;
}
staticModels.erase(
remove_if(staticModels.begin(), staticModels.end(), [](Model *model) { return model == nullptr; }),
staticModels.end());
dynamicModels.erase(
remove_if(dynamicModels.begin(), dynamicModels.end(), [](Model *model) { return model == nullptr; }),
dynamicModels.end());
deadCount = 0;
}
void Model::nextFrame() {
coolMan.fullCheck();
if (deadCount > maxDeadCount)
cleanRenderQueue();
}
void Model::destroyAll() {
for (auto &staticModel : staticModels)
if (!staticModel->active)
delete staticModel;
for (auto &dynamicModel : dynamicModels)
if (!dynamicModel->active)
delete dynamicModel;
staticModels.clear();
dynamicModels.clear();
deadCount = 0;
}
void Model::onHit() {
health--;
if (health <= 0) {
coolMan.pop(this);
destroy();
}
score++;
}
bool insideView(int x, int y) {
return x >= 0 && x <= worldWidth && y >= 0 && y < worldHeight;
}