2024-11-06 07:42:21 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"image"
|
|
|
|
|
"log"
|
|
|
|
|
|
|
|
|
|
_ "embed"
|
|
|
|
|
"image/color"
|
|
|
|
|
_ "image/png"
|
|
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
heroImage *ebiten.Image
|
2024-11-07 10:41:30 -05:00
|
|
|
heroDeath *ebiten.Image
|
2024-11-06 07:42:21 -05:00
|
|
|
|
|
|
|
|
//go:embed hero.png
|
|
|
|
|
hero_img []byte
|
2024-11-07 10:41:30 -05:00
|
|
|
|
|
|
|
|
//go:embed herodeath.png
|
|
|
|
|
herodeath_img []byte
|
2024-11-06 07:42:21 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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)
|
2024-11-07 10:41:30 -05:00
|
|
|
|
|
|
|
|
img, _, err = image.Decode(bytes.NewReader(herodeath_img))
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
heroDeath = ebiten.NewImageFromImage(img)
|
2024-11-06 07:42:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
2024-11-07 10:41:30 -05:00
|
|
|
case HeroActionDying:
|
|
|
|
|
m.Sprite.DrawImage(heroDeath.SubImage(image.Rect(x0, y0, x1, y1)).(*ebiten.Image), nil)
|
|
|
|
|
|
2024-11-07 15:01:10 -05:00
|
|
|
if m.dyingcount >= 31 {
|
2024-11-07 10:41:30 -05:00
|
|
|
m.cycles = 0
|
|
|
|
|
m.Action++
|
|
|
|
|
}
|
|
|
|
|
case HeroActionExploding:
|
|
|
|
|
m.Sprite.DrawImage(heroDeath.SubImage(image.Rect(48*3, 0, 48*4, 48)).(*ebiten.Image), nil)
|
2024-11-06 07:42:21 -05:00
|
|
|
default:
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *Hero) Update() {
|
2024-11-07 15:01:10 -05:00
|
|
|
|
|
|
|
|
if m.Action == HeroActionDying {
|
|
|
|
|
m.dyingcount++
|
|
|
|
|
}
|
2024-11-06 07:42:21 -05:00
|
|
|
m.cycles++
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-07 10:41:30 -05:00
|
|
|
// one hit death for the hero
|
2024-11-06 07:42:21 -05:00
|
|
|
func (m *Hero) SetHit() {
|
2024-11-07 10:41:30 -05:00
|
|
|
m.Action = MoverActionDying
|
2024-11-07 15:01:10 -05:00
|
|
|
m.dyingcount = 0
|
2024-11-07 10:41:30 -05:00
|
|
|
m.Angle = 0
|
|
|
|
|
m.cycles = 0
|
2024-11-06 07:42:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *Hero) ToggleColor() {
|
|
|
|
|
//m.Toggled = !m.Toggled
|
|
|
|
|
if m.Action == HeroActionDefault {
|
|
|
|
|
m.Action = HeroActionDamaged
|
|
|
|
|
} else if m.Action == HeroActionDamaged {
|
|
|
|
|
m.Action = HeroActionDefault
|
|
|
|
|
}
|
|
|
|
|
}
|