Map Loading

Started by
10 comments, last by BennettSteele 12 years, 8 months ago
[color=#1C2837][size=2]I have a framework set up for a 2D side scrolling game, with a constant background that is scrolled, and my player can now move around in the scene but now I want to place my certain sprites in certain exact locations of the game world. How do I do that.. I don't have any idea about this (please describe from basic)...
Advertisement
you cald use a *simple* text file for the level.

example
00100203000345
12990043003040
12045065643212

where 0 would be blank, 1 through 9 would mean some type of object

and that is a reall simple file format, the XNA sidscroller project uses that.

to save it, you could start by by building a coordinate based container of some kind and fill that with what entity and position -> then write it starting at 0,0 and move along the x axis and write the values of the object, then move onto next line.

make sense ?

Never say Never, Because Never comes too soon. - ryan20fun

Disclaimer: Each post of mine is intended as an attempt of helping and/or bringing some meaningfull insight to the topic at hand. Due to my nature, my good intentions will not always be plainly visible. I apologise in advance and assure you I mean no harm and do not intend to insult anyone.

It might help to know what you're using; which language, and which engines? Also, when you say sprite, to you mean any graphical sprite, or moving character or monster sprites? That, and it depends on your setup, and how your world is stored, I suppose. How do you place the objects that your played encounters? Do you have a separate editor?

I did something similar not too long ago.

I had a tile-based setup, and a separate editor program in which I set the tiles to certain values in order to create various objects in the game. When I wanted to place things like mobs or items, I right-clicked on the position I wanted them at, and stored the mouse's position in a vector of positions for that item type (i.e. I had three mob types, and a vector for positions of mobs of each type). To change the type of mob I was placing, I had the space key change a value "MobType" to a value of between 0 and 2, going back to 0 if it hit 3.

Then I added those positions to my save file (a text tile). The save file consisted of a grid of letters representing tile types, followed by a list of integers, which were broken up by strings that would tell the game what the integers represented. It then created the mob at that position when I loaded the map up in the same program; for me, X and Y coordinates were parameters in the constructor for the mob class, so I didn't need to do anything else other than let the mob class constructor place the mob.

For example:

tilemap
aaaa
abaa
abaa
aaaa
player
3
7
mobs
3
15
6
17
12
6

where every pair of integers after "mobs" is interpreted as the XY coordinates of a mob.

What have you tried so far? What problems are you facing?

you cald use a *simple* text file for the level.

example
00100203000345
12990043003040
12045065643212

where 0 would be blank, 1 through 9 would mean some type of object

and that is a reall simple file format, the XNA sidscroller project uses that.

to save it, you could start by by building a coordinate based container of some kind and fill that with what entity and position -> then write it starting at 0,0 and move along the x axis and write the values of the object, then move onto next line.

make sense ?

hey!! I thought of making the levels the same way... but even though I can set each character read from a file to represent a certain item... How do I get the location of the item on the screen.??

It might help to know what you're using; which language, and which engines? Also, when you say sprite, to you mean any graphical sprite, or moving character or monster sprites? That, and it depends on your setup, and how your world is stored, I suppose. How do you place the objects that your played encounters? Do you have a separate editor?

I did something similar not too long ago.

I had a tile-based setup, and a separate editor program in which I set the tiles to certain values in order to create various objects in the game. When I wanted to place things like mobs or items, I right-clicked on the position I wanted them at, and stored the mouse's position in a vector of positions for that item type (i.e. I had three mob types, and a vector for positions of mobs of each type). To change the type of mob I was placing, I had the space key change a value "MobType" to a value of between 0 and 2, going back to 0 if it hit 3.

Then I added those positions to my save file (a text tile). The save file consisted of a grid of letters representing tile types, followed by a list of integers, which were broken up by strings that would tell the game what the integers represented. It then created the mob at that position when I loaded the map up in the same program; for me, X and Y coordinates were parameters in the constructor for the mob class, so I didn't need to do anything else other than let the mob class constructor place the mob.

For example:

tilemap
aaaa
abaa
abaa
aaaa
player
3
7
mobs
3
15
6
17
12
6

where every pair of integers after "mobs" is interpreted as the XY coordinates of a mob.

What have you tried so far? What problems are you facing?


Thanks for your reply!! By the way I am using c++ with SFML library. Its not the player or monsters that I want, I want objects like walls, etc... to be positioned... and no I don't have an editor ( not yet )
I like your idea of storing the position of the objects in the next lines... will give it a try... but I think it is absolutely necessary to build an editor for this?? (Please correct me if I am wrong)

[quote name='ryan20fun' timestamp='1313657069' post='4850661']
you cald use a *simple* text file for the level.

example
00100203000345
12990043003040
12045065643212

where 0 would be blank, 1 through 9 would mean some type of object

and that is a reall simple file format, the XNA sidscroller project uses that.

to save it, you could start by by building a coordinate based container of some kind and fill that with what entity and position -> then write it starting at 0,0 and move along the x axis and write the values of the object, then move onto next line.

make sense ?

hey!! I thought of making the levels the same way... but even though I can set each character read from a file to represent a certain item... How do I get the location of the item on the screen.??
[/quote]

right now i can think of two ways.

one, is to have the sprite no larger then X size, and each entry in the text file represents that block on screen.

two, more complicated.
first store the how many entries X and Y are in the file.
read in till tab, denotes X position -> read again till tab, denots Y position -> read till newline, denots entity type or 0 for none.
repeat till you have read in all the specified Y lines.

was that clear ?

Never say Never, Because Never comes too soon. - ryan20fun

Disclaimer: Each post of mine is intended as an attempt of helping and/or bringing some meaningfull insight to the topic at hand. Due to my nature, my good intentions will not always be plainly visible. I apologise in advance and assure you I mean no harm and do not intend to insult anyone.


[quote name='newbie_gamer' timestamp='1313674038' post='4850750']
[quote name='ryan20fun' timestamp='1313657069' post='4850661']
you cald use a *simple* text file for the level.

example
00100203000345
12990043003040
12045065643212

where 0 would be blank, 1 through 9 would mean some type of object

and that is a reall simple file format, the XNA sidscroller project uses that.

to save it, you could start by by building a coordinate based container of some kind and fill that with what entity and position -> then write it starting at 0,0 and move along the x axis and write the values of the object, then move onto next line.

make sense ?

hey!! I thought of making the levels the same way... but even though I can set each character read from a file to represent a certain item... How do I get the location of the item on the screen.??
[/quote]

right now i can think of two ways.

one, is to have the sprite no larger then X size, and each entry in the text file represents that block on screen.

two, more complicated.
first store the how many entries X and Y are in the file.
read in till tab, denotes X position -> read again till tab, denots Y position -> read till newline, denots entity type or 0 for none.
repeat till you have read in all the specified Y lines.

was that clear ?
[/quote]
I didn't quite get the second method... could you please be more descriptive..

[quote name='ryan20fun' timestamp='1313674903' post='4850758']
[quote name='newbie_gamer' timestamp='1313674038' post='4850750']
[quote name='ryan20fun' timestamp='1313657069' post='4850661']
you cald use a *simple* text file for the level.

example
00100203000345
12990043003040
12045065643212

where 0 would be blank, 1 through 9 would mean some type of object

and that is a reall simple file format, the XNA sidscroller project uses that.

to save it, you could start by by building a coordinate based container of some kind and fill that with what entity and position -> then write it starting at 0,0 and move along the x axis and write the values of the object, then move onto next line.

make sense ?

hey!! I thought of making the levels the same way... but even though I can set each character read from a file to represent a certain item... How do I get the location of the item on the screen.??
[/quote]

right now i can think of two ways.

one, is to have the sprite no larger then X size, and each entry in the text file represents that block on screen.

two, more complicated.
first store the how many entries X and Y are in the file.
read in till tab, denotes X position -> read again till tab, denots Y position -> read till newline, denots entity type or 0 for none.
repeat till you have read in all the specified Y lines.

was that clear ?
[/quote]
I didn't quite get the second method... could you please be more descriptive..
[/quote]

ill combine it with the way Saldan did his.

the first line of the file could be a nuber specyfiing how many objects are to be places, or read till EOF.

then read in till you find a tab, this would be the X position of the objecy ->
then read in till you find another tab, this will be the Y position of the object ->
then read in till new line( end of the current line ), this would be a int or string indicating what object it is supposed to be.

here is some code i used to load a entity into a game i made.

int numberofframes = 1;
float width = 1;
float height = 1;
float fsx = 1;
float fsy = 1;
float alpha = 0;
wstring ws;

char *buf = new char[20];
memset(buf, 0, 20);

fstream fs(Path);

if(!fs)
{
wstring ws(L"File Not Found");
ws.append(L"\n");
ws.append(Path);
ws.append(L"\n");
ws.append(L"I will probably crash when you click Ok");
MessageBox(NULL, ws.c_str(), L"Error", MB_OK);
MessageBox(NULL, L"Noooooooooooooo", L"Help", MB_OK);
HRESULT r = MessageBox(NULL, L"Continue \?", L"Why you looking at me \?", MB_OKCANCEL);
if(r == IDCANCEL)
{
MsgProc(hWnd, WM_DESTROY, NULL, NULL); // PostQuitMessage(0);
_isquiting = true;
}
}

fs.getline(buf, 20, '\t');
width = (float)atof(buf); // width

fs.getline(buf, 20, '\n');
height = (float)atof(buf); // height

fs.getline(buf, 20, '\t');
fsx = (float)atof(buf); // frame size x

fs.getline(buf, 20, '\n');
fsy = (float)atof(buf); // frame size y

fs.getline(buf, 20, '\n');
numberofframes = (int)atof(buf); // number of frames

fs.getline(buf, 20, '\n');
alpha = (float)atof(buf); // alpha

fs.getline(buf, 20, '\n');
string s;
s.append(mediafolder);
s.append(buf);
ws.append(s.begin(), s.end()); // path to sprite sheet

delete []buf;
sprites.push_back(AnimatedSprite(D3DCOLOR_COLORVALUE(1.0f, 1.0f, 1.0f, alpha), numberofframes, PosX, PosY, Type, (UINT)width, (UINT)height, fsx, fsy, ws.c_str()));


it would expect a file formated like this

320 384
64 64
29
1
donut.bmp


if you did not read the comments in the code:
320 is the width of the image
384 is the height of the image
64 is the horizontal frame size of the animation
64 is the vertical frame size of the animation
29 is how many animations the sprite has
1 is the alpha value, IE is black tranparent ?

understand ?

you could do a similar thing, just make the first line tell the parser how many entities exist.
instread of seperating it by relevance( and because this is map data ) just seperate each entry( for an individual object ) in the file by a tab and do it that way.

it should be simple to addapt the code i pasted into something remotly usable for the level data.

:) :) :)

