UnrealScript Game Programming Cookbook

(Chris Devlin) #1
Chapter 7

193


  1. Now we need to draw the actual bar graph to represent our health. We'll be using
    the same function to draw a bar for both our health and ammo.
    /**

    • Draw bar graph for health / ammo and background HP bar.

    • Called by DrawHealth()and DrawAmmo().
      **/
      simulated function DrawBarGraph(float X, float Y, float
      Width, float MaxWidth, float Height, Canvas DrawCanvas,
      Color BarColor, Color BackColor)
      {
      /* Draw the dark bar behind our current one /
      if ( MaxWidth > 24.0 )
      {
      DrawCanvas.DrawColor = BackColor;
      DrawCanvas.SetPos(X,Y);
      DrawCanvas.DrawTile(BarTexture,MaxWidth*2,Height,
      407,479,FMin(MaxWidth,118),16);
      }




/** Draw the bar */
DrawCanvas.DrawColor = BarColor;
DrawCanvas.SetPos(X, Y);
DrawCanvas.DrawTile(BarTexture,Width*2,Height,
BarCoords.U,BarCoords.V,BarCoords.UL,BarCoords.VL);
}

We're really going to be drawing two bars here. The first one is going to be a light
gray color and will represent our full health. This is always drawn and the value
will not change.
Beneath that, we'll be drawing another bar which represents our health in its current
state. If we lose health, then this bar shrinks in size as well.


  1. We need a function to draw our distinct health bar now. When the time comes to draw
    our ammo bar, you'll see that we've created a similar function for that as well. This
    bar will change color too, depending on our current health values. It's always nice to
    have a bit of a warning when our health is getting low.
    /**

    • Draw player's health. Adjusts the bar color based on health
      **/
      simulated function DrawHealthBar(float X, float Y, float
      Width, float MaxWidth, float Height,Canvas DrawCanvas,
      optional byte Alpha=255)
      {
      local float HealthX;
      local color DrawColor, BackColor;




// Color of bar relies on the player's current HP
HealthX = Width/MaxWidth;
Free download pdf