-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollisionManager.cpp
More file actions
141 lines (129 loc) · 4.23 KB
/
CollisionManager.cpp
File metadata and controls
141 lines (129 loc) · 4.23 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
/*
Made with ❤ by srinSkit.
Created on 30 April 2018.
*/
#include <cstdio>
#include <iostream>
#include "Model.h"
#include "Common.h"
#include "Graphics.h"
#include <vector>
void Model::CollisionManager::push(Model *model) {
if (model->hCode < 0 || !model->active) return;
int yb = model->y + model->hb, yt = model->y + model->ht, xl = model->x + model->hl, xr = model->x + model->hr;
limitY(yb);
limitY(yt);
limitX(xl);
limitX(xr);
int gx, gy;
for (int y = yb; y <= yt; ++y)
for (int x = xl; x <= xr; ++x) {
gx = x;
gy = y;
worldToGrid(gx, gy);
approxGrid[gy][gx] = true;
buff[y][x] += model->hCode;
}
}
void Model::CollisionManager::pop(Model *model) {
if (model->hCode < 0 || !model->active) return;
int yb = model->y + model->hb, yt = model->y + model->ht, xl = model->x + model->hl, xr = model->x + model->hr;
limitY(yb);
limitY(yt);
limitX(xl);
limitX(xr);
int gx, gy;
for (int y = yb; y <= yt; ++y)
for (int x = xl; x <= xr; ++x) {
buff[y][x] -= model->hCode;
if (buff[y][x] < 0) {
gx = x;
gy = y;
worldToGrid(gx, gy);
approxGrid[gy][gx] = false;
}
}
}
Model::CollisionManager::CollisionManager(vector<Model *> *staticModels, vector<Model *> *dynamicModels) {
buff.resize(static_cast<unsigned long>(worldHeight) + 1,
vector<int>(static_cast<unsigned long>(worldWidth) + 1, -1));
approxGrid.resize(10, vector<bool>(10, false));
maxCodeSize = 0;
this->staticModels = staticModels;
this->dynamicModels = dynamicModels;
}
void Model::CollisionManager::login(Model *model) {
if (codes.empty()) {
for (int i = 0; i < 20; ++i)
codes.push(i + maxCodeSize);
maxCodeSize += 20;
}
model->hCode = codes.top();
codes.pop();
}
void Model::CollisionManager::logout(Model *model) {
codes.push(model->hCode);
}
vector<vector<bool>> *Model::CollisionManager::makeGrid() {
auto grid = new vector<vector<bool> >(10, vector<bool>(10));
for (int i = 0; i < nCellsX; ++i) {
for (int j = 0; j < nCellsY; ++j)
(*grid)[i][j] = (buff[i * cellHeight + cellHeight / 2][j * cellWidth + cellWidth / 2] >= 0);
}
return grid;
}
void Model::CollisionManager::fullCheck() {
for (auto i = 0; i < dynamicModels->size(); ++i) {
auto model = (*dynamicModels)[i];
if (model == nullptr)continue;
if (model->hCode < 0 || !model->active) continue;
int yb = model->y + model->hb, yt = model->y + model->ht, xl = model->x + model->hl, xr = model->x + model->hr;
limitY(yb);
limitY(yt);
limitX(xl);
limitX(xr);
for (int y = yb, x; y <= yt; ++y) {
for (x = xl; x <= xr; ++x) {
if (buff[y][x] - model->hCode >= 0) {
dHitTracker.push(i);
break;
}
}
if (x <= xr)break;
}
}
if (!dHitTracker.empty()) {
for (auto i = 0; i < staticModels->size(); ++i) {
auto model = (*staticModels)[i];
if (model == nullptr)continue;
if (model->hCode < 0 || !model->active) continue;
int yb = model->y + model->hb, yt = model->y + model->ht, xl = model->x + model->hl, xr =
model->x + model->hr;
limitY(yb);
limitY(yt);
limitX(xl);
limitX(xr);
for (int y = yb, x; y <= yt; ++y) {
for (x = xl; x <= xr; ++x) {
if (buff[y][x] - model->hCode >= 0) {
sHitTracker.push(i);
break;
}
}
if (x <= xr)break;
}
}
}
while (!dHitTracker.empty()) {
(*dynamicModels)[dHitTracker.top()]->onHit();
dHitTracker.pop();
}
while (!sHitTracker.empty()) {
(*staticModels)[sHitTracker.top()]->onHit();
sHitTracker.pop();
}
}
bool Model::CollisionManager::unsafe(int x, int y) {
if (x < 0 || y < 0 || x >= nCellsX || y >= nCellsY)return false;
return approxGrid[y][x];
}