Never say Never, Because Never comes too soon. - ryan20fun

Disclaimer: Each post of mine is intended as an attempt of helping and/or bringing some meaningfull insight to the topic at hand. Due to my nature, my good intentions will not always be plainly visible. I apologise in advance and assure you I mean no harm and do not intend to insult anyone.


[quote name='newbie_gamer' timestamp='1313677795' post='4850782']
[quote name='ryan20fun' timestamp='1313674903' post='4850758']
[quote name='newbie_gamer' timestamp='1313674038' post='4850750']
[quote name='ryan20fun' timestamp='1313657069' post='4850661']
you cald use a *simple* text file for the level.

example
00100203000345
12990043003040
12045065643212

where 0 would be blank, 1 through 9 would mean some type of object

and that is a reall simple file format, the XNA sidscroller project uses that.

to save it, you could start by by building a coordinate based container of some kind and fill that with what entity and position -> then write it starting at 0,0 and move along the x axis and write the values of the object, then move onto next line.

make sense ?

hey!! I thought of making the levels the same way... but even though I can set each character read from a file to represent a certain item... How do I get the location of the item on the screen.??
[/quote]

right now i can think of two ways.

one, is to have the sprite no larger then X size, and each entry in the text file represents that block on screen.

two, more complicated.
first store the how many entries X and Y are in the file.
read in till tab, denotes X position -> read again till tab, denots Y position -> read till newline, denots entity type or 0 for none.
repeat till you have read in all the specified Y lines.

