Optimizations and debug options

This commit is contained in:
2025-11-25 01:06:35 -07:00
parent 57d08f2f04
commit c84ba37353
13 changed files with 377 additions and 212 deletions

View File

@@ -54,15 +54,15 @@ type GameplayScreen struct {
func NewGameplayScreen(screenWidth, screenHeight int, fpsEnabled *bool, portalVisibility *bool) *GameplayScreen {
cfg := config.Default()
map1, map2 := maps.CreateDefaultMaps(float64(screenWidth), float64(screenHeight))
plains, desert := maps.CreateDefaultMaps(float64(screenWidth), float64(screenHeight))
allMaps := make(map[string]*maps.Map)
allMaps["map1"] = map1
allMaps["map2"] = map2
allMaps["plains"] = plains
allMaps["desert"] = desert
portalMgr := portal.NewManager()
for _, p := range map1.Portals {
for _, p := range plains.Portals {
portalMgr.AddPortal(p)
}
@@ -87,9 +87,9 @@ func NewGameplayScreen(screenWidth, screenHeight int, fpsEnabled *bool, portalVi
X: cfg.HUD.X,
Y: cfg.HUD.Y,
Color: color.White,
ScreenName: "Map 1",
ScreenName: "Plains",
},
world: map1.World,
world: plains.World,
projectiles: projectile.NewManager(),
portals: portalMgr,
bounds: hero.Bounds{
@@ -101,7 +101,7 @@ func NewGameplayScreen(screenWidth, screenHeight int, fpsEnabled *bool, portalVi
gameStartTime: time.Now(),
fpsEnabled: fpsEnabled,
portalVisibility: portalVisibility,
currentMap: map1,
currentMap: plains,
allMaps: allMaps,
}
@@ -111,6 +111,11 @@ func NewGameplayScreen(screenWidth, screenHeight int, fpsEnabled *bool, portalVi
}
func (g *GameplayScreen) Update(input GameplayInput, delta time.Duration) {
// clamp delta to prevent physics issues from lag spikes
if delta > 100*time.Millisecond {
delta = 100 * time.Millisecond
}
dt := delta.Seconds()
g.hero.Update(hero.Input{
@@ -131,13 +136,14 @@ 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
// check for portal collisions
g.checkPortalCollision()
g.totalPlayTime += delta
g.trackFPS(delta)
// update save notification timer
if g.showSaveNotification {
g.saveNotificationTimer -= delta
if g.saveNotificationTimer <= 0 {
@@ -146,7 +152,7 @@ func (g *GameplayScreen) Update(input GameplayInput, delta time.Duration) {
}
}
// Portal collision detection
// portal collision detection
func (g *GameplayScreen) checkPortalCollision() {
heroRadius := g.hero.Radius
heroX := g.hero.X - heroRadius
@@ -157,19 +163,6 @@ func (g *GameplayScreen) checkPortalCollision() {
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) {
@@ -188,7 +181,13 @@ func (g *GameplayScreen) handlePortalTransition(event portal.TransitionEvent) {
g.portals.AddPortal(p)
}
g.updatePortalVisibility()
// set portal visibility
if g.portalVisibility != nil {
visible := *g.portalVisibility
for _, p := range g.portals.Portals {
p.Visible = visible
}
}
// Find destination portal and position hero
destPortal := destMap.GetPortalByID(event.DestinationPortal)
@@ -242,7 +241,7 @@ func (g *GameplayScreen) Draw(screen *ebiten.Image) {
g.hero.Draw(screen)
if g.currentMap != nil {
g.hud.ScreenName = fmt.Sprintf("Map %d", g.currentMap.Number)
g.hud.ScreenName = g.currentMap.DisplayName
}
staminaColor := cfg.Visual.StaminaNormalColor
@@ -327,11 +326,11 @@ func (g *GameplayScreen) Reset() {
screenWidth := int(g.bounds.Width)
screenHeight := int(g.bounds.Height)
map1, map2 := maps.CreateDefaultMaps(float64(screenWidth), float64(screenHeight))
plains, desert := 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.allMaps["plains"] = plains
g.allMaps["desert"] = desert
g.currentMap = plains
g.hero = hero.New(hero.Config{
StartX: cfg.Hero.StartX,
@@ -343,14 +342,22 @@ func (g *GameplayScreen) Reset() {
StaminaDrain: cfg.Hero.StaminaDrain,
StaminaRegen: cfg.Hero.StaminaRegen,
})
g.world = map1.World
g.world = plains.World
g.projectiles = projectile.NewManager()
g.portals = portal.NewManager()
for _, p := range map1.Portals {
for _, p := range plains.Portals {
g.portals.AddPortal(p)
}
g.portals.OnTransition = g.handlePortalTransition
g.updatePortalVisibility()
// set portal visibility
if g.portalVisibility != nil {
visible := *g.portalVisibility
for _, p := range g.portals.Portals {
p.Visible = visible
}
}
g.bounds = hero.Bounds{
Width: float64(screenWidth),
Height: float64(screenHeight),
@@ -365,7 +372,12 @@ func (g *GameplayScreen) Reset() {
}
func (g *GameplayScreen) SaveState() *save.GameState {
currentMapID := "map1"
if g.currentMap != nil {
currentMapID = g.currentMap.ID
}
return &save.GameState{
CurrentMap: currentMapID,
HeroX: g.hero.X,
HeroY: g.hero.Y,
HeroStamina: g.hero.Stamina,
@@ -378,11 +390,17 @@ func (g *GameplayScreen) LoadState(state *save.GameState) {
screenWidth := int(g.bounds.Width)
screenHeight := int(g.bounds.Height)
map1, map2 := maps.CreateDefaultMaps(float64(screenWidth), float64(screenHeight))
plains, desert := 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.allMaps["plains"] = plains
g.allMaps["desert"] = desert
// load the saved map or default to plains
savedMap := g.allMaps[state.CurrentMap]
if savedMap == nil {
savedMap = plains
}
g.currentMap = savedMap
g.hero = hero.New(hero.Config{
StartX: state.HeroX,
@@ -395,14 +413,22 @@ func (g *GameplayScreen) LoadState(state *save.GameState) {
StaminaRegen: cfg.Hero.StaminaRegen,
})
g.hero.Stamina = state.HeroStamina
g.world = map1.World
g.world = g.currentMap.World
g.projectiles = projectile.NewManager()
g.portals = portal.NewManager()
for _, p := range map1.Portals {
for _, p := range g.currentMap.Portals {
g.portals.AddPortal(p)
}
g.portals.OnTransition = g.handlePortalTransition
g.updatePortalVisibility()
// set portal visibility
if g.portalVisibility != nil {
visible := *g.portalVisibility
for _, p := range g.portals.Portals {
p.Visible = visible
}
}
g.bounds = hero.Bounds{
Width: float64(screenWidth),
Height: float64(screenHeight),

View File

@@ -14,8 +14,16 @@ type FPSCapSetting interface {
Cycle()
}
type settingsScreen int
const (
settingsMain settingsScreen = iota
settingsDebugOptions
)
type SettingsScreen struct {
selectedIndex int
currentScreen settingsScreen
fpsMonitorValue *bool
fpsCapValue FPSCapSetting
portalVisibilityValue *bool
@@ -24,6 +32,7 @@ type SettingsScreen struct {
func NewSettingsScreen() *SettingsScreen {
return &SettingsScreen{
selectedIndex: 0,
currentScreen: settingsMain,
}
}
@@ -41,10 +50,22 @@ func (s *SettingsScreen) SetPortalVisibility(enabled *bool) {
func (s *SettingsScreen) Update() bool {
if inpututil.IsKeyJustPressed(ebiten.KeyEscape) {
if s.currentScreen == settingsDebugOptions {
s.currentScreen = settingsMain
s.selectedIndex = 0
return false
}
return true
}
settingsCount := 3
if s.currentScreen == settingsDebugOptions {
return s.updateDebugOptions()
}
return s.updateMain()
}
func (s *SettingsScreen) updateMain() bool {
settingsCount := 2
if inpututil.IsKeyJustPressed(ebiten.KeyArrowUp) || inpututil.IsKeyJustPressed(ebiten.KeyW) {
s.selectedIndex--
if s.selectedIndex < 0 {
@@ -58,13 +79,41 @@ func (s *SettingsScreen) Update() bool {
}
}
if inpututil.IsKeyJustPressed(ebiten.KeyEnter) || inpututil.IsKeyJustPressed(ebiten.KeySpace) {
if s.selectedIndex == 0 && s.fpsCapValue != nil {
s.fpsCapValue.Cycle()
} else if s.selectedIndex == 1 {
s.currentScreen = settingsDebugOptions
s.selectedIndex = 0
}
}
return false
}
func (s *SettingsScreen) updateDebugOptions() bool {
debugOptionsCount := 3
if inpututil.IsKeyJustPressed(ebiten.KeyArrowUp) || inpututil.IsKeyJustPressed(ebiten.KeyW) {
s.selectedIndex--
if s.selectedIndex < 0 {
s.selectedIndex = 0
}
}
if inpututil.IsKeyJustPressed(ebiten.KeyArrowDown) || inpututil.IsKeyJustPressed(ebiten.KeyS) {
s.selectedIndex++
if s.selectedIndex >= debugOptionsCount {
s.selectedIndex = debugOptionsCount - 1
}
}
if inpututil.IsKeyJustPressed(ebiten.KeyEnter) || inpututil.IsKeyJustPressed(ebiten.KeySpace) {
if s.selectedIndex == 0 && s.fpsMonitorValue != nil {
*s.fpsMonitorValue = !*s.fpsMonitorValue
} else if s.selectedIndex == 1 && s.fpsCapValue != nil {
s.fpsCapValue.Cycle()
} else if s.selectedIndex == 2 && s.portalVisibilityValue != nil {
} else if s.selectedIndex == 1 && s.portalVisibilityValue != nil {
*s.portalVisibilityValue = !*s.portalVisibilityValue
} else if s.selectedIndex == 2 {
s.currentScreen = settingsMain
s.selectedIndex = 0
}
}
@@ -80,6 +129,52 @@ func (s *SettingsScreen) Draw(screen *ebiten.Image, screenWidth, screenHeight in
titleY := screenHeight/3 - 50
s.drawText(screen, title, color.White, titleX, titleY)
if s.currentScreen == settingsDebugOptions {
s.drawDebugOptions(screen, screenWidth, screenHeight)
} else {
s.drawMain(screen, screenWidth, screenHeight)
}
}
func (s *SettingsScreen) drawMain(screen *ebiten.Image, screenWidth, screenHeight int) {
startY := screenHeight/2 - 20
leftMargin := screenWidth/2 - 120
// FPS cap setting
fpsCapText := "FPS Cap: "
if s.fpsCapValue != nil {
fpsCapText += s.fpsCapValue.String()
} else {
fpsCapText += "60 FPS"
}
if s.selectedIndex == 0 {
indicatorX := leftMargin - 20
s.drawText(screen, ">", color.RGBA{R: 255, G: 200, B: 0, A: 255}, indicatorX, startY)
s.drawText(screen, fpsCapText, color.RGBA{R: 255, G: 255, B: 100, A: 255}, leftMargin, startY)
} else {
s.drawText(screen, fpsCapText, color.RGBA{R: 180, G: 180, B: 200, A: 255}, leftMargin, startY)
}
// debug options submenu
debugOptionsText := "Debug Options >"
debugY := startY + 40
if s.selectedIndex == 1 {
indicatorX := leftMargin - 20
s.drawText(screen, ">", color.RGBA{R: 255, G: 200, B: 0, A: 255}, indicatorX, debugY)
s.drawText(screen, debugOptionsText, color.RGBA{R: 255, G: 255, B: 100, A: 255}, leftMargin, debugY)
} else {
s.drawText(screen, debugOptionsText, color.RGBA{R: 180, G: 180, B: 200, A: 255}, leftMargin, debugY)
}
// Instructions
hintText := "Enter/Space to select, ESC to go back"
hintX := (screenWidth / 2) - (len(hintText) * 7 / 2)
hintY := screenHeight - 50
s.drawText(screen, hintText, color.RGBA{R: 120, G: 120, B: 150, A: 255}, hintX, hintY)
}
func (s *SettingsScreen) drawDebugOptions(screen *ebiten.Image, screenWidth, screenHeight int) {
startY := screenHeight/2 - 20
leftMargin := screenWidth/2 - 120
@@ -99,23 +194,6 @@ func (s *SettingsScreen) Draw(screen *ebiten.Image, screenWidth, screenHeight in
s.drawText(screen, fpsMonitorText, color.RGBA{R: 180, G: 180, B: 200, A: 255}, leftMargin, startY)
}
// FPS cap setting
fpsCapText := "FPS Cap: "
if s.fpsCapValue != nil {
fpsCapText += s.fpsCapValue.String()
} else {
fpsCapText += "60 FPS"
}
capY := startY + 40
if s.selectedIndex == 1 {
indicatorX := leftMargin - 20
s.drawText(screen, ">", color.RGBA{R: 255, G: 200, B: 0, A: 255}, indicatorX, capY)
s.drawText(screen, fpsCapText, color.RGBA{R: 255, G: 255, B: 100, A: 255}, leftMargin, capY)
} else {
s.drawText(screen, fpsCapText, color.RGBA{R: 180, G: 180, B: 200, A: 255}, leftMargin, capY)
}
// Portal visibility toggle
portalVisText := "Portal Visibility: "
if s.portalVisibilityValue != nil && *s.portalVisibilityValue {
@@ -124,8 +202,8 @@ func (s *SettingsScreen) Draw(screen *ebiten.Image, screenWidth, screenHeight in
portalVisText += "OFF"
}
portalY := startY + 80
if s.selectedIndex == 2 {
portalY := startY + 40
if s.selectedIndex == 1 {
indicatorX := leftMargin - 20
s.drawText(screen, ">", color.RGBA{R: 255, G: 200, B: 0, A: 255}, indicatorX, portalY)
s.drawText(screen, portalVisText, color.RGBA{R: 255, G: 255, B: 100, A: 255}, leftMargin, portalY)
@@ -133,8 +211,19 @@ func (s *SettingsScreen) Draw(screen *ebiten.Image, screenWidth, screenHeight in
s.drawText(screen, portalVisText, color.RGBA{R: 180, G: 180, B: 200, A: 255}, leftMargin, portalY)
}
// back option
backText := "< Back"
backY := startY + 80
if s.selectedIndex == 2 {
indicatorX := leftMargin - 20
s.drawText(screen, ">", color.RGBA{R: 255, G: 200, B: 0, A: 255}, indicatorX, backY)
s.drawText(screen, backText, color.RGBA{R: 255, G: 255, B: 100, A: 255}, leftMargin, backY)
} else {
s.drawText(screen, backText, color.RGBA{R: 180, G: 180, B: 200, A: 255}, leftMargin, backY)
}
// Instructions
hintText := "Enter/Space to toggle, ESC to go back"
hintText := "Enter/Space to select, ESC to go back"
hintX := (screenWidth / 2) - (len(hintText) * 7 / 2)
hintY := screenHeight - 50
s.drawText(screen, hintText, color.RGBA{R: 120, G: 120, B: 150, A: 255}, hintX, hintY)
@@ -149,4 +238,5 @@ func (s *SettingsScreen) drawText(screen *ebiten.Image, txt string, clr color.Co
func (s *SettingsScreen) Reset() {
s.selectedIndex = 0
s.currentScreen = settingsMain
}