2024-11-11 10:25:02 -05:00
|
|
|
package screens
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"image/color"
|
2024-11-11 10:48:44 -05:00
|
|
|
"math"
|
2024-11-11 14:07:02 -05:00
|
|
|
"mover/assets"
|
2024-11-11 10:25:02 -05:00
|
|
|
"mover/fonts"
|
|
|
|
|
"mover/gamedata"
|
|
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/text"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type StartScreen struct {
|
2024-11-11 10:48:44 -05:00
|
|
|
eHandler map[ScreenManagerEvent]func()
|
|
|
|
|
dimensions gamedata.Area
|
|
|
|
|
target gamedata.Coordinates
|
|
|
|
|
current gamedata.Coordinates
|
|
|
|
|
targetreached bool
|
2024-11-11 14:07:02 -05:00
|
|
|
audioplayed bool
|
|
|
|
|
cycle int
|
2024-11-11 10:25:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewStartScreen() *StartScreen {
|
|
|
|
|
s := &StartScreen{
|
2024-11-11 10:48:44 -05:00
|
|
|
eHandler: make(map[ScreenManagerEvent]func()),
|
|
|
|
|
target: gamedata.Coordinates{X: 640/2 - 150, Y: 480 / 2},
|
|
|
|
|
current: gamedata.Coordinates{X: 640/2 - 150, Y: -100},
|
|
|
|
|
targetreached: false,
|
2024-11-11 14:07:02 -05:00
|
|
|
cycle: 0,
|
|
|
|
|
audioplayed: false,
|
2024-11-11 10:25:02 -05:00
|
|
|
}
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *StartScreen) Update() error {
|
|
|
|
|
|
2024-11-11 10:48:44 -05:00
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyEnter) ||
|
|
|
|
|
ebiten.IsStandardGamepadButtonPressed(0, ebiten.StandardGamepadButtonCenterRight) {
|
2024-11-11 10:25:02 -05:00
|
|
|
s.eHandler[EventCompleted]()
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-11 14:07:02 -05:00
|
|
|
if !s.audioplayed {
|
|
|
|
|
player := audioContext.NewPlayerFromBytes(assets.Survive)
|
|
|
|
|
player.Play()
|
|
|
|
|
s.audioplayed = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !s.targetreached {
|
|
|
|
|
s.current.X += (s.target.X - s.current.X) / 8
|
|
|
|
|
s.current.Y += (s.target.Y - s.current.Y) / 8
|
|
|
|
|
} else {
|
|
|
|
|
s.current.Y += 0.5 * math.Sin(float64(s.cycle)/(math.Pi*8))
|
|
|
|
|
}
|
2024-11-11 10:48:44 -05:00
|
|
|
|
|
|
|
|
if math.Abs(s.current.Y-s.target.Y) < 1 && !s.targetreached {
|
|
|
|
|
s.targetreached = true
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-11 14:07:02 -05:00
|
|
|
s.cycle++
|
2024-11-11 10:25:02 -05:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *StartScreen) Draw(screen *ebiten.Image) {
|
|
|
|
|
screen.Clear()
|
2024-11-11 10:48:44 -05:00
|
|
|
text.Draw(screen, "survive", fonts.SurviveFont.ArcadeLarge, int(s.current.X), int(s.current.Y), color.White)
|
|
|
|
|
|
|
|
|
|
if s.targetreached {
|
|
|
|
|
text.Draw(screen, "press start", fonts.SurviveFont.Arcade, 640/2-25, 300, color.White)
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-11 15:00:44 -05:00
|
|
|
text.Draw(screen, "©bsoft games", fonts.SurviveFont.ArcadeSmall, 640/2+25, 180, color.White)
|
|
|
|
|
|
2024-11-11 10:25:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *StartScreen) SetEventHandler(e ScreenManagerEvent, f func()) {
|
|
|
|
|
s.eHandler[e] = f
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *StartScreen) SetDimensions(a gamedata.Area) {
|
|
|
|
|
s.dimensions = a
|
|
|
|
|
}
|