Script - Example HUD

From FreeSpace Wiki
Revision as of 04:12, 1 July 2006 by WMCoolmon (talk | contribs)
Jump to: navigation, search
Script-hudexample.jpg
Partial HUD created with scripting


Function

Creates new HUD or rather an example of scripted HUD. (Original source by WMCoolmon)

Table Entry

Scripting.tbl

#Global Hooks
$GameInit:  [[init.lua]]
$HUD:    [[hud.lua]]
   +Override: YES
#End

init.lua

--#################### HUD ####################--
drawBevRect = function(x1, y1, x2, y2)
   -------Draw background
   gr.setColor(0, 0, 48)
   for x=1,5 do
      --"/ \"
      gr.drawLine(x1+(5-x), y1+x, x2-(5-x), y1+x)
      --"\ /"
      gr.drawLine(x1+(5-x), y2-x, x2-(5-x), y2-x)
   end
   -- "| |"
   gr.drawRectangle(x1+1, y1+5, x2, y2-6, true)
   
   -------Draw lines
   gr.setColor(0, 128, 255)
   --" - "
   gr.drawLine(x1+5, y1, x2-6, y1)
   --"/ \"
   gr.drawLine(x1, y1+5, x1+5, y1)
   gr.drawLine(x2, y1+5, x2-5, y1)
   --"| |"
   gr.drawLine(x1, y1+5, x1, y2-5)
   gr.drawLine(x2, y1+5, x2, y2-5)
   --"\ /"
   gr.drawLine(x1, y2-5, x1+5, y2)
   gr.drawLine(x2, y2-5, x2-5, y2)
   --" - "
   gr.drawLine(x1+5, y2, x2-6, y2)
end

setDamageColor = function(dcm)
   local r
   local g
   local b
   if dcm > .5 then
      r = 255 * math.sin(dcm*math.pi)
      g = 128+128 * math.sin(dcm*math.pi)
      b = -255 * math.cos(2*dcm*math.pi)
   else
      r = 255
      g = 255 * math.sin(dcm*math.pi)
      b = 0
   end

   gr.setColor(r, g, b)
end

hud.lua

if slf then
-------Draw speed
   x = 50
   y = 50
   
   drawBevRect(x, y, x+100, y+30)
   
   local vel = slf.Orientation:rotateVector(slf.Velocity)
   local mvel = slf.MaxVelocity
   
   --Skip beveled area
   x = x+5
   y = y+5

   --Draw X component
   local speed = (vel.x/mvel.x)*10
   gr.drawRectangle(x, y+5, x+20, y+15, false)
   gr.drawRectangle(x+10, y+5, x+10+speed, y+15)
   
   --Skip last gauge(20 px)
   x = x+20+5
   
   --Draw Y component
   local speed = (vel.y/mvel.y)*10
   gr.drawRectangle(x, y, x+10, y+20, false)
   gr.drawRectangle(x, y+15, x+10, y+15+speed)
   
   --Skip last gauge(10 px)
   x=x+10+5
   
   --Draw Z component
   local speed = (vel.z/mvel.z)*50
   gr.drawRectangle(x, y+5, x+50, y+15, false)
   gr.drawRectangle(x, y+5, x+speed, y+15)
   
-------Draw life
   x = 25
   y = 200
   
   drawBevRect(x, y, x+20, y+110)
   
   x = x+5
   y = y+5

   dcm = slf.HitpointsLeft/slf.HitpointsMax
   
   --Draw bar
   gr.drawRectangle(x, y, x+10, y+100, false)
   setDamageColor(dcm)
   gr.drawRectangle(x, y+(1-dcm)*100, x+10, y+100)
   
-------Draw mission time
   x = gr.getScreenWidth()-125
   y = 200
   
   drawBevRect(x, y, x+100, y+20)
   
   x = x+5
   y = y+5

   gr.drawString("Flight time: ", x, y)


-------Draw gun ammo
   x = 25
   y = gr.getScreenHeight()-45
   
   local pbank = slf.PrimaryBanks[1]

   if pbank then
      local ammo = pbank.AmmoLeft/pbank.AmmoMax*70
      
      drawBevRect(x, y, x+120, y+20)

      --Skip beveled area
      x = x+5
      y = y+5
      
      --Draw bar
      gr.drawRectangle(x, y, x+70, y+10, false)
      gr.drawRectangle(x, y, x+ammo, y+10)
      
      --Skip bar(70px)
      x = x+70+5
      
      --Draw number
      gr.drawString(pbank.AmmoLeft, x, y)
   end

-------Draw missile ammo
   x = gr.getScreenWidth()-190
   y = gr.getScreenHeight()-45
   
   local sbank = slf.SecondaryBanks[1]

   if sbank then
      local sb_left = sbank.AmmoLeft
      local sb_max = sbank.AmmoMax

      drawBevRect(x, y, x+165, y+20)

      x = x+5
      y = y+5

      for i = 1,16 do
         if i > sb_max then
            break
         elseif i > sb_left then
            gr.drawRectangle(x, y, x+5, y+10, false)
         else
            gr.drawRectangle(x, y, x+5, y+10)
         end

         x = x+10
      end
   end
   
-------Draw countermeasures
   x = (gr.getScreenWidth()/2)-(105/2)
   y = gr.getScreenHeight()-40
   
   local cm_max = slf.Class.CountermeasuresMax
   
   if cm_max then
      local cm_left = slf.CountermeasuresLeft
      drawBevRect(x, y, x+105, y+15)
   
      x = x+5
      y = y+5

      for i = 1,10 do
         if i > cm_max then
            break
         elseif i > cm_left then
            --"-"
            gr.drawLine(x+2, y+2, x+3, y+2)
            --"|"
            gr.drawLine(x+2, y+2, x+2, y+3)
         else
            --"-"
            gr.drawLine(x, y+2, x+5, y+2)
            --"|"
            gr.drawLine(x+2, y, x+2, y+5)
            --"\"
            gr.drawLine(x, y, x+5, y+5)
            --"/"
            gr.drawLine(x, y+5, x+5, y)
         end

         x = x+10
      end
   end

--/slf
end