Sign in to follow this  
Nicholas Kong

How to draw enemy death animation if its collision is valid

Recommended Posts

Nicholas Kong    1535

My game loop is structured like this:

 

update 

draw

checkforcollision

sleep

 

I want to draw the death animation in my draw method but the problem is

by the time I check for collision, the draw method already been executed which

means it won't execute this draw code in my Ghost class.

 

 

// if ghost is in a dead state
if(getDeadState())
{
 
 
g.drawImage(ghostMovementAnimation.get(3), (int)position.getX(), (int)position.getY(), null);
}

 

 

The code posted below is in my Laser class.

 

Quick explanation of my collision code:

if collision is valid, remove the laser

set monster dead state to be true

remove the monster

 

 

 

 
/* check the intersection of the laser's rectangle and all the monster's
*  rectangle. If both rectangles intersects, it is a collision
*/
 
if(rectangle.intersects(((Collidable) component).getRectangle()))
{
 
 
// remove laser upon monster contact
Game.getInstance().remove(this);
 
// set monster to be dead
monsterIsDead = true; 
 
// set monster to be dead
((Monster) component).setDeadState(monsterIsDead);
 
 
// remove the monster upon contact of the laser
Game.getInstance().remove(component);
 
Edited by warnexus

Share this post


Link to post
Share on other sites
Squared'D    2427
If you're worried about having to check every frame to see if an entity has died, you can use 2 list, a dead list and a live list. That can also be good for collision detection as you only need to check with things in the live list.

Share this post


Link to post
Share on other sites
Nicholas Kong    1535

Have you considered setting it to a dead state but not removing it? Then after a few frames or a few seconds, you can remove it.

Since I am not removing it upon collision, it would draw the dead animation of the monster. It is in the game looking dead.

 

I am using milliseconds for frames. But I am having trouble figuring out how to make it a few seconds pass as a condition to trigger the removal of the monster.

 

Is it not preferred to draw an image every frame?

Edited by warnexus

Share this post


Link to post
Share on other sites
Squared'D    2427
Ah, then you'll have to keep track of the game time and then store the time of death some where. You can either store it directly in the monster, or if you have a dead list, the dead list can have a structure that includes a reference to the dead monster as well as the time it died.

Also, if you are using a fixed time step, you can just count how many frames have passed.

That's just one suggestion. I'm sure many others will probably also have good ideas.

Share this post


Link to post
Share on other sites
Khatharr    8812

You could place a particle at the enemy location that runs the death animation and then deactivates and signals for cleanup. That would be a hack, though. What's going on with your entity that makes it so strongly coupled to collision testing?

 

Actually, I don't really understand the question, I guess. How are you executing other animations?

Edited by Khatharr

Share this post


Link to post
Share on other sites
Nicholas Kong    1535

You could place a particle at the enemy location that runs the death animation and then deactivates and signals for cleanup. That would be a hack, though. What's going on with your entity that makes it so strongly coupled to collision testing?

 

Actually, I don't really understand the question, I guess. How are you executing other animations?

The game is a 2D game in Java. The monster is stored in a list. Game goes through this list that has object that needs to be updated or drawn(like the monster). Laser checks through the list looking for objects of a Monster Type. If laser has found objects of a Monster Type. Create rectangle object around the monster. Test intersection of its rectangle with the laser rectangle. If valid, perform the removal of the monster. 

 

I can perform the removal of the monster. I just cannot figure out how to make the dead animation appear because the monster is already removed.

 

If I do not remove the monster, the dead animation is shown but the monster does not get removed.

 

The monster animation is done by picking out a specific image to be drawn from the list (where these animations files are stored).

 

What do you mean by a hack?

Edited by warnexus

Share this post


Link to post
Share on other sites
Satharis    2443
Two things.

One I'm not really sure why you're drawing before updating.. is there some good reason for that?

Two I don't really see why your collision is so coupled to removing your entities. If anything when an enemy gets hit it should go into a sort of "dead mode" where it plays its animation but is still a live entity in the entity list, getting updated and drawn. Once it's animation is complete then you would decide what to do with it, maybe set some flag on it to "disposed" and have your update loop remove disposed entities when it spins around again.

There's not really a rush to remove entities.

Share this post


Link to post
Share on other sites
Nicholas Kong    1535

Two things.

One I'm not really sure why you're drawing before updating.. is there some good reason for that?

Two I don't really see why your collision is so coupled to removing your entities. If anything when an enemy gets hit it should go into a sort of "dead mode" where it plays its animation but is still a live entity in the entity list, getting updated and drawn. Once it's animation is complete then you would decide what to do with it, maybe set some flag on it to "disposed" and have your update loop remove disposed entities when it spins around again.

There's not really a rush to remove entities.

oops...

I actually update first then draw. I flipped it around... 

 

I will think about #2. I am still learning game programming so it is nice to see that is how I should be doing it based on #2.

Edited by warnexus

Share this post


Link to post
Share on other sites
Satharis    2443

oops...

I actually update first then draw. I flipped it around... 

 

I will think about #2. I am still learning game programming so it is nice to see that is how I should be doing it based on #2.

Given game programming, there is no silver bullet or one fits all methodology of course, but.

 

Usual generic way to handle collision in a game with a bunch of like "entities" is to have some higher up object like the gamestate or.. whatever do the actual collision checks between entities, perhaps with the use of a helper class that provides the methods.

 

However the entities themselves should probably respond to the collisions, if two entities collide you could call something like HandleCollision(Entity&) on them, to let them "decide" how to react to what they collided with. This lets both objects know they collided and gives you room for expansion. Maybe you collided with a powerup, the player could set the buff the powerup gives on itself, the powerup could deal with destroying itself and maybe spawning particle effects or something.

 

It's more natural to let the objects control their reactions to the environment than to let another object do it unless the object is some kind of.. controller, and the entities themselves are just dumb data.

Share this post


Link to post
Share on other sites
VladR    722

You definitely need two separates states in your FiniteStateMachine:

 

enum EnemyState
{
.....
Dying,
Dead,
.......
};

 

 

The Logic to make sure you handle the switch to Dying status just once (even if the laser will be on screen few more seconds):

 

if (Is_There_Collision_Of_Laser_With_Enemy ())
  if ((EnemyStatus !=Dying) && (EnemyStatus != Dead))
  {
    EnemyStatus = stDying;
    Start_Dying_Animation (Time_Of_Current_Frame, Length_Of_Dying_Animation)
  }

 

 

.....

Now you need to switch from Dying to Dead, when Dying animation is over in the place where you handle the states:

 

if (EnemyStatus == Dying)
{
  if (Time_Of_Current_Frame >= Time_End_Of_Dying_Animation)
  {
    EnemyStatus = Dead;
  }
}

 

 

Share this post


Link to post
Share on other sites
dmatter    4821

warnexus, on 11 May 2013 - 06:09, said:
I am having trouble figuring out how to make it a few seconds pass as a condition to trigger the removal of the monster.

You must know when the death animation finishes though? I.e. How many frames (or milliseconds) the animation requires/has-remaining before it finishes.

I think the point is that you need to model the state transitions as a kind of state in its own right, e.g. moving between being "alive" to being "dead" involves a state-transition called "dying". So a monster gets hit by a laser, it enters into a "dying" state; your update logic will respond to that state by playing the death animation. Once the animation completes your update logic removes the monster (or if the monster should just hang around being dead then you set it to a "dead" state).

Share this post


Link to post
Share on other sites
Nicholas Kong    1535

warnexus, on 11 May 2013 - 06:09, said:
I am having trouble figuring out how to make it a few seconds pass as a condition to trigger the removal of the monster.

You must know when the death animation finishes though? I.e. How many frames (or milliseconds) the animation requires/has-remaining before it finishes.

I think the point is that you need to model the state transitions as a kind of state in its own right, e.g. moving between being "alive" to being "dead" involves a state-transition called "dying". So a monster gets hit by a laser, it enters into a "dying" state; your update logic will respond to that state by playing the death animation. Once the animation completes your update logic removes the monster (or if the monster should just hang around being dead then you set it to a "dead" state).

Thanks for the ideas! I got it to work!

 

Why is there a -1?

Edited by warnexus

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this