make heart collectable

This commit is contained in:
2025-12-07 00:44:56 +01:00
parent 54458293ba
commit 6254eb2501
6 changed files with 63 additions and 15 deletions

View File

@@ -7,6 +7,7 @@ public enum EntityType {
MONSTER, MONSTER,
ITEM, ITEM,
WORLD, WORLD,
PICKUP,
PROJECTILE, PROJECTILE,
WEAPON, WEAPON,
SHIELD; SHIELD;

View File

@@ -1,10 +1,7 @@
package de.miaurizius.jgame2d.core.handlers; package de.miaurizius.jgame2d.core.handlers;
import de.miaurizius.jgame2d.core.GamePanel; import de.miaurizius.jgame2d.core.GamePanel;
import de.miaurizius.jgame2d.entity.item.AxeObj; import de.miaurizius.jgame2d.entity.item.*;
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.npc.OldManNPC;
import de.miaurizius.jgame2d.entity.monster.GreenSlimeMON; import de.miaurizius.jgame2d.entity.monster.GreenSlimeMON;
@@ -18,12 +15,12 @@ public class AssetSetter {
public void setObject() { public void setObject() {
int i = 0; int i = 0;
panel.obj[i] = new KeyObj(panel); panel.obj[i] = new HeartObj(panel);
panel.obj[i].worldX = panel.tileSize*25; panel.obj[i].worldX = panel.tileSize*25;
panel.obj[i].worldY = panel.tileSize*23; panel.obj[i].worldY = panel.tileSize*23;
i++; i++;
panel.obj[i] = new KeyObj(panel); panel.obj[i] = new CoinObj(panel);
panel.obj[i].worldX = panel.tileSize*21; panel.obj[i].worldX = panel.tileSize*21;
panel.obj[i].worldY = panel.tileSize*19; panel.obj[i].worldY = panel.tileSize*19;
i++; i++;

View File

@@ -72,6 +72,7 @@ public class Entity {
public int defenseValue; public int defenseValue;
public String description; public String description;
public int useCost; public int useCost;
public int value;
public Entity(GamePanel panel) { public Entity(GamePanel panel) {
this.panel = panel; this.panel = panel;

View File

@@ -48,6 +48,8 @@ public class Player extends Entity {
// DEFAULT // DEFAULT
public void update() { public void update() {
if(life > maxLife) life = maxLife;
if(attacking) { if(attacking) {
attacking(); attacking();
return; return;
@@ -124,7 +126,14 @@ public class Player extends Entity {
// INTERACTION // INTERACTION
public void pickObject(int index) { public void pickObject(int index) {
if(index == 999) return; if(index == 999 || panel.obj[index] == null) return;
// PICKUP ONLY
if(panel.obj[index].type == EntityType.PICKUP) {
panel.obj[index].use(this);
}
// INVENTORY ITEMS
else {
if(inventory.size() == maxInvSize) { if(inventory.size() == maxInvSize) {
panel.ui.addMessage("Your inventory is full!"); panel.ui.addMessage("Your inventory is full!");
return; return;
@@ -132,6 +141,7 @@ public class Player extends Entity {
inventory.add(panel.obj[index]); inventory.add(panel.obj[index]);
panel.playSE(1); 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; panel.obj[index] = null;
} }

View File

@@ -0,0 +1,26 @@
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 CoinObj extends Entity {
GamePanel panel;
public CoinObj(GamePanel panel) {
super(panel);
this.panel = panel;
name = "Coin";
type = EntityType.PICKUP;
value = 1;
down1 = initEntitySprites("/objects/coin_bronze");
}
public void use(Entity entity) {
panel.playSE(1);
panel.ui.addMessage("Coin +"+value);
panel.player.coins += value;
}
}

View File

@@ -6,13 +6,26 @@ import de.miaurizius.jgame2d.entity.Entity;
public class HeartObj extends Entity { public class HeartObj extends Entity {
GamePanel panel;
public HeartObj(GamePanel panel) { public HeartObj(GamePanel panel) {
super(panel); super(panel);
name = "Heart Container"; this.panel = panel;
type = EntityType.ITEM;
name = "Heart";
type = EntityType.PICKUP;
value = 2;
down1 = initEntitySprites("objects/heart_full");
image = initEntitySprites("objects/heart_full"); image = initEntitySprites("objects/heart_full");
image2 = initEntitySprites("objects/heart_half"); image2 = initEntitySprites("objects/heart_half");
image3 = initEntitySprites("objects/heart_blank"); image3 = initEntitySprites("objects/heart_blank");
} }
public void use(Entity entity) {
panel.playSE(2);
panel.ui.addMessage("Life +"+value);
entity.life += value;
}
} }