doctorlectro/system/win.go

71 lines
1.5 KiB
Go

package system
import (
"image/color"
_ "image/png"
"code.rocketnine.space/tslocum/doctorlectro/asset"
"code.rocketnine.space/tslocum/doctorlectro/component"
"code.rocketnine.space/tslocum/doctorlectro/world"
"code.rocketnine.space/tslocum/gohan"
"github.com/hajimehoshi/ebiten/v2"
)
type WinSystem struct {
Position *component.Position
Velocity *component.Velocity
Player *component.Player
op *ebiten.DrawImageOptions
img *ebiten.Image
}
func NewWinSystem() *WinSystem {
s := &WinSystem{
op: &ebiten.DrawImageOptions{},
img: ebiten.NewImage(world.ScreenWidth, world.ScreenHeight),
}
s.img.Fill(color.RGBA{0, 0, 0, 255})
return s
}
func (s *WinSystem) Update(_ gohan.Entity) error {
if world.GameOverTicks == 0 {
return nil
}
world.GameOverTicks++
return nil
}
func (s *WinSystem) Draw(e gohan.Entity, screen *ebiten.Image) error {
if world.GameOverTicks == 0 {
return nil
}
const fadeOutTime = 144 * 3
var alpha float64
if world.GameOverTicks < fadeOutTime {
alpha = float64(world.GameOverTicks) / fadeOutTime
} else if world.GameOverTicks == fadeOutTime {
// Draw newspaper
s.img = asset.ImgNewspaper
alpha = 0
} else if world.GameOverTicks >= fadeOutTime*2 {
alpha = 1
} else {
// Fade in newspaper
alpha = float64(world.GameOverTicks-fadeOutTime) / fadeOutTime
}
if world.GameOverTicks >= fadeOutTime {
screen.Fill(color.RGBA{0, 0, 0, 255})
}
s.op.ColorM.Reset()
s.op.ColorM.Scale(1, 1, 1, alpha)
screen.DrawImage(s.img, s.op)
return nil
}