Fixed lighting

This commit is contained in:
2025-12-16 09:13:22 -07:00
parent de5f47f47b
commit 32df388c77
6 changed files with 85 additions and 66 deletions

View File

@@ -91,9 +91,7 @@ type Hero struct {
ProjectileConfig projectile.ProjectileConfig ProjectileConfig projectile.ProjectileConfig
// Lighting state Brightness float64
InShadow bool
ShadowIntensity float64
} }
type Config struct { type Config struct {
@@ -149,6 +147,7 @@ func New(cfg Config) *Hero {
Radius: 4.0, Radius: 4.0,
Color: color.NRGBA{R: 255, G: 0, B: 0, A: 255}, Color: color.NRGBA{R: 255, G: 0, B: 0, A: 255},
}, },
Brightness: 1.0,
} }
} }
@@ -362,9 +361,8 @@ func (h *Hero) Draw(screen *ebiten.Image) {
// no color modification for idle // no color modification for idle
} }
if h.InShadow { if h.Brightness < 1.0 {
darkness := 1.0 - h.ShadowIntensity op.ColorScale.Scale(float32(h.Brightness), float32(h.Brightness), float32(h.Brightness), 1.0)
op.ColorScale.Scale(float32(darkness), float32(darkness), float32(darkness), 1.0)
} }
op.Filter = ebiten.FilterNearest op.Filter = ebiten.FilterNearest

View File

