Game structure

Ok so ive been working on a mario clone game for a while and i realized that if my code continued to grow larger at the rate that it was, it would become extremely cluttered, as well as have many class dependencies which would get annoying. This probably doesnt matter for such a small project, but id rather have an efficient game structure now than having to worry about it in the future. And so I searched for a better way to accept events, handle them, as well as distribute data amongst my classes without them knowing of each other. This lead me to the observer pattern. However, ive run into a few issues. First of all, Im confused as to how my observers should handle the events theyre notified of. Also, I dont know how i should give the observer class the information it needs to handle the event in the first place. An example being that i have Player_1 walking left and Enemy_1 walking right.when they collide, Player_1 should die. But how would i detect their collision, have the subject notify all the object of movement? but then how would i detect the collision without direct access to each of their collision data...

also im unsure as how to structure events as at the moment im just using enums set to different states like keyboard event, tick event, etc., but this is inefficient as it doesnt give any real data of the event, just the type of event occuring.

Overall id like to know of an efficient way to implement it as it has me... not utterly confused but rather annoyed as im not sure how to implement the psuedo code ive seen correctly.
which library are you using to implement the code? Like sfml or sdl?

If you learn those two libraries you might answer some of your questions.
Last edited on
all im using is sdl with opengl.
Collision detection in opengl has been described several places on the web. Generally it is testing clipping rectangles of drawn objects. if you are receiving the events in a loop you can farm the event out to something that is useful. There are tutorials on game design with SDL and/or opengl, I would recommend them. Data of events may just tell you the states of the event how you choose to handle them is your choice in the context of the game. Most games follow the same ideas in general design. I don't remember any web sites for this it has been a while since I looked it up.
well i understand how to detect a collision, its just that i need each class's collision data to check if they collide, which im having trouble doing under the observer pattern... i suppose a way to do it is in the notify function call include a parameter telling what type the event is and a pointer to the class performing the event, etc... So my problem isnt the detection, its the complications that im running into trying to get the data to check for a collision.
usually in game design you have a loop controlling things, one part polls user event and another part things control the movement of objects. In the Movement of object we would compare to all the objects to see if we collided with something. If it collided we might pump a message, with offending objects we to another part of the program to react properly. This is one thought. There are other methods for doing this operation.
yeah but my problem is the observer pattern not the collision detection
Topic archived. No new replies allowed.