Files
survive/elements/mover.go

151 lines
3.2 KiB
Go
Raw Normal View History

package elements
2024-11-05 06:28:08 -05:00
import (
"image"
"mover/assets"
"mover/gamedata"
2024-11-05 06:28:08 -05:00
_ "embed"
"image/color"
_ "image/png"
"github.com/hajimehoshi/ebiten/v2"
)
const (
MoverActionDefault = iota
MoverActionDamaged
MoverActionDying
MoverActionExploding
MoverActionDead
MoverActionMax
)
type MoverAction uint
type Mover struct {
Sprite *ebiten.Image
Maks *ebiten.Image
MaksDest *ebiten.Image
Angle float64
Pos gamedata.Coordinates
Origin gamedata.Coordinates
2024-11-05 06:28:08 -05:00
Action MoverAction
cycles int
rotating bool
Toggled bool
Hit bool
Touched bool
2024-11-05 06:28:08 -05:00
dyingcount int
}
func NewMover() *Mover {
m := &Mover{
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-05 06:28:08 -05:00
Action: MoverActionDefault,
cycles: 4,
Angle: 0,
rotating: false,
Toggled: false,
dyingcount: 0,
}
m.Maks.Fill(color.White)
return m
}
func (m *Mover) ToggleRotate() {
m.rotating = !m.rotating
}
func (m *Mover) SetAngle(a float64) {
m.Angle = a
}
func (m *Mover) SetOrigin(coords gamedata.Coordinates) {
2024-11-05 06:28:08 -05:00
m.Origin = coords
m.Pos = coords
}
func (m *Mover) 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 MoverActionDefault:
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(14, 40)
m.Sprite.DrawImage(assets.ImageBank[assets.FlyEyeShadow], op)
m.Sprite.DrawImage(assets.ImageBank[assets.FlyEyeNormal].SubImage(image.Rect(x0, y0, x1, y1)).(*ebiten.Image), nil)
2024-11-05 06:28:08 -05:00
case MoverActionDamaged:
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(14, 40)
m.Sprite.DrawImage(assets.ImageBank[assets.FlyEyeShadow], op)
m.Sprite.DrawImage(assets.ImageBank[assets.FlyEyeDamaged].SubImage(image.Rect(x0, y0, x1, y1)).(*ebiten.Image), nil)
2024-11-05 06:28:08 -05:00
case MoverActionDying:
2024-11-05 06:28:08 -05:00
if (m.cycles/5)%2 == 0 {
m.MaksDest.DrawImage(assets.ImageBank[assets.FlyEyeDamaged].SubImage(image.Rect(x0, y0, x1, y1)).(*ebiten.Image), nil)
2024-11-05 06:28:08 -05:00
op := &ebiten.DrawImageOptions{}
op.GeoM.Reset()
op.Blend = ebiten.BlendSourceAtop
m.MaksDest.DrawImage(m.Maks, op)
m.Sprite.DrawImage(m.MaksDest, nil)
} else {
m.Sprite.DrawImage(assets.ImageBank[assets.FlyEyeDamaged].SubImage(image.Rect(x0, y0, x1, y1)).(*ebiten.Image), nil)
2024-11-05 06:28:08 -05:00
}
if m.dyingcount >= 31 {
2024-11-05 06:28:08 -05:00
m.cycles = 0
m.SetHit()
}
case MoverActionExploding:
m.Sprite.DrawImage(assets.ImageBank[assets.FlyEyeDying].SubImage(image.Rect(x0, y0, x1, y1)).(*ebiten.Image), nil)
2024-11-05 06:28:08 -05:00
if idx == 3 {
m.SetHit()
}
default:
}
}
func (m *Mover) Update() {
/*
dx := 0. //40 * math.Cos(float64(m.cycles)/16)
dy := 0. //40 * math.Sin(float64(m.cycles)/16)
m.Pos = Coordinates{X: m.Origin.X + dx, Y: m.Origin.Y + dy}
*/
/*
if m.rotating {
m.Angle = float64(m.cycles) / (math.Pi * 2)
}
*/
if m.Action == MoverActionDying {
m.dyingcount++
}
2024-11-05 06:28:08 -05:00
m.cycles++
}
func (m *Mover) SetHit() {
m.Action++ // = (m.Action + 1) % MoverActionMax
}
func (m *Mover) ToggleColor() {
//m.Toggled = !m.Toggled
if m.Action == MoverActionDefault {
m.Action = MoverActionDamaged
} else if m.Action == MoverActionDamaged {
m.Action = MoverActionDefault
}
2024-11-05 06:28:08 -05:00
}