implement day-night cycle with dynamic lighting adjustments

This commit is contained in:
2025-12-13 14:06:01 +01:00
parent 2105a0e8af
commit 7b3a8bca0f

View File

@@ -9,6 +9,9 @@ public class Lighting {
GamePanel panel;
BufferedImage darknessFilter;
int dayCount;
float filterAlpha;
DayState dayState = DayState.DAY;
public Lighting(GamePanel panel) {
this.panel = panel;
@@ -16,12 +19,59 @@ public class Lighting {
}
public void draw(Graphics2D g2) {
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, filterAlpha));
g2.drawImage(darknessFilter, 0, 0, null);
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1F));
// DEBUG
String s;
switch (dayState) {
case DAY -> s = "DAY";
case DUSK -> s = "DUSK";
case NIGHT -> s = "NIGHT";
case DAWN -> s = "DAWN";
default -> s = "UNKNOWN";
}
g2.setColor(Color.white);
g2.setFont(g2.getFont().deriveFont(50f));
g2.drawString(s, 800, 500);
}
public void update() {
if(!panel.player.lightUpdated) return;
setLightSource();
panel.player.lightUpdated = false;
if(panel.player.lightUpdated) {
setLightSource();
panel.player.lightUpdated = false;
}
switch (dayState) {
case DAY -> {
dayCount++;
if (dayCount > 600) { //10 seconds
dayState = DayState.DUSK;
dayCount = 0;
}
}
case DUSK -> {
filterAlpha += 0.001f;
if (filterAlpha > 1f) {
filterAlpha = 1f;
dayState = DayState.NIGHT;
}
}
case NIGHT -> {
dayCount++;
if (dayCount > 600) { //10 seconds
dayState = DayState.DAWN;
dayCount = 0;
}
}
case DAWN -> {
filterAlpha -= 0.001f;
if (filterAlpha < 0f) {
filterAlpha = 0f;
dayState = DayState.DAY;
}
}
}
}
// ...
@@ -69,4 +119,11 @@ public class Lighting {
return new RadialGradientPaint(centerX, centerY, (panel.player.currentLight == null ? 75 : panel.player.currentLight.lightRadius), fraction, color);
}
public enum DayState {
DAY,
DUSK,
NIGHT,
DAWN,
}
}