Zombie and health bar
This commit is contained in:
114
zombie/zombie.go
Normal file
114
zombie/zombie.go
Normal file
@@ -0,0 +1,114 @@
|
||||
package zombie
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"gitlab.com/kbr4/9heroja/configuration"
|
||||
"gitlab.com/kbr4/9heroja/resources"
|
||||
"image"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
const WalkSpeedMs = 50
|
||||
|
||||
var (
|
||||
zombieImage *ebiten.Image
|
||||
walkTicker *time.Ticker
|
||||
)
|
||||
|
||||
type Zombie struct {
|
||||
step int
|
||||
direction configuration.Direction
|
||||
IsWalking bool
|
||||
X float64
|
||||
Y float64
|
||||
OffsetX float64
|
||||
OffsetY float64
|
||||
}
|
||||
|
||||
type spritePosition struct {
|
||||
x int
|
||||
y int
|
||||
}
|
||||
|
||||
func (z *Zombie) DrawZombie(screen *ebiten.Image) {
|
||||
|
||||
// set movement positions to be hashmap of sprite positions for each direction
|
||||
|
||||
movementPositions := map[configuration.Direction][]spritePosition{}
|
||||
movementPositions[configuration.North] = []spritePosition{
|
||||
{0, 0},
|
||||
{32, 0},
|
||||
{64, 0},
|
||||
{96, 0},
|
||||
}
|
||||
movementPositions[configuration.NorthEast] = movementPositions[configuration.North]
|
||||
movementPositions[configuration.East] = movementPositions[configuration.North]
|
||||
movementPositions[configuration.SouthEast] = movementPositions[configuration.North]
|
||||
movementPositions[configuration.South] = movementPositions[configuration.North]
|
||||
movementPositions[configuration.SouthWest] = movementPositions[configuration.North]
|
||||
movementPositions[configuration.West] = movementPositions[configuration.North]
|
||||
movementPositions[configuration.NorthWest] = movementPositions[configuration.North]
|
||||
|
||||
p := movementPositions[z.direction][z.step%4]
|
||||
|
||||
op := &ebiten.DrawImageOptions{}
|
||||
op.GeoM.Reset()
|
||||
op.GeoM.Translate(z.X+z.OffsetX, z.Y+z.OffsetY)
|
||||
op.GeoM.Scale(1, 1)
|
||||
|
||||
screen.DrawImage(zombieImage.SubImage(image.Rect(p.x+1, p.y, p.x+resources.ZombieTileSize, p.y+resources.HeroTileSize)).(*ebiten.Image), op)
|
||||
}
|
||||
|
||||
func init() {
|
||||
img, _, err := image.Decode(bytes.NewReader(resources.Zombie_png))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
zombieImage = ebiten.NewImageFromImage(img)
|
||||
walkTicker = time.NewTicker(WalkSpeedMs * time.Millisecond)
|
||||
// go routine that runs on every tick and increases step
|
||||
|
||||
}
|
||||
|
||||
func NewZombie() *Zombie {
|
||||
hero := &Zombie{
|
||||
step: 0,
|
||||
IsWalking: false,
|
||||
}
|
||||
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-walkTicker.C:
|
||||
hero.step++
|
||||
if hero.step > 3 {
|
||||
hero.step = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
return hero
|
||||
}
|
||||
|
||||
func (z *Zombie) Walk() {
|
||||
|
||||
if !z.IsWalking {
|
||||
walkTicker.Reset(WalkSpeedMs * time.Millisecond)
|
||||
z.IsWalking = true
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (z *Zombie) ChangeDirection(d configuration.Direction) {
|
||||
if d != configuration.PreviouslyHeld {
|
||||
z.direction = d
|
||||
}
|
||||
}
|
||||
|
||||
func (z *Zombie) Stop() {
|
||||
z.step = 2
|
||||
walkTicker.Stop()
|
||||
z.IsWalking = false
|
||||
}
|
||||
Reference in New Issue
Block a user