''' <summary>
''' A class to create health bars on controls.
''' </summary>
''' <remarks></remarks>
Public Class HPBar
Private FullHP As Single
Private Health As Single
Private BarSize As Rectangle
''' <summary>
''' Occurs when there is no longer any HP left.
''' </summary>
''' <remarks></remarks>
Public Event OnHPDepleted()
''' <summary>
''' Occurs when the HP is full.
''' </summary>
''' <remarks></remarks>
Public Event OnHPFull()
''' <summary>
''' Occurs when the HP has been decremented
''' </summary>
''' <remarks></remarks>
Public Event OnHPDecrement()
''' <summary>
''' Occurs when the HP has been incremented
''' </summary>
''' <remarks></remarks>
Public Event OnHPIncrement()
''' <summary>
''' Occurs when the HP has changed
''' </summary>
''' <remarks></remarks>
Public Event OnHPChange()
''' <summary>
''' The Constructor of the Health Bar class.
''' </summary>
''' <param name="MaxHP">The maximum HP value</param>
''' <param name="BarRect">The rectangle of the HP bar</param>
''' <remarks></remarks>
Public Sub New(ByVal MaxHP As Integer, ByVal BarRect As Rectangle)
FullHP = MaxHP
Health = MaxHP
BarSize = BarRect
End Sub
''' <summary>
''' The subroutine to draw the health bar.
''' </summary>
''' <param name="GraphicsToUse">The Graphics object to draw to</param>
''' <remarks>Use e.Graphics in the Form's paint event for example :)</remarks>
Public Sub DrawBar(ByRef GraphicsToUse As Graphics)
' The formula used to set the width of the Health bar's width
' is (in pseudocode): maximum hp bar width x (current health / maximum hp value)
GraphicsToUse.FillRectangle(Brushes.Red, BarSize.X, BarSize.Y, _
Convert.ToInt32(BarSize.Width * (Health / FullHP)), _
BarSize.Height)
End Sub
''' <summary>
''' Set the health to a specified value
''' </summary>
''' <param name="intHealth">The integer value to set the health to</param>
''' <remarks></remarks>
Public Sub SetHealth(ByVal intHealth As Single)
If intHealth > FullHP Or intHealth < 0 Then
Throw New Exception("Health value must be between 0-" + FullHP.ToString() + ".")
End If
Dim prevHealth As Single = Health
Health = intHealth
RaiseEvent OnHPChange()
If prevHealth > Health Then RaiseEvent OnHPDecrement()
If prevHealth < Health Then RaiseEvent OnHPIncrement()
If Health <= 0 Then RaiseEvent OnHPDepleted()
If Health >= FullHP Then RaiseEvent OnHPFull()
End Sub
''' <summary>
''' Get the current health of the health bar
''' </summary>
''' <returns>The health value</returns>
''' <remarks></remarks>
Public Function GetHealth() As Single
Return Health
End Function
End Class