Files
old-9heroja/hero/hero.go

287 lines
5.8 KiB
Go
Raw Normal View History

2023-11-19 08:06:40 +01:00
package hero
import (
"bytes"
"github.com/hajimehoshi/ebiten/v2"
2023-11-30 21:01:49 +01:00
"gitlab.com/kbr4/9heroja/collision"
2023-11-21 20:12:56 +01:00
"gitlab.com/kbr4/9heroja/configuration"
2023-11-19 08:06:40 +01:00
"gitlab.com/kbr4/9heroja/resources"
2023-12-28 14:42:20 +01:00
"gitlab.com/kbr4/9heroja/weapons"
2023-11-19 08:06:40 +01:00
"image"
2023-11-30 14:49:01 +01:00
"image/color"
2023-11-19 08:06:40 +01:00
"log"
2023-11-19 14:19:34 +01:00
"time"
2023-11-19 08:06:40 +01:00
)
2023-11-21 20:12:56 +01:00
const WalkSpeedMs = 300
2024-01-04 05:57:21 +01:00
const ReloadSpeedMs = 1000
2023-11-19 14:19:34 +01:00
2023-11-19 08:06:40 +01:00
var (
2024-01-04 05:57:21 +01:00
heroImage *ebiten.Image
walkTicker *time.Ticker
reloadTicker *time.Ticker
2023-11-19 08:06:40 +01:00
)
type Hero struct {
2023-11-21 20:12:56 +01:00
step int
direction configuration.Direction
2023-11-28 07:29:17 +01:00
IsWalking bool
2023-11-30 14:49:01 +01:00
Health int // Health as a percentage (0-100)
2023-11-30 21:01:49 +01:00
X float64
Y float64
2024-01-04 05:57:21 +01:00
gunLoaded bool
2023-11-19 14:19:34 +01:00
}
2023-11-30 21:01:49 +01:00
type spriteFramePosition struct {
2023-11-19 14:19:34 +01:00
x int
y int
2023-11-19 08:06:40 +01:00
}
func (h *Hero) DrawHero(screen *ebiten.Image) {
2023-11-19 14:19:34 +01:00
2023-11-21 20:12:56 +01:00
// set movement positions to be hashmap of sprite positions for each direction
2023-11-30 21:01:49 +01:00
movementPositions := map[configuration.Direction][]spriteFramePosition{}
movementPositions[configuration.North] = []spriteFramePosition{
2023-11-19 14:19:34 +01:00
{0, 166},
{33, 166},
{66, 166},
{99, 166},
{132, 166},
{99, 166},
{66, 166},
{33, 166},
}
2023-11-30 21:01:49 +01:00
movementPositions[configuration.NorthEast] = []spriteFramePosition{
2023-11-21 20:12:56 +01:00
{168, 0},
{201, 0},
{0, 33},
{33, 33},
{66, 33},
{33, 33},
{0, 33},
{201, 0},
}
2023-11-30 21:01:49 +01:00
movementPositions[configuration.East] = []spriteFramePosition{
2023-11-23 20:38:34 +01:00
{132, 99},
{165, 99},
{198, 99},
{0, 132},
{33, 132},
{0, 132},
{198, 99},
{165, 99},
}
2023-11-30 21:01:49 +01:00
movementPositions[configuration.SouthEast] = []spriteFramePosition{
2023-11-23 20:38:34 +01:00
{33, 66},
{66, 66},
{99, 66},
{132, 66},
{165, 66},
{132, 66},
{99, 66},
{66, 66},
}
2023-11-30 21:01:49 +01:00
movementPositions[configuration.South] = []spriteFramePosition{
2023-11-21 20:12:56 +01:00
{66, 132},
{0, 99},
{33, 99},
{66, 99},
{99, 99},
{66, 99},
{33, 99},
{0, 99},
}
2023-11-23 20:38:34 +01:00
2023-11-30 21:01:49 +01:00
movementPositions[configuration.SouthWest] = []spriteFramePosition{
2023-11-23 20:38:34 +01:00
{99, 33},
{33, 0},
{66, 0},
{99, 0},
{132, 0},
{99, 0},
{66, 0},
{33, 0},
}
2023-11-30 21:01:49 +01:00
movementPositions[configuration.West] = []spriteFramePosition{
2023-11-23 20:38:34 +01:00
{198, 66},
{99, 132},
{132, 132},
{165, 132},
{198, 132},
{165, 132},
{132, 132},
{99, 132},
}
2023-11-30 21:01:49 +01:00
movementPositions[configuration.NorthWest] = []spriteFramePosition{
2023-11-23 20:38:34 +01:00
{0, 0},
{132, 33},
{165, 33},
{198, 33},
{0, 66},
{198, 33},
{165, 33},
{132, 33},
}
2023-11-21 20:12:56 +01:00
p := movementPositions[h.direction][h.step%8]
2023-11-19 14:19:34 +01:00
2023-11-19 08:06:40 +01:00
op := &ebiten.DrawImageOptions{}
screenWidth := screen.Bounds().Max.X
screenHeight := screen.Bounds().Max.Y
// ground
op.GeoM.Reset()
2023-11-30 21:01:49 +01:00
h.X = float64(screenWidth/2 - 16)
h.Y = float64(screenHeight/2 - 16)
op.GeoM.Translate(h.X, h.Y)
2023-11-19 08:06:40 +01:00
//op.GeoM.Translate(float64(i*tileSize-floorMod(g.cameraX, tileSize)),
// float64((ny-1)*tileSize-floorMod(g.cameraY, tileSize)))
2023-11-19 14:19:34 +01:00
screen.DrawImage(heroImage.SubImage(image.Rect(p.x+1, p.y, p.x+resources.HeroTileSize, p.y+resources.HeroTileSize)).(*ebiten.Image), op)
2023-11-30 14:49:01 +01:00
// 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
2023-11-30 21:01:49 +01:00
healthBarX := float64(h.X) // Align with the hero
healthBarY := float64(h.Y + 37) // Position below the hero
2023-11-30 14:49:01 +01:00
// 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)
2023-11-19 08:06:40 +01:00
}
func init() {
img, _, err := image.Decode(bytes.NewReader(resources.Hero_png))
if err != nil {
log.Fatal(err)
}
heroImage = ebiten.NewImageFromImage(img)
2023-11-19 14:19:34 +01:00
walkTicker = time.NewTicker(WalkSpeedMs * time.Millisecond)
2024-01-04 05:57:21 +01:00
reloadTicker = time.NewTicker(ReloadSpeedMs * time.Millisecond)
2023-11-19 14:19:34 +01:00
// go routine that runs on every tick and increases step
}
func NewHero() *Hero {
hero := &Hero{
2023-11-28 07:29:17 +01:00
step: 0,
IsWalking: false,
2023-11-30 21:01:49 +01:00
Health: 100,
2024-01-04 05:57:21 +01:00
gunLoaded: true,
2023-11-19 14:19:34 +01:00
}
go func() {
for {
select {
case <-walkTicker.C:
hero.step++
if hero.step > 7 {
hero.step = 0
}
}
}
}()
2024-01-04 05:57:21 +01:00
go func() {
for {
select {
case <-reloadTicker.C:
hero.gunLoaded = true
}
}
}()
2023-11-19 14:19:34 +01:00
return hero
}
func (h *Hero) Walk() {
2023-11-28 07:29:17 +01:00
if !h.IsWalking {
walkTicker.Reset(WalkSpeedMs * time.Millisecond)
h.IsWalking = true
}
2023-11-19 14:19:34 +01:00
}
2023-11-21 20:12:56 +01:00
func (h *Hero) ChangeDirection(d configuration.Direction) {
2023-11-28 07:29:17 +01:00
if d != configuration.PreviouslyHeld {
h.direction = d
}
2023-11-21 20:12:56 +01:00
}
2023-11-19 14:19:34 +01:00
func (h *Hero) Stop() {
h.step = 2
walkTicker.Stop()
2023-11-28 07:29:17 +01:00
h.IsWalking = false
2023-11-19 08:06:40 +01:00
}
2023-11-30 21:01:49 +01:00
2024-01-04 05:57:21 +01:00
func (h *Hero) StopShooting() {
h.gunLoaded = false
reloadTicker.Stop()
}
2023-11-30 21:01:49 +01:00
func (h *Hero) CollisionShape() collision.Polygon {
return collision.Polygon{
Points: []collision.Point{
{X: h.X, Y: h.Y},
{X: h.X + 33, Y: h.Y},
{X: h.X + 33, Y: h.Y + 33},
{X: h.X, Y: h.Y + 33},
},
}
}
func (h *Hero) CollisionObjectType() collision.ObjectType {
return collision.Hero
}
func (h *Hero) HandleCollisionEvent(other collision.Collidable) {
coll := other.CollisionObjectType()
switch coll {
case collision.Zombie:
h.Health -= 25
if h.Health <= 0 {
h.Health = 10
}
}
}
2023-12-28 14:42:20 +01:00
2024-01-04 05:57:21 +01:00
func (h *Hero) Fire(offsetX float64, offsetY float64) (bullet *weapons.Handgun) {
if h.gunLoaded {
h.gunLoaded = false
2023-12-28 14:42:20 +01:00
bullet = weapons.NewHandgun()
2024-01-04 05:57:21 +01:00
bullet.X = h.X + (resources.HeroTileSize / 2)
bullet.Y = h.Y + (resources.HeroTileSize / 2)
bullet.OffsetXAtStart = offsetX
bullet.OffsetYAtStart = offsetY
2023-12-28 14:42:20 +01:00
bullet.Fire(h.direction)
return bullet
} else {
return nil
}
}