Removing GameObjects(Units)

Hi,

I’m having troubles to remove an entity and it’s bound sprite object when it should die.
a unit is an entity
an entity has 3 states:
IDLE, DESTROYED,

My design:
gameState has ref to model and gui
when model changes event is sent to gui
gui can read from the model

So:
2 units batter
1 units hp is 0
that unit changes to the DESTROYED state

the gui sprite reads the state and changes to the explosion animation:
inside the sprite:
if ent.getState() == destroyed
setAnim(explosion)

I know how long the explosion anim would take

the explosion anim is done.

Now how should I remove the entity in the model…
I know howto remove an entity: map.getTile(0,0).setEntity(null) and that’s it.
But

I can only remove it when the explosion time has elapsed.
else the sprite won’t paint itselfs(ie the explosion) anymore
I can not remove it from within the gui(see my design)

my painting code:
for(Entity ent : map.getAllEntities()) {
if(spriteCache.contains(ent) {
spriteCache.get(ent).paintComponent(g);
} else {
// Create new sprite for this ent.
}
}

So if i remove the entity in the model it will not be painted anymore :confused:

I can think of one way remove the explosionAnim from the unitSprite and add it to the main gui
then if ent.state == destroyed play the anim and when done remove the sprite bound to the ent.

What other solutions are there. or what is the general way to handle this problem.

Maybe add a state for dying, which is when the explosion or other death animation plays. And when that animation finishes, it sets the state to destroyed. Any object in the destroyed state could then be safely removed.

You can also just change your program structure a bit so that you can simply inform the model when it’s the right time to remove the object.