Compare commits

...

5 Commits

15 changed files with 251 additions and 30 deletions

View File

@@ -7,14 +7,21 @@ public class Boot {
public static final Logger logger = Logger.getLogger("JDGame2D");
public static JFrame window;
public static GamePanel gamePanel = new GamePanel();
static void main() {
generateWindow();
gamePanel.setupGame();
gamePanel.startGameThread();
}
public static void generateWindow() {
window = new JFrame();
window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
window.setResizable(false);
window.setTitle("JGame2D");
GamePanel gamePanel = new GamePanel();
window.add(gamePanel);
gamePanel.config.load();
@@ -24,9 +31,6 @@ public class Boot {
window.setLocationRelativeTo(null);
window.setVisible(true);
gamePanel.setupGame();
gamePanel.startGameThread();
}
}

View File

@@ -6,6 +6,7 @@ import de.miaurizius.jgame2d.core.enums.Map;
import de.miaurizius.jgame2d.core.handlers.*;
import de.miaurizius.jgame2d.entity.Entity;
import de.miaurizius.jgame2d.entity.Player;
import de.miaurizius.jgame2d.environment.EnvironmentManager;
import de.miaurizius.jgame2d.tile.TileManager;
import de.miaurizius.jgame2d.tile.interactive.InteractiveTile;
@@ -56,6 +57,7 @@ public class GamePanel extends JPanel implements Runnable {
public Sound music = new Sound();
public Config config = new Config(this);
public PathFinder pFinder = new PathFinder(this);
public EnvironmentManager eManager = new EnvironmentManager(this);
Thread gameThread;
// ENTITY AND OBJECT
@@ -146,6 +148,7 @@ public class GamePanel extends JPanel implements Runnable {
}
}
for(Entity entity : iTile[currentMap.getIndex()]) if(entity != null) entity.update();
eManager.update();
break;
case PAUSE:
break;
@@ -177,6 +180,10 @@ public class GamePanel extends JPanel implements Runnable {
for(Entity entity : entityList) entity.draw(fg2);
entityList.clear();
// ENVIRONMENT
eManager.draw(fg2);
// UI
ui.draw(fg2);
long drawEnd = System.nanoTime();
@@ -229,6 +236,7 @@ public class GamePanel extends JPanel implements Runnable {
assetSetter.setNPC();
assetSetter.setMonster();
assetSetter.setITiles();
eManager.setup();
gameState = GameState.TITLE;
tempScreen = new BufferedImage(screenWidth, screenHeight, BufferedImage.TYPE_INT_RGB);

View File

@@ -194,11 +194,33 @@ public class UI {
for(int i = 0; i < entity.inventory.size(); i++) {
// 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.fillRoundRect(slotX, slotY, panel.tileSize, panel.tileSize, 10, 10);
}
graphics2d.drawImage(entity.inventory.get(i).down1, slotX, slotY, null);
// AMOUNT
if(entity == panel.player && entity.inventory.get(i).amt > 1) {
graphics2d.setFont(graphics2d.getFont().deriveFont(32F));
int amtX;
int amtY;
String amtS = String.valueOf(entity.inventory.get(i).amt);
amtX = getAlignedToRightX(amtS, slotX+44);
amtY = slotY + panel.tileSize;
// SHADOW
graphics2d.setColor(new Color(60, 60, 60));
graphics2d.drawString(amtS, amtX, amtY);
// NUMBER
graphics2d.setColor(Color.white);
graphics2d.drawString(amtS, amtX-3, amtY-3);
}
slotX += slotSize;
if (i == 4 || i == 9 || i == 14) {
slotX = slotXStart;
@@ -450,15 +472,14 @@ public class UI {
drawDialogueScreen();
return;
}
if(panel.player.inventory.size() == panel.player.maxInvSize) {
tradeState = TradeState.SELECT;
panel.gameState = GameState.DIALOGUE;
currentDialogue = "Your inventory is full!";
drawDialogueScreen();
if(panel.player.canObtainItem(tradingNPC.inventory.get(itemIndex))) {
panel.player.coins -= tradingNPC.inventory.get(itemIndex).price;
return;
}
panel.player.coins -= tradingNPC.inventory.get(itemIndex).price;
panel.player.inventory.add(tradingNPC.inventory.get(itemIndex));
tradeState = TradeState.SELECT;
panel.gameState = GameState.DIALOGUE;
currentDialogue = "Your inventory is full!";
drawDialogueScreen();
}
}
private void tradeSell() {
@@ -499,7 +520,8 @@ public class UI {
currentDialogue = "You cannot sell an equipped item!";
return;
}
panel.player.inventory.remove(itemIndex);
if(panel.player.inventory.get(itemIndex).amt <= 1) panel.player.inventory.remove(itemIndex);
else panel.player.inventory.get(itemIndex).amt--;
panel.player.coins += Integer.parseInt(price);
}
}

View File

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

View File

@@ -39,6 +39,26 @@ public class AssetSetter {
panel.obj[Map.OVERWORLD.getIndex()][i].worldX = panel.tileSize*30;
panel.obj[Map.OVERWORLD.getIndex()][i].worldY = panel.tileSize*28;
i++;
panel.obj[Map.OVERWORLD.getIndex()][i] = new PotionObj(panel);
panel.obj[Map.OVERWORLD.getIndex()][i].worldX = panel.tileSize*21;
panel.obj[Map.OVERWORLD.getIndex()][i].worldY = panel.tileSize*20;
i++;
panel.obj[Map.OVERWORLD.getIndex()][i] = new PotionObj(panel);
panel.obj[Map.OVERWORLD.getIndex()][i].worldX = panel.tileSize*20;
panel.obj[Map.OVERWORLD.getIndex()][i].worldY = panel.tileSize*20;
i++;
panel.obj[Map.OVERWORLD.getIndex()][i] = new PotionObj(panel);
panel.obj[Map.OVERWORLD.getIndex()][i].worldX = panel.tileSize*17;
panel.obj[Map.OVERWORLD.getIndex()][i].worldY = panel.tileSize*21;
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() {
@@ -88,6 +108,16 @@ public class AssetSetter {
panel.iTile[Map.OVERWORLD.getIndex()][i] = new DryTreeTI(panel,32,12);i++;
panel.iTile[Map.OVERWORLD.getIndex()][i] = new DryTreeTI(panel,33,12);i++;
panel.iTile[Map.OVERWORLD.getIndex()][i] = new DryTreeTI(panel,29,21);i++;
panel.iTile[Map.OVERWORLD.getIndex()][i] = new DryTreeTI(panel,25,27);i++;
panel.iTile[Map.OVERWORLD.getIndex()][i] = new DryTreeTI(panel,26,27);i++;
panel.iTile[Map.OVERWORLD.getIndex()][i] = new DryTreeTI(panel,27,27);i++;
panel.iTile[Map.OVERWORLD.getIndex()][i] = new DryTreeTI(panel,27,28);i++;
panel.iTile[Map.OVERWORLD.getIndex()][i] = new DryTreeTI(panel,27,29);i++;
panel.iTile[Map.OVERWORLD.getIndex()][i] = new DryTreeTI(panel,28,29);i++;
panel.iTile[Map.OVERWORLD.getIndex()][i] = new DryTreeTI(panel,29,29);i++;
panel.iTile[Map.OVERWORLD.getIndex()][i] = new DryTreeTI(panel,18,40);i++;
panel.iTile[Map.OVERWORLD.getIndex()][i] = new DryTreeTI(panel,17,40);i++;
panel.iTile[Map.OVERWORLD.getIndex()][i] = new DryTreeTI(panel,16,40);i++;

View File

@@ -70,6 +70,7 @@ public class Entity {
public int mana;
public Entity currentWeapon;
public Entity currentShield;
public Entity currentLight;
public Projectile projectile;
public ArrayList<Entity> inventory = new ArrayList<>();
public final int maxInvSize = 20;
@@ -83,6 +84,9 @@ public class Entity {
public int value;
public int price;
public int knockbackVal;
public boolean stackable;
public int amt = 1;
public float lightRadius;
public Entity(GamePanel panel) {
this.panel = panel;
@@ -229,8 +233,8 @@ public class Entity {
alive = false;
}
}
public void use(Entity entity) {
public boolean use(Entity entity) {
return false;
} //If entity is consumable
public void checkDrop() {

View File

@@ -5,6 +5,7 @@ import de.miaurizius.jgame2d.core.enums.Direction;
import de.miaurizius.jgame2d.core.enums.EntityType;
import de.miaurizius.jgame2d.core.enums.GameState;
import de.miaurizius.jgame2d.core.handlers.KeyHandler;
import de.miaurizius.jgame2d.entity.item.KeyObj;
import de.miaurizius.jgame2d.entity.item.ShieldWoodObj;
import de.miaurizius.jgame2d.entity.item.SwordNormalObj;
import de.miaurizius.jgame2d.entity.projectile.FireballObj;
@@ -19,6 +20,7 @@ public class Player extends Entity {
// STATE
public boolean attackCancel;
public boolean lightUpdated;
public Player(GamePanel panel, KeyHandler keyH) {
super(panel);
@@ -150,11 +152,10 @@ public class Player extends Entity {
}
// INVENTORY ITEMS
else {
if(inventory.size() == maxInvSize) {
if(!canObtainItem(panel.obj[panel.currentMap.getIndex()][index])) {
panel.ui.addMessage("Your inventory is full!");
return;
}
inventory.add(panel.obj[panel.currentMap.getIndex()][index]);
panel.playSE(1);
panel.ui.addMessage("Picked up " + panel.obj[panel.currentMap.getIndex()][index].name + "!");
}
@@ -289,9 +290,13 @@ public class Player extends Entity {
currentShield = selectedItem;
getDefense();
}
if(selectedItem.consumable) {
selectedItem.use(this);
if(selectedItem.type == EntityType.LIGHT) {
if(currentLight == selectedItem) currentLight = null; else currentLight = selectedItem;
lightUpdated = true;
}
if(selectedItem.consumable)
if(selectedItem.use(this))
if(selectedItem.amt > 1) selectedItem.amt--; else inventory.remove(selectedItem);
}
public void setDefaultPositions() {
worldX = panel.tileSize * 23;
@@ -320,7 +325,7 @@ public class Player extends Entity {
dexterity = 1;
exp = 0;
nextLevelExp = 5;
coins = 0;
coins = 500;
currentWeapon = new SwordNormalObj(panel);
currentShield = new ShieldWoodObj(panel);
projectile = new FireballObj(panel);
@@ -331,6 +336,7 @@ public class Player extends Entity {
inventory.clear();
inventory.add(currentWeapon);
inventory.add(currentShield);
inventory.add(new KeyObj(panel));
}
public int getAttack() {
attackArea = currentWeapon.attackArea;
@@ -373,5 +379,29 @@ public class Player extends Entity {
break;
}
}
public int searchItemInInventory(String itemName) {
for(int i = 0; i < inventory.size(); i++) {
if(inventory.get(i).name.equals(itemName)) {
return i;
}
}
return 999;
}
public boolean canObtainItem(Entity item) {
int i = searchItemInInventory(item.name);
if(item.stackable) {
if(i != 999) {
inventory.get(i).amt++;
return true;
} else if(inventory.size() != maxInvSize) {
inventory.add(item);
return true;
}
} else if(inventory.size() != maxInvSize) {
inventory.add(item);
return true;
}
return false;
}
}

View File

@@ -18,10 +18,10 @@ public class CoinObj extends Entity {
down1 = initEntitySprites("/objects/coin_bronze");
}
public void use(Entity entity) {
public boolean use(Entity entity) {
panel.playSE(1);
panel.ui.addMessage("Coin +"+value);
panel.player.coins += value;
panel.player.inventory.remove(this);
return true;
}
}

View File

@@ -22,10 +22,12 @@ public class HeartObj extends Entity {
image3 = initEntitySprites("objects/heart_blank");
}
public void use(Entity entity) {
@Override
public boolean use(Entity entity) {
panel.playSE(2);
panel.ui.addMessage("Life +"+value);
entity.life += value;
return true;
}
}

View File

@@ -16,20 +16,21 @@ public class KeyObj extends Entity {
down1 = initEntitySprites("objects/key");
price = 50;
stackable = true;
}
@Override
public void use(Entity entity) {
public boolean use(Entity entity) {
panel.gameState = GameState.DIALOGUE;
int objIndex = getDetected(entity, panel.obj, "door");
if(objIndex == 999) {
panel.ui.currentDialogue = "What are you doing?\nThere is no door nearby.";
return;
return false;
}
panel.ui.currentDialogue = "You used a " + name + "!\nThe door is now open.";
panel.playSE(3);
panel.obj[panel.currentMap.getIndex()][objIndex] = null;
entity.inventory.remove(this);
return true;
}
}

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

@@ -21,16 +21,17 @@ public class PotionObj extends Entity {
description = "[" + name + "]\nHeals your life by " + value + ".";
price = 50;
stackable = true;
}
public void use(Entity entity) {
public boolean use(Entity entity) {
panel.gameState = GameState.DIALOGUE;
panel.ui.currentDialogue = "You drank a " + name + "!\n" +
"Your life has been recovered by " + value + ".";
entity.life += value;
if(panel.player.life > panel.player.maxLife) panel.player.life = panel.player.maxLife;
panel.playSE(2);
panel.player.inventory.remove(this);
return true;
}
}

View File

@@ -42,13 +42,12 @@ public class ChestObj extends Entity {
panel.playSE(3);
StringBuilder sb = new StringBuilder();
sb.append("You open the chest and find a ").append(loot.name).append("!");
if(panel.player.inventory.size() == panel.player.maxInvSize) {
if(!panel.player.canObtainItem(loot)) {
sb.append("\nBut your inventory is full...");
panel.ui.currentDialogue = sb.toString();
return;
}
sb.append("\nYou obtain the ").append(loot.name).append("!");
panel.player.inventory.add(loot);
down1 = image2;
opened = true;
panel.ui.currentDialogue = sb.toString();

View File

@@ -0,0 +1,28 @@
package de.miaurizius.jgame2d.environment;
import de.miaurizius.jgame2d.core.GamePanel;
import java.awt.*;
public class EnvironmentManager {
GamePanel panel;
Lighting lighting;
public EnvironmentManager(GamePanel panel) {
this.panel = panel;
}
public void draw(Graphics2D g2) {
lighting.draw(g2);
}
// SETTING THINGS UP
public void setup() {
lighting = new Lighting(panel);
}
public void update() {
lighting.update();
}
}

View File

@@ -0,0 +1,72 @@
package de.miaurizius.jgame2d.environment;
import de.miaurizius.jgame2d.core.GamePanel;
import java.awt.*;
import java.awt.image.BufferedImage;
public class Lighting {
GamePanel panel;
BufferedImage darknessFilter;
public Lighting(GamePanel 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);
Graphics2D g2 = darknessFilter.createGraphics();
int centerX = panel.player.screenX + panel.tileSize/2;
int centerY = panel.player.screenY + panel.tileSize/2;
RadialGradientPaint gPaint = getRadialGradientPaint(centerX, centerY);
g2.setPaint(gPaint);
g2.fillRect(0, 0, panel.screenWidth, panel.screenHeight);
g2.dispose();
}
// UTILITY
private RadialGradientPaint getRadialGradientPaint(int centerX, int centerY) {
Color[] color = new Color[] {
new Color(0, 0, 0, 0.1f),
new Color(0, 0, 0, 0.42f),
new Color(0, 0, 0, 0.52f),
new Color(0, 0, 0, 0.61f),
new Color(0, 0, 0, 0.69f),
new Color(0, 0, 0, 0.76f),
new Color(0, 0, 0, 0.82f),
new Color(0, 0, 0, 0.87f),
new Color(0, 0, 0, 0.91f),
new Color(0, 0, 0, 0.94f),
new Color(0, 0, 0, 0.96f),
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[] {
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);
}
}