was that clear ?
[/quote]
I didn't quite get the second method... could you please be more descriptive..
[/quote]

ill combine it with the way Saldan did his.

the first line of the file could be a nuber specyfiing how many objects are to be places, or read till EOF.

then read in till you find a tab, this would be the X position of the objecy ->
then read in till you find another tab, this will be the Y position of the object ->
then read in till new line( end of the current line ), this would be a int or string indicating what object it is supposed to be.

here is some code i used to load a entity into a game i made.

int numberofframes = 1;
float width = 1;
float height = 1;
float fsx = 1;
float fsy = 1;
float alpha = 0;
wstring ws;

char *buf = new char[20];
memset(buf, 0, 20);

fstream fs(Path);

if(!fs)
{
wstring ws(L"File Not Found");
ws.append(L"\n");
ws.append(Path);
ws.append(L"\n");
ws.append(L"I will probably crash when you click Ok");
MessageBox(NULL, ws.c_str(), L"Error", MB_OK);
MessageBox(NULL, L"Noooooooooooooo", L"Help", MB_OK);
HRESULT r = MessageBox(NULL, L"Continue \?", L"Why you looking at me \?", MB_OKCANCEL);
if(r == IDCANCEL)
{
MsgProc(hWnd, WM_DESTROY, NULL, NULL); // PostQuitMessage(0);
_isquiting = true;
}
}

