Compare commits

..

2 Commits

Author SHA1 Message Date
825b084a68 obstacles and chest loot 2025-12-13 00:44:53 +01:00
1d6f53a70e added knockback to monsters 2025-12-12 23:45:57 +01:00
17 changed files with 241 additions and 74 deletions

View File

@@ -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

View File

@@ -344,13 +344,13 @@ public class UI {
x = getCenteredX(text);
y += panel.tileSize*4;
graphics2d.drawString(text, x, y);
if(commandNum == 0) graphics2d.drawString(">", x-panel.tileSize, y);
if(commandNum == 0) graphics2d.drawString(">", x-panel.tileSize, y);
text = "Return to title";
x = getCenteredX(text);
y += 55;
graphics2d.drawString(text, x, y);
if(commandNum == 1) graphics2d.drawString(">", x-panel.tileSize, y);
if(commandNum == 1) graphics2d.drawString(">", x-panel.tileSize, y);
}
private void drawTransitionScreen() {
transCounter++;

View File

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

View File

@@ -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++;
}

View File

@@ -151,6 +151,7 @@ public class KeyHandler implements KeyListener {
}
if(panel.ui.commandNum == 1) {
panel.gameState = GameState.TITLE;
panel.ui.commandNum = 0;
panel.restart();
panel.stopMusic();
}

View File

