From 825b084a685617ea4762b151863c0eb80d8dd5bf Mon Sep 17 00:00:00 2001 From: Maurice Date: Sat, 13 Dec 2025 00:44:53 +0100 Subject: [PATCH] obstacles and chest loot --- assets/maps/world3.map | 6 +- .../jgame2d/core/enums/EntityType.java | 2 +- .../jgame2d/core/handlers/AssetSetter.java | 29 +++++----- src/de/miaurizius/jgame2d/entity/Entity.java | 51 +++++++++++++++++ src/de/miaurizius/jgame2d/entity/Player.java | 16 +++++- .../jgame2d/entity/item/CoinObj.java | 1 + .../jgame2d/entity/item/KeyObj.java | 16 ++++++ .../jgame2d/entity/item/PotionObj.java | 1 + .../jgame2d/entity/obstacle/ChestObj.java | 57 +++++++++++++++++++ .../entity/{world => obstacle}/DoorObj.java | 14 ++++- .../jgame2d/entity/world/ChestObj.java | 14 ----- 11 files changed, 169 insertions(+), 38 deletions(-) create mode 100644 src/de/miaurizius/jgame2d/entity/obstacle/ChestObj.java rename src/de/miaurizius/jgame2d/entity/{world => obstacle}/DoorObj.java (55%) delete mode 100644 src/de/miaurizius/jgame2d/entity/world/ChestObj.java diff --git a/assets/maps/world3.map b/assets/maps/world3.map index 9f4b3ac..8912592 100644 --- a/assets/maps/world3.map +++ b/assets/maps/world3.map @@ -25,9 +25,9 @@ 12 12 12 12 12 12 12 12 18 41 10 41 41 10 41 41 41 41 41 41 41 41 31 26 30 41 41 41 41 41 41 41 41 41 41 31 26 30 11 17 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 13 12 22 21 41 10 41 41 10 10 41 41 41 41 41 41 41 31 26 30 41 41 41 41 41 41 41 41 41 41 31 26 30 10 17 12 12 12 13 12 12 12 12 12 12 12 12 12 12 12 12 12 18 41 41 10 41 41 41 10 41 41 41 41 41 41 41 31 26 30 41 41 41 41 41 41 41 41 41 41 31 26 30 10 17 12 12 12 12 12 12 12 12 12 12 -12 12 12 13 12 12 12 18 41 41 10 41 41 41 10 41 41 41 41 41 41 41 31 26 30 41 41 10 41 41 41 41 41 41 41 31 26 30 10 17 12 13 12 12 12 12 12 12 12 12 -12 12 12 12 12 12 12 18 41 41 10 41 41 41 10 41 41 41 41 41 41 41 31 26 30 41 41 41 41 41 10 41 41 41 41 31 26 30 10 17 12 12 12 12 12 12 12 12 12 12 -12 12 12 12 12 12 13 18 41 41 10 10 10 10 10 10 41 41 41 41 41 41 31 26 30 41 41 41 41 41 10 41 41 41 41 37 28 38 10 17 12 12 12 12 13 12 12 12 12 12 +12 12 12 13 12 12 12 18 41 41 10 41 41 41 10 41 41 41 41 41 41 41 31 26 30 10 10 10 41 41 41 41 41 41 41 31 26 30 10 17 12 13 12 12 12 12 12 12 12 12 +12 12 12 12 12 12 12 18 41 41 10 41 41 41 10 41 41 41 41 41 41 41 31 26 30 41 41 10 41 41 10 41 41 41 41 31 26 30 10 17 12 12 12 12 12 12 12 12 12 12 +12 12 12 12 12 12 13 18 41 41 10 10 10 10 10 10 41 41 41 41 41 41 31 26 30 41 41 10 10 10 10 41 41 41 41 37 28 38 10 17 12 12 12 12 13 12 12 12 12 12 12 12 12 12 12 12 12 18 41 41 11 10 10 10 10 10 41 41 41 41 41 41 31 26 30 41 41 41 41 41 41 41 41 41 41 41 10 41 14 25 12 12 13 12 12 12 12 12 12 12 12 12 12 12 12 12 12 24 16 41 10 10 10 10 10 10 41 41 41 41 41 41 31 26 30 41 41 41 41 41 41 41 41 41 41 10 10 10 17 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 13 12 12 18 41 10 10 10 11 10 10 41 41 41 41 41 41 31 26 30 41 41 41 41 41 41 41 41 41 41 10 10 10 17 12 12 12 12 12 12 12 13 12 12 12 diff --git a/src/de/miaurizius/jgame2d/core/enums/EntityType.java b/src/de/miaurizius/jgame2d/core/enums/EntityType.java index d31626c..ad7bcb8 100644 --- a/src/de/miaurizius/jgame2d/core/enums/EntityType.java +++ b/src/de/miaurizius/jgame2d/core/enums/EntityType.java @@ -6,7 +6,7 @@ public enum EntityType { NPC, MONSTER, ITEM, - WORLD, + OBSTACLE, PICKUP, PROJECTILE, WEAPON, diff --git a/src/de/miaurizius/jgame2d/core/handlers/AssetSetter.java b/src/de/miaurizius/jgame2d/core/handlers/AssetSetter.java index 86eb7c3..33ae5ab 100644 --- a/src/de/miaurizius/jgame2d/core/handlers/AssetSetter.java +++ b/src/de/miaurizius/jgame2d/core/handlers/AssetSetter.java @@ -6,6 +6,8 @@ import de.miaurizius.jgame2d.entity.item.*; import de.miaurizius.jgame2d.entity.npc.MerchantNPC; import de.miaurizius.jgame2d.entity.npc.OldManNPC; import de.miaurizius.jgame2d.entity.monster.GreenSlimeMON; +import de.miaurizius.jgame2d.entity.obstacle.ChestObj; +import de.miaurizius.jgame2d.entity.obstacle.DoorObj; import de.miaurizius.jgame2d.tile.interactive.DryTreeTI; public class AssetSetter { @@ -18,29 +20,24 @@ public class AssetSetter { public void setObject() { int i = 0; - panel.obj[Map.OVERWORLD.getIndex()][i] = new HeartObj(panel); - panel.obj[Map.OVERWORLD.getIndex()][i].worldX = panel.tileSize*25; - panel.obj[Map.OVERWORLD.getIndex()][i].worldY = panel.tileSize*23; - i++; - - panel.obj[Map.OVERWORLD.getIndex()][i] = new CoinObj(panel); - panel.obj[Map.OVERWORLD.getIndex()][i].worldX = panel.tileSize*21; - panel.obj[Map.OVERWORLD.getIndex()][i].worldY = panel.tileSize*19; - i++; - panel.obj[Map.OVERWORLD.getIndex()][i] = new AxeObj(panel); panel.obj[Map.OVERWORLD.getIndex()][i].worldX = panel.tileSize*33; panel.obj[Map.OVERWORLD.getIndex()][i].worldY = panel.tileSize*21; i++; - panel.obj[Map.OVERWORLD.getIndex()][i] = new IronShieldObj(panel); - panel.obj[Map.OVERWORLD.getIndex()][i].worldX = panel.tileSize*35; - panel.obj[Map.OVERWORLD.getIndex()][i].worldY = panel.tileSize*21; + panel.obj[Map.OVERWORLD.getIndex()][i] = new DoorObj(panel); + panel.obj[Map.OVERWORLD.getIndex()][i].worldX = panel.tileSize*14; + 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*22; - panel.obj[Map.OVERWORLD.getIndex()][i].worldY = panel.tileSize*27; + panel.obj[Map.OVERWORLD.getIndex()][i] = new DoorObj(panel); + panel.obj[Map.OVERWORLD.getIndex()][i].worldX = panel.tileSize*12; + panel.obj[Map.OVERWORLD.getIndex()][i].worldY = panel.tileSize*12; + i++; + + panel.obj[Map.OVERWORLD.getIndex()][i] = new ChestObj(panel, new KeyObj(panel)); + panel.obj[Map.OVERWORLD.getIndex()][i].worldX = panel.tileSize*30; + panel.obj[Map.OVERWORLD.getIndex()][i].worldY = panel.tileSize*28; i++; } diff --git a/src/de/miaurizius/jgame2d/entity/Entity.java b/src/de/miaurizius/jgame2d/entity/Entity.java index 0ead7cd..5638285 100644 --- a/src/de/miaurizius/jgame2d/entity/Entity.java +++ b/src/de/miaurizius/jgame2d/entity/Entity.java @@ -230,6 +230,7 @@ public class Entity { } } public void use(Entity entity) { + } //If entity is consumable public void checkDrop() { @@ -255,6 +256,36 @@ public class Entity { } } } + public void interact() { + + } + public int getDetected(Entity user, Entity[][] target, String targetName) { + int index = 999; + int nextWorldX = user.getLeftX(); + int nextWorldY = user.getTopY(); + + switch(user.direction) { + case UP -> nextWorldY = user.getTopY()-1; + case DOWN -> nextWorldY = user.getBottomY()+1; + case LEFT -> nextWorldX = user.getLeftX()-1; + case RIGHT -> nextWorldX = user.getRightX()+1; + } + int col = nextWorldX / panel.tileSize; + int row = nextWorldY / panel.tileSize; + + for(int i = 0; i < target[panel.currentMap.getIndex()].length; i++) { + if(target[panel.currentMap.getIndex()][i] == null) continue; + if( + target[panel.currentMap.getIndex()][i].getCol() == col && + target[panel.currentMap.getIndex()][i].getRow() == row && + target[panel.currentMap.getIndex()][i].name.equalsIgnoreCase(targetName) + ) { + index = i; + break; + } + } + return index; + } // PARTICLE SETUP public Color getParticleColor() { @@ -285,6 +316,26 @@ public class Entity { panel.particleList.add(p4); } + // GETTERS + public int getLeftX() { + return worldX + solidArea.x; + } + public int getRightX() { + return worldX + solidArea.x + solidArea.width; + } + public int getTopY() { + return worldY + solidArea.y; + } + public int getBottomY() { + return worldY + solidArea.y + solidArea.height; + } + public int getCol() { + return (worldX + solidArea.x) / panel.tileSize; + } + public int getRow() { + return (worldY + solidArea.y) / panel.tileSize; + } + // SETTING THINGS UP BufferedImage parseSprite() { return switch (direction) { diff --git a/src/de/miaurizius/jgame2d/entity/Player.java b/src/de/miaurizius/jgame2d/entity/Player.java index 9477fd1..7fca996 100644 --- a/src/de/miaurizius/jgame2d/entity/Player.java +++ b/src/de/miaurizius/jgame2d/entity/Player.java @@ -5,6 +5,8 @@ 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.PotionObj; import de.miaurizius.jgame2d.entity.item.ShieldWoodObj; import de.miaurizius.jgame2d.entity.item.SwordNormalObj; import de.miaurizius.jgame2d.entity.projectile.FireballObj; @@ -135,9 +137,18 @@ public class Player extends Entity { public void pickObject(int index) { if(index == 999 || panel.obj[panel.currentMap.getIndex()][index] == null) return; - // PICKUP ONLY + // PICKUP ONLY ITEMS if(panel.obj[panel.currentMap.getIndex()][index].type == EntityType.PICKUP) { panel.obj[panel.currentMap.getIndex()][index].use(this); + panel.obj[panel.currentMap.getIndex()][index] = null; + return; + } + // OBSTACLES + if(panel.obj[panel.currentMap.getIndex()][index].type == EntityType.OBSTACLE) { + if(!keyH.spacePressed) return; + attackCancel = true; + panel.obj[panel.currentMap.getIndex()][index].interact(); + return; } // INVENTORY ITEMS else { @@ -229,7 +240,7 @@ public class Player extends Entity { entity.speed += knockbackVal; entity.knockback = true; } - + public void interactTile(int index) { if(index == 999 || !panel.iTile[panel.currentMap.getIndex()][index].destructible || panel.iTile[panel.currentMap.getIndex()][index].invincible) return; if(!panel.iTile[panel.currentMap.getIndex()][index].meetItemReq(this)) return; @@ -282,7 +293,6 @@ public class Player extends Entity { } if(selectedItem.consumable) { selectedItem.use(this); - inventory.remove(itemIndex); } } public void setDefaultPositions() { diff --git a/src/de/miaurizius/jgame2d/entity/item/CoinObj.java b/src/de/miaurizius/jgame2d/entity/item/CoinObj.java index 6033048..1dada1a 100644 --- a/src/de/miaurizius/jgame2d/entity/item/CoinObj.java +++ b/src/de/miaurizius/jgame2d/entity/item/CoinObj.java @@ -22,5 +22,6 @@ public class CoinObj extends Entity { panel.playSE(1); panel.ui.addMessage("Coin +"+value); panel.player.coins += value; + panel.player.inventory.remove(this); } } diff --git a/src/de/miaurizius/jgame2d/entity/item/KeyObj.java b/src/de/miaurizius/jgame2d/entity/item/KeyObj.java index a691067..2d3d5bf 100644 --- a/src/de/miaurizius/jgame2d/entity/item/KeyObj.java +++ b/src/de/miaurizius/jgame2d/entity/item/KeyObj.java @@ -2,6 +2,7 @@ package de.miaurizius.jgame2d.entity.item; import de.miaurizius.jgame2d.core.GamePanel; import de.miaurizius.jgame2d.core.enums.EntityType; +import de.miaurizius.jgame2d.core.enums.GameState; import de.miaurizius.jgame2d.entity.Entity; public class KeyObj extends Entity { @@ -11,9 +12,24 @@ public class KeyObj extends Entity { name = "Normal Key"; description = "[" + name + "]\nIt opens a door."; type = EntityType.ITEM; + consumable = true; down1 = initEntitySprites("objects/key"); price = 50; } + @Override + public void 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; + } + 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); + } + } diff --git a/src/de/miaurizius/jgame2d/entity/item/PotionObj.java b/src/de/miaurizius/jgame2d/entity/item/PotionObj.java index 4cdc579..5089c30 100644 --- a/src/de/miaurizius/jgame2d/entity/item/PotionObj.java +++ b/src/de/miaurizius/jgame2d/entity/item/PotionObj.java @@ -30,6 +30,7 @@ public class PotionObj extends Entity { entity.life += value; if(panel.player.life > panel.player.maxLife) panel.player.life = panel.player.maxLife; panel.playSE(2); + panel.player.inventory.remove(this); } } diff --git a/src/de/miaurizius/jgame2d/entity/obstacle/ChestObj.java b/src/de/miaurizius/jgame2d/entity/obstacle/ChestObj.java new file mode 100644 index 0000000..750e28b --- /dev/null +++ b/src/de/miaurizius/jgame2d/entity/obstacle/ChestObj.java @@ -0,0 +1,57 @@ +package de.miaurizius.jgame2d.entity.obstacle; + +import de.miaurizius.jgame2d.core.GamePanel; +import de.miaurizius.jgame2d.core.enums.EntityType; +import de.miaurizius.jgame2d.core.enums.GameState; +import de.miaurizius.jgame2d.entity.Entity; + +public class ChestObj extends Entity { + + GamePanel panel; + Entity loot; + boolean opened; + + public ChestObj(GamePanel panel, Entity loot) { + super(panel); + this.panel = panel; + this.loot = loot; + type = EntityType.OBSTACLE; + name = "chest"; + + image = initEntitySprites("objects/chest"); + image2 = initEntitySprites("objects/chest_opened"); + down1 = image; + collision = true; + + solidArea.x = 4; + solidArea.y = 16; + solidArea.width = 40; + solidArea.height = 32; + solidAreaDefaultX = solidArea.x; + solidAreaDefaultY = solidArea.y; + } + + @Override + public void interact() { + panel.gameState = GameState.DIALOGUE; + + if(opened) { + panel.ui.currentDialogue = "It's already empty..."; + return; + } + 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) { + 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(); + } + +} diff --git a/src/de/miaurizius/jgame2d/entity/world/DoorObj.java b/src/de/miaurizius/jgame2d/entity/obstacle/DoorObj.java similarity index 55% rename from src/de/miaurizius/jgame2d/entity/world/DoorObj.java rename to src/de/miaurizius/jgame2d/entity/obstacle/DoorObj.java index 0b44c26..d2fc940 100644 --- a/src/de/miaurizius/jgame2d/entity/world/DoorObj.java +++ b/src/de/miaurizius/jgame2d/entity/obstacle/DoorObj.java @@ -1,13 +1,19 @@ -package de.miaurizius.jgame2d.entity.world; +package de.miaurizius.jgame2d.entity.obstacle; import de.miaurizius.jgame2d.core.GamePanel; +import de.miaurizius.jgame2d.core.enums.EntityType; +import de.miaurizius.jgame2d.core.enums.GameState; import de.miaurizius.jgame2d.entity.Entity; public class DoorObj extends Entity { + GamePanel panel; + public DoorObj(GamePanel panel) { super(panel); + this.panel = panel; name = "door"; + type = EntityType.OBSTACLE; down1 = initEntitySprites("objects/door"); collision = true; @@ -19,4 +25,10 @@ public class DoorObj extends Entity { solidAreaDefaultY = solidArea.y; } + @Override + public void interact() { + panel.gameState = GameState.DIALOGUE; + panel.ui.currentDialogue = "You need a key to open this."; + } + } diff --git a/src/de/miaurizius/jgame2d/entity/world/ChestObj.java b/src/de/miaurizius/jgame2d/entity/world/ChestObj.java deleted file mode 100644 index 1dd30a1..0000000 --- a/src/de/miaurizius/jgame2d/entity/world/ChestObj.java +++ /dev/null @@ -1,14 +0,0 @@ -package de.miaurizius.jgame2d.entity.world; - -import de.miaurizius.jgame2d.core.GamePanel; -import de.miaurizius.jgame2d.entity.Entity; - -public class ChestObj extends Entity { - - public ChestObj(GamePanel panel) { - super(panel); - name = "chest"; - down1 = initEntitySprites("objects/chest"); - } - -}