Skip to content

Instantly share code, notes, and snippets.

@Achie72
Last active October 10, 2022 20:52
Show Gist options
  • Select an option

  • Save Achie72/326912729305dad8b2662ac375a28d91 to your computer and use it in GitHub Desktop.

Select an option

Save Achie72/326912729305dad8b2662ac375a28d91 to your computer and use it in GitHub Desktop.
TIC-80 HealthBar
-- made for my Devtober2022 entry
-- you can read the corresponding devlog on:
-- https://ko-fi.com/post/Eng-Devtober2020-2--Graphical-Enhancements-X8X3FKCIB
-- https://itch.io/jam/devtober-2022/topic/2406837/achies-devlog-shmup-game
function add_enemy(_x,_tpe,_family)
local enemy = {
x = _x,
y = -4,
sx = 0,
sy = 0.2,
sprite = 64+(_family*16) + _tpe*2,
hp = 2*(1+_tpe),
maxHp = 2*(1+_tpe),
tpe = _tpe,
isTarget = false,
nextShoot = 10,
shoot = 0,
damaged = 0 --added a new variable to signal the time it was damaged
}
table.insert(enemies, enemy)
end
-- in the enemy update loop I added:
if enemy.shoot > 0 then
enemy.shoot = enemy.shoot - 1
end
-- and in the bullet collision check i just set the timer for the fade
if collide(bul, enemy) then
local damage = 1
if bul.tpe == 1 then damage = 4 end
enemy.hp = enemy.hp - damage
table.remove(bullets,_)
enemy.damaged = 90
end
-- After all this setup
-- in the enemy draw call I can do such:
if enemy.damaged > 0 then
draw_healthbar(enemy.x-1,enemy.y-4,enemy.hp,enemy.maxHp,3,1)
end
-- Two color values one for the darker background, one for
-- the acutal color for contrast.
function draw_healthbar(_x,_y,_hp,_maxHp,_clrHp,_clrMax)
rect(_x,_y,10,3,12)
rect(_x+1,_y+1,8,1,_clrMax)
-- calculate the percentage of the HP and draw the healthbar
-- according to that length
rect(_x+1,_y+1,8*(_hp/_maxHp),1,_clrHp)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment