Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Handling level logic ( events ) in the engine.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 xynapse   Members   -  Reputation: 151

Like
0Likes
Like

Posted 12 May 2012 - 03:48 AM

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

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

Posted Image

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

Sponsor:

#2 AlanSmithee   Members   -  Reputation: 1022

Like
0Likes
Like

Posted 12 May 2012 - 07:36 AM

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.

Edited by AlanSmithee, 12 May 2012 - 07:39 AM.


#3 xynapse   Members   -  Reputation: 151

Like
0Likes
Like

Posted 12 May 2012 - 08:19 AM

Clear, thanks ;)

I have made it more complex, by creating a

- Game State manager

and

- Internal messaging ( Event handling )


Posted Image

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.

Posted Image

Edited by xynapse, 12 May 2012 - 08:52 AM.

perfection.is.the.key

#4 AlanSmithee   Members   -  Reputation: 1022

Like
0Likes
Like

Posted 12 May 2012 - 10:46 AM

Looking good :)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS