Better commenting throughout
This commit is contained in:
@@ -11,8 +11,6 @@ import (
|
||||
"github.com/atridad/LilGuy/internal/ui/menu"
|
||||
)
|
||||
|
||||
// Window and display configuration.
|
||||
|
||||
const (
|
||||
ScreenWidth = 960
|
||||
ScreenHeight = 540
|
||||
@@ -20,29 +18,17 @@ const (
|
||||
WindowTitle = "Lil Guy"
|
||||
)
|
||||
|
||||
// Game states define the different screens and modes the game can be in.
|
||||
// To add a new state:
|
||||
// 1. Add a new constant to the gameState enum below
|
||||
// 2. Create a new screen type in internal/screens/ (see splash.go, title.go, gameplay.go as examples)
|
||||
// 3. Add the screen instance to the 'state' struct
|
||||
// 4. Handle state transitions in Update() method
|
||||
// 5. Handle rendering in Draw() method
|
||||
//
|
||||
// State Flow:
|
||||
// stateSplash -> stateTitle -> statePlaying <-> statePaused
|
||||
// ^____________|
|
||||
|
||||
// Game states
|
||||
type gameState int
|
||||
|
||||
const (
|
||||
stateSplash gameState = iota // Initial splash screen with game logo
|
||||
stateTitle // Main menu (Play/Quit options)
|
||||
statePlaying // Active gameplay
|
||||
statePaused // Game paused (overlay menu)
|
||||
stateSplash gameState = iota
|
||||
stateTitle
|
||||
statePlaying
|
||||
statePaused
|
||||
)
|
||||
|
||||
// FPS cap options for performance tuning.
|
||||
|
||||
// FPS cap options
|
||||
type FPSCap int
|
||||
|
||||
const (
|
||||
@@ -82,8 +68,7 @@ func (f *FPSCap) Cycle() {
|
||||
*f = (*f + 1) % fpsCapCount
|
||||
}
|
||||
|
||||
// Input state for player controls.
|
||||
|
||||
// Player input
|
||||
type controls struct {
|
||||
Left bool
|
||||
Right bool
|
||||
@@ -106,34 +91,28 @@ type Game struct {
|
||||
state *state
|
||||
}
|
||||
|
||||
// state holds all game state including screens, settings, and current mode.
|
||||
// Main game state
|
||||
type state struct {
|
||||
// Current state
|
||||
gameState gameState
|
||||
lastTick time.Time
|
||||
|
||||
// Screens - each screen manages its own UI and logic
|
||||
splashScreen *screens.SplashScreen
|
||||
titleScreen *screens.TitleScreen
|
||||
gameplayScreen *screens.GameplayScreen
|
||||
pauseMenu *menu.PauseMenu
|
||||
|
||||
// Settings
|
||||
fpsEnabled bool
|
||||
fpsCap FPSCap
|
||||
saveManager *save.Manager
|
||||
|
||||
// Auto-save
|
||||
lastAutoSave time.Time
|
||||
autoSaveInterval time.Duration
|
||||
}
|
||||
|
||||
// New creates a new game instance.
|
||||
func New() *Game {
|
||||
return &Game{state: newState()}
|
||||
}
|
||||
|
||||
// newState initializes a fresh game state.
|
||||
func newState() *state {
|
||||
now := time.Now()
|
||||
s := &state{
|
||||
@@ -142,19 +121,15 @@ func newState() *state {
|
||||
fpsEnabled: false,
|
||||
fpsCap: FPSCap60,
|
||||
lastAutoSave: now,
|
||||
autoSaveInterval: 30 * time.Second, // Auto-save every 30 seconds
|
||||
autoSaveInterval: 30 * time.Second,
|
||||
}
|
||||
|
||||
// Initialize save manager
|
||||
saveManager, err := save.NewManager()
|
||||
if err != nil {
|
||||
// If save manager fails, continue without it (settings won't persist)
|
||||
// TODO: Show error to user
|
||||
saveManager = nil
|
||||
}
|
||||
s.saveManager = saveManager
|
||||
|
||||
// Load settings if available
|
||||
if saveManager != nil {
|
||||
if settings, err := saveManager.LoadSettings(); err == nil {
|
||||
s.fpsEnabled = settings.FPSMonitor
|
||||
@@ -171,27 +146,25 @@ func newState() *state {
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize all screens
|
||||
// Initialize screens
|
||||
s.splashScreen = screens.NewSplashScreen()
|
||||
s.titleScreen = screens.NewTitleScreen()
|
||||
s.gameplayScreen = screens.NewGameplayScreen(ScreenWidth, ScreenHeight, &s.fpsEnabled)
|
||||
s.pauseMenu = menu.NewPauseMenu()
|
||||
|
||||
// Configure settings references for title screen and pause menu
|
||||
// Wire up settings references
|
||||
s.titleScreen.SetFPSMonitor(&s.fpsEnabled)
|
||||
s.titleScreen.SetFPSCap(&s.fpsCap)
|
||||
s.pauseMenu.SetFPSMonitor(&s.fpsEnabled)
|
||||
s.pauseMenu.SetFPSCap(&s.fpsCap)
|
||||
|
||||
// Check if saved game exists
|
||||
if saveManager != nil {
|
||||
s.titleScreen.SetHasSaveGame(saveManager.HasSavedGame())
|
||||
}
|
||||
|
||||
// Set initial TPS
|
||||
ebiten.SetTPS(s.fpsCap.TPS())
|
||||
|
||||
// Save initial settings to create data.toml on first launch
|
||||
// Create initial save file
|
||||
if saveManager != nil {
|
||||
settings := &save.Settings{
|
||||
FPSMonitor: s.fpsEnabled,
|
||||
@@ -203,7 +176,6 @@ func newState() *state {
|
||||
return s
|
||||
}
|
||||
|
||||
// Helper function for converting FPSCap to string (used in initialization)
|
||||
func (s *state) fpCapToStringHelper(cap FPSCap) string {
|
||||
switch cap {
|
||||
case FPSCap60:
|
||||
@@ -217,14 +189,10 @@ func (s *state) fpCapToStringHelper(cap FPSCap) string {
|
||||
}
|
||||
}
|
||||
|
||||
// Update is called every frame and handles state transitions and input.
|
||||
|
||||
func (g *Game) Update() error {
|
||||
// Track previous FPS settings to detect changes
|
||||
prevFPSEnabled := g.state.fpsEnabled
|
||||
prevFPSCap := g.state.fpsCap
|
||||
|
||||
// Update TPS if FPS cap changed
|
||||
currentTPS := g.state.fpsCap.TPS()
|
||||
if currentTPS < 0 {
|
||||
ebiten.SetTPS(ebiten.SyncWithFPS)
|
||||
@@ -232,7 +200,7 @@ func (g *Game) Update() error {
|
||||
ebiten.SetTPS(currentTPS)
|
||||
}
|
||||
|
||||
// Handle state-specific updates
|
||||
// Update current screen
|
||||
var err error
|
||||
switch g.state.gameState {
|
||||
case stateSplash:
|
||||
@@ -245,7 +213,6 @@ func (g *Game) Update() error {
|
||||
err = g.updatePaused()
|
||||
}
|
||||
|
||||
// Auto-save settings if they changed
|
||||
if prevFPSEnabled != g.state.fpsEnabled || prevFPSCap != g.state.fpsCap {
|
||||
g.saveSettings()
|
||||
}
|
||||
@@ -253,7 +220,8 @@ func (g *Game) Update() error {
|
||||
return err
|
||||
}
|
||||
|
||||
// updateSplash handles the splash screen state.
|
||||
// Screen update handlers
|
||||
|
||||
func (g *Game) updateSplash() error {
|
||||
if g.state.splashScreen.Update() {
|
||||
g.state.gameState = stateTitle
|
||||
@@ -261,12 +229,10 @@ func (g *Game) updateSplash() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// updateTitle handles the title screen state.
|
||||
func (g *Game) updateTitle() error {
|
||||
if selectedOption := g.state.titleScreen.Update(); selectedOption != nil {
|
||||
switch *selectedOption {
|
||||
case screens.TitleOptionContinue:
|
||||
// Load saved game
|
||||
if g.state.saveManager != nil {
|
||||
if gameState, err := g.state.saveManager.LoadGameState(); err == nil && gameState != nil {
|
||||
g.state.gameplayScreen.LoadState(gameState)
|
||||
@@ -278,13 +244,10 @@ func (g *Game) updateTitle() error {
|
||||
g.state.gameState = statePlaying
|
||||
g.state.gameplayScreen.Reset()
|
||||
g.state.lastTick = time.Now()
|
||||
// Delete old save if it exists
|
||||
if g.state.saveManager != nil {
|
||||
g.state.saveManager.DeleteGameState()
|
||||
}
|
||||
case screens.TitleOptionSettings:
|
||||
// Settings are handled within the title screen itself
|
||||
// No state change needed
|
||||
case screens.TitleOptionQuit:
|
||||
return ebiten.Termination
|
||||
}
|
||||
@@ -292,21 +255,17 @@ func (g *Game) updateTitle() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// updatePlaying handles the active gameplay state.
|
||||
func (g *Game) updatePlaying() error {
|
||||
// Check for pause
|
||||
if inpututil.IsKeyJustPressed(ebiten.KeyEscape) {
|
||||
g.state.gameState = statePaused
|
||||
g.state.pauseMenu.Reset()
|
||||
return nil
|
||||
}
|
||||
|
||||
// Calculate delta time
|
||||
now := time.Now()
|
||||
delta := now.Sub(g.state.lastTick)
|
||||
g.state.lastTick = now
|
||||
|
||||
// Update gameplay
|
||||
input := readControls()
|
||||
g.state.gameplayScreen.Update(screens.GameplayInput{
|
||||
Left: input.Left,
|
||||
@@ -316,7 +275,6 @@ func (g *Game) updatePlaying() error {
|
||||
Shoot: input.Shoot,
|
||||
}, delta)
|
||||
|
||||
// Periodic auto-save
|
||||
if now.Sub(g.state.lastAutoSave) >= g.state.autoSaveInterval {
|
||||
g.saveGame()
|
||||
g.state.gameplayScreen.ShowSaveNotification()
|
||||
@@ -326,29 +284,24 @@ func (g *Game) updatePlaying() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// updatePaused handles the pause menu state.
|
||||
func (g *Game) updatePaused() error {
|
||||
// Allow ESC to resume
|
||||
if inpututil.IsKeyJustPressed(ebiten.KeyEscape) {
|
||||
g.state.gameState = statePlaying
|
||||
g.state.lastTick = time.Now()
|
||||
return nil
|
||||
}
|
||||
|
||||
// Handle pause menu selection
|
||||
if selectedOption := g.state.pauseMenu.Update(); selectedOption != nil {
|
||||
switch *selectedOption {
|
||||
case menu.OptionResume:
|
||||
g.state.gameState = statePlaying
|
||||
g.state.lastTick = time.Now()
|
||||
case menu.OptionSave:
|
||||
// Save game immediately
|
||||
g.saveGame()
|
||||
g.state.gameplayScreen.ShowSaveNotification()
|
||||
g.state.gameState = statePlaying
|
||||
g.state.lastTick = time.Now()
|
||||
case menu.OptionMainMenu:
|
||||
// Save game before returning to main menu
|
||||
g.saveGame()
|
||||
g.state.gameState = stateTitle
|
||||
if g.state.saveManager != nil {
|
||||
@@ -356,7 +309,6 @@ func (g *Game) updatePaused() error {
|
||||
}
|
||||
g.state.titleScreen.Reset()
|
||||
case menu.OptionQuit:
|
||||
// Save game before quitting
|
||||
g.saveGame()
|
||||
return ebiten.Termination
|
||||
}
|
||||
@@ -365,23 +317,21 @@ func (g *Game) updatePaused() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// saveGame saves the current game state and settings.
|
||||
// Save/load operations
|
||||
|
||||
func (g *Game) saveGame() {
|
||||
if g.state.saveManager == nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Save game state if in playing mode
|
||||
if g.state.gameState == statePlaying || g.state.gameState == statePaused {
|
||||
gameState := g.state.gameplayScreen.SaveState()
|
||||
g.state.saveManager.SaveGameState(gameState)
|
||||
}
|
||||
|
||||
// Save settings
|
||||
g.saveSettings()
|
||||
}
|
||||
|
||||
// saveSettings saves only the settings.
|
||||
func (g *Game) saveSettings() {
|
||||
if g.state.saveManager == nil {
|
||||
return
|
||||
@@ -394,7 +344,6 @@ func (g *Game) saveSettings() {
|
||||
g.state.saveManager.SaveSettings(settings)
|
||||
}
|
||||
|
||||
// fpCapToString converts FPSCap to string for saving.
|
||||
func (g *Game) fpCapToString(cap FPSCap) string {
|
||||
switch cap {
|
||||
case FPSCap60:
|
||||
@@ -408,7 +357,7 @@ func (g *Game) fpCapToString(cap FPSCap) string {
|
||||
}
|
||||
}
|
||||
|
||||
// Draw renders the current game state to the screen.
|
||||
// Rendering
|
||||
|
||||
func (g *Game) Draw(screen *ebiten.Image) {
|
||||
switch g.state.gameState {
|
||||
@@ -419,13 +368,11 @@ func (g *Game) Draw(screen *ebiten.Image) {
|
||||
case statePlaying:
|
||||
g.state.gameplayScreen.Draw(screen)
|
||||
case statePaused:
|
||||
// Draw gameplay in background, then overlay pause menu
|
||||
g.state.gameplayScreen.Draw(screen)
|
||||
g.state.pauseMenu.Draw(screen, ScreenWidth, ScreenHeight)
|
||||
}
|
||||
}
|
||||
|
||||
// Layout returns the game's logical screen size.
|
||||
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
|
||||
return ScreenWidth, ScreenHeight
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user