Sign in to follow this  

Handling level logic ( events ) in the engine.

This topic is 2042 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am thinking of a way to handle level logic in the game.

Let's imagine we have a situation like this ( quick drawing )

[img]http://extraordinaire.pl/gd/logic/0.jpg[/img]

We have two rooms ( Room1, Room2 ) and Doors.

Player has to go from Room1 to Room2 in order to progress with the game.
To achieve that,

- he has to grab ( collide with ) the key,
- collide with the doors ( what will check if he has the key and if true, doors will open )
- after he goes through the doors to Room2 player hits 'trigger object' ( collides with it ) and doors close - so there is no way you can go back to Room1.


How is this normally handled in small games ?
How is logic handled for levels like the one above and more complicated situations ?

Is it called Event Handling ( [url="http://www.gamedev.net/page/resources/_/technical/game-programming/effective-event-handling-in-c-r2459"]http://www.gamedev.net/page/resources/_/technical/game-programming/effective-event-handling-in-c-r2459[/url] ) ?

Thanks!

Share this post


Link to post
Share on other sites
Hello.

The very basics is just straight-forward conditional programming:
[code]if (this condition)
then do this
else if (this condition)
then do this[/code]
Actually very much like what you presented yourself.

Here is what some psuedo-code of your situation might loook like:
[code]if (check_collision(player->hitbox(), key->hitbox())
player->has_key(true);

if (check_collision(player->hitbox(), door->hitbox())
if (player->has_key() && state == STATE_ROOM_ONE)
{
state_manager->change_state(STATE_ROOM_TWO);
player->choords().x = 0; // Move player-entity from right side (where he enter) of room 1 to left side of room 2 (where he exits)
}[/code]

A state-manager is an advanced "switch" that keeps track of the current state and handle changing states. It would be hard for me to explain it in detail to you here but just google "finite state machine" and it should get you started.

And that's pretty much all you need.

1. Register events (key presses, mouse klicks and what else event you might want to act upon)
2. Move enteties and check other logic such as collision detection
3. Change state if needed
4. Render
5. Repeat from step 1

Ofcourse the complexity grows with the size of the project but the basics are still the same, a general tip is to keep it simple.
That logic that you presented is solid, very straight.forward and gets the job done, go with it! =)

BR / AS. Edited by AlanSmithee

Share this post


Link to post
Share on other sites
Clear, thanks ;)

I have made it more complex, by creating a

- [url="http://gamedevgeek.com/tutorials/managing-game-states-in-c/"] Game State manager[/url]

and

- Internal messaging ( [url="http://www.gamedev.net/page/reference/index.html/_/technical/game-programming/simple-event-handling-r2141"]Event handling[/url] )


[img]http://extraordinaire.pl/gd/logic/1.jpg[/img]

So thanks to this, when my GUI button is clicked i get message sent to Engine, which then forwards this to m_pCurrentGameState ( which is a Game State that has been activated by the client ).


Now after loading the level ( via Level Manager Class ) - when collision occurs, message is sent through the disptacher (singleton) to the engine.
This message has additional payload that consists of ObjectA and ObjectB pointers, but every message payload can be customized.

So this message is received in Engine Class and then forwarded to Current Game State,
where inside this Game State i just handle incoming messages from engine by doing 'switch' - thanks to this
i can write down the level logic by testing messages coming in like: COLLISION PLAYER WITH "ObjectX", GUI BUTTON CLICKED "GUI control ID" and i have finally
managed to generalize my internal engine messaging system.

[img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] Edited by xynapse

Share this post


Link to post
Share on other sites

This topic is 2042 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this