player can now choose between different shields and weapons. added consumable items (healing potion)

This commit is contained in:
2025-11-30 18:29:47 +01:00
parent fb96035c99
commit 51c262d9ac
13 changed files with 167 additions and 24 deletions

View File

@@ -175,6 +175,12 @@ public class UI {
// DRAW PLAYER ITEMS
for(int i = 0; i < panel.player.inventory.size(); i++) {
// EQUIP CURSOR
if(panel.player.inventory.get(i) == panel.player.currentWeapon || panel.player.inventory.get(i) == panel.player.currentShield) {
graphics2d.setColor(new Color(240, 190,90));
graphics2d.fillRoundRect(slotX, slotY, panel.tileSize, panel.tileSize, 10, 10);
}
graphics2d.drawImage(panel.player.inventory.get(i).down1, slotX, slotY, null);
slotX += slotSize;
if (i == 4 || i == 9 || i == 14) {
@@ -230,7 +236,7 @@ public class UI {
int height = panel.tileSize*4;
drawSubWindow(x, y, width, height);
graphics2d.setFont(graphics2d.getFont().deriveFont(Font.PLAIN, 28F));
graphics2d.setFont(graphics2d.getFont().deriveFont(Font.PLAIN, 30F));
x += panel.tileSize;
y += panel.tileSize;
@@ -300,12 +306,12 @@ public class UI {
public int getAlignedToRightX(String text, int tailX) {
return tailX - (int) graphics2d.getFontMetrics().getStringBounds(text, graphics2d).getWidth();
}
public int getItemIndex() {
return slotCol + slotRow*5;
}
public void addMessage(String text) {
messages.add(text);
messageCounter.add(0);
}
public int getItemIndex() {
return slotCol + slotRow*5;
}
}

View File

@@ -6,6 +6,13 @@ public enum EntityType {
NPC,
MONSTER,
ITEM,
WORLD
WORLD,
WEAPON,
SHIELD;
public enum WeaponType {
SWORD,
AXE
}
}

View File

@@ -1,7 +1,10 @@
package de.miaurizius.jgame2d.core.handlers;
import de.miaurizius.jgame2d.core.GamePanel;
import de.miaurizius.jgame2d.entity.item.AxeObj;
import de.miaurizius.jgame2d.entity.item.IronShieldObj;
import de.miaurizius.jgame2d.entity.item.KeyObj;
import de.miaurizius.jgame2d.entity.item.PotionObj;
import de.miaurizius.jgame2d.entity.npc.OldManNPC;
import de.miaurizius.jgame2d.entity.monster.GreenSlimeMON;
@@ -25,10 +28,20 @@ public class AssetSetter {
panel.obj[i].worldY = panel.tileSize*19;
i++;
panel.obj[i] = new KeyObj(panel);
panel.obj[i].worldX = panel.tileSize*26;
panel.obj[i] = new AxeObj(panel);
panel.obj[i].worldX = panel.tileSize*33;
panel.obj[i].worldY = panel.tileSize*21;
i++;
panel.obj[i] = new IronShieldObj(panel);
panel.obj[i].worldX = panel.tileSize*35;
panel.obj[i].worldY = panel.tileSize*21;
i++;
panel.obj[i] = new PotionObj(panel);
panel.obj[i].worldX = panel.tileSize*22;
panel.obj[i].worldY = panel.tileSize*27;
i++;
}
public void setNPC() {

View File

@@ -95,8 +95,12 @@ public class KeyHandler implements KeyListener {
panel.ui.slotCol++;
panel.playSE(9);
break;
case KeyEvent.VK_SPACE:
panel.player.selectItem();
break;
// EXIT STATE
case KeyEvent.VK_C:
case KeyEvent.VK_C, KeyEvent.VK_ESCAPE:
panel.gameState = GameState.PLAY;
break;
}

View File

@@ -5,6 +5,7 @@ import de.miaurizius.jgame2d.core.enums.Direction;
import de.miaurizius.jgame2d.core.GamePanel;
import de.miaurizius.jgame2d.core.Utility;
import de.miaurizius.jgame2d.core.enums.EntityType;
import de.miaurizius.jgame2d.core.enums.GameState;
import javax.imageio.ImageIO;
import java.awt.*;
@@ -36,6 +37,7 @@ public class Entity {
public boolean alive = true;
public boolean dying;
public boolean hpBarOn;
public boolean consumable;
// COUNTER
public int spriteCounter;
@@ -62,6 +64,7 @@ public class Entity {
public Entity currentShield;
// ITEM ATTRIBUTES
public EntityType.WeaponType weaponType;
public int attackValue;
public int defenseValue;
public String description;
@@ -189,6 +192,8 @@ public class Entity {
alive = false;
}
}
public void use(Entity entity) {
} //If entity is consumable
// SETTING THINGS UP
BufferedImage parseSprite() {

View File

@@ -41,9 +41,6 @@ public class Player extends Entity {
solidArea.width = 24;
solidArea.height = 24;
attackArea.width = 36;
attackArea.height = 36;
setDefaultValues();
getPlayerImage();
getPlayerAttackImage();
@@ -128,7 +125,7 @@ public class Player extends Entity {
}
inventory.add(panel.obj[index]);
panel.playSE(1);
panel.ui.addMessage("Picked up " + panel.obj[index].name);
panel.ui.addMessage("Picked up " + panel.obj[index].name + "!");
panel.obj[index] = null;
}
@@ -229,6 +226,24 @@ public class Player extends Entity {
panel.gameState = GameState.DIALOGUE;
panel.ui.currentDialogue = "You are level " + level + " now!\nYou feel stronger!";
}
public void selectItem() {
int itemIndex = panel.ui.getItemIndex();
if(itemIndex >= inventory.size()) return;
Entity selectedItem = inventory.get(itemIndex);
if(selectedItem.type == EntityType.WEAPON) {
currentWeapon = selectedItem;
getAttack();
getPlayerAttackImage();
}
if(selectedItem.type == EntityType.SHIELD) {
currentShield = selectedItem;
getDefense();
}
if(selectedItem.consumable) {
selectedItem.use(this);
inventory.remove(itemIndex);
}
}
// SETTING THINGS UP
public void setDefaultValues() {
@@ -256,6 +271,7 @@ public class Player extends Entity {
inventory.add(currentShield);
}
public int getAttack() {
attackArea = currentWeapon.attackArea;
return attack = strength * currentWeapon.attackValue;
}
public int getDefense() {
@@ -272,6 +288,8 @@ public class Player extends Entity {
right2 = initEntitySprites("player/boy_right_2");
}
public void getPlayerAttackImage() {
switch(currentWeapon.weaponType) {
case SWORD:
attackUp1 = initEntitySprites("player/attack/boy_attack_up_1", panel.tileSize, panel.tileSize * 2);
attackUp2 = initEntitySprites("player/attack/boy_attack_up_2", panel.tileSize, panel.tileSize * 2);
attackDown1 = initEntitySprites("player/attack/boy_attack_down_1", panel.tileSize, panel.tileSize * 2);
@@ -280,6 +298,18 @@ public class Player extends Entity {
attackLeft2 = initEntitySprites("player/attack/boy_attack_left_2", panel.tileSize * 2, panel.tileSize);
attackRight1 = initEntitySprites("player/attack/boy_attack_right_1", panel.tileSize * 2, panel.tileSize);
attackRight2 = initEntitySprites("player/attack/boy_attack_right_2", panel.tileSize * 2, panel.tileSize);
break;
case AXE:
attackUp1 = initEntitySprites("player/attack/boy_axe_up_1", panel.tileSize, panel.tileSize * 2);
attackUp2 = initEntitySprites("player/attack/boy_axe_up_2", panel.tileSize, panel.tileSize * 2);
attackDown1 = initEntitySprites("player/attack/boy_axe_down_1", panel.tileSize, panel.tileSize * 2);
attackDown2 = initEntitySprites("player/attack/boy_axe_down_2", panel.tileSize, panel.tileSize * 2);
attackLeft1 = initEntitySprites("player/attack/boy_axe_left_1", panel.tileSize * 2, panel.tileSize);
attackLeft2 = initEntitySprites("player/attack/boy_axe_left_2", panel.tileSize * 2, panel.tileSize);
attackRight1 = initEntitySprites("player/attack/boy_axe_right_1", panel.tileSize * 2, panel.tileSize);
attackRight2 = initEntitySprites("player/attack/boy_axe_right_2", panel.tileSize * 2, panel.tileSize);
break;
}
}
}

View File

@@ -0,0 +1,22 @@
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 AxeObj extends Entity {
public AxeObj(GamePanel panel) {
super(panel);
name = "Woodcutter's Axe";
description = "[" + name + "]\nA bit rusty but still can \ncut some trees.";
type = EntityType.WEAPON;
weaponType = EntityType.WeaponType.AXE;
down1 = initEntitySprites("/objects/axe");
attackValue = 2;
attackArea.width = 30;
attackArea.height = 30;
}
}

View File

@@ -8,7 +8,7 @@ public class BootsObj extends Entity {
public BootsObj(GamePanel panel) {
super(panel);
name = "boots";
name = "Boots";
type = EntityType.ITEM;
down1 = initEntitySprites("objects/boots.png");
}

View File

@@ -8,7 +8,7 @@ public class HeartObj extends Entity {
public HeartObj(GamePanel panel) {
super(panel);
name = "heart";
name = "Heart Container";
type = EntityType.ITEM;
image = initEntitySprites("objects/heart_full");
image2 = initEntitySprites("objects/heart_half");

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 IronShieldObj extends Entity {
public IronShieldObj(GamePanel panel) {
super(panel);
name = "Iron Shield";
description = "[" + name + "]\nAn iron shield. It's very\nheavy.";
type = EntityType.SHIELD;
down1 = initEntitySprites("objects/shield_blue");
defenseValue = 2;
}
}

View File

@@ -0,0 +1,33 @@
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 PotionObj extends Entity {
GamePanel panel;
int value = 2;
public PotionObj(GamePanel panel) {
super(panel);
this.panel = panel;
type = EntityType.ITEM;
consumable = true;
name = "Red Potion";
down1 = initEntitySprites("objects/potion_red");
description = "[" + name + "]\nHeals your life by " + value + ".";
}
public void 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);
}
}

View File

@@ -10,7 +10,7 @@ public class ShieldWoodObj extends Entity {
super(panel);
name = "Wooden Shield";
description = "[" + name + "]\nAn old shield. It's not\nthat strong but its okay.";
type = EntityType.ITEM;
type = EntityType.SHIELD;
down1 = initEntitySprites("objects/shield_wood");
defenseValue = 1;
}

View File

@@ -11,9 +11,13 @@ public class SwordNormalObj extends Entity {
name = "Normal Sword";
description = "[" + name + "]\nAn old sword.";
type = EntityType.ITEM;
type = EntityType.WEAPON;
weaponType = EntityType.WeaponType.SWORD;
down1 = initEntitySprites("objects/sword_normal");
attackValue = 1;
attackArea.width = 36;
attackArea.height = 36;
}
}