views:

157

answers:

2

I have an idea of how I want to approach this but i'm not sure if it is ideal. By event I mean for example, if the player wins, a bunch of sparks fly for 1 second. I was thinking of creating my game engine class, then creating a game event base class that has 3 void functions, update, draw, render. There could be for example fireforks for collecting 100 coins for 3 seconds. The way I want to implement it is by having an event vector in my game engine where I can push the fireforks animation in. once something is pushed in the vector, the game does event[i].render() etc... For removing it I thought that each event could have an event length in frames, and each frame a uint is increased, if the uint matches the length, it is popped from the vector. I just wasnt sure if doing it like this was the best way.

Thanks

+1  A: 

There are many, many different ways to skin this cat. For example, one of your routines could return a bool indicating whether or not this animation should be popped off the animation queue. I'm not sure what the differences are between update, draw and render, but all that is besides the point...

My recommendation would be to read. Read up on what other animation engines are doing, go find a good graphics programming gems book, and glean ideas and techniques from preexisting and established implementations. An even better solution would be to use one of them, saving you much pain and agony.

fbrereto
Unless of course they are a masochist.
ChaosPandion
+1  A: 

I would have each event instance have a method called isDone, or something like that. Then, for each frame, iterate through your events and:

if (event.isDone()) {
    //remove the event
} else {
    event.update();
}

Doing it this way allows for easier changes in the future. Not all events will last for a fixed amount of time (this might not be true for your game), some might even depend on things other than the current frame.

But in your eventBaseClass, you could define isDone as:

return this.endFrame >= game.currentFrame;

and override it in any events that you need to.

Wallacoloo
I really like this! Thanks!, But increasing the currectframe all the time can lead to overflow?
Milo
Assuming a 32bit architecture, and 60 frames a second, it would overflow after 828.5 days: (2**32)/60/60/60/24
Wallacoloo
Ah, I need to brush up on my math :-p, Thanks, Ill implement it this way, but I still think that I should make an if so that after 828.5 days it resets to 0 ;-)
Milo