-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMapper.cpp
More file actions
152 lines (119 loc) · 3.37 KB
/
CMapper.cpp
File metadata and controls
152 lines (119 loc) · 3.37 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
#include "CMapper.h"
//--------------------------- Init ---------------------------------------
//
// This method needs to be called before you can use the instance.
//------------------------------------------------------------------------
void CMapper::Init(int MaxRangeX, int MaxRangeY)
{
//if already initialized return
if(m_NumCellsX) return;
m_dCellSize = CParams::dCellSize;
//first calculate how many segments we will require
m_NumCellsX = (int)(MaxRangeX/m_dCellSize)+1;
m_NumCellsY = (int)(MaxRangeY/m_dCellSize)+1;
//create the 2d vector of blank segments
for (int x=0; x<m_NumCellsX; ++x)
{
vector<SCell> temp;
for (int y=0; y<m_NumCellsY; ++y)
{
temp.push_back(SCell(x*m_dCellSize, (x+1)*m_dCellSize, y*m_dCellSize, (y+1)*m_dCellSize));
}
m_2DvecCells.push_back(temp);
}
m_iTotalCells = m_NumCellsX * m_NumCellsY;
}
//-------------------------------------------------------------
void CMapper::Update(double xPos, double yPos)
{
//check to make sure positions are within range
if ( (xPos < 0) || (xPos > CParams::WindowWidth) ||
(yPos < 0) || (yPos > CParams::WindowHeight) )
{
return;
}
int cellX = (int)(xPos / m_dCellSize );
int cellY = (int)(yPos / m_dCellSize );
m_2DvecCells[cellX][cellY].Update();
return;
}
//---------------------------------------------------------------
int CMapper::TicksLingered(double xPos, double yPos)const
{
//bounds check the incoming values
if ( (xPos > CParams::WindowWidth) || (xPos < 0) ||
(yPos > CParams::WindowHeight)|| (yPos < 0))
{
return 999;
}
int cellX = (int)(xPos / m_dCellSize);
int cellY = (int)(yPos / m_dCellSize);
return m_2DvecCells[cellX][cellY].iTicksSpentHere;
}
//------------------------- Visited --------------------------------------
//
//------------------------------------------------------------------------
bool CMapper::BeenVisited(double xPos, double yPos)const
{
int cellX = (int)(xPos / m_dCellSize);
int cellY = (int)(yPos / m_dCellSize);
if (m_2DvecCells[cellX][cellY].iTicksSpentHere > 0)
{
return true;
}
else
{
return false;
}
}
//--------------------------------- Render -------------------------------
//
// renders the visited cells. The color gets darker the more frequently
// the cell has been visited.
//------------------------------------------------------------------------
void CMapper::Render(HDC surface)
{
for (int x=0; x<m_NumCellsX; ++x)
{
for (int y=0; y<m_NumCellsY; ++y)
{
if (m_2DvecCells[x][y].iTicksSpentHere > 0)
{
int shading = 2 * m_2DvecCells[x][y].iTicksSpentHere;
if (shading >220)
{
shading = 220;
}
HBRUSH lightbrush = CreateSolidBrush(RGB(240,220-shading,220-shading));
FillRect(surface, &m_2DvecCells[x][y].Cell, lightbrush);
DeleteObject(lightbrush);
}
}
}
}
//----------------------------------- Reset ------------------------------
void CMapper::Reset()
{
for (int x=0; x<m_NumCellsX; ++x)
{
for (int y=0; y<m_NumCellsY; ++y)
{
m_2DvecCells[x][y].Reset();
}
}
}
int CMapper::NumCellsVisited() const
{
int total = 0;
for (int x=0; x<m_NumCellsX; ++x)
{
for (int y=0; y<m_NumCellsY; ++y)
{
if (m_2DvecCells[x][y].iTicksSpentHere > 0)
{
++total;
}
}
}
return total;
}