TGC Codebase Backup



Health Bar by Eternal Darkness

11th Apr 2004 8:23
Summary

This code was originally coded by Jess Ticular , to help me understand the principals of increasing and decreasing health but of can also be used for others like ammo or energy. Th



Description



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    Sync On : Sync Rate 0


Make Object Cylinder 1,1         : `This will be our Health-Bar object
Ghost Object On 1                : `Just a visual effect

Scale Object 1,400,50,50         : `Scale the object so it's nice and long and thin
ZRotate Object 1,90              : `Rotate it so it's facing the right way ( on it's side )
Fix Object Pivot 1               : `Fix the pivot so that all rotations are zero'd

start = Timer()                  : `Just a simple Timer function to see how long the function takes

_pos_HUD(1,Screen Width()/2,40,5) : `The actual positioning of the object onscreen

endt = Timer() - start

Do


Text 0,0,"Time Taken To Position = " + Str$(endt) + "ms"



If UpKey() = 1 Then Inc health    : `Just a simulation of the health going up and down
If DownKey() = 1 Then Dec health

_update_health(health,1,1)        : ` The call to update the Health bar object with the new status


Sync
Loop




Function _pos_HUD(obj,sx,sy,zdist)


   `obj is the Object Number that you wish to position
   `sx is the Screen X coordinate you wish to position the object at
   `sy is the Screen Y coordinate
   `zdist is simply the distance you want the object to be from the camera ( the smaller the better )


   `What the function actuallly does is it checks the Objects Screen coordintates against the desired
   `coordinates for near equality... If the object is in position, the loops are broken, otherwise,
   `the loops keep moving the object by little amounts untill it is in the right position


   Position Camera 0,0,0
   Point Camera 0,0,10

   Position Object obj,0,0,zdist


   ox = Object Screen X(obj)
   oy = Object Screen Y(obj)

   While ox > sx + 2 Or ox < sx - 2
      If ox > sx + 2 Then temp# = -0.01 Else temp# = 0.01
      Position Object obj,(Object Position X(obj) + temp#),Object Position Y(obj),zdist
      ox = Object Screen X(obj)
      oy = Object Screen Y(obj)
   EndWhile

   While oy > sy + 2 Or oy < sy - 2
      If oy < sy - 2 Then temp# = -0.01 Else temp# = 0.01
      Position Object obj, Object Position X(obj),(Object Position Y(obj) + temp#),zdist
      ox = Object Screen X(obj)
      oy = Object Screen Y(obj)
   EndWhile


EndFunction




Function _update_health(perc,img,obj)


   `perc is the the Percent of the "health"
   `img is the image number you wish to use for the texture for the Health Bar object
   `obj is the Object Number of the Health Bar

   `This Simply draws a box to the screen, with red indicating health, white indicating a lack of health,
   `the image is then captured as a texture and applied to the object.

   `You'll notice that you can see the box being drawn to the screen, this should disapear when you put
   `this function into your main app...
   `If it does not, at the start of your code, put the command Draw To Back
   `This will draw all 2D operations behind any 3D on the screen.


   Ink RGB(255,255,255),0
   Box 0,0,10,100
   Ink RGB(255,0,0),0
   Box 0,0,10,perc

   Get Image img,0,0,10,100,1

   Texture Object obj,img


EndFunction