How to create a more efficient Health System for my character

Started by
8 comments, last by Nicholas Kong 11 years ago

I already implemented a health system for my ship in my game which is written in Java. The way the health works is this: everytime the ship takes a hit from the fire projectile, the health drops 1 heart.

healthSystem_zps44789f01.png

healthSystem1_zpsc8afc345.png

The first issue is:

Although the code works, I feel the following code implementation does not seem to be efficient in a situation where the ship can start off with more than 3 hearts.

The way I created the effect of the heart disappearing is using if statements to set different images that displays a different numbers of hearts depending on the life of the ship. 2 Pictures of setting different images of heart display to a JLabel object is not so bad using 2 if statements...but if my ship had 20 hearts when the game starts, that would mean 20 if statements. The code will be painful to implement or worse to debug. It is also duplicated code as it is.

On a side note: The ShipLifeTitle is a JLabel that is attached to the BorderLayout.North of the JPanel.

Here are 3 ImageIcons that are set to a single JLabel for the game:

shipLifeTitle3_zpsedca8a42.png

shipLifeTitle2_zpsf147bee1.png

shipLifeTitle1_zps9d551cc4.png

The second issue is:

The code is tied to a particular skill from a monster. But if for example, my game had 20 skills, I would be using the same heart animating code for all 20 skills which would means I will need to copy and paste 20 times for the hearts to animate properly. But if I were to change my game to have 20 hearts instead of 3 hearts, I would need to edit my heart animating code in all 20 classes...

I have two questions:

Can everyone suggest an efficient way or a common way to create a more efficient health system?

Should I insert the health animation code in a collision function of a enemy skill class?


 
 
 
public class OneEyeFireSkill extends Sprite {
 
 
public void checkShipCollision(ArrayList<GameComponent> gameObjects, ShipLifeTitle shipLifeTitle )
{
 
for(GameComponent component: gameObjects)
{
if(component instanceof Ship)
{
// if collision happens 
if((rectangle.intersects(((Ship) component).getRectangle())))
{
((Ship) component).setLife(((Ship) component).getLife()-1);
 
if(((Ship) component).getLife() == 2)
{
shipLifeTitle.setTitleToTwoHearts();
}
 
if(((Ship) component).getLife() == 1)
{
shipLifeTitle.setTitleToOneHearts();
}
 
if(((Ship) component).getLife() == 0)
{
shipLifeTitle.setGameOverTitle();
((Ship) component).removeAtDeath();
}
 
 
Game.getInstance().remove(this);
 
}
}
}
 
 
}


 
public class ShipLifeTitle extends JLabel {
 
 
 
public ShipLifeTitle() throws IOException
{
 
 
ImageIcon threeHeartsIcon = new ImageIcon("src/Ship/shipLifeTitle3.png");
setIcon(threeHeartsIcon);
 
}
 
public void setTitleToTwoHearts()
{
 
ImageIcon twoHeartsIcon = new ImageIcon("src/Ship/shipLifeTitle2.png");
setIcon(twoHeartsIcon);
 
 
}
 
public void setTitleToOneHearts()
{
 
ImageIcon oneHeartIcon = new ImageIcon("src/Ship/shipLifeTitle1.png");
setIcon(oneHeartIcon);
 
}
Advertisement
Try something like:
if( hit )
{
    health -= 1;

    if( health <= 0)
    {
        dead = true;
    }
}
Later, when it's time to draw:
//First draw the "Life" text or image.
DrawText("Life:", x, y);
//...or...
DrawImage(LifeTextImage, x, y);

for(int heart = 0; heart < health; heart++)
{
    heartOffsetX = (heart * (heartWidth + spaceBetweenHearts));
    DrawImageOnScreen(HeartImage, (distanceFromLeftOfScreen + heartOffsetX), distanceFromTopOfScreen);
}
Re-use the heart image and just draw it multiple times and shift the image over each time.

Re-use the heart image and just draw it multiple times and shift the image over each time.

Thanks Servant of the Lord for the advice.

You need to increase the x value for the hearts location for each heart, equal to the width of the heart image. This method will work well until the amount of hearts you have is greater then the width of the screen.

Servant of the Lord is right. Simply break your life meter into two separate images: "-LIFE-" and a single heart. Draw the heart as many times as the player has hearts, with an offset that increases for each heart (as in the code that was suggested).

As for the health animation code, no, it should not be in a collision function of a enemy skill class. Ideally it would be in whatever class the HUD system is.

You need to increase the x value for the hearts location for each heart, equal to the width of the heart image. This method will work well until the amount of hearts you have is greater then the width of the screen.

I agree with the first statement. But the second statement seems odd, wouldn't the method work all the time for that following condition. A screen width can be an integer value of 580 which is always certainly greater than an arbitrary value of 20 hearts.

I agree with the first statement. But the second statement seems odd, wouldn't the method work all the time for that following condition. A screen width can be an integer value of 580 which is always certainly greater than an arbitrary value of 20 hearts.

Yes, but what if you want more than 20 hearts later on? Or add more things (like armor, ammo indicators, which also take up screen space and reduce the amount of screen estate available for showing life)? I believe this is what was meant.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

that is what I ment, and that also depends on the size of your hearts and the size of your window. If your hearts are huge or your window small 20 won't even fit. I like that term, screen estate.

If you are worried about it overflowing, simply add the screen width - health image / heart size to find out how many total fit.

Then, use the floor to figure out which row the heart belongs in (y position), and the mod of above equation to see which column....

Of course, for saving real estate, it might be a wise practice to simply do the life icon, then a heart, then a numerical representation (x3, x6, x20, x Many lives).

I agree with the first statement. But the second statement seems odd, wouldn't the method work all the time for that following condition. A screen width can be an integer value of 580 which is always certainly greater than an arbitrary value of 20 hearts.

Yes, but what if you want more than 20 hearts later on? Or add more things (like armor, ammo indicators, which also take up screen space and reduce the amount of screen estate available for showing life)? I believe this is what was meant.

If you are worried about it overflowing, simply add the screen width - health image / heart size to find out how many total fit.

Then, use the floor to figure out which row the heart belongs in (y position), and the mod of above equation to see which column....

Of course, for saving real estate, it might be a wise practice to simply do the life icon, then a heart, then a numerical representation (x3, x6, x20, x Many lives).

Good idea in regards to the numerical representation of lives

This topic is closed to new replies.

Advertisement