2024-11-11 09:54:30 -05:00
|
|
|
package elements
|
2024-11-06 07:42:21 -05:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"image"
|
2024-11-11 09:54:30 -05:00
|
|
|
"mover/assets"
|
|
|
|
|
"mover/gamedata"
|
2024-11-06 07:42:21 -05:00
|
|
|
|
|
|
|
|
_ "embed"
|
|
|
|
|
"image/color"
|
|
|
|
|
_ "image/png"
|
|
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
|
)
|
|
|
|
|
|
2024-11-11 09:54:30 -05:00
|
|
|
const (
|
|
|
|
|
MOVER_WIDTH = 48
|
|
|
|
|
MOVER_HEIGHT = 48
|
2024-11-06 07:42:21 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
HeroActionDefault = iota
|
|
|
|
|
HeroActionDamaged
|
|
|
|
|
HeroActionDying
|
|
|
|
|
HeroActionExploding
|
|
|
|
|
HeroActionDead
|
|
|
|
|
HeroActionMax
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type HeroAction uint
|
|
|
|
|
|
|
|
|
|
type Hero struct {
|
|
|
|
|
Sprite *ebiten.Image
|
|
|
|
|
Maks *ebiten.Image
|
|
|
|
|
MaksDest *ebiten.Image
|
|
|
|
|
Angle float64
|
2024-11-11 09:54:30 -05:00
|
|
|
Pos gamedata.Coordinates
|
|
|
|
|
Origin gamedata.Coordinates
|
|
|
|
|
Lastpos gamedata.Coordinates
|
2024-11-06 07:42:21 -05:00
|
|
|
Action HeroAction
|
|
|
|
|
cycles int
|
2024-11-08 16:02:41 -05:00
|
|
|
Upgrade bool
|
2024-11-06 07:42:21 -05:00
|
|
|
rotating bool
|
|
|
|
|
Toggled bool
|
|
|
|
|
Hit bool
|
|
|
|
|
Touched bool
|
2024-11-07 17:39:39 -05:00
|
|
|
Left bool
|
2024-11-06 07:42:21 -05:00
|
|
|
dyingcount int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewHero() *Hero {
|
|
|
|
|
m := &Hero{
|
2024-11-08 07:29:34 -05:00
|
|
|
Sprite: ebiten.NewImage(MOVER_WIDTH, MOVER_HEIGHT),
|
|
|
|
|
Maks: ebiten.NewImage(MOVER_WIDTH, MOVER_HEIGHT),
|
|
|
|
|
MaksDest: ebiten.NewImage(MOVER_WIDTH, MOVER_HEIGHT),
|
2024-11-06 07:42:21 -05:00
|
|
|
Action: HeroActionDefault,
|
|
|
|
|
cycles: 4,
|
|
|
|
|
Angle: 0,
|
|
|
|
|
rotating: false,
|
|
|
|
|
Toggled: false,
|
|
|
|
|
dyingcount: 0,
|
2024-11-08 16:02:41 -05:00
|
|
|
Upgrade: true,
|
2024-11-06 07:42:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m.Maks.Fill(color.White)
|
|
|
|
|
return m
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *Hero) ToggleRotate() {
|
|
|
|
|
m.rotating = !m.rotating
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *Hero) SetAngle(a float64) {
|
|
|
|
|
m.Angle = a
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-11 09:54:30 -05:00
|
|
|
func (m *Hero) SetOrigin(coords gamedata.Coordinates) {
|
2024-11-06 07:42:21 -05:00
|
|
|
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
|
|
|
|
|
|
2024-11-07 17:39:39 -05:00
|
|
|
op := &ebiten.DrawImageOptions{}
|
|
|
|
|
if m.Left {
|
|
|
|
|
op.GeoM.Scale(-1, 1)
|
|
|
|
|
op.GeoM.Translate(MOVER_WIDTH, 0)
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-06 07:42:21 -05:00
|
|
|
switch m.Action {
|
|
|
|
|
case HeroActionDefault:
|
2024-11-11 09:54:30 -05:00
|
|
|
m.Sprite.DrawImage(assets.ImageBank[assets.HeroNormal].SubImage(image.Rect(x0, y0, x1, y1)).(*ebiten.Image), op)
|
2024-11-07 10:41:30 -05:00
|
|
|
case HeroActionDying:
|
2024-11-11 09:54:30 -05:00
|
|
|
m.Sprite.DrawImage(assets.ImageBank[assets.HeroDying].SubImage(image.Rect(x0, y0, x1, y1)).(*ebiten.Image), op)
|
2024-11-07 10:41:30 -05:00
|
|
|
|
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:
|
2024-11-11 09:54:30 -05:00
|
|
|
m.Sprite.DrawImage(assets.ImageBank[assets.HeroDying].SubImage(image.Rect(48*3, 0, 48*4, 48)).(*ebiten.Image), op)
|
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-08 07:29:34 -05:00
|
|
|
m.Action = HeroActionDying
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|