-
Notifications
You must be signed in to change notification settings - Fork 0
/
MapLoader.cpp
313 lines (256 loc) · 8.7 KB
/
MapLoader.cpp
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include "StdAfx.h"
#include "MapLoader.h"
#include "Globals.h"
MapLoader::MapLoader(dxManager* d, char* mapFileName,char* keyFileName, int rows, int cols, float blockSize)
{
dx = d; // get the refrence to the dxManager
mapLength = cols; // how far across the map is
mapHeight = rows; // how tall the map is
gridBlockSize = blockSize; // size of each block in our gird based map
// Make our array size dynamically
mapArray = new char*[rows];
for( int i = 0 ; i < rows ; i++ )
mapArray[i] = new char [cols];
keyList.clear(); // make sure our map is empty
finishPoint = new Checkpoint();
checkpointCounter = 0; // will be incremented every time we add a checkpoint
checkpointArray = new Checkpoint*[NUM_OF_CHECKPOINTS]; // Initalize our array of checkpoints
int HR = readMapFile(mapFileName); // process the map text file
int HR2 = readKeyFile(keyFileName); // process the key to the map file
if(HR != 0 || HR2 != 0)
cout << "Problem Reading Text File" << endl;
createMap(-2.0f,1.1f);
}
// reads in the MapRepresentation file and stores all the chars in an array
int MapLoader::readMapFile(char* fileName)
{
// read a text file
ifstream input;
// open for reading
input.open(fileName);
// Make sure we can open the file
if(input.fail())
return -1;
// read in a chracter at a time
char charData;
int rowCounter = 0;
int colCounter = 0;
// loop through the text file
while(!input.eof())
{
charData = input.get(); // grab each character individually
// if we have reached the end of the text file, exit the loop
if((int)charData == -1)
break;
// check if we reached the end of a line in the text file, dont want to store that
if((int)charData != 10)
{
mapArray[rowCounter][colCounter] = charData;
colCounter++;
}
else
{
rowCounter++;
colCounter = 0;
}
}
input.close();
return 0;
}
// reads in the MapKey file that determines which char values in the map represent which sprite
int MapLoader::readKeyFile(char* fileName)
{
// read a text file
ifstream input;
// open for reading
input.open(fileName);
// Make sure we can open the file
if(input.fail())
return -1;
// reads each line from the txt file
char text[40];
while(!input.eof())
{
input.getline(text,40);
char key = text[0]; // the representation in the MapFile
if(key == 'E')// E is put at the end of the text file to signify we are done
break;
char* value = new char[(int)input.gcount()-1];
// copy the char values over from text to value, ignoring the beginning key part
for(int i = 2; i < (int)input.gcount(); i++)
{
value[i-2] = text[i];
if(text[i] == '\0')//reached the end of the line
break;
}
keyList.insert(map<char,char*>::value_type(key,value)); // store the key and value in the keyList
}
input.close();
return 0;
}
// space out our sprites on the map depending on where they were in the text file
// parameters define where the top left hand corner or the map is
void MapLoader::createMap(float topLeftX, float topLeftY)
{
float xPos = topLeftX;
float yPos = topLeftY;
bool foundP2 = false;
map<char,char*>::iterator ptr = keyList.begin();
int counter = 0;
for(int i = 0; i < mapHeight; i++) // rows
{
for(int j = 0; j < mapLength; j++) // cols
{
char key = mapArray[i][j]; // grab the char from each row,col
ptr = keyList.find(key);
if(ptr != keyList.end()) // make sure there is a match
{
char* imgFile = (ptr->second); // grab the name of the image file the key uses
if (strcmp(imgFile, "Images/platformSingle.png") == 0 || strcmp(imgFile, "Images/platformVert.png") == 0 || strcmp(imgFile, "Images/platformLeft.png") == 0 || strcmp(imgFile, "Images/platformMiddle.png") == 0 || strcmp(imgFile, "Images/platformRight.png") == 0)
{
Wall* newWall = new Wall(dx, charToTCHAR(imgFile));
newWall->SetPosition(xPos, yPos);
dx->AddWall(newWall);
}
else if (strcmp(imgFile, "floorT") == 0)
{
Wall* newWall2 = new Wall(dx, charToTCHAR("Images/platformV.png"));
newWall2->SetPosition(xPos+gridBlockSize/2-0.025f, yPos-0.025f);
dx->AddWall(newWall2);
Wall* newWall = new Wall(dx, charToTCHAR("Images/platformMiddle.png"));
newWall->SetPosition(xPos, yPos);
dx->AddWall(newWall);
}
else if (strcmp(imgFile, "crossPlatform") == 0)
{
Wall* newWall2 = new Wall(dx, charToTCHAR("Images/crossPlatH.png"));
newWall2->SetPosition(xPos, yPos);
dx->AddWall(newWall2);
Wall* newWall = new Wall(dx, charToTCHAR("Images/crossPlatV.png"));
newWall->SetPosition(xPos, yPos);
dx->AddWall(newWall);
}
else if (strcmp(imgFile, "Images/flame1.png") == 0)
{
Trap* newTrap = new Trap(dx,imgFile, xPos, yPos);
newTrap->SetAnimStepTime(3000); //3 second flames
dx->AddTrap(newTrap);
}
else if (strcmp(imgFile, "Images/saw1.png") == 0)
{
Trap* newTrap = new Trap(dx,imgFile, xPos, yPos-gridBlockSize);
newTrap->setTileSize(gridBlockSize);
dx->AddTrap(newTrap);
}
else if (strcmp(imgFile, "Images/Turret.png") == 0)
{
Trap* newTrap = new Trap(dx,imgFile, xPos, yPos-gridBlockSize);
dx->AddTrap(newTrap);
Trap* newTrap2 = new Trap(dx,"crosshairs", xPos+(gridBlockSize*1), yPos-(gridBlockSize*1)); //1 down, 1 over
newTrap->SetTriggerTarget(newTrap2); //set this turrent to these crosshairs
dx->AddTrap(newTrap2);
}
else if (strcmp(imgFile, "Images/spikes.png") == 0)
{
Trap* newTrap = new Trap(dx,imgFile, xPos, yPos-gridBlockSize+0.16f);
dx->AddTrap(newTrap);
}
else if (strcmp(imgFile, "Images/spikesHang.png") == 0)
{
Trap* newTrap = new Trap(dx,imgFile, xPos, yPos+gridBlockSize-0.14f);
dx->AddTrap(newTrap);
}
else if (strcmp(imgFile, "Images/checkpoint.png") == 0) // lets us add our checkpoints
{
SimpleSprite* currentSprite; // Add the img of our checkpoint
dx->MakeSprite(charToWchar(imgFile), ¤tSprite);
currentSprite->SetPosition(xPos, yPos, 0.0f); // Set the sprites position and scale
currentSprite->SetScaling(0.4f,0.5f);
// store the checkpoints location in an array
checkpointArray[checkpointCounter] = new Checkpoint(xPos, yPos);
checkpointCounter++;
}
else if(strcmp(imgFile, "playerstart") == 0) // found in the map where player 1 is starting
{
p1StartX = xPos;
p1StartY = yPos;
}
else if(strcmp(imgFile, "player2start") == 0) // found in the map where player 2 is starting
{
p2StartX = xPos;
p2StartY = yPos;
foundP2 = true;
}
else if(strcmp(imgFile, "FinishPoint") == 0) // found the finish point for the race
{
finishPoint->x = xPos;
finishPoint->y = yPos;
SimpleSprite* currentSprite;
dx->MakeSprite(charToWchar("Images/JewelGear.png"), ¤tSprite);
currentSprite->SetPosition(xPos, yPos, 0.0f);
}
else if(strcmp(imgFile, "empty") != 0) // we wont be loading a sprite if there is an empty space
{
SimpleSprite* currentSprite;
dx->MakeSprite(charToWchar(imgFile), ¤tSprite);
currentSprite->SetPosition(xPos, yPos, 0.0f);
}
xPos += gridBlockSize;
counter++;
}
}
xPos = topLeftX;
yPos -= gridBlockSize;
}
if (!foundP2)
{
p2StartX = p1StartX;
p2StartY = p1StartY;
}
}
// lets us convert from a char to a tchar
TCHAR* MapLoader::charToTCHAR(char* myString)
{
char* pchString = myString;
// Find out required string size
int iRequiredSize = ::MultiByteToWideChar(CP_ACP, NULL, pchString, -1, NULL, 0);
// Alloc buffer for converted string
TCHAR* pwchString = new TCHAR[iRequiredSize];
// Do the string conversion
MultiByteToWideChar(CP_ACP, NULL, pchString , -1, pwchString, iRequiredSize);
return pwchString;
}
// returns the length(how far horz) of the map, in pixels based on the gird block size
float MapLoader::getMapLengthP(void)
{
return mapLength*gridBlockSize;
}
// returns the height(how far vert) of the map, in pixels based on the gird block size
float MapLoader::getMapHeightP(void)
{
return mapHeight*gridBlockSize;
}
//gets the start position of player 1
void MapLoader::getP1Start(float* x, float* y)
{
*x = p1StartX;
*y = p1StartY;
}
//gets the start position of player 2
void MapLoader::getP2Start(float* x, float* y)
{
*x = p2StartX;
*y = p2StartY;
}
Checkpoint* MapLoader::getFinishPoint()
{
return finishPoint;
}
int MapLoader::getNumOfCheckpoints(void)
{
return checkpointCounter;
}
//deconstructor
MapLoader::~MapLoader(void)
{
}