package hero import ( "bytes" "github.com/hajimehoshi/ebiten/v2" "gitlab.com/kbr4/9heroja/configuration" "gitlab.com/kbr4/9heroja/resources" "image" "log" "time" ) const WalkSpeedMs = 300 var ( heroImage *ebiten.Image walkTicker *time.Ticker ) type Hero struct { step int direction configuration.Direction IsWalking bool } type spritePosition struct { x int y int } func (h *Hero) DrawHero(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, 166}, {33, 166}, {66, 166}, {99, 166}, {132, 166}, {99, 166}, {66, 166}, {33, 166}, } movementPositions[configuration.NorthEast] = []spritePosition{ {168, 0}, {201, 0}, {0, 33}, {33, 33}, {66, 33}, {33, 33}, {0, 33}, {201, 0}, } movementPositions[configuration.East] = []spritePosition{ {132, 99}, {165, 99}, {198, 99}, {0, 132}, {33, 132}, {0, 132}, {198, 99}, {165, 99}, } movementPositions[configuration.SouthEast] = []spritePosition{ {33, 66}, {66, 66}, {99, 66}, {132, 66}, {165, 66}, {132, 66}, {99, 66}, {66, 66}, } movementPositions[configuration.South] = []spritePosition{ {66, 132}, {0, 99}, {33, 99}, {66, 99}, {99, 99}, {66, 99}, {33, 99}, {0, 99}, } movementPositions[configuration.SouthWest] = []spritePosition{ {99, 33}, {33, 0}, {66, 0}, {99, 0}, {132, 0}, {99, 0}, {66, 0}, {33, 0}, } movementPositions[configuration.West] = []spritePosition{ {198, 66}, {99, 132}, {132, 132}, {165, 132}, {198, 132}, {165, 132}, {132, 132}, {99, 132}, } movementPositions[configuration.NorthWest] = []spritePosition{ {0, 0}, {132, 33}, {165, 33}, {198, 33}, {0, 66}, {198, 33}, {165, 33}, {132, 33}, } p := movementPositions[h.direction][h.step%8] op := &ebiten.DrawImageOptions{} screenWidth := screen.Bounds().Max.X screenHeight := screen.Bounds().Max.Y // ground op.GeoM.Reset() op.GeoM.Translate(float64(screenWidth/2-16), float64(screenHeight/2-16)) //op.GeoM.Translate(float64(i*tileSize-floorMod(g.cameraX, tileSize)), // 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) } func init() { img, _, err := image.Decode(bytes.NewReader(resources.Hero_png)) if err != nil { log.Fatal(err) } heroImage = ebiten.NewImageFromImage(img) walkTicker = time.NewTicker(WalkSpeedMs * time.Millisecond) // go routine that runs on every tick and increases step } func NewHero() *Hero { hero := &Hero{ step: 0, IsWalking: false, } go func() { for { select { case <-walkTicker.C: hero.step++ if hero.step > 7 { hero.step = 0 } } } }() return hero } func (h *Hero) Walk() { if !h.IsWalking { walkTicker.Reset(WalkSpeedMs * time.Millisecond) h.IsWalking = true } } func (h *Hero) ChangeDirection(d configuration.Direction) { if d != configuration.PreviouslyHeld { h.direction = d } } func (h *Hero) Stop() { h.step = 2 walkTicker.Stop() h.IsWalking = false }