add lantern object and implement light management in player and environment

This commit is contained in:
2025-12-13 13:44:52 +01:00
parent 7ee7614c6c
commit 2105a0e8af
9 changed files with 86 additions and 27 deletions

View File

@@ -148,6 +148,7 @@ public class GamePanel extends JPanel implements Runnable {
} }
} }
for(Entity entity : iTile[currentMap.getIndex()]) if(entity != null) entity.update(); for(Entity entity : iTile[currentMap.getIndex()]) if(entity != null) entity.update();
eManager.update();
break; break;
case PAUSE: case PAUSE:
break; break;

View File

@@ -194,7 +194,11 @@ public class UI {
for(int i = 0; i < entity.inventory.size(); i++) { for(int i = 0; i < entity.inventory.size(); i++) {
// EQUIP CURSOR // EQUIP CURSOR
if(entity.inventory.get(i) == entity.currentWeapon || entity.inventory.get(i) == entity.currentShield) { if(
entity.inventory.get(i) == entity.currentWeapon ||
entity.inventory.get(i) == entity.currentShield ||
entity.inventory.get(i) == entity.currentLight
) {
graphics2d.setColor(new Color(240, 190,90)); graphics2d.setColor(new Color(240, 190,90));
graphics2d.fillRoundRect(slotX, slotY, panel.tileSize, panel.tileSize, 10, 10); graphics2d.fillRoundRect(slotX, slotY, panel.tileSize, panel.tileSize, 10, 10);
} }

View File

@@ -10,6 +10,7 @@ public enum EntityType {
PICKUP, PICKUP,
PROJECTILE, PROJECTILE,
WEAPON, WEAPON,
LIGHT,
SHIELD; SHIELD;
public enum WeaponType { public enum WeaponType {

View File

@@ -54,6 +54,11 @@ public class AssetSetter {
panel.obj[Map.OVERWORLD.getIndex()][i].worldX = panel.tileSize*17; panel.obj[Map.OVERWORLD.getIndex()][i].worldX = panel.tileSize*17;
panel.obj[Map.OVERWORLD.getIndex()][i].worldY = panel.tileSize*21; panel.obj[Map.OVERWORLD.getIndex()][i].worldY = panel.tileSize*21;
i++; i++;
panel.obj[Map.OVERWORLD.getIndex()][i] = new LanternObj(panel);
panel.obj[Map.OVERWORLD.getIndex()][i].worldX = panel.tileSize*18;
panel.obj[Map.OVERWORLD.getIndex()][i].worldY = panel.tileSize*20;
i++;
} }
public void setNPC() { public void setNPC() {

View File

@@ -70,6 +70,7 @@ public class Entity {
public int mana; public int mana;
public Entity currentWeapon; public Entity currentWeapon;
public Entity currentShield; public Entity currentShield;
public Entity currentLight;
public Projectile projectile; public Projectile projectile;
public ArrayList<Entity> inventory = new ArrayList<>(); public ArrayList<Entity> inventory = new ArrayList<>();
public final int maxInvSize = 20; public final int maxInvSize = 20;
@@ -85,6 +86,7 @@ public class Entity {
public int knockbackVal; public int knockbackVal;
public boolean stackable; public boolean stackable;
public int amt = 1; public int amt = 1;
public float lightRadius;
public Entity(GamePanel panel) { public Entity(GamePanel panel) {
this.panel = panel; this.panel = panel;

View File

@@ -20,6 +20,7 @@ public class Player extends Entity {
// STATE // STATE
public boolean attackCancel; public boolean attackCancel;
public boolean lightUpdated;
public Player(GamePanel panel, KeyHandler keyH) { public Player(GamePanel panel, KeyHandler keyH) {
super(panel); super(panel);
@@ -289,6 +290,10 @@ public class Player extends Entity {
currentShield = selectedItem; currentShield = selectedItem;
getDefense(); getDefense();
} }
if(selectedItem.type == EntityType.LIGHT) {
if(currentLight == selectedItem) currentLight = null; else currentLight = selectedItem;
lightUpdated = true;
}
if(selectedItem.consumable) if(selectedItem.consumable)
if(selectedItem.use(this)) if(selectedItem.use(this))
if(selectedItem.amt > 1) selectedItem.amt--; else inventory.remove(selectedItem); if(selectedItem.amt > 1) selectedItem.amt--; else inventory.remove(selectedItem);

View File

@@ -0,0 +1,19 @@
package de.miaurizius.jgame2d.entity.item;
import de.miaurizius.jgame2d.core.GamePanel;
import de.miaurizius.jgame2d.core.enums.EntityType;
import de.miaurizius.jgame2d.entity.Entity;
public class LanternObj extends Entity {
public LanternObj(GamePanel panel) {
super(panel);
type = EntityType.LIGHT;
name = "Lantern";
down1 = initEntitySprites("objects/lantern");
description = "[" + name + "]\nA lantern that lights up\nthe surrounding area.";
price = 100;
lightRadius = 250;
}
}

View File

@@ -13,12 +13,16 @@ public class EnvironmentManager {
this.panel = panel; this.panel = panel;
} }
public void setup() {
lighting = new Lighting(panel, 300);
}
public void draw(Graphics2D g2) { public void draw(Graphics2D g2) {
lighting.draw(g2); lighting.draw(g2);
} }
// SETTING THINGS UP
public void setup() {
lighting = new Lighting(panel);
}
public void update() {
lighting.update();
}
} }

View File

@@ -10,45 +10,63 @@ public class Lighting {
GamePanel panel; GamePanel panel;
BufferedImage darknessFilter; BufferedImage darknessFilter;
public Lighting(GamePanel panel, int circleSize) { public Lighting(GamePanel panel) {
this.panel = panel; this.panel = panel;
setLightSource();
}
public void draw(Graphics2D g2) {
g2.drawImage(darknessFilter, 0, 0, null);
}
public void update() {
if(!panel.player.lightUpdated) return;
setLightSource();
panel.player.lightUpdated = false;
}
// ...
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);
Graphics2D g2 = darknessFilter.createGraphics(); Graphics2D g2 = darknessFilter.createGraphics();
int centerX = panel.player.screenX + panel.tileSize/2; int centerX = panel.player.screenX + panel.tileSize/2;
int centerY = panel.player.screenY + panel.tileSize/2; int centerY = panel.player.screenY + panel.tileSize/2;
RadialGradientPaint gPaint = getRadialGradientPaint((float) circleSize, centerX, centerY); RadialGradientPaint gPaint = getRadialGradientPaint(centerX, centerY);
g2.setPaint(gPaint); g2.setPaint(gPaint);
g2.fillRect(0, 0, panel.screenWidth, panel.screenHeight); g2.fillRect(0, 0, panel.screenWidth, panel.screenHeight);
g2.dispose(); g2.dispose();
} }
private static RadialGradientPaint getRadialGradientPaint(float circleSize, int centerX, int centerY) { // UTILITY
private RadialGradientPaint getRadialGradientPaint(int centerX, int centerY) {
Color[] color = new Color[] { Color[] color = new Color[] {
new Color(0, 0, 0, 0.1f), new Color(0, 0, 0, 0.1f),
new Color(0, 0, 0, 0.42f), new Color(0, 0, 0, 0.42f),
new Color(0, 0, 0, 0.52f), new Color(0, 0, 0, 0.52f),
new Color(0, 0, 0, 0.61f), new Color(0, 0, 0, 0.61f),
new Color(0, 0, 0, 0.69f), new Color(0, 0, 0, 0.69f),
new Color(0, 0, 0, 0.76f), new Color(0, 0, 0, 0.76f),
new Color(0, 0, 0, 0.82f), new Color(0, 0, 0, 0.82f),
new Color(0, 0, 0, 0.87f), new Color(0, 0, 0, 0.87f),
new Color(0, 0, 0, 0.91f), new Color(0, 0, 0, 0.91f),
new Color(0, 0, 0, 0.94f), new Color(0, 0, 0, 0.94f),
new Color(0, 0, 0, 0.96f), new Color(0, 0, 0, 0.96f),
new Color(0, 0, 0, 0.97f) new Color(0, 0, 0, 0.97f)
}; };
final int lim = 3;
if(panel.player.currentLight == null) {
for(int i = 0; i < lim; i++) {
color[i] = color[lim];
}
}
float[] fraction = new float[] { float[] fraction = new float[] {
0f, 0.4f, 0.5f, 0.6f, 0.65f, 0.7f, 0.75f, 0.8f, 0.85f, 0.9f, 0.95f, 1f 0f, 0.4f, 0.5f, 0.6f, 0.65f, 0.7f, 0.75f, 0.8f, 0.85f, 0.9f, 0.95f, 1f
}; };
return new RadialGradientPaint(centerX, centerY, (panel.player.currentLight == null ? 75 : panel.player.currentLight.lightRadius), fraction, color);
return new RadialGradientPaint(centerX, centerY, (circleSize /2), fraction, color);
}
public void draw(Graphics2D g2) {
g2.drawImage(darknessFilter, 0, 0, null);
} }
} }