PDA

View Full Version : best way for event based levels?


jankoM
04-28-2006, 07:39 PM
Hi,

I have done games with tile based levels and non-tile but the same "location based levels" (you put some stuff to some positions and you have a level).

Now I am making something of a shmupy nature and level has nothing to do with location or position of things but timing of the events. To make a level happen I have to trigger certain events (enemies, powerups..) at certain times and certain locations with certain parameters (velocity, movement...).

This kind of thing is probably used a lot so I am interested how this is normally done... I have a way which I think is nice - but I don't know - maybe there is something much simpler you folks do to make this.

So this is how I do it.

I have a separate xml file for each level. which has records for events like this
, constants are explained in () near them so you see what I mean:

<event time="2000" family="0(enemy)" type="1(small ufo bomber)" positionX="-100" positionY="10" velocityX="3" velocityY="0" intTag1="0(used for kind of movement at this type of object - like: straigth line/sin... - means something else for another type for example cannon)" intTag2="5(how many bombs to drop in case of this object" intTag3="0(unused)" />

<event time="2100" family="0(enemy)" type="1(small ufo bomber)" ........... />

Then I have a class levelManager which has methods:
load(int levelNum) - loads xml to vector of struct
isEventNow(int time) - gets called in main loop during game returns true if event is there
getCurrentEvent() - returns sturcture with all data of current event to execute . Code in main loop then uses this data to spawn new enemy or powerup or whatever wit parameters it got from there.

Am I overcomplicating? how do you do it? or am I shooting totally in wrong direction..

I imagine platypus might use time-trigered events but now that I thougth of another example absolute blue I think it might still be location based (abs. blue must be)... but I do need time based, there is no location in this game.

RoadMaster
04-28-2006, 08:28 PM
Well most of the logic seems fine to me, except at the end things are somewhat abstracted.

One thing I'd do is when you parse the XML file, you could sort the events by their time tag (or preferrably, have them sorted to begin with) and put them into a heap. Then, when your time intervals change, you only need to compare the time with the top of the heap, and pop the top if the time has passed. You'd of course want to check the new top after popping, in case 2 or more events happen simultaneously. This way you won't have to go through every possible event, and only have to check in the order of how many enemies SHOULD spawn.

You could really think of the problem as time or location. In platypus, you theoretically move, it's just that the screen is only moving at a constant speed (say, 10 "units" per second) and you'd use that knowledge to make a location-based map. Of course, this would mean you'd use a different method to spawn enemies than what I mentioned before... but it's just to show you can think of the problem in different ways.

jankoM
04-28-2006, 08:43 PM
Good to see I am kicking is the right direction..

