PLEX Loading Maps From A File

loadmapfromfile_screenshot.jpgTotal Time – 6 hours.

Okay, so I finally got a map to load from a file. I had a few problems I had to deal with. Reading the file in I had issues with the formatting so I did have to change it a little from what I previously described in the last post. Instead of having all of the maps in one map file, I’ve decided to break them up into multiple files

My reasoning was: A character can only be on one map at a time. If I kept all of the maps in a single map file then I would have to parse all the way through the map file to find where the map data started. When I started thinking about this I realized this may end up being a problem. Maps can have a huge amount of data in them, after all they are at least 15 wide x 15 tall x 4 deep. So, the easier solution would be to keep each map in its own separate file. Therefore I added on a map class that will keep track of which map file data is to be loaded for the different maps in the game.

Because each game someone plays the character will now have multiple maps as well as a player file and npc file, I decided to make a single file for the saved game which outlines all the different file a single game uses. For instance, when you save your game it will create unique files for the state of every map in the game, as well as a player file and npc file. It saves the names of these files in a saved game file.

So, when a saved game is loaded, it opens the saved game file, reads in the files for the player data, npc data, and then all the files for the maps. These filenames are stored in the map class and the game class. From there, the game can load the player and npc data to setup the game state back to where it originally stopped, as well as read in the data for whichever map the character is currently on.

The only problem I’m running into right now has to do with drawing the blocks on the game screen. Using the function I wrote before for the online version of the game, I was expecting it to draw blocks the same way it was before. Instead the blocks seem to be covering the screen completely and I’ll need to go in and figure out why. I believe it has something to do with the format I’m using to draw the blocks to the screen but I’m not 100% on that.

One decision I decided to make was to have every plex file have a .plex extension, no matter which type of file it was (map, player, npc). My thought process here was if I use a saved game file then I already know which format each file is in and I can parse them accordingly.

Map Parser

/***********
* Purpose: parse a story map file
* Precondition: file is valid and decrypted
* Postcondition: map has been loaded into the story map array
***********/

bool game::parseMapFile(char *filename)

{

ifstream file;

char name[255], peek, block[2], x[3], y[3], z[3];file.open(filename);

if (!file) //could not open the file

return false;

while (!file.eof())

{

file.get(x, 3, ','); //get first coordinate

file.ignore();

file.get(y, 3, ','); //get second coordinate

file.ignore();

file.get(z, 3, 't'); //get third coordinate

file.ignore();

file.get(block, 2, 'n'); //get the block

file.ignore();

int newz = atoi(z);

int newx = atoi(x);

int newy = atoi(y);

int newblock;

if (block[0] == '-') //no block in that spot

newblock = -1;

else

newblock = atoi(block);

//covert coordinates to integers

//fill the map array

switch (newz)

{

case 0: storymap0[newx][newy] = newblock;

break;

case 1: storymap1[newx][newy] = newblock;

break;

case 2: storymap2[newx][newy] = newblock;

break;

case 3: storymap3[newx][newy] = newblock;

break;

case 4: storymap4[newx][newy] = newblock;

break;

default:

break;

}

}

return true;

}

You may also like...

Leave a Reply