Menu and save system
This commit is contained in:
209
internal/screens/title.go
Normal file
209
internal/screens/title.go
Normal file
@@ -0,0 +1,209 @@
|
||||
package screens
|
||||
|
||||
import (
|
||||
"image/color"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
||||
"github.com/hajimehoshi/ebiten/v2/text/v2"
|
||||
"github.com/hajimehoshi/ebiten/v2/vector"
|
||||
)
|
||||
|
||||
// Represents the options available on the title screen.
|
||||
type TitleMenuOption int
|
||||
|
||||
const (
|
||||
TitleOptionContinue TitleMenuOption = iota
|
||||
TitleOptionNewGame
|
||||
TitleOptionSettings
|
||||
TitleOptionQuit
|
||||
titleOptionCount
|
||||
)
|
||||
|
||||
type titleScreenMode int
|
||||
|
||||
const (
|
||||
titleModeMain titleScreenMode = iota
|
||||
titleModeSettings
|
||||
)
|
||||
|
||||
// Displays the main menu with Continue, New Game, Settings, and Quit options.
|
||||
// This is shown after the splash screen and when returning from the pause menu.
|
||||
type TitleScreen struct {
|
||||
selectedIndex int
|
||||
currentMode titleScreenMode
|
||||
settingsScreen *SettingsScreen
|
||||
hasSaveGame bool
|
||||
}
|
||||
|
||||
// Creates a new title screen instance.
|
||||
func NewTitleScreen() *TitleScreen {
|
||||
return &TitleScreen{
|
||||
selectedIndex: 0,
|
||||
currentMode: titleModeMain,
|
||||
settingsScreen: NewSettingsScreen(),
|
||||
hasSaveGame: false,
|
||||
}
|
||||
}
|
||||
|
||||
// Sets the FPS monitor toggle reference for settings.
|
||||
func (t *TitleScreen) SetFPSMonitor(enabled *bool) {
|
||||
t.settingsScreen.SetFPSMonitor(enabled)
|
||||
}
|
||||
|
||||
// Sets the FPS cap setting reference for settings.
|
||||
func (t *TitleScreen) SetFPSCap(cap FPSCapSetting) {
|
||||
t.settingsScreen.SetFPSCap(cap)
|
||||
}
|
||||
|
||||
// Sets whether a saved game exists.
|
||||
func (t *TitleScreen) SetHasSaveGame(hasSave bool) {
|
||||
t.hasSaveGame = hasSave
|
||||
// If no save game, skip Continue option
|
||||
if !hasSave && t.selectedIndex == 0 {
|
||||
t.selectedIndex = 1 // Move to New Game
|
||||
}
|
||||
}
|
||||
|
||||
// Processes title screen input and returns the selected option if any.
|
||||
func (t *TitleScreen) Update() *TitleMenuOption {
|
||||
// Handle settings screen
|
||||
if t.currentMode == titleModeSettings {
|
||||
if t.settingsScreen.Update() {
|
||||
t.currentMode = titleModeMain
|
||||
t.selectedIndex = 0
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Handle main menu
|
||||
if inpututil.IsKeyJustPressed(ebiten.KeyArrowUp) || inpututil.IsKeyJustPressed(ebiten.KeyW) {
|
||||
t.selectedIndex--
|
||||
if t.selectedIndex < 0 {
|
||||
t.selectedIndex = int(titleOptionCount) - 1
|
||||
}
|
||||
// Skip Continue if no save exists
|
||||
if t.selectedIndex == int(TitleOptionContinue) && !t.hasSaveGame {
|
||||
t.selectedIndex--
|
||||
if t.selectedIndex < 0 {
|
||||
t.selectedIndex = int(titleOptionCount) - 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if inpututil.IsKeyJustPressed(ebiten.KeyArrowDown) || inpututil.IsKeyJustPressed(ebiten.KeyS) {
|
||||
t.selectedIndex++
|
||||
if t.selectedIndex >= int(titleOptionCount) {
|
||||
t.selectedIndex = 0
|
||||
}
|
||||
// Skip Continue if no save exists
|
||||
if t.selectedIndex == int(TitleOptionContinue) && !t.hasSaveGame {
|
||||
t.selectedIndex++
|
||||
if t.selectedIndex >= int(titleOptionCount) {
|
||||
t.selectedIndex = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if inpututil.IsKeyJustPressed(ebiten.KeyEnter) || inpututil.IsKeyJustPressed(ebiten.KeySpace) {
|
||||
selected := TitleMenuOption(t.selectedIndex)
|
||||
if selected == TitleOptionSettings {
|
||||
t.currentMode = titleModeSettings
|
||||
t.settingsScreen.Reset()
|
||||
return nil
|
||||
}
|
||||
return &selected
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Renders the title screen.
|
||||
func (t *TitleScreen) Draw(screen *ebiten.Image, screenWidth, screenHeight int) {
|
||||
// If in settings mode, draw settings screen
|
||||
if t.currentMode == titleModeSettings {
|
||||
t.settingsScreen.Draw(screen, screenWidth, screenHeight, "SETTINGS")
|
||||
return
|
||||
}
|
||||
|
||||
// Draw main menu
|
||||
screen.Fill(color.RGBA{R: 20, G: 20, B: 30, A: 255})
|
||||
|
||||
// Draw game title
|
||||
titleText := "BIG FEELINGS"
|
||||
scale := 3.0
|
||||
charWidth := 7.0 * scale
|
||||
textWidth := float64(len(titleText)) * charWidth
|
||||
|
||||
titleX := float64(screenWidth)/2 - textWidth/2
|
||||
titleY := float64(screenHeight) / 3
|
||||
|
||||
op := &text.DrawOptions{}
|
||||
op.GeoM.Scale(scale, scale)
|
||||
op.GeoM.Translate(titleX, titleY-basicFaceAscent*scale)
|
||||
op.ColorScale.ScaleWithColor(color.RGBA{R: 210, G: 220, B: 255, A: 255})
|
||||
text.Draw(screen, titleText, basicFace, op)
|
||||
|
||||
// Draw menu options
|
||||
options := []string{"Continue", "New Game", "Settings", "Quit"}
|
||||
startY := screenHeight/2 + 10
|
||||
|
||||
for i, option := range options {
|
||||
// Skip Continue option if no save exists
|
||||
if i == int(TitleOptionContinue) && !t.hasSaveGame {
|
||||
continue
|
||||
}
|
||||
optionY := startY + (i * 50)
|
||||
optionX := (screenWidth / 2) - (len(option) * 7 / 2)
|
||||
|
||||
// Dim Continue option if it exists but is disabled
|
||||
optionColor := color.RGBA{R: 180, G: 180, B: 200, A: 255}
|
||||
if i == int(TitleOptionContinue) && !t.hasSaveGame {
|
||||
optionColor = color.RGBA{R: 80, G: 80, B: 100, A: 255}
|
||||
}
|
||||
|
||||
if i == t.selectedIndex {
|
||||
// Draw selection indicator
|
||||
indicatorX := optionX - 20
|
||||
t.drawText(screen, ">", color.RGBA{R: 255, G: 200, B: 0, A: 255}, indicatorX, optionY)
|
||||
t.drawText(screen, option, color.RGBA{R: 255, G: 255, B: 100, A: 255}, optionX, optionY)
|
||||
|
||||
// Draw selection box
|
||||
boxPadding := float32(10.0)
|
||||
boxWidth := float32(len(option)*7) + boxPadding*2
|
||||
boxHeight := float32(20)
|
||||
boxX := float32(optionX) - boxPadding
|
||||
boxY := float32(optionY) - float32(basicFaceAscent) - boxPadding/2
|
||||
|
||||
vector.StrokeRect(screen, boxX, boxY, boxWidth, boxHeight, 2,
|
||||
color.RGBA{R: 255, G: 200, B: 0, A: 255}, false)
|
||||
} else {
|
||||
t.drawText(screen, option, optionColor, optionX, optionY)
|
||||
}
|
||||
}
|
||||
|
||||
// Draw hint text
|
||||
hintText := "Use Arrow Keys/WASD to navigate, Enter/Space to select"
|
||||
hintX := (screenWidth / 2) - (len(hintText) * 7 / 2)
|
||||
hintY := screenHeight - 50
|
||||
t.drawText(screen, hintText, color.RGBA{R: 120, G: 120, B: 150, A: 255}, hintX, hintY)
|
||||
}
|
||||
|
||||
func (t *TitleScreen) drawText(screen *ebiten.Image, txt string, clr color.Color, x, y int) {
|
||||
op := &text.DrawOptions{}
|
||||
op.GeoM.Translate(float64(x), float64(y)-basicFaceAscent)
|
||||
op.ColorScale.ScaleWithColor(clr)
|
||||
text.Draw(screen, txt, basicFace, op)
|
||||
}
|
||||
|
||||
// Resets the title screen to its initial state.
|
||||
func (t *TitleScreen) Reset() {
|
||||
// Start at Continue if available, otherwise New Game
|
||||
if t.hasSaveGame {
|
||||
t.selectedIndex = 0
|
||||
} else {
|
||||
t.selectedIndex = 1
|
||||
}
|
||||
t.currentMode = titleModeMain
|
||||
t.settingsScreen.Reset()
|
||||
}
|
||||
Reference in New Issue
Block a user