Lighting experiments

This commit is contained in:
2025-12-16 00:44:52 -07:00
parent c70f85abe5
commit de5f47f47b
12 changed files with 664 additions and 17 deletions

View File

@@ -14,6 +14,7 @@ import (
"github.com/atridad/LilGuy/internal/maps"
"github.com/atridad/LilGuy/internal/portal"
"github.com/atridad/LilGuy/internal/projectile"
"github.com/atridad/LilGuy/internal/raycast"
"github.com/atridad/LilGuy/internal/save"
"github.com/atridad/LilGuy/internal/status"
"github.com/atridad/LilGuy/internal/ui/hud"
@@ -48,9 +49,14 @@ type GameplayScreen struct {
showSaveNotification bool
portalVisibility *bool
// Raycasting system
raycastSystem *raycast.System
raycastEnabled *bool
raycastDebugMode *bool
}
func NewGameplayScreen(screenWidth, screenHeight int, mapManager *maps.Manager, fpsEnabled *bool, portalVisibility *bool) *GameplayScreen {
func NewGameplayScreen(screenWidth, screenHeight int, mapManager *maps.Manager, fpsEnabled *bool, portalVisibility *bool, raycastEnabled *bool, raycastDebugMode *bool) *GameplayScreen {
cfg := config.Default()
// Ensure we have a current map
@@ -100,6 +106,9 @@ func NewGameplayScreen(screenWidth, screenHeight int, mapManager *maps.Manager,
gameStartTime: time.Now(),
fpsEnabled: fpsEnabled,
portalVisibility: portalVisibility,
raycastSystem: raycast.NewSystem(screenWidth, screenHeight),
raycastEnabled: raycastEnabled,
raycastDebugMode: raycastDebugMode,
}
gs.portals.OnTransition = gs.handlePortalTransition
@@ -132,6 +141,9 @@ func (g *GameplayScreen) Update(input GameplayInput, delta time.Duration) {
g.projectiles.Update(dt, g.bounds.Width, g.bounds.Height)
g.portals.Update(dt)
// Update raycasting lights based on current map
g.updateRaycastLights()
// check for portal collisions
g.checkPortalCollision()
@@ -148,6 +160,40 @@ func (g *GameplayScreen) Update(input GameplayInput, delta time.Duration) {
}
}
func (g *GameplayScreen) updateRaycastLights() {
g.raycastSystem.ClearLights()
currentMap := g.mapManager.CurrentMap()
if currentMap == nil {
return
}
screenWidth := g.bounds.Width
lightX := float64(screenWidth - 80)
lightY := 80.0
if currentMap.TimeOfDay == maps.Daytime {
g.raycastSystem.SetShadowIntensity(0.25)
g.raycastSystem.AddLight(&raycast.LightSource{
X: lightX,
Y: lightY,
Radius: 800.0,
Color: color.RGBA{R: 255, G: 250, B: 220, A: 255},
Intensity: 1.0,
})
} else {
g.raycastSystem.SetShadowIntensity(0.7)
g.raycastSystem.AddLight(&raycast.LightSource{
X: lightX,
Y: lightY,
Radius: 500.0,
Color: color.RGBA{R: 200, G: 220, B: 255, A: 255},
Intensity: 0.7,
})
}
}
// portal collision detection
func (g *GameplayScreen) checkPortalCollision() {
heroRadius := g.hero.Radius
@@ -229,6 +275,26 @@ func (g *GameplayScreen) Draw(screen *ebiten.Image) {
if currentMap != nil {
currentMap.Draw(screen)
g.hud.ScreenName = currentMap.DisplayName
// Apply nighttime darkening overlay BEFORE raycasting
if currentMap.TimeOfDay == maps.Nighttime {
g.drawNightOverlay(screen)
}
// Draw raycasting shadows/lighting
if g.raycastEnabled != nil && *g.raycastEnabled {
g.raycastSystem.Draw(screen, g.world)
if g.raycastDebugMode != nil && *g.raycastDebugMode {
g.raycastSystem.DrawDebug(screen, g.world)
}
}
// Draw sun or moon circle in top-right
g.drawCelestialBody(screen, currentMap)
// Update hero lighting state
g.updateHeroLighting(currentMap)
} else {
screen.Fill(cfg.Visual.BackgroundColor)
}
@@ -307,6 +373,55 @@ func (g *GameplayScreen) drawSaveNotification(screen *ebiten.Image) {
ebitenutil.DebugPrintAt(screen, msg, int(textX), int(textY))
}
func (g *GameplayScreen) updateHeroLighting(currentMap *maps.Map) {
if g.raycastEnabled == nil || !*g.raycastEnabled {
g.hero.InShadow = false
g.hero.ShadowIntensity = 0
return
}
// Check if hero is in shadow
inShadow := g.raycastSystem.IsPointInShadow(g.hero.X, g.hero.Y, g.world)
g.hero.InShadow = inShadow
if inShadow {
if currentMap.TimeOfDay == maps.Daytime {
g.hero.ShadowIntensity = 0.3
} else {
g.hero.ShadowIntensity = 0.6
}
} else {
g.hero.ShadowIntensity = 0
}
}
func (g *GameplayScreen) drawNightOverlay(screen *ebiten.Image) {
opts := &ebiten.DrawImageOptions{}
opts.ColorScale.Scale(0.4, 0.4, 0.6, 1.0)
overlay := ebiten.NewImage(int(g.bounds.Width), int(g.bounds.Height))
overlay.Fill(color.RGBA{R: 0, G: 0, B: 30, A: 120})
screen.DrawImage(overlay, &ebiten.DrawImageOptions{})
}
func (g *GameplayScreen) drawCelestialBody(screen *ebiten.Image, currentMap *maps.Map) {
if currentMap == nil {
return
}
cx := float32(g.bounds.Width - 80)
cy := float32(80)
radius := float32(25)
if currentMap.TimeOfDay == maps.Daytime {
vector.DrawFilledCircle(screen, cx, cy, radius, color.RGBA{R: 255, G: 230, B: 100, A: 255}, true)
vector.DrawFilledCircle(screen, cx, cy, radius+3, color.RGBA{R: 255, G: 240, B: 150, A: 100}, true)
} else {
vector.DrawFilledCircle(screen, cx, cy, radius, color.RGBA{R: 240, G: 240, B: 255, A: 255}, true)
vector.DrawFilledCircle(screen, cx, cy, radius+3, color.RGBA{R: 200, G: 200, B: 255, A: 80}, true)
}
}
func (g *GameplayScreen) ShowSaveNotification() {
g.showSaveNotification = true
g.saveNotificationTimer = config.SaveNotificationDuration
@@ -431,3 +546,33 @@ func (g *GameplayScreen) LoadState(state *save.GameState) {
g.fpsAccumulator = 0
g.fpsValue = 0
}
// ToggleRaycast toggles the raycasting lighting system on/off
func (g *GameplayScreen) ToggleRaycast() {
if g.raycastEnabled != nil {
*g.raycastEnabled = !*g.raycastEnabled
}
}
// ToggleRaycastDebug toggles the raycasting debug visualization
func (g *GameplayScreen) ToggleRaycastDebug() {
if g.raycastDebugMode != nil {
*g.raycastDebugMode = !*g.raycastDebugMode
}
}
// IsRaycastEnabled returns whether raycasting is enabled
func (g *GameplayScreen) IsRaycastEnabled() bool {
if g.raycastEnabled == nil {
return false
}
return *g.raycastEnabled
}
// IsRaycastDebugMode returns whether raycasting debug mode is enabled
func (g *GameplayScreen) IsRaycastDebugMode() bool {
if g.raycastDebugMode == nil {
return false
}
return *g.raycastDebugMode
}

View File

@@ -27,6 +27,8 @@ type SettingsScreen struct {
fpsMonitorValue *bool
fpsCapValue FPSCapSetting
portalVisibilityValue *bool
raycastEnabledValue *bool
raycastDebugValue *bool
}
func NewSettingsScreen() *SettingsScreen {
@@ -48,6 +50,11 @@ func (s *SettingsScreen) SetPortalVisibility(enabled *bool) {
s.portalVisibilityValue = enabled
}
func (s *SettingsScreen) SetRaycastSettings(enabled *bool, debugMode *bool) {
s.raycastEnabledValue = enabled
s.raycastDebugValue = debugMode
}
func (s *SettingsScreen) Update() bool {
if inpututil.IsKeyJustPressed(ebiten.KeyEscape) {
if s.currentScreen == settingsDebugOptions {
@@ -92,7 +99,7 @@ func (s *SettingsScreen) updateMain() bool {
}
func (s *SettingsScreen) updateDebugOptions() bool {
debugOptionsCount := 3
debugOptionsCount := 5 // FPS Monitor, Portal Visibility, Raycast Enabled, Raycast Debug, Back
if inpututil.IsKeyJustPressed(ebiten.KeyArrowUp) || inpututil.IsKeyJustPressed(ebiten.KeyW) {
s.selectedIndex--
if s.selectedIndex < 0 {
@@ -111,7 +118,11 @@ func (s *SettingsScreen) updateDebugOptions() bool {
*s.fpsMonitorValue = !*s.fpsMonitorValue
} else if s.selectedIndex == 1 && s.portalVisibilityValue != nil {
*s.portalVisibilityValue = !*s.portalVisibilityValue
} else if s.selectedIndex == 2 {
} else if s.selectedIndex == 2 && s.raycastEnabledValue != nil {
*s.raycastEnabledValue = !*s.raycastEnabledValue
} else if s.selectedIndex == 3 && s.raycastDebugValue != nil {
*s.raycastDebugValue = !*s.raycastDebugValue
} else if s.selectedIndex == 4 {
s.currentScreen = settingsMain
s.selectedIndex = 0
}
@@ -211,10 +222,44 @@ func (s *SettingsScreen) drawDebugOptions(screen *ebiten.Image, screenWidth, scr
s.drawText(screen, portalVisText, color.RGBA{R: 180, G: 180, B: 200, A: 255}, leftMargin, portalY)
}
// Raycast enabled toggle
raycastText := "Lighting: "
if s.raycastEnabledValue != nil && *s.raycastEnabledValue {
raycastText += "ON"
} else {
raycastText += "OFF"
}
raycastY := startY + 80
if s.selectedIndex == 2 {
indicatorX := leftMargin - 20
s.drawText(screen, ">", color.RGBA{R: 255, G: 200, B: 0, A: 255}, indicatorX, raycastY)
s.drawText(screen, raycastText, color.RGBA{R: 255, G: 255, B: 100, A: 255}, leftMargin, raycastY)
} else {
s.drawText(screen, raycastText, color.RGBA{R: 180, G: 180, B: 200, A: 255}, leftMargin, raycastY)
}
// Raycast debug toggle
raycastDebugText := "Debug Rays: "
if s.raycastDebugValue != nil && *s.raycastDebugValue {
raycastDebugText += "ON"
} else {
raycastDebugText += "OFF"
}
raycastDebugY := startY + 120
if s.selectedIndex == 3 {
indicatorX := leftMargin - 20
s.drawText(screen, ">", color.RGBA{R: 255, G: 200, B: 0, A: 255}, indicatorX, raycastDebugY)
s.drawText(screen, raycastDebugText, color.RGBA{R: 255, G: 255, B: 100, A: 255}, leftMargin, raycastDebugY)
} else {
s.drawText(screen, raycastDebugText, color.RGBA{R: 180, G: 180, B: 200, A: 255}, leftMargin, raycastDebugY)
}
// back option
backText := "< Back"
backY := startY + 80
if s.selectedIndex == 2 {
backY := startY + 160
if s.selectedIndex == 4 {
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)

View File

@@ -55,6 +55,10 @@ func (t *TitleScreen) SetPortalVisibility(enabled *bool) {
t.settingsScreen.SetPortalVisibility(enabled)
}
func (t *TitleScreen) SetRaycastSettings(enabled *bool, debugMode *bool) {
t.settingsScreen.SetRaycastSettings(enabled, debugMode)
}
func (t *TitleScreen) SetHasSaveGame(hasSave bool) {
t.hasSaveGame = hasSave
if !hasSave && t.selectedIndex == 0 {