Optimizations and debug options
This commit is contained in:
@@ -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),
|
||||
|
||||
Reference in New Issue
Block a user