@@ -41,16 +41,19 @@ public class Entity {
public boolean hpBarOn;
public boolean consumable;
public boolean onPath;
public boolean knockback;
// COUNTER
public int spriteCounter;
public int spriteCount;
public int actionLock;
public int invincibleCount;
int dyingCount;
int hpBarCounter;
int hpBarCount;
int knockbackCount;
// CHARACTER ATTRIBUTES
public EntityType type;
public int defaultSpeed;
public String name;
public int speed;
public int maxLife;
@@ -79,6 +82,7 @@ public class Entity {
public int useCost;
public int value;
public int price;
public int knockbackVal;
public Entity(GamePanel panel) {
this.panel = panel;
@@ -86,24 +90,48 @@ public class Entity {
// DEFAULT
public void update() {
setAction();
checkCollision();
if(knockback) {
checkCollision();
if(collisionOn) {
knockbackCount = 0;
knockback = false;
speed = defaultSpeed;
return;
}
if(!collisionOn) {
switch (direction) {
switch(panel.player.direction) {
case UP -> worldY -= speed;
case DOWN -> worldY += speed;
case LEFT ->worldX -= speed;
case RIGHT -> worldX += speed;
}
knockbackCount++;
if(knockbackCount != 10) return;
knockback = false;
knockbackCount = 0;
speed = defaultSpeed;
} else {
setAction();
checkCollision();
if(!collisionOn) {
switch (direction) {
case UP -> worldY -= speed;
case DOWN -> worldY += speed;
case LEFT ->worldX -= speed;
case RIGHT -> worldX += speed;
}
}
}
spriteCounter++;
if(spriteCounter > 24) {
spriteCount++;
if(spriteCount > 24) {
if(spriteNum == 1) spriteNum = 2;
else if(spriteNum == 2) spriteNum = 1;
else spriteNum = 0;
spriteCounter = 0;
spriteCount = 0;
}
// INVINCIBLE COUNTER
@@ -132,9 +160,9 @@ public class Entity {
graphics2d.setColor(new Color(255, 0, 30));
graphics2d.fillRect(screenX, screenY-5, (int) ((double) panel.tileSize/maxLife)*life, 10);
hpBarCounter++;
if(hpBarCounter > 600) { //bar disappears after 10 seconds
hpBarCounter = 0;
hpBarCount++;
if(hpBarCount > 600) { //bar disappears after 10 seconds
hpBarCount = 0;
hpBarOn = false;
}
}
@@ -142,7 +170,7 @@ public class Entity {
// DRAW ENTITY
if(invincible) {
hpBarOn = true;
hpBarCounter = 0;
hpBarCount = 0;
changeOpacity(graphics2d, 0.4f);
}
if(dying) dyingAnimation(graphics2d);
@@ -202,6 +230,7 @@ public class Entity {
}
}
public void use(Entity entity) {
} //If entity is consumable
public void checkDrop() {
@@ -227,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() {
@@ -257,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) {

View File

@@ -4,8 +4,9 @@ import de.miaurizius.jgame2d.core.*;
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.enums.Map;
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;
@@ -95,18 +96,18 @@ public class Player extends Entity {
if(keyH.spacePressed && !attackCancel) {
panel.playSE(7);
attacking = true;
spriteCounter = 0;
spriteCount = 0;
}
attackCancel = false;
panel.keyH.spacePressed = false;
spriteCounter++;
if(spriteCounter > 12) {
spriteCount++;
if(spriteCount > 12) {
if(spriteNum == 1) spriteNum = 2;
else if(spriteNum == 2) spriteNum = 1;
else spriteNum = 0;
spriteCounter = 0;
spriteCount = 0;
}
}
@@ -136,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 {
@@ -168,9 +178,9 @@ public class Player extends Entity {
}
public void attacking() {
if(attackCancel) return;
spriteCounter++;
if(spriteCounter <= 5) spriteNum = 1;
if(spriteCounter > 5 && spriteCounter <= 25) {
spriteCount++;
if(spriteCount <= 5) spriteNum = 1;
if(spriteCount > 5 && spriteCount <= 25) {
spriteNum = 2;
int currentWorldX = worldX;
int currentWorldY = worldY;
@@ -187,7 +197,7 @@ public class Player extends Entity {
solidArea.height = attackArea.height;
int monsterIndex = panel.collisionH.checkEntity(this, panel.monster[panel.currentMap.getIndex()]);
damageMonster(monsterIndex, attack);
damageMonster(monsterIndex, attack, currentWeapon.knockbackVal);
int iTileIndex = panel.collisionH.checkEntity(this, panel.iTile[panel.currentMap.getIndex()]);
interactTile(iTileIndex);
@@ -198,13 +208,13 @@ public class Player extends Entity {
solidArea.height = solidAreaHeight;
}
if(spriteCounter > 25) {
if(spriteCount > 25) {
spriteNum = 1;
spriteCounter = 0;
spriteCount = 0;
attacking = false;
}
}
public void damageMonster(int index, int attack) {
public void damageMonster(int index, int attack, int knockbackVal) {
if(index == 999) return;
if(panel.monster[panel.currentMap.getIndex()][index].invincible) return;
@@ -212,6 +222,7 @@ public class Player extends Entity {
if(damage > 0) {
panel.playSE(5);
if(knockbackVal > 0) knockback(panel.monster[panel.currentMap.getIndex()][index], knockbackVal);
panel.monster[panel.currentMap.getIndex()][index].life -= damage;
panel.monster[panel.currentMap.getIndex()][index].invincible = true;
}
@@ -224,6 +235,12 @@ public class Player extends Entity {
checkLevelUp();
}
}
public void knockback(Entity entity, int knockbackVal) {
entity.direction = direction;
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;
@@ -276,7 +293,6 @@ public class Player extends Entity {
}
if(selectedItem.consumable) {
selectedItem.use(this);
inventory.remove(itemIndex);
}
}
public void setDefaultPositions() {
@@ -294,11 +310,8 @@ public class Player extends Entity {
worldX = panel.tileSize * 23;
worldY = panel.tileSize * 21;
// worldX = panel.tileSize * 12;
// worldY = panel.tileSize * 12;
// panel.currentMap = Map.HUT;
speed = 4;
defaultSpeed = 4;
speed = defaultSpeed;
direction = Direction.DOWN;
// PLAYER STATUS (1 heart = 2 lives)

View File

@@ -14,6 +14,7 @@ public class AxeObj extends Entity {
weaponType = EntityType.WeaponType.AXE;
down1 = initEntitySprites("/objects/axe");
price = 50;
knockbackVal = 10;
attackValue = 2;
attackArea.width = 30;

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -15,6 +15,7 @@ public class SwordNormalObj extends Entity {
weaponType = EntityType.WeaponType.SWORD;
down1 = initEntitySprites("objects/sword_normal");
price = 50;
knockbackVal = 2;
attackValue = 1;
attackArea.width = 36;

View File

@@ -17,7 +17,8 @@ public class GreenSlimeMON extends Entity {
super(panel);
type = EntityType.MONSTER;
name = "green-slime";
speed = 1;
defaultSpeed = 1;
speed = defaultSpeed;
maxLife = 4;
life = maxLife;
attack = 5;

View File

@@ -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();
}
}

View File

@@ -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.";
}
}

View File

@@ -8,6 +8,7 @@ import de.miaurizius.jgame2d.entity.Entity;
public class Projectile extends Entity {
Entity user;
//TODO: parry projectiles: https://youtu.be/8KitXuxcuqI?list=PL_QPQmz5C6WUF-pOQDsbsKbaBZqXj4qSq&t=75
public Projectile(GamePanel panel) {
super(panel);
@@ -27,7 +28,7 @@ public class Projectile extends Entity {
if(user.type == EntityType.PLAYER) {
int monsterIndex = panel.collisionH.checkEntity(this, panel.monster[panel.currentMap.getIndex()]);
if(monsterIndex != 999) {
panel.player.damageMonster(monsterIndex, attack);
panel.player.damageMonster(monsterIndex, attack, knockbackVal);
generateParticle(user.projectile, panel.monster[panel.currentMap.getIndex()][monsterIndex]);
alive = false;
}
@@ -54,12 +55,12 @@ public class Projectile extends Entity {
return;
}
spriteCounter++;
if(spriteCounter > 12) {
spriteCount++;
if(spriteCount > 12) {
if(spriteNum == 1) spriteNum = 2;
else if(spriteNum == 2) spriteNum = 1;
else spriteNum = 0;
spriteCounter = 0;
spriteCount = 0;
}
}

View File

@@ -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");
}
}