TGC Codebase Backup



Health Bar Function by Trek

7th Apr 2006 12:22
Summary

A well documented example showing how to easily make a texture for a health indicator object.



Description

I've often wanted an easy way to make a simple texture showing the current health level as a white rectangle inside a black rectange which represents the total health capacity. This function does it effeciently, quickly, and simply. All you have to do is tell it the current health percentage out of the total health capacity and the image number you want to make the texture and it takes care of the rest.

This function is being used for my entry in the Caiman TotalPack contest so if you want to find out more info on the contest, go to http://www.caimen.be/forum/topic.asp?TOPIC_ID=1405

If you want to download some of my past productions, including Codename Countdown which placed third in the last Caiman Contest, go to www.TrekSoftware.tk



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    ` Health Bar Texture Function
` Programmed by Trek Manager D3
` http://roborangers.home.comcast.net/Trek.html
` Can be used without credit in any production.

` Setup intial conditions

autocam off
sync on
sync rate 20

position camera 0,0,0
point camera 0,0,5

` Make a sample object.

make object plain 1,5,.1
position object 1,0,0,5

` Set the initial Health of the object

Health=100

do
   ` Reduces the health by 1 each time the program loops and when the object is dead, exits.

   if Health>0
      Health=Health-1
   else
      Exit
   endif

   ` Uses the function to make a texture (using image number 1) of the current health level.

   MakeBarTexture(Health,1)

   ` Textures the sample object with image 1.

   texture object 1,1

   sync
loop

function MakeBarTexture(Percentage,ImageNumber)

   ` Clears the bitmap by deleting it and making a new one

   if bitmap exist(1)=1 then delete bitmap 1
   create bitmap 1,100,5

   ` Makes a box symbolizing the current health level.

   box 0,0,Percentage,5

   ` Grabs the entire bitmap so that the remaining health is shown as white and the
   ` used health is shown as black.

   Get image ImageNumber, 0, 0, 100, 5

   ` Returns the current bitmap to 0

   set current bitmap 0

endfunction