Consolodated config, added portals, etc.

This commit is contained in:
2025-11-24 15:03:56 -07:00
parent f39684873b
commit 57d08f2f04
15 changed files with 895 additions and 212 deletions

View File

@@ -9,7 +9,10 @@ import (
"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"
@@ -17,51 +20,6 @@ import (
"github.com/atridad/LilGuy/internal/world"
)
// Screen and hero defaults
var (
backgroundColor = color.NRGBA{R: 135, G: 206, B: 235, A: 255}
saveNotificationColor = color.NRGBA{R: 50, G: 200, B: 50, A: 255}
)
const (
heroStartX = 960 / 2
heroStartY = 540 / 2
heroRadius = 28.0
heroSpeed = 180.0
heroMaxStamina = 100.0
heroStaminaDrain = 50.0
heroStaminaRegen = 30.0
saveNotificationDuration = 2 * time.Second
)
var (
heroColor = color.NRGBA{R: 210, G: 220, B: 255, A: 255}
)
// HUD positioning and colors
const (
hudX = 960 - 220
hudY = 20
)
var (
staminaNormalColor = color.NRGBA{R: 0, G: 255, B: 180, A: 255}
staminaLowColor = color.NRGBA{R: 255, G: 60, B: 60, A: 255}
fpsGoodColor = color.NRGBA{R: 120, G: 255, B: 120, A: 255}
fpsWarnColor = color.NRGBA{R: 255, G: 210, B: 100, A: 255}
fpsPoorColor = color.NRGBA{R: 255, G: 120, B: 120, A: 255}
)
// FPS monitoring thresholds
const (
staminaLowThreshold = 0.2
fpsWarnThreshold = 0.85
fpsPoorThreshold = 0.6
fpsSampleWindow = time.Second
targetTPS = 60
)
type GameplayInput struct {
Left bool
Right bool
@@ -75,6 +33,7 @@ type GameplayScreen struct {
hud hud.Overlay
world *world.World
projectiles *projectile.Manager
portals *portal.Manager
bounds hero.Bounds
lastTick time.Time
gameStartTime time.Time
@@ -86,48 +45,69 @@ type GameplayScreen struct {
saveNotificationTimer time.Duration
showSaveNotification bool
portalVisibility *bool
currentMap *maps.Map
allMaps map[string]*maps.Map
}
func NewGameplayScreen(screenWidth, screenHeight int, fpsEnabled *bool) *GameplayScreen {
w := world.NewWorld()
func NewGameplayScreen(screenWidth, screenHeight int, fpsEnabled *bool, portalVisibility *bool) *GameplayScreen {
cfg := config.Default()
map1, map2 := maps.CreateDefaultMaps(float64(screenWidth), float64(screenHeight))
groundHeight := 16.0
w.AddSurface(&world.Surface{
X: 0,
Y: float64(screenHeight) - groundHeight,
Width: float64(screenWidth),
Height: groundHeight,
Tag: world.TagGround,
Color: color.NRGBA{R: 34, G: 139, B: 34, A: 255},
})
allMaps := make(map[string]*maps.Map)
allMaps["map1"] = map1
allMaps["map2"] = map2
return &GameplayScreen{
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: heroStartX,
StartY: heroStartY,
Radius: heroRadius,
Speed: heroSpeed,
Color: heroColor,
MaxStamina: heroMaxStamina,
StaminaDrain: heroStaminaDrain,
StaminaRegen: heroStaminaRegen,
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: hudX,
Y: hudY,
Color: color.White,
X: cfg.HUD.X,
Y: cfg.HUD.Y,
Color: color.White,
ScreenName: "Map 1",
},
world: w,
world: map1.World,
projectiles: projectile.NewManager(),
portals: portalMgr,
bounds: hero.Bounds{
Width: float64(screenWidth),
Height: float64(screenHeight),
Ground: float64(screenHeight) - groundHeight,
Ground: float64(screenHeight) - config.GroundHeight,
},
lastTick: time.Now(),
gameStartTime: time.Now(),
fpsEnabled: fpsEnabled,
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) {
@@ -149,6 +129,10 @@ func (g *GameplayScreen) Update(input GameplayInput, delta time.Duration) {
}
g.projectiles.Update(dt, g.bounds.Width, g.bounds.Height)
g.portals.Update(dt)
// Check for portal collisions
g.checkPortalCollision()
g.totalPlayTime += delta
@@ -162,6 +146,71 @@ func (g *GameplayScreen) Update(input GameplayInput, delta time.Duration) {
}
}
// 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 {
@@ -171,7 +220,7 @@ func (g *GameplayScreen) trackFPS(delta time.Duration) {
g.fpsAccumulator += delta
g.fpsFrames++
if g.fpsAccumulator >= fpsSampleWindow {
if g.fpsAccumulator >= config.FPSSampleWindow {
g.fpsValue = float64(g.fpsFrames) / g.fpsAccumulator.Seconds()
g.fpsAccumulator = 0
g.fpsFrames = 0
@@ -180,15 +229,25 @@ func (g *GameplayScreen) trackFPS(delta time.Duration) {
// Rendering
func (g *GameplayScreen) Draw(screen *ebiten.Image) {
screen.Fill(backgroundColor)
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)
staminaColor := staminaNormalColor
if g.hero.Stamina < g.hero.MaxStamina*staminaLowThreshold {
staminaColor = staminaLowColor
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{
@@ -201,13 +260,13 @@ func (g *GameplayScreen) Draw(screen *ebiten.Image) {
meters := []status.Meter{staminaMeter}
if g.fpsEnabled != nil && *g.fpsEnabled {
ratio := g.fpsValue / float64(targetTPS)
fpsColor := fpsGoodColor
ratio := g.fpsValue / float64(config.TargetTPS)
fpsColor := cfg.Visual.FPSGoodColor
switch {
case ratio < fpsPoorThreshold:
fpsColor = fpsPoorColor
case ratio < fpsWarnThreshold:
fpsColor = fpsWarnColor
case ratio < config.FPSPoorThreshold:
fpsColor = cfg.Visual.FPSPoorColor
case ratio < config.FPSWarnThreshold:
fpsColor = cfg.Visual.FPSWarnColor
}
fpsMeter := status.Meter{
@@ -233,7 +292,8 @@ func (g *GameplayScreen) drawSaveNotification(screen *ebiten.Image) {
boxWidth := float32(140)
boxHeight := float32(40)
vector.DrawFilledRect(screen,
cfg := config.Default()
vector.FillRect(screen,
centerX-boxWidth/2,
centerY-boxHeight/2,
boxWidth,
@@ -247,7 +307,7 @@ func (g *GameplayScreen) drawSaveNotification(screen *ebiten.Image) {
boxWidth,
boxHeight,
2,
saveNotificationColor,
cfg.Visual.SaveNotificationColor,
false)
msg := "Game Saved!"
@@ -258,41 +318,43 @@ func (g *GameplayScreen) drawSaveNotification(screen *ebiten.Image) {
func (g *GameplayScreen) ShowSaveNotification() {
g.showSaveNotification = true
g.saveNotificationTimer = saveNotificationDuration
g.saveNotificationTimer = config.SaveNotificationDuration
}
// State management
func (g *GameplayScreen) Reset() {
cfg := config.Default()
screenWidth := int(g.bounds.Width)
screenHeight := int(g.bounds.Height)
groundHeight := 16.0
w := world.NewWorld()
w.AddSurface(&world.Surface{
X: 0,
Y: float64(screenHeight) - groundHeight,
Width: float64(screenWidth),
Height: groundHeight,
Tag: world.TagGround,
Color: color.NRGBA{R: 34, G: 139, B: 34, A: 255},
})
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: heroStartX,
StartY: heroStartY,
Radius: heroRadius,
Speed: heroSpeed,
Color: heroColor,
MaxStamina: heroMaxStamina,
StaminaDrain: heroStaminaDrain,
StaminaRegen: heroStaminaRegen,
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 = w
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) - groundHeight,
Ground: float64(screenHeight) - config.GroundHeight,
}
g.lastTick = time.Now()
g.gameStartTime = time.Now()
@@ -312,37 +374,39 @@ func (g *GameplayScreen) SaveState() *save.GameState {
}
func (g *GameplayScreen) LoadState(state *save.GameState) {
cfg := config.Default()
screenWidth := int(g.bounds.Width)
screenHeight := int(g.bounds.Height)
groundHeight := 16.0
w := world.NewWorld()
w.AddSurface(&world.Surface{
X: 0,
Y: float64(screenHeight) - groundHeight,
Width: float64(screenWidth),
Height: groundHeight,
Tag: world.TagGround,
Color: color.NRGBA{R: 34, G: 139, B: 34, A: 255},
})
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: heroRadius,
Speed: heroSpeed,
Color: heroColor,
MaxStamina: heroMaxStamina,
StaminaDrain: heroStaminaDrain,
StaminaRegen: heroStaminaRegen,
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 = w
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) - groundHeight,
Ground: float64(screenHeight) - config.GroundHeight,
}
g.totalPlayTime = time.Duration(state.PlayTimeMS) * time.Millisecond
g.lastTick = time.Now()