- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLevelGridManager.cpp
More file actions
Latest commit
147 lines (124 loc) · 4.51 KB
/
Copy pathLevelGridManager.cpp
File metadata and controls
147 lines (124 loc) · 4.51 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
/**
* DroidRacers
* by Marlon Etheredge
*/
#include"StdAfx.h"
#include"LevelGridManager.h"
LevelGridManager::LevelGridManager(void)
{
this->m_World = this->m_Engine->GetWorld();
this->m_CameraBoundaries = newhgeRect();
// TODO: Find me a place
LevelGrid::StartX = 15;
LevelGrid::StartY = 0;
// Set cell pointers
this->m_UpperCellPointer = 0;
this->m_LeftCellPointer = 0;
this->m_RightCellPointer = 0;
// Initialize the grid manager
this->Initialize();
}
LevelGridManager::~LevelGridManager(void)
{
// Housekeeping
deletethis->m_CameraBoundaries;
}
LevelGrid *LevelGridManager::AddGrid(hgeVector offset, bool addLevel)
{
LevelGrid *l;
hgeVector pos(offset.x + this->m_Engine->GetWidth(), offset.y + this->m_Engine->GetHeight());
l = newLevelGrid(pos, this->m_CameraBoundaries, 10, 40, addLevel);
this->m_World->m_Objects.push_front(l);
return l;
}
voidLevelGridManager::Initialize(void)
{
this->m_CurrentGrid = this->AddGrid(hgeVector(), true);
this->m_StartGridOffset = hgeVector();
}
voidLevelGridManager::Render(float dt)
{
#ifdef RENDER_BOUNDARIES
hgeVector a(this->m_CameraBoundaries->x1, this->m_CameraBoundaries->y1);
hgeVector b(this->m_CameraBoundaries->x2, this->m_CameraBoundaries->y1);
hgeVector c(this->m_CameraBoundaries->x2, this->m_CameraBoundaries->y2);
hgeVector d(this->m_CameraBoundaries->x1, this->m_CameraBoundaries->y2);
this->m_Engine->DrawLine(
a,
c,
0xFFFF0000,
1.0f
);
this->m_Engine->DrawLine(
d,
b,
0xFF00FF00,
1.0f
);
#endif
}
voidLevelGridManager::Update(float dt)
{
// Store height and width locally
unsignedint width = this->m_Engine->GetWidth();
unsignedint height = this->m_Engine->GetHeight();
// Update camera boundaries
this->m_CameraBoundaries->x1 = -this->m_World->WorldCamera->Position.x;
this->m_CameraBoundaries->y1 = -this->m_World->WorldCamera->Position.y;
this->m_CameraBoundaries->x2 = this->m_CameraBoundaries->x1 + width;
this->m_CameraBoundaries->y2 = this->m_CameraBoundaries->y1 + height;
// Check if we should add a new cell:
// [a-b ] A: Upper left, B: Upper right
// [|p| ] C: Lower right, D: Lower left
// [d-c ]
//
// Determine cell indices of every vertex of the camera space
// (camera boundaries), in case the specific cell pointer has not yet been
// incremented to the cell index determined, add a new cell and increase
// the specific cell pointer
// Retrieve camera world space vertices
hgeVector a(this->m_CameraBoundaries->x1, this->m_CameraBoundaries->y1);
hgeVector b(this->m_CameraBoundaries->x2, this->m_CameraBoundaries->y1);
hgeVector c(this->m_CameraBoundaries->x2, this->m_CameraBoundaries->y2);
hgeVector d(this->m_CameraBoundaries->x1, this->m_CameraBoundaries->y2);
// Determine indices
int cxl = abs(a.x / width - 1);
int cxr = (b.x + width * 0.5f) / width - 1;
int cyu = abs((a.y - height * 0.5f) / height);
if (cxl == 0 && cxr == 0 && cyu == 0)
{
return;
}
// Check if we should add a new upper cell
if (cyu > this->m_UpperCellPointer)
{
// Create and add a new cell in the upper region
hgeVector pos = this->m_StartGridOffset; pos.y -= height * cyu;
this->AddGrid(pos, true);
// Increase the upper cell pointer to indicate that we've just added a cell
// to the upper region
++this->m_UpperCellPointer;
// In case we move one row up, automagically rebuild the left and right grids
this->m_LeftCellPointer = 0; this->m_RightCellPointer = 0;
}
// Check if we should add a new left cell
/*if (cxl > this->m_LeftCellPointer)
{
// Create and add a new cell in the left region
hgeVector pos = this->m_StartGridOffset; pos.x -= width * cxl; pos.y -= height * cyu;
this->AddGrid(pos, false);
// Increase the upper cell pointer to indicate that we've just added a cell
// to the upper region
++this->m_LeftCellPointer;
}
// Check if we should add a new right cell
if (cxr > this->m_RightCellPointer)
{
// Create and add a new cell in the right region
hgeVector pos = this->m_StartGridOffset; pos.x += width * cxr; pos.y -= height * cyu;
this->AddGrid(pos, false);
// Increase the upper cell pointer to indicate that we've just added a cell
// to the upper region
++this->m_RightCellPointer;
}*/
}