fs.getline(buf, 20, '\t');
width = (float)atof(buf); // width

fs.getline(buf, 20, '\n');
height = (float)atof(buf); // height

fs.getline(buf, 20, '\t');
fsx = (float)atof(buf); // frame size x

fs.getline(buf, 20, '\n');
fsy = (float)atof(buf); // frame size y

fs.getline(buf, 20, '\n');
numberofframes = (int)atof(buf); // number of frames

fs.getline(buf, 20, '\n');
alpha = (float)atof(buf); // alpha

fs.getline(buf, 20, '\n');
string s;
s.append(mediafolder);
s.append(buf);
ws.append(s.begin(), s.end()); // path to sprite sheet

delete []buf;
sprites.push_back(AnimatedSprite(D3DCOLOR_COLORVALUE(1.0f, 1.0f, 1.0f, alpha), numberofframes, PosX, PosY, Type, (UINT)width, (UINT)height, fsx, fsy, ws.c_str()));


it would expect a file formated like this

320 384
64 64
29
1
donut.bmp


if you did not read the comments in the code:
320 is the width of the image
384 is the height of the image
64 is the horizontal frame size of the animation
64 is the vertical frame size of the animation
29 is how many animations the sprite has
1 is the alpha value, IE is black tranparent ?

understand ?

you could do a similar thing, just make the first line tell the parser how many entities exist.
instread of seperating it by relevance( and because this is map data ) just seperate each entry( for an individual object ) in the file by a tab and do it that way.

it should be simple to addapt the code i pasted into something remotly usable for the level data.

:) :) :)
[/quote]


Thank you for such a detailed reply. I think I get it some what... will definitely give it a try!! biggrin.gifbiggrin.gif

There is a fairly simple example in NetHack --- which also has source available for your enjoyment.


It is a 2D dungeon crawler, but the requirements for maps are not dramatically different.


They have a simple Dungeon file format that gives everything you need in that style of game.

Some example levels are Vlad the Impaler's Tower, the gnomish mines, and so on. They include rules for the randomly-generated portions of dungeon, plus rules for specifically-generated areas as well. Some maps mix random areas and fixed areas.

This topic is closed to new replies.

Advertisement