Hello all,
I am a newbie in Java game development though I have been doing general java programming for quite some time now. This post is more of a question about software engineering for game development.
One of the things that I discover I need to do a lot during game development is animation. As I was making a few mini games, a question appears and got my attention - How should I program for the “updating” animation?
I was quite used to Model-View-Controller and making ordinary Swing applications. Traditionally with Swing application, when something happens in the “Model”, I want the “View” to update straight away. Similarly, when I do something with the “Controller”, I want “View” to response straight away.
However it’s not quite the same in game development when animation involves. Sometimes, when something happens in model, the view does not update straight away, instead, it has to go through some animation before the view catches up with the model’s latest status. I just don’t know what is the best way to program the updates with animations… Some advice would be great here…
For example, I have a Hero object and a Monster object. The Hero object has a attack method that will shred away Monster’s health. If Monster’s health reaches zero, it dies. A traditional and simplest way would be:
Class Hero {
…
void attack (Monster m) {
m.health–;
if (m.health <= 0) {
m.die();
}
}
}
As you can see, everything happens within a fraction of a second… and there’s no time to wait for the animation to finish.
My thoughts on inserting animation is to have multiple threads - one for the model and one for the animation… The animation thread is looped with timed sleep intervals like many game developers would do. The model thread get suspended when certain animations are playing:
Class Hero {
…
void attack (Monster m) {
playAttackAnimation(); // falls into an infinite while loop and wait for the animation to finish
m.health–;
if (m.health <= 0) {
m.playDyingAnimation (); // falls into an infinite while loop and wait for the animation to finish
m.die();
}
}
void playAttackAnimation() {
status = HERO_ATTACKING;
while (status == HERO_ATTACKING) { // the infinite while loop and wait for the animation thread to update status to something else
}
}
}
For the animation thread:
while (…) {
…
switch (hero.status) {
case Hero.HERO_ATTACKING:
renderHeroAttack(); // how this renders depends on the value of heroAttackFrame which acts like a frame counter for the animation
heroAttackFrame++;
if (heroAttackFrame >= heroAttackEndFrame) {
hero.status = HERO_STANDING; // or any other status after ATTACKING
heroAttackFrame = 0; // reset the attack frame counter back to zero
}
}
}
Personally I don’t think using infinite while loop is a wise way of doing animation. However on the other hand, I don’t think it’s a very good idea either to finish everything in the model and then pause and wait for the animation to play and catch up the latest status of the model, i.e. finish running m.health–; and m.die(); before start calling playAttackAnimation(); and m.playDyingAnimation (); . (Of course, playAttackAnimation(); and m.playDyingAnimation (); are not just empty while loops now. They handle the animations here, so there is only one thread to handle both model and animation. ) Understand what I mean here? The main point of this post basically is about how and when I should animate the updates triggered by the model or the controller…
Surely I don’t want to do this:
Class Hero {
…
void attack (Monster m) {
renderAttackFrame1();
Thread.sleep(…);
renderAttackFrame2();
Thread.sleep(…);
renderAttackFrame3();
Thread.sleep(…);
renderAttackFrame4();
Thread.sleep(…);
…
m.health–;
renderAttackFrameX();
Thread.sleep(...);
renderAttackFrameY();
Thread.sleep(...);
renderAttackFrameZ();
Thread.sleep(...);
...
if (m.health <= 0) {
m.die();
m.renderDyingFrame1 ();
Thread.sleep(...);
m.renderDyingFrame2 ();
Thread.sleep(...);
m.renderDyingFrame3 ();
Thread.sleep(...);
...
}
}
}
Any better suggestions from anyone?