Jump to content

  • Log In with Google      Sign In   
  • Create Account


How to draw enemy death animation if its collision is valid


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
12 replies to this topic

#1 warnexus   Prime Members   -  Reputation: 1409

Like
0Likes
Like

Posted 10 May 2013 - 09:11 PM

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, 11 May 2013 - 01:25 PM.


Sponsor:

#2 Squared'D   Members   -  Reputation: 2230

Like
0Likes
Like

Posted 10 May 2013 - 09:41 PM

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.

Learn all about my current projects and watch some of the game development videos that I've made.

Squared Programming Home

New Personal Journal

 


#3 Squared'D   Members   -  Reputation: 2230

Like
0Likes
Like

Posted 10 May 2013 - 10:08 PM

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.

Learn all about my current projects and watch some of the game development videos that I've made.

Squared Programming Home

New Personal Journal

 


#4 warnexus   Prime Members   -  Reputation: 1409

Like
0Likes
Like

Posted 10 May 2013 - 11:10 PM

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, 11 May 2013 - 09:07 AM.


#5 Squared'D   Members   -  Reputation: 2230

Like
0Likes
Like

Posted 10 May 2013 - 11:52 PM

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.

Learn all about my current projects and watch some of the game development videos that I've made.

Squared Programming Home

New Personal Journal

 


#6 Khatharr   Crossbones+   -  Reputation: 2959

Like
0Likes
Like

Posted 11 May 2013 - 02:51 AM

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, 11 May 2013 - 02:52 AM.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

#7 warnexus   Prime Members   -  Reputation: 1409

Like
0Likes
Like

Posted 11 May 2013 - 08:55 AM

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, 11 May 2013 - 09:04 AM.


#8 Satharis   Members   -  Reputation: 949

Like
0Likes
Like

Posted 11 May 2013 - 10:04 AM

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.

#9 warnexus   Prime Members   -  Reputation: 1409

Like
0Likes
Like

Posted 11 May 2013 - 10:33 AM

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, 11 May 2013 - 10:35 AM.


#10 Satharis   Members   -  Reputation: 949

Like
0Likes
Like

Posted 11 May 2013 - 10:45 AM

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.



#11 VladR   Members   -  Reputation: 722

Like
1Likes
Like

Posted 11 May 2013 - 05:29 PM

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;
  }
}

 

 


VladR    My 3rd person action RPG on GreenLight:    http://steamcommunity.com/sharedfiles/filedetails/?id=92951596

 


#12 dmatter   Crossbones+   -  Reputation: 3028

Like
0Likes
Like

Posted 11 May 2013 - 07:18 PM

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).

#13 warnexus   Prime Members   -  Reputation: 1409

Like
0Likes
Like

Posted 11 May 2013 - 10:15 PM

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, 12 May 2013 - 12:28 PM.





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