• Advertisement
Sign in to follow this  

Calculating damage calculation in game loop

This topic is 1805 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

 
So my game in Java has collision detection done. Now the problem is calculating the damage the ship takes. Calculating it during the collision detection process seems to be a bad idea seeing as the ship initial lives of 3 decreases to 0 pretty quickly. This is because my collision detection is done using a for each loop.
 
It would seem the culprit would be the bounding box collision detection in the gameloop.
 
Each attack from the monsters is only suppose to take out one life from the ship.
 
The thing is I can only calculate the ship damage if a collision detection happens and the for each loop is the culprit that makes damage calculation goes too quick.
 
public class Ship extends Sprite implements KeyListener{
 
 
             private int life;
 
             public Ship(double x, double y) {
            // ship life
            life = 3;
            }
 
            public void setLife(int life)
            {
            this.life = life;
            }
        
            public int getLife()
            {
            return life;
            }
 
}
 
public class Game extends Canvas implements KeyListener{
 
 
public void run(){
        // basic game loop
        // do not break out of the game loop when I change the state of the isRunning
        while(!isLooping)
        {
            // laser check for every OneEye monsters in the ArrayList
                for(GameComponent component: gameObjects){
                    if(component instanceof Laser)
                    {
                    ((Laser) component).checkMonsterCollision(gameObjects);
                        
                    }
                    if(component instanceof OneEyeSlashSkill)
                    {
                    ((OneEyeSlashSkill) component).checkShipCollision(gameObjects);
                    }    
                }
            
        }
}
 
public class OneEyeSlashSkill extends Sprite {
 
public void checkShipCollision(ArrayList<GameComponent> gameObjects )
    {
        
            for(GameComponent component: gameObjects)
            {
                if(component instanceof Ship)
                {
                    // if collision happens 
                    if((rectangle.intersects(((Ship) component).getRectangle())))
                    {
                        ((Ship) component).setLife(((Ship) component).getLife() - 1);
                        // remove ship object
 
                        if(((Ship) component).getLife() == 0)
                        {
                        Game.getInstance().remove((Ship)component);
                        // remove the key listener associated with the ship object
                        Game.getInstance().removeKeyListener((KeyListener) component);
                        }
                    }
                }
            }
    }
 
}
Edited by warnexus

Share this post


Link to post
Share on other sites
Advertisement

Your problem isn't you calculating damage on the collision method (though i think it's wrong); I think you problem is that the OneEyeSkillSlash collides with your ship through various frames, making your ship life decrease rapidly.

So you have some options.

- Destroy the OneEyeSkillSlash after a hit.

- Make your OneEyeSkillSlash not be able to damage again the ship.

- Make your ship invincible for a short time after being hit.

- Implement some other mechanic.

Share this post


Link to post
Share on other sites

Your problem isn't you calculating damage on the collision method (though i think it's wrong); I think you problem is that the OneEyeSkillSlash collides with your ship through various frames, making your ship life decrease rapidly.

So you have some options.

- Destroy the OneEyeSkillSlash after a hit.

- Make your OneEyeSkillSlash not be able to damage again the ship.

- Make your ship invincible for a short time after being hit.

- Implement some other mechanic.

Ah yes, that is correct. It is checking every frame!

 

Thank you. I know what to do now.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement