Files
survive/hero.go

121 lines
1.9 KiB
Go
Raw Normal View History

package main
import (
"bytes"
"image"
"log"
_ "embed"
"image/color"
_ "image/png"
"github.com/hajimehoshi/ebiten/v2"
)
var (
heroImage *ebiten.Image
//go:embed hero.png
hero_img []byte
)
const (
HeroActionDefault = iota
HeroActionDamaged
HeroActionDying
HeroActionExploding
HeroActionDead
HeroActionMax
)
type HeroAction uint
func init() {
img, _, err := image.Decode(bytes.NewReader(hero_img))
if err != nil {
log.Fatal(err)
}
heroImage = ebiten.NewImageFromImage(img)
}
type Hero struct {
Sprite *ebiten.Image
Maks *ebiten.Image
MaksDest *ebiten.Image
Angle float64
Pos Coordinates
Origin Coordinates
Action HeroAction
cycles int
rotating bool
Toggled bool
Hit bool
Touched bool
dyingcount int
}
func NewHero() *Hero {
m := &Hero{
Sprite: ebiten.NewImage(48, 48),
Maks: ebiten.NewImage(48, 48),
MaksDest: ebiten.NewImage(48, 48),
Action: HeroActionDefault,
cycles: 4,
Angle: 0,
rotating: false,
Toggled: false,
dyingcount: 0,
}
m.Maks.Fill(color.White)
return m
}
func (m *Hero) ToggleRotate() {
m.rotating = !m.rotating
}
func (m *Hero) SetAngle(a float64) {
m.Angle = a
}
func (m *Hero) SetOrigin(coords Coordinates) {
m.Origin = coords
m.Pos = coords
}
func (m *Hero) Draw() {
m.Sprite.Clear()
m.MaksDest.Clear()
idx := (m.cycles / 8) % 4
y0 := 0
y1 := 48
x0 := 48 * idx
x1 := x0 + 48
switch m.Action {
case HeroActionDefault:
m.Sprite.DrawImage(heroImage.SubImage(image.Rect(x0, y0, x1, y1)).(*ebiten.Image), nil)
default:
}
}
func (m *Hero) Update() {
m.cycles++
}
func (m *Hero) SetHit() {
m.Action++ // = (m.Action + 1) % HeroActionMax
}
func (m *Hero) ToggleColor() {
//m.Toggled = !m.Toggled
if m.Action == HeroActionDefault {
m.Action = HeroActionDamaged
} else if m.Action == HeroActionDamaged {
m.Action = HeroActionDefault
}
}