Files
LilGuy/internal/screens/gameplay.go

418 lines
10 KiB
Go

package screens
import (
"fmt"
"image/color"
"time"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/vector"
"github.com/atridad/LilGuy/internal/config"
"github.com/atridad/LilGuy/internal/hero"
"github.com/atridad/LilGuy/internal/maps"
"github.com/atridad/LilGuy/internal/portal"
"github.com/atridad/LilGuy/internal/projectile"
"github.com/atridad/LilGuy/internal/save"
"github.com/atridad/LilGuy/internal/status"
"github.com/atridad/LilGuy/internal/ui/hud"
"github.com/atridad/LilGuy/internal/world"
)
type GameplayInput struct {
Left bool
Right bool
Jump bool
Sprint bool
Shoot bool
}
type GameplayScreen struct {
hero *hero.Hero
hud hud.Overlay
world *world.World
projectiles *projectile.Manager
portals *portal.Manager
bounds hero.Bounds
lastTick time.Time
gameStartTime time.Time
totalPlayTime time.Duration
fpsEnabled *bool
fpsFrames int
fpsAccumulator time.Duration
fpsValue float64
saveNotificationTimer time.Duration
showSaveNotification bool
portalVisibility *bool
currentMap *maps.Map
allMaps map[string]*maps.Map
}
func NewGameplayScreen(screenWidth, screenHeight int, fpsEnabled *bool, portalVisibility *bool) *GameplayScreen {
cfg := config.Default()
map1, map2 := maps.CreateDefaultMaps(float64(screenWidth), float64(screenHeight))
allMaps := make(map[string]*maps.Map)
allMaps["map1"] = map1
allMaps["map2"] = map2
portalMgr := portal.NewManager()
for _, p := range map1.Portals {
portalMgr.AddPortal(p)
}
if portalVisibility != nil && *portalVisibility {
for _, p := range portalMgr.Portals {
p.Visible = true
}
}
gs := &GameplayScreen{
hero: hero.New(hero.Config{
StartX: cfg.Hero.StartX,
StartY: cfg.Hero.StartY,
Radius: cfg.Hero.Radius,
Speed: cfg.Hero.Speed,
Color: cfg.Hero.Color,
MaxStamina: cfg.Hero.MaxStamina,
StaminaDrain: cfg.Hero.StaminaDrain,
StaminaRegen: cfg.Hero.StaminaRegen,
}),
hud: hud.Overlay{
X: cfg.HUD.X,
Y: cfg.HUD.Y,
Color: color.White,
ScreenName: "Map 1",
},
world: map1.World,
projectiles: projectile.NewManager(),
portals: portalMgr,
bounds: hero.Bounds{
Width: float64(screenWidth),
Height: float64(screenHeight),
Ground: float64(screenHeight) - config.GroundHeight,
},
lastTick: time.Now(),
gameStartTime: time.Now(),
fpsEnabled: fpsEnabled,
portalVisibility: portalVisibility,
currentMap: map1,
allMaps: allMaps,
}
gs.portals.OnTransition = gs.handlePortalTransition
return gs
}
func (g *GameplayScreen) Update(input GameplayInput, delta time.Duration) {
dt := delta.Seconds()
g.hero.Update(hero.Input{
Left: input.Left,
Right: input.Right,
Jump: input.Jump,
Sprint: input.Sprint,
}, dt, g.bounds)
if input.Shoot {
direction := 1.0
if g.hero.GetDirection() == hero.DirLeft {
direction = -1.0
}
g.projectiles.Shoot(g.hero.X, g.hero.Y-20, direction, g.hero.ProjectileConfig)
}
g.projectiles.Update(dt, g.bounds.Width, g.bounds.Height)
g.portals.Update(dt)
// Check for portal collisions
g.checkPortalCollision()
g.totalPlayTime += delta
g.trackFPS(delta)
if g.showSaveNotification {
g.saveNotificationTimer -= delta
if g.saveNotificationTimer <= 0 {
g.showSaveNotification = false
}
}
}
// Portal collision detection
func (g *GameplayScreen) checkPortalCollision() {
heroRadius := g.hero.Radius
heroX := g.hero.X - heroRadius
heroY := g.hero.Y - heroRadius
heroWidth := heroRadius * 2
heroHeight := heroRadius * 2
if p := g.portals.CheckCollision(heroX, heroY, heroWidth, heroHeight); p != nil {
g.portals.TriggerTransition(p, g.hero.X, g.hero.Y)
}
g.updatePortalVisibility()
}
func (g *GameplayScreen) updatePortalVisibility() {
if g.portalVisibility == nil {
return
}
visible := *g.portalVisibility
for _, p := range g.portals.Portals {
p.Visible = visible
}
}
func (g *GameplayScreen) handlePortalTransition(event portal.TransitionEvent) {
destMap, exists := g.allMaps[event.DestinationMap]
if !exists {
return
}
// Switch to destination map
g.currentMap = destMap
g.world = destMap.World
// Clear and reload portals for new map
g.portals.Clear()
for _, p := range destMap.Portals {
g.portals.AddPortal(p)
}
g.updatePortalVisibility()
// Find destination portal and position hero
destPortal := destMap.GetPortalByID(event.DestinationPortal)
if destPortal != nil {
// Position hero based on which side they're entering from
switch destPortal.Side {
case portal.SideLeft:
g.hero.X = destPortal.X + destPortal.Width + g.hero.Radius + 10
g.hero.Y = event.HeroY
case portal.SideRight:
g.hero.X = destPortal.X - g.hero.Radius - 10
g.hero.Y = event.HeroY
case portal.SideTop:
g.hero.X = event.HeroX
g.hero.Y = destPortal.Y + destPortal.Height + g.hero.Radius + 10
case portal.SideBottom:
g.hero.X = event.HeroX
g.hero.Y = destPortal.Y - g.hero.Radius - 10
}
}
}
// FPS tracking
func (g *GameplayScreen) trackFPS(delta time.Duration) {
if g.fpsEnabled == nil || !*g.fpsEnabled {
return
}
g.fpsAccumulator += delta
g.fpsFrames++
if g.fpsAccumulator >= config.FPSSampleWindow {
g.fpsValue = float64(g.fpsFrames) / g.fpsAccumulator.Seconds()
g.fpsAccumulator = 0
g.fpsFrames = 0
}
}
// Rendering
func (g *GameplayScreen) Draw(screen *ebiten.Image) {
cfg := config.Default()
bgColor := cfg.Visual.BackgroundColor
if g.currentMap != nil {
bgColor = g.currentMap.BackgroundColor
}
screen.Fill(bgColor)
g.world.Draw(screen)
g.portals.Draw(screen)
g.projectiles.Draw(screen)
g.hero.Draw(screen)
if g.currentMap != nil {
g.hud.ScreenName = fmt.Sprintf("Map %d", g.currentMap.Number)
}
staminaColor := cfg.Visual.StaminaNormalColor
if g.hero.Stamina < g.hero.MaxStamina*config.StaminaLowThreshold {
staminaColor = cfg.Visual.StaminaLowColor
}
staminaMeter := status.Meter{
Label: "Stamina",
Base: g.hero.MaxStamina,
Level: g.hero.Stamina,
Color: staminaColor,
}
meters := []status.Meter{staminaMeter}
if g.fpsEnabled != nil && *g.fpsEnabled {
ratio := g.fpsValue / float64(config.TargetTPS)
fpsColor := cfg.Visual.FPSGoodColor
switch {
case ratio < config.FPSPoorThreshold:
fpsColor = cfg.Visual.FPSPoorColor
case ratio < config.FPSWarnThreshold:
fpsColor = cfg.Visual.FPSWarnColor
}
fpsMeter := status.Meter{
Label: fmt.Sprintf("Framerate: %3.0f FPS", g.fpsValue),
Base: -1,
Level: 0,
Color: fpsColor,
}
meters = append(meters, fpsMeter)
}
g.hud.Draw(screen, meters)
if g.showSaveNotification {
g.drawSaveNotification(screen)
}
}
func (g *GameplayScreen) drawSaveNotification(screen *ebiten.Image) {
centerX := float32(g.bounds.Width / 2)
centerY := float32(30)
boxWidth := float32(140)
boxHeight := float32(40)
cfg := config.Default()
vector.FillRect(screen,
centerX-boxWidth/2,
centerY-boxHeight/2,
boxWidth,
boxHeight,
color.NRGBA{R: 0, G: 0, B: 0, A: 180},
false)
vector.StrokeRect(screen,
centerX-boxWidth/2,
centerY-boxHeight/2,
boxWidth,
boxHeight,
2,
cfg.Visual.SaveNotificationColor,
false)
msg := "Game Saved!"
textX := centerX - 45
textY := centerY - 5
ebitenutil.DebugPrintAt(screen, msg, int(textX), int(textY))
}
func (g *GameplayScreen) ShowSaveNotification() {
g.showSaveNotification = true
g.saveNotificationTimer = config.SaveNotificationDuration
}
// State management
func (g *GameplayScreen) Reset() {
cfg := config.Default()
screenWidth := int(g.bounds.Width)
screenHeight := int(g.bounds.Height)
map1, map2 := maps.CreateDefaultMaps(float64(screenWidth), float64(screenHeight))
g.allMaps = make(map[string]*maps.Map)
g.allMaps["map1"] = map1
g.allMaps["map2"] = map2
g.currentMap = map1
g.hero = hero.New(hero.Config{
StartX: cfg.Hero.StartX,
StartY: cfg.Hero.StartY,
Radius: cfg.Hero.Radius,
Speed: cfg.Hero.Speed,
Color: cfg.Hero.Color,
MaxStamina: cfg.Hero.MaxStamina,
StaminaDrain: cfg.Hero.StaminaDrain,
StaminaRegen: cfg.Hero.StaminaRegen,
})
g.world = map1.World
g.projectiles = projectile.NewManager()
g.portals = portal.NewManager()
for _, p := range map1.Portals {
g.portals.AddPortal(p)
}
g.portals.OnTransition = g.handlePortalTransition
g.updatePortalVisibility()
g.bounds = hero.Bounds{
Width: float64(screenWidth),
Height: float64(screenHeight),
Ground: float64(screenHeight) - config.GroundHeight,
}
g.lastTick = time.Now()
g.gameStartTime = time.Now()
g.totalPlayTime = 0
g.fpsFrames = 0
g.fpsAccumulator = 0
g.fpsValue = 0
}
func (g *GameplayScreen) SaveState() *save.GameState {
return &save.GameState{
HeroX: g.hero.X,
HeroY: g.hero.Y,
HeroStamina: g.hero.Stamina,
PlayTimeMS: g.totalPlayTime.Milliseconds(),
}
}
func (g *GameplayScreen) LoadState(state *save.GameState) {
cfg := config.Default()
screenWidth := int(g.bounds.Width)
screenHeight := int(g.bounds.Height)
map1, map2 := maps.CreateDefaultMaps(float64(screenWidth), float64(screenHeight))
g.allMaps = make(map[string]*maps.Map)
g.allMaps["map1"] = map1
g.allMaps["map2"] = map2
g.currentMap = map1
g.hero = hero.New(hero.Config{
StartX: state.HeroX,
StartY: state.HeroY,
Radius: cfg.Hero.Radius,
Speed: cfg.Hero.Speed,
Color: cfg.Hero.Color,
MaxStamina: cfg.Hero.MaxStamina,
StaminaDrain: cfg.Hero.StaminaDrain,
StaminaRegen: cfg.Hero.StaminaRegen,
})
g.hero.Stamina = state.HeroStamina
g.world = map1.World
g.projectiles = projectile.NewManager()
g.portals = portal.NewManager()
for _, p := range map1.Portals {
g.portals.AddPortal(p)
}
g.portals.OnTransition = g.handlePortalTransition
g.updatePortalVisibility()
g.bounds = hero.Bounds{
Width: float64(screenWidth),
Height: float64(screenHeight),
Ground: float64(screenHeight) - config.GroundHeight,
}
g.totalPlayTime = time.Duration(state.PlayTimeMS) * time.Millisecond
g.lastTick = time.Now()
g.gameStartTime = time.Now()
g.fpsFrames = 0
g.fpsAccumulator = 0
g.fpsValue = 0
}