Yes events in xml will be sorted by time to begin with. They are pushed to stl vector. Then start waiting for event no.1 to happen when iut happenes I delete the struct from memory and remove it from vector (well maybe I should just leave it there and clear the whole vector at the end (I think I read it resorts after erase) them and wait for time of event event no2 and so on.... so I never loop througth all the events to find them. Maybe some other structure would be more appropriate for this but I am more or less new to c++ and heaps and such stuff.

Events wont happen at the same time at least not in same milisecond to simplify stuff.

In my game I dont move at all (that's why no location), I remembered another example which suits this more than platypus - clash n' slash

Thanks for your feedback. I will look into heaps and other structures.

(I made the class in the mean-time. It just spawns like million ships right now must debug it a little) If anyone needs source code of very very very simple xml parser that has no dependecies I can give the code (just around 100 lines).

jankoM
04-30-2006, 09:38 AM
Just to update the solution. "time" atribute is now relative from the last event. Simpler to edit and copy paste and if I regualte times (make bigger) between two events all others stay the same (I don't have to change all times behind that event). It allready works and I am satisfied with it.

Bunkai
06-02-2006, 03:12 AM
Hello Janko,

Is there any very easy way to parse XML files, or, should I prefer using some third party XML parser for your idea? I like the fact that the movements are in readable XML file.. Everythink is neat and well organized.

With regards.

jankoM
06-02-2006, 04:00 AM
I made my very mini mini parser that is just one .h file with aprox. 250 lines and does the job (no STL or anything). It supports only the subset of xml that I need it to support.

I used irrXML in winter Break & catch as it minimal and all, but when I tried converting game to mac I had trouble with it (something about STL and stuff) - I looked for others but there wasn't anything really mini and simple and I really don't wanth to complicate where there is no need.

I can upload you the file somewhere if you wish to take a look at it or use it.

edit: otherwise there is also TinyXML http://www.grinninglizard.com/tinyxmldocs/index.html which should also be tiny :)

Bunkai
06-02-2006, 05:46 AM
Yeah, Janko,

I develop for WindowsCE... I will maybe create my own parser too, but i will took at TinyXML as well.. it looks amazing, the idea with readable XML file.. Or maybe, it does not have to be XML file, a text file might be just OK, to store all the variables.. Anyway, I would prefer storing the XML in the registry...

Why have you decided to use XML and not just simple TXT file? this way you have to bother with parsing XML text..

Regads,

Bunkai
06-03-2006, 11:57 AM
Janko,

I just want to let you know, that I started studying XML, how to manipulate with it etc... It seems to be nice to have the code well organized.. However, parsing the file would take some timeprobably during game initialization.. Does it take some noticable time in your case?

Rene.

Bunkai
06-03-2006, 12:02 PM
And, do you store the .xml file in the application resources later?

jankoM
06-03-2006, 12:25 PM
Why have you decided to use XML and not just simple TXT file? this way you have to bother with parsing XML text..

It seemed the most practical thing to me (by far) for certan types of data (I wouldn't make tilemap in it for example).

- You have to make a parser for that simple TXT file too - and you have to decide every time how you will format the text to define all the data you need in this game
* how will you separate objects definitions (newline probably),
* how will you tell the type of object-dataset (first word is it)
* how will you define parameters of objects (name=value and space in between)
* how will you define string params with spaces (I need name="value with space now"

So you get something like:
LEVEL description="Survive this!!"
GREYALIEN delay="100" y="100" velocityX="2" velocityY="2"
GREYALIEN delay="120" y="100" velocityX="2" velocityY="2"
METEOR delay="10" x="10" y="200" velocityX="0" velocityY="5"
POWERUP .........

which is same thing as XML except that with XML you don't care about newlines, tabs, spaces.... AND with XML you have ability to structure data in any way you like...
<LEVEL description="Survive this!!">
<GREYALIEN delay="100" y="100" velocityX="2" velocityY="2" />
<GREYALIEN delay="120" y="100" velocityX="2" velocityY="2" />
<METEOR delay="10" x="10" y="200"
velocityX="0" velocityY="5" />
<POWERUP .........
</LEVEL>

.... you make XML parser only once and you can use it with all your games without any change which I wouldn't be sure for custom data format

------

I didn't notice any speed problems and I think if you make a very simple parser for a well defined subset of XML it shouldn't be any slower than parsing normal text.

You can try WB & C where I used this. I use this in a shooter now too where file is much longer and I didn't notice any delay either and I load it at the begining of each level. I would like to make a stream for this second game (Tango / working title) where I would open level XML file and slowly read/parse events (to allways hold just data for next few events )

Anyway you can see this *very WAT (Work At Beginning)* game here (it has 3 levels - when you notice that position has reset that's when it loaded/parsed and started new level so I could say it takes no time at all.

You can download this game here: http://itmmetelko.com/storage/tango.zip
and you can get my XML parser here: http://itmmetelko.com/storage/nanoxml.h

I have levels in files in a folder levels.

Bunkai
06-03-2006, 01:02 PM
Hello Janko,

Thank you very much for sharing your resources. I am going to download the game to see how it looks like, and thank you for the parser too. You are right, and I will probably use TinyXML. It has decent documentation, so we will see..

Does "Delay" parameter say, when an object should appear in the game? Say there is a click counter, and as it is increasing, if there is match with "Delay", the object would appear. To ensure that the object would be appeared when needed, maybe in Tile map, we coudl implement transparent triggers that would say, when an object should be read from XML.

Regards,

TimS
06-03-2006, 04:49 PM
Sorry to support the side-topic here though I have little to contribute to the main topic, but I just wanted to chime in with support for TinyXML. Very small (mebbe not as small as 250 lines) and easy to use.

As to Janko's original issue, I'd say you're going to need more than just time triggers for wave release... Last time I did a shmup-type game we had a few different trigger types... time, distance of enemy to player (distance of wave, really), number of enemies left in wave (so you could trigger a retreat action when it was down to one guy or whatever), and location of player (we HAD location, though... 'twas important for boss battle screen clearing). I guess if you have "all enemies from this wave are dead" as an event, time between events could be enough, but it'll lead to some very linear wave, wave, wave... gameplay... like space invaders.

Bunkai
06-04-2006, 04:53 AM
Ah Tim,

I see. Thanks for contribution for TinyXML. I will use it then.

Ah ok, I will need to accept the fact that creating a single trigger will not be enough probably.. Well... The game itself will show what kind of triggers will I need. But at least, now I see, thinking that one kind of trigger will be enough is wrong.

Thanks a lot,
Regards,

Bunkai
06-04-2006, 04:55 AM
Janko,

yeah, slowly I get used to the fact that XML will be the way for me go to.. Indeed, have you used simple text editor to create your XML files, or have you used some special purpose XML editor?

I know that whatewer tool I will use, I can get along with it, but I am curious, if there is something well suitable for this purpose.

With regards,