implement day-night cycle with dynamic lighting adjustments
This commit is contained in:
@@ -9,6 +9,9 @@ public class Lighting {
|
|||||||
|
|
||||||
GamePanel panel;
|
GamePanel panel;
|
||||||
BufferedImage darknessFilter;
|
BufferedImage darknessFilter;
|
||||||
|
int dayCount;
|
||||||
|
float filterAlpha;
|
||||||
|
DayState dayState = DayState.DAY;
|
||||||
|
|
||||||
public Lighting(GamePanel panel) {
|
public Lighting(GamePanel panel) {
|
||||||
this.panel = panel;
|
this.panel = panel;
|
||||||
@@ -16,14 +19,61 @@ public class Lighting {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void draw(Graphics2D g2) {
|
public void draw(Graphics2D g2) {
|
||||||
|
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, filterAlpha));
|
||||||
g2.drawImage(darknessFilter, 0, 0, null);
|
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() {
|
public void update() {
|
||||||
if(!panel.player.lightUpdated) return;
|
if(panel.player.lightUpdated) {
|
||||||
setLightSource();
|
setLightSource();
|
||||||
panel.player.lightUpdated = false;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ...
|
// ...
|
||||||
public void setLightSource() {
|
public void setLightSource() {
|
||||||
darknessFilter = new BufferedImage(panel.screenWidth, panel.screenHeight, BufferedImage.TYPE_INT_ARGB);
|
darknessFilter = new BufferedImage(panel.screenWidth, panel.screenHeight, BufferedImage.TYPE_INT_ARGB);
|
||||||
@@ -69,4 +119,11 @@ public class Lighting {
|
|||||||
return new RadialGradientPaint(centerX, centerY, (panel.player.currentLight == null ? 75 : panel.player.currentLight.lightRadius), fraction, color);
|
return new RadialGradientPaint(centerX, centerY, (panel.player.currentLight == null ? 75 : panel.player.currentLight.lightRadius), fraction, color);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public enum DayState {
|
||||||
|
DAY,
|
||||||
|
DUSK,
|
||||||
|
NIGHT,
|
||||||
|
DAWN,
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user