Brought in screen manager, minor refactor.

This commit is contained in:
2024-11-11 09:54:30 -05:00
parent 9130155999
commit 6f794b7bb2
21 changed files with 373 additions and 170 deletions

42
elements/explosion.go Normal file
View File

@@ -0,0 +1,42 @@
package elements
import "mover/gamedata"
type Explosion struct {
Radius float64
Origin gamedata.Coordinates
cycle int
Active bool
}
func NewExplosion() *Explosion {
return &Explosion{
cycle: 1,
Active: false,
}
}
func (e *Explosion) Draw() {
}
func (e *Explosion) Update() {
if e.Active {
e.Radius = float64(e.cycle) / 0.15
e.cycle++
}
}
func (e *Explosion) SetOrigin(origin gamedata.Coordinates) {
e.Origin = origin
}
func (e *Explosion) ToggleActivate() {
e.Active = !e.Active
}
func (e *Explosion) Reset() {
e.cycle = 1
}

137
elements/hero.go Normal file
View File

@@ -0,0 +1,137 @@
package elements
import (
"image"
"mover/assets"
"mover/gamedata"
_ "embed"
"image/color"
_ "image/png"
"github.com/hajimehoshi/ebiten/v2"
)
const (
MOVER_WIDTH = 48
MOVER_HEIGHT = 48
)
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
Pos gamedata.Coordinates
Origin gamedata.Coordinates
Lastpos gamedata.Coordinates
Action HeroAction
cycles int
Upgrade bool
rotating bool
Toggled bool
Hit bool
Touched bool
Left bool
dyingcount int
}
func NewHero() *Hero {
m := &Hero{
Sprite: ebiten.NewImage(MOVER_WIDTH, MOVER_HEIGHT),
Maks: ebiten.NewImage(MOVER_WIDTH, MOVER_HEIGHT),
MaksDest: ebiten.NewImage(MOVER_WIDTH, MOVER_HEIGHT),
Action: HeroActionDefault,
cycles: 4,
Angle: 0,
rotating: false,
Toggled: false,
dyingcount: 0,
Upgrade: true,
}
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 gamedata.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
op := &ebiten.DrawImageOptions{}
if m.Left {
op.GeoM.Scale(-1, 1)
op.GeoM.Translate(MOVER_WIDTH, 0)
}
switch m.Action {
case HeroActionDefault:
m.Sprite.DrawImage(assets.ImageBank[assets.HeroNormal].SubImage(image.Rect(x0, y0, x1, y1)).(*ebiten.Image), op)
case HeroActionDying:
m.Sprite.DrawImage(assets.ImageBank[assets.HeroDying].SubImage(image.Rect(x0, y0, x1, y1)).(*ebiten.Image), op)
if m.dyingcount >= 31 {
m.cycles = 0
m.Action++
}
case HeroActionExploding:
m.Sprite.DrawImage(assets.ImageBank[assets.HeroDying].SubImage(image.Rect(48*3, 0, 48*4, 48)).(*ebiten.Image), op)
default:
}
}
func (m *Hero) Update() {
if m.Action == HeroActionDying {
m.dyingcount++
}
m.cycles++
}
// one hit death for the hero
func (m *Hero) SetHit() {
m.Action = HeroActionDying
m.dyingcount = 0
m.Angle = 0
m.cycles = 0
}
func (m *Hero) ToggleColor() {
//m.Toggled = !m.Toggled
if m.Action == HeroActionDefault {
m.Action = HeroActionDamaged
} else if m.Action == HeroActionDamaged {
m.Action = HeroActionDefault
}
}

150
elements/mover.go Normal file
View File

@@ -0,0 +1,150 @@
package elements
import (
"image"
"mover/assets"
"mover/gamedata"
_ "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
Action MoverAction
cycles int
rotating bool
Toggled bool
Hit bool
Touched bool
dyingcount int
}
func NewMover() *Mover {
m := &Mover{
Sprite: ebiten.NewImage(MOVER_WIDTH, MOVER_HEIGHT),
Maks: ebiten.NewImage(MOVER_WIDTH, MOVER_HEIGHT),
MaksDest: ebiten.NewImage(MOVER_WIDTH, MOVER_HEIGHT),
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) {
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)
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)
case MoverActionDying:
if (m.cycles/5)%2 == 0 {
m.MaksDest.DrawImage(assets.ImageBank[assets.FlyEyeDamaged].SubImage(image.Rect(x0, y0, x1, y1)).(*ebiten.Image), nil)
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)
}
if m.dyingcount >= 31 {
m.cycles = 0
m.SetHit()
}
case MoverActionExploding:
m.Sprite.DrawImage(assets.ImageBank[assets.FlyEyeDying].SubImage(image.Rect(x0, y0, x1, y1)).(*ebiten.Image), nil)
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++
}
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
}
}

34
elements/projectile.go Normal file
View File

@@ -0,0 +1,34 @@
package elements
import (
"math"
"mover/gamedata"
)
type Projectile struct {
Pos gamedata.Coordinates
Velocity float64
a float64
}
func NewProjectile(origin gamedata.Coordinates, angle, velocity float64) *Projectile {
return &Projectile{
Velocity: velocity,
a: angle,
Pos: origin,
}
}
func (p *Projectile) Update() {
dx := p.Velocity * math.Cos(p.a)
dy := p.Velocity * math.Sin(p.a)
p.Pos.X += dx
p.Pos.Y += dy
}
func (p *Projectile) Draw() {
}