A quick and easy way to add a healthbar (or power, shield, whatever you need) to your game project, I have tried to mimic GameMaker functionality for quick prototyping
currently supports the following functions
HealthBarCreate()
HealthBarDelete()
HealthBarHide()
HealthBarGetState()
HealthBarSetState()
HealthBarSetPosition()
HealthBarSetSize()
HealthBarGetMax()
HealthBarSetMax()
HealthBarSetColor()
HealthBar.agc
Type tHealthBar
spr_back
spr_front
spr_border
show_border
is_vertical
max_range
EndType
Function HealthBarCreate(hb ref as tHealthBar, x, y, width, height, vertical, range)
sprBorder = CreateSprite(0)
SetSpritePosition(sprBorder, x-1, y-1)
SetSpriteSize(sprBorder, width+2, height+2)
SetSpriteColor(sprBorder, 0, 0, 0, 255)
sprBack = CreateSprite(0)
SetSpritePosition(sprBack, x, y)
SetSpriteSize(sprBack, width, height)
SetSpriteColor(sprBack, 255, 255, 255, 255)
sprFront = CreateSprite(0)
SetSpritePosition(sprFront, x, y)
SetSpriteSize(sprFront, width, height)
SetSpriteColor(sprFront, 255, 0, 0, 255)
hb.spr_border=sprBorder
hb.spr_back=sprBack
hb.spr_front=sprFront
hb.is_vertical=vertical
hb.max_range=range
EndFunction
Function HealthBarDelete(hb ref as tHealthBar)
if GetSpriteExists(hb.spr_border) : DeleteSprite(hb.spr_border) : endif
if GetSpriteExists(hb.spr_back) : DeleteSprite(hb.spr_back) : endif
if GetSpriteExists(hb.spr_front) : DeleteSprite(hb.spr_front) : endif
EndFunction
Function HealthBarHide(hb ref as tHealthBar, hide)
if GetSpriteExists(hb.spr_border) and GetSpriteExists(hb.spr_back) and GetSpriteExists(hb.spr_front)
SetSpriteVisible(hb.spr_border, abs(hide-1))
SetSpriteVisible(hb.spr_back, abs(hide-1))
SetSpriteVisible(hb.spr_front, abs(hide-1))
SetSpriteActive(hb.spr_border, abs(hide-1))
SetSpriteActive(hb.spr_back, abs(hide-1))
SetSpriteActive(hb.spr_front, abs(hide-1))
endif
EndFunction
Function HealthBarSetState(hb as tHealthbar, pos)
if GetSpriteExists(hb.spr_back)
bar_width=GetSpriteWidth(hb.spr_back)
bar_height=GetSpriteHeight(hb.spr_back)
new_width=0
new_height=0
if hb.is_vertical = 1
new_width = bar_width
new_height = bar_height*(pos/hb.max_range)
else
new_width = bar_width*pos/hb.max_range
new_height = bar_height
endif
SetSpriteSize(hb.spr_front, new_width, new_height)
endif
EndFunction
Function HealthBarGetState(hb as tHealthbar)
if GetSpriteExists(hb.spr_back)
bar_width=GetSpriteWidth(hb.spr_back)
bar_height=GetSpriteHeight(hb.spr_back)
front_width=GetSpriteWidth(hb.spr_front)
front_height=GetSpriteHeight(hb.spr_front)
if hb.is_vertical = 1
max_Y=GetSpriteY(hb.spr_back)+GetSpriteHeight(hb.spr_back)
min_Y=GetSpriteY(hb.spr_back)
percent=(GetSpriteHeight(hb.spr_front) / (max_Y-min_Y)) * hb.max_range
else
max_X=GetSpriteX(hb.spr_back)+GetSpriteWidth(hb.spr_back)
min_X=GetSpriteX(hb.spr_back)
percent=(GetSpriteWidth(hb.spr_front) / (max_X-min_X)) * hb.max_range
endif
endif
EndFunction percent
Function HealthBarSetPosition(hb as tHealthBar, x, y)
if GetSpriteExists(hb.spr_border) : SetSpritePosition(hb.spr_border, x-1, y-1) : endif
if GetSpriteExists(hb.spr_back) : SetSpritePosition(hb.spr_back, x, y) : endif
if GetSpriteExists(hb.spr_front) : SetSpritePosition(hb.spr_front, x, y) : endif
EndFunction
// TODO: add HealthBarGetPosition
Function HealthBarSetSize(hb as tHealthBar, width, height)
if GetSpriteExists(hb.spr_border) and GetSpriteExists(hb.spr_back) and GetSpriteExists(hb.spr_front)
old_state=HealthBarGetState(hb)
SetSpriteSize(hb.spr_border, width+2, height+2)
SetSpriteSize(hb.spr_back, width, height)
SetSpriteSize(hb.spr_front, width, height)
HealthBarSetState(hb, old_state)
endif
EndFunction
// TODO: add HealthBarGetSize
Function HealthBarSetMax(hb ref as tHealthBar, max)
if GetSpriteExists(hb.spr_border) and GetSpriteExists(hb.spr_back) and GetSpriteExists(hb.spr_front)
old_state=HealthBarGetState(hb)
hb.max_range=max
HealthBarSetState(hb, old_state)
endif
EndFunction
Function HealthBarGetMax(hb as tHealthBar)
max=hb.max_range
EndFunction max
Function HealthBarSetColor(hb as tHealthBar, border, back, front, alpha)
if GetSpriteExists(hb.spr_border) and GetSpriteExists(hb.spr_back) and GetSpriteExists(hb.spr_front)
border_r=GetColorRed(border)
border_g=GetColorRed(border)
border_b=GetColorRed(border)
SetSpriteColor(hb.spr_border, border_r, border_g, border_b, alpha)
back_r=GetColorRed(back)
back_g=GetColorRed(back)
back_b=GetColorRed(back)
SetSpriteColor(hb.spr_back, back_r, back_g, back_b, alpha)
front_r=GetColorRed(front)
front_g=GetColorRed(front)
front_b=GetColorRed(front)
SetSpriteColor(hb.spr_front, front_r, front_g, front_b, alpha)
endif
EndFunction
// TODO: add HealthBarGetColor
Example
#include "HealthBar.agc"
power as tHealthBar
HealthBarCreate(power, 10, 10, 200, 30, 0, 100)
//HealthBarDelete(power)
//HealthBarHide(power, 1)
//HealthBarHide(power, 0)
HealthBarSetPosition(power, 150,150)
HealthBarSetState(power, 55)
HealthBarSetSize(power, 300, 20)
HealthBarSetMax(power, 200)
border=MakeColor(255,255,255)
back=MakeColor(25,35,69)
front=MakeColor(255,255,255)
HealthBarSetColor(power, border, back, front, 200)
time#=timer()
dir=1
do
if timer()-time# >= 0.1
cur_state=HealthBarGetState(power)
if cur_state>0 and dir = 1
dec cur_state, 1
elseif cur_state=0 and dir = 1
dir=0
elseif cur_state<HealthBarGetMax(power) and dir = 0
inc cur_state, 1
elseif cur_state=HealthBarGetMax(power) and dir = 0
dir=1
endif
time#=timer()
HealthBarSetState(power, cur_state)
endif
Print( timer()-time# )
Sync()
loop