Handling level logic ( events ) in the engine.

Started by
2 comments, last by AlanSmithee 11 years, 11 months ago
I am thinking of a way to handle level logic in the game.

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

0.jpg

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 ( http://www.gamedev.net/page/resources/_/technical/game-programming/effective-event-handling-in-c-r2459 ) ?

Thanks!
perfection.is.the.key
Advertisement
Hello.

The very basics is just straight-forward conditional programming:
if (this condition)
then do this
else if (this condition)
then do this

Actually very much like what you presented yourself.

Here is what some psuedo-code of your situation might loook like:
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)
}


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.
Clear, thanks ;)

I have made it more complex, by creating a

- Game State manager

and

- Internal messaging ( Event handling )


1.jpg

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.

smile.png
perfection.is.the.key
Looking good :)

This topic is closed to new replies.

Advertisement