@@ -10,8 +10,20 @@ import (
func CreateDesert(screenWidth, screenHeight float64) *Map { func CreateDesert(screenWidth, screenHeight float64) *Map {
m := NewMap("desert", 2, "Desert", screenWidth, screenHeight) m := NewMap("desert", 2, "Desert", screenWidth, screenHeight)
m.BackgroundColor = color.NRGBA{R: 15, G: 20, B: 40, A: 255} // Dark blue night sky m.BackgroundColor = color.NRGBA{R: 15, G: 20, B: 40, A: 255}
m.TimeOfDay = Nighttime m.Lighting = LightConfig{
AmbientBrightness: 0.1,
Sources: []LightSource{
{
X: screenWidth - 80,
Y: 80,
Radius: 450,
Color: color.RGBA{R: 200, G: 220, B: 255, A: 255},
Intensity: 0.5,
ShadowDarkness: 0.7,
},
},
}
// ground surface // ground surface
m.World.AddSurface(&world.Surface{ m.World.AddSurface(&world.Surface{

View File

@@ -8,12 +8,18 @@ import (
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
) )
type TimeOfDay string type LightSource struct {
X, Y float64
Radius float64
Color color.RGBA
Intensity float64
ShadowDarkness float64
}
const ( type LightConfig struct {
Daytime TimeOfDay = "day" AmbientBrightness float64
Nighttime TimeOfDay = "night" Sources []LightSource
) }
type Map struct { type Map struct {
ID string ID string
@@ -24,7 +30,7 @@ type Map struct {
World *world.World World *world.World
Portals []*portal.Portal Portals []*portal.Portal
BackgroundColor color.NRGBA BackgroundColor color.NRGBA
TimeOfDay TimeOfDay // Day or Night tag Lighting LightConfig
bakedImage *ebiten.Image bakedImage *ebiten.Image
} }
@@ -39,7 +45,10 @@ func NewMap(id string, number int, displayName string, width, height float64) *M
World: world.NewWorld(), World: world.NewWorld(),
Portals: make([]*portal.Portal, 0), Portals: make([]*portal.Portal, 0),
BackgroundColor: color.NRGBA{R: 135, G: 206, B: 235, A: 255}, BackgroundColor: color.NRGBA{R: 135, G: 206, B: 235, A: 255},
TimeOfDay: Daytime, // Default to daytime Lighting: LightConfig{
AmbientBrightness: 1.0,
Sources: []LightSource{},
},
} }
} }

View File

@@ -11,7 +11,19 @@ import (
func CreatePlains(screenWidth, screenHeight float64) *Map { func CreatePlains(screenWidth, screenHeight float64) *Map {
m := NewMap("plains", 1, "Plains", screenWidth, screenHeight) m := NewMap("plains", 1, "Plains", screenWidth, screenHeight)
m.BackgroundColor = color.NRGBA{R: 135, G: 206, B: 235, A: 255} m.BackgroundColor = color.NRGBA{R: 135, G: 206, B: 235, A: 255}
m.TimeOfDay = Daytime m.Lighting = LightConfig{
AmbientBrightness: 0.3,
Sources: []LightSource{
{
X: screenWidth - 80,
Y: 80,
Radius: 600,
Color: color.RGBA{R: 255, G: 250, B: 220, A: 255},
Intensity: 0.7,
ShadowDarkness: 0.5,
},
},
}
// ground surface // ground surface
groundColor := color.NRGBA{R: 34, G: 139, B: 34, A: 255} groundColor := color.NRGBA{R: 34, G: 139, B: 34, A: 255}

View File

@@ -337,7 +337,7 @@ func (s *System) IsPointInShadow(x, y float64, w *world.World) bool {
if ix, iy, ok := intersection(rayToPoint, wall); ok { if ix, iy, ok := intersection(rayToPoint, wall); ok {
distanceToIntersection := math.Sqrt((ix-light.X)*(ix-light.X) + (iy-light.Y)*(iy-light.Y)) distanceToIntersection := math.Sqrt((ix-light.X)*(ix-light.X) + (iy-light.Y)*(iy-light.Y))
if distanceToIntersection < distanceToPoint-5.0 { if distanceToIntersection < distanceToPoint-10.0 {
blocked = true blocked = true
break break
} }

View File

@@ -3,6 +3,7 @@ package screens
import ( import (
"fmt" "fmt"
"image/color" "image/color"
"math"
"time" "time"
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
@@ -168,28 +169,14 @@ func (g *GameplayScreen) updateRaycastLights() {
return return
} }
screenWidth := g.bounds.Width for _, lightSrc := range currentMap.Lighting.Sources {
g.raycastSystem.SetShadowIntensity(lightSrc.ShadowDarkness)
lightX := float64(screenWidth - 80)
lightY := 80.0
if currentMap.TimeOfDay == maps.Daytime {
g.raycastSystem.SetShadowIntensity(0.25)
g.raycastSystem.AddLight(&raycast.LightSource{ g.raycastSystem.AddLight(&raycast.LightSource{
X: lightX, X: lightSrc.X,
Y: lightY, Y: lightSrc.Y,
Radius: 800.0, Radius: lightSrc.Radius,
Color: color.RGBA{R: 255, G: 250, B: 220, A: 255}, Color: lightSrc.Color,
Intensity: 1.0, Intensity: lightSrc.Intensity,
})
} 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,
}) })
} }
} }
@@ -276,10 +263,7 @@ func (g *GameplayScreen) Draw(screen *ebiten.Image) {
currentMap.Draw(screen) currentMap.Draw(screen)
g.hud.ScreenName = currentMap.DisplayName g.hud.ScreenName = currentMap.DisplayName
// Apply nighttime darkening overlay BEFORE raycasting g.drawAmbientOverlay(screen, currentMap)
if currentMap.TimeOfDay == maps.Nighttime {
g.drawNightOverlay(screen)
}
// Draw raycasting shadows/lighting // Draw raycasting shadows/lighting
if g.raycastEnabled != nil && *g.raycastEnabled { if g.raycastEnabled != nil && *g.raycastEnabled {
@@ -375,32 +359,37 @@ func (g *GameplayScreen) drawSaveNotification(screen *ebiten.Image) {
func (g *GameplayScreen) updateHeroLighting(currentMap *maps.Map) { func (g *GameplayScreen) updateHeroLighting(currentMap *maps.Map) {
if g.raycastEnabled == nil || !*g.raycastEnabled { if g.raycastEnabled == nil || !*g.raycastEnabled {
g.hero.InShadow = false g.hero.Brightness = 1.0
g.hero.ShadowIntensity = 0
return return
} }
// Check if hero is in shadow ambient := currentMap.Lighting.AmbientBrightness
inShadow := g.raycastSystem.IsPointInShadow(g.hero.X, g.hero.Y, g.world) maxDirectLight := 0.0
g.hero.InShadow = inShadow
if inShadow { for _, light := range currentMap.Lighting.Sources {
if currentMap.TimeOfDay == maps.Daytime { inShadow := g.raycastSystem.IsPointInShadow(g.hero.X, g.hero.Y, g.world)
g.hero.ShadowIntensity = 0.3
if !inShadow {
maxDirectLight = math.Max(maxDirectLight, light.Intensity)
} else { } else {
g.hero.ShadowIntensity = 0.6 indirect := light.Intensity * (1.0 - light.ShadowDarkness)
maxDirectLight = math.Max(maxDirectLight, indirect)
} }
} else {
g.hero.ShadowIntensity = 0
} }
g.hero.Brightness = math.Min(1.0, ambient+maxDirectLight)
} }
func (g *GameplayScreen) drawNightOverlay(screen *ebiten.Image) { func (g *GameplayScreen) drawAmbientOverlay(screen *ebiten.Image, currentMap *maps.Map) {
opts := &ebiten.DrawImageOptions{} if currentMap.Lighting.AmbientBrightness >= 1.0 {
opts.ColorScale.Scale(0.4, 0.4, 0.6, 1.0) return
}
darkness := 1.0 - currentMap.Lighting.AmbientBrightness
alpha := uint8(darkness * 200)
overlay := ebiten.NewImage(int(g.bounds.Width), int(g.bounds.Height)) overlay := ebiten.NewImage(int(g.bounds.Width), int(g.bounds.Height))
overlay.Fill(color.RGBA{R: 0, G: 0, B: 30, A: 120}) overlay.Fill(color.RGBA{R: 0, G: 0, B: 0, A: alpha})
screen.DrawImage(overlay, &ebiten.DrawImageOptions{}) screen.DrawImage(overlay, &ebiten.DrawImageOptions{})
} }
@@ -409,16 +398,15 @@ func (g *GameplayScreen) drawCelestialBody(screen *ebiten.Image, currentMap *map
return return
} }
cx := float32(g.bounds.Width - 80) for _, lightSrc := range currentMap.Lighting.Sources {
cy := float32(80) cx := float32(lightSrc.X)
radius := float32(25) cy := float32(lightSrc.Y)
radius := float32(25)
if currentMap.TimeOfDay == maps.Daytime { vector.DrawFilledCircle(screen, cx, cy, radius, lightSrc.Color, true)
vector.DrawFilledCircle(screen, cx, cy, radius, color.RGBA{R: 255, G: 230, B: 100, A: 255}, true) glowColor := lightSrc.Color
vector.DrawFilledCircle(screen, cx, cy, radius+3, color.RGBA{R: 255, G: 240, B: 150, A: 100}, true) glowColor.A = 80
} else { vector.DrawFilledCircle(screen, cx, cy, radius+3, glowColor, true)
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)
} }
} }