Jump to content

  • Log In with Google      Sign In   
  • Create Account

How to create a more efficient Health System for my character


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

#1 warnexus   Prime Members   -  Reputation: 1505

Like
0Likes
Like

Posted 27 March 2013 - 08:47 PM

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

Edited by warnexus, 27 March 2013 - 09:25 PM.


Sponsor:

#2 Servant of the Lord   Crossbones+   -  Reputation: 21198

Like
5Likes
Like

Posted 27 March 2013 - 08:57 PM

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.

Edited by Servant of the Lord, 27 March 2013 - 09:04 PM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#3 warnexus   Prime Members   -  Reputation: 1505

Like
0Likes
Like

Posted 27 March 2013 - 09:44 PM

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. 


Edited by warnexus, 27 March 2013 - 11:56 PM.


#4 MichaelNIII   Members   -  Reputation: 195

Like
3Likes
Like

Posted 27 March 2013 - 10:02 PM

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.

#5 Ludus   Members   -  Reputation: 970

Like
2Likes
Like

Posted 27 March 2013 - 11:10 PM

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.



#6 warnexus   Prime Members   -  Reputation: 1505

Like
1Likes
Like

Posted 28 March 2013 - 12:03 AM

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.



#7 Bacterius   Crossbones+   -  Reputation: 9305

Like
1Likes
Like

Posted 28 March 2013 - 12:06 AM

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.


Edited by Bacterius, 28 March 2013 - 12:07 AM.

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#8 MichaelNIII   Members   -  Reputation: 195

Like
0Likes
Like

Posted 28 March 2013 - 12:12 AM

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.

#9 tisdadd   Members   -  Reputation: 313

Like
0Likes
Like

Posted 28 March 2013 - 01:58 PM

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.



#10 warnexus   Prime Members   -  Reputation: 1505

Like
0Likes
Like

Posted 28 March 2013 - 02:54 PM

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






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