Jump to content
  • Advertisement
Sign in to follow this  

Handling level logic ( events ) in the engine.

This topic is 2403 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 )


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 ) ?


Share this post

Link to post
Share on other sites

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())

if (check_collision(player->hitbox(), door->hitbox())
if (player->has_key() && state == STATE_ROOM_ONE)
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. Edited by AlanSmithee

Share this post

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

I have made it more complex, by creating a

- Game State manager


- Internal messaging ( Event handling )


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 Edited by xynapse

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!