[ZScript] Boss health bars with RenderOverlay

Post your example zscripts/ACS scripts/etc here.

[ZScript] Boss health bars with RenderOverlay

Postby Rip and Tear » Sat Jun 02, 2018 8:00 pm

Uses event handlers to display health bars with RenderOverlay for any active monsters with the +BOSS flag. The monsters do not need to be modified. It correctly supports multiple active bosses and uses the user’s fullscreen HUD scale. You need to supply a “BOSSBAR” graphic showing a filled bar, and set the `barSize` and `barOff` values accordingly.

Code: Select allExpand view
class BossHealthBars : EventHandler {
   Array<actor> bosses;
      
   override void WorldThingSpawned(WorldEvent e) {
      if (e.Thing.bBOSS) {
         bosses.Push(e.thing);
      }
   }
   
   override void WorldThingDied(WorldEvent e) {
      if (e.Thing.bBOSS && bosses.Find(e.thing) != bosses.Size()) {
         bosses.Delete(bosses.Find(e.thing));
      }
   }
   
   override void WorldThingDestroyed(WorldEvent e) {
      if (e.Thing.bBOSS && bosses.Find(e.thing) != bosses.Size()) {
         bosses.Delete(bosses.Find(e.thing));
      }
   }
   
   override void RenderOverlay(RenderEvent e) {
      TextureID barTex = TexMan.CheckForTexture("BOSSBAR", TexMan.Type_Any);
      
      Vector2 hudScale = StatusBar.GetHUDScale();
      Vector2 barSize = (30, 3) * hudscale.x; // Size of the filled part of the bar
      Vector2 barOff = (1, 1) * hudscale.x; // Bar offset from the top-left of the graphic
      Vector2 barPos = (15, 25); // Where the first bar should be drawn
      int barSpace = 10; // Spacing between bars
      
      int barCount = 0;
      for (int i = 0; i < bosses.Size(); i++) {
         if (!bosses[i].target || !bosses[i].target.player) {
            continue;
         }
         
         int vertOffset = barCount * barSpace;
         int vw = screen.GetWidth() / hudScale.x;
         int vh = screen.GetHeight() / hudScale.x;
         Screen.DrawTexture(barTex, true, barPos.x / hudscale.x, barPos.y / hudscale.x + vertOffset, DTA_KeepRatio, true, DTA_VirtualWidth, vw, DTA_VirtualHeight, vh);
         
         double healthRatio = bosses[i].health / Double(bosses[i].SpawnHealth());
         healthRatio = clamp(healthRatio, 0, 1);
         
         int right = barPos.x + barOff.x + barSize.x;
         int left = right - barSize.x * (1 - healthRatio);
         int top = barPos.y + barOff.y + vertOffset * hudscale.x;
         int bottom = top + barSize.y;
         Screen.Clear(left, top, right, bottom, "#000000");
         
         barcount++;
      }
   }
}

Feedback welcome.
Last edited by Rip and Tear on Wed Jun 13, 2018 2:28 pm, edited 1 time in total.
User avatar
Rip and Tear
King of creative usernames
 
Joined: 02 May 2017

Re: [ZScript] Boss health bars

Postby Rip and Tear » Sat Jun 09, 2018 9:20 am

Added a demo file with an example BOSSBAR graphic.

Image
Attachments
BossBars.pk3
(1.76 KiB) Downloaded 53 times
User avatar
Rip and Tear
King of creative usernames
 
Joined: 02 May 2017


Return to Script Library

Who is online

Users browsing this forum: No registered users and 1 guest