Zombie and health bar

This commit is contained in:
2023-11-30 14:49:01 +01:00
parent 9ea4c3beb2
commit c5b8fc40cb
10 changed files with 188 additions and 10 deletions

View File

@@ -6,6 +6,7 @@ import (
"gitlab.com/kbr4/9heroja/configuration"
"gitlab.com/kbr4/9heroja/resources"
"image"
"image/color"
"log"
"time"
)
@@ -21,6 +22,7 @@ type Hero struct {
step int
direction configuration.Direction
IsWalking bool
Health int // Health as a percentage (0-100)
}
type spritePosition struct {
@@ -132,6 +134,35 @@ func (h *Hero) DrawHero(screen *ebiten.Image) {
// float64((ny-1)*tileSize-floorMod(g.cameraY, tileSize)))
screen.DrawImage(heroImage.SubImage(image.Rect(p.x+1, p.y, p.x+resources.HeroTileSize, p.y+resources.HeroTileSize)).(*ebiten.Image), op)
// Drawing the dynamic health bar below the hero
maxHealthBarWidth := resources.HeroTileSize // Maximum width of the health bar (same as the character width)
healthBarHeight := 5 // Height of the health bar
healthBarX := float64(screenWidth/2 - 16) // Align with the hero
healthBarY := float64(screenHeight/2 + 20) // Position below the hero
// Calculate the current width of the health bar based on the hero's health
currentHealthBarWidth := int(float64(maxHealthBarWidth) * (float64(h.Health) / 100.0))
// Determine health bar color based on health
var healthColor color.Color
switch {
case h.Health >= 70:
healthColor = color.RGBA{0, 255, 0, 255} // Green
case h.Health >= 30:
healthColor = color.RGBA{255, 165, 0, 255} // Orange
default:
healthColor = color.RGBA{255, 0, 0, 255} // Red
}
// Create health bar image with the current width
healthBarImg := ebiten.NewImage(currentHealthBarWidth, healthBarHeight)
healthBarImg.Fill(healthColor)
// Draw health bar
hbop := &ebiten.DrawImageOptions{}
hbop.GeoM.Translate(healthBarX, healthBarY)
screen.DrawImage(healthBarImg, hbop)
}
func init() {
@@ -149,6 +180,7 @@ func NewHero() *Hero {
hero := &Hero{
step: 0,
IsWalking: false,
Health: 33,
}
go func() {