[ZScript] Boss health bars with RenderOverlay

Post your example zscripts/ACS scripts/etc here.
Forum rules
The Projects forums are only for projects. If you are asking questions about a project, either find that project's thread, or start a thread in the General section instead.

Got a cool project idea but nothing else? Put it in the project ideas thread instead!

Projects for any Doom-based engine (especially 3DGE) are perfectly acceptable here too.

Please read the full rules for more details.
Post Reply
User avatar
Rip and Tear
Posts: 185
Joined: Tue May 02, 2017 3:54 pm

[ZScript] Boss health bars with RenderOverlay

Post by Rip and Tear »

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 all

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 1:28 pm, edited 1 time in total.
User avatar
Rip and Tear
Posts: 185
Joined: Tue May 02, 2017 3:54 pm

Re: [ZScript] Boss health bars

Post by Rip and Tear »

Added a demo file with an example BOSSBAR graphic.

Image
Attachments
BossBars.pk3
(1.76 KiB) Downloaded 312 times
Post Reply

Return to “Script Library”