From 07549cf2415d2ef1b16c38e118c3d516d93a193a Mon Sep 17 00:00:00 2001 From: "Maurice L." Date: Tue, 24 Mar 2026 13:34:52 +0100 Subject: [PATCH] refactor collision handling and improve game state reset functionality --- src/de/miaurizius/jgame2d/core/UI.java | 3 +-- .../core/handlers/CollisionHandler.java | 21 +++++++++++++++++-- .../jgame2d/core/handlers/KeyHandler.java | 1 + src/de/miaurizius/jgame2d/data/SaveLoad.java | 2 +- src/de/miaurizius/jgame2d/entity/Player.java | 1 - 5 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/de/miaurizius/jgame2d/core/UI.java b/src/de/miaurizius/jgame2d/core/UI.java index ef87473..ecc128e 100644 --- a/src/de/miaurizius/jgame2d/core/UI.java +++ b/src/de/miaurizius/jgame2d/core/UI.java @@ -29,7 +29,7 @@ public class UI { private int transCount; private int sleepCount; private int charIndex; - private String combinedText = ""; + private String combinedText; // SUB-STATES public TradeState tradeState = TradeState.SELECT; @@ -757,7 +757,6 @@ public class UI { if(commandNum == 1) { graphics2d.drawString(">", textX-25, textY); if(panel.keyH.spacePressed) optionState = OptionState.OVERVIEW; - panel.resetGame(true); } } diff --git a/src/de/miaurizius/jgame2d/core/handlers/CollisionHandler.java b/src/de/miaurizius/jgame2d/core/handlers/CollisionHandler.java index 785ddfc..d36ee75 100644 --- a/src/de/miaurizius/jgame2d/core/handlers/CollisionHandler.java +++ b/src/de/miaurizius/jgame2d/core/handlers/CollisionHandler.java @@ -59,6 +59,10 @@ public class CollisionHandler { } public int checkObject(Entity entity, boolean player) { int index = 999; + + Direction direction = entity.direction; + if(entity.knockback) direction = entity.knockbackDirection; + int c = -1; for(Entity obj : panel.obj[panel.currentMap.getIndex()]) { @@ -71,7 +75,7 @@ public class CollisionHandler { obj.solidArea.x += obj.worldX; obj.solidArea.y += obj.worldY; - parseSolidArea(entity); + parseSolidArea(entity, direction); if (entity.solidArea.intersects(obj.solidArea)) { if (obj.collision) entity.collisionOn = true; @@ -90,6 +94,10 @@ public class CollisionHandler { //NPC OR MONSTER COLLISION public int checkEntity(Entity entity, Entity[] target) { int index = 999; + + Direction direction = entity.direction; + if(entity.knockback) direction = entity.knockbackDirection; + int c = -1; for(Entity e : target) { @@ -102,7 +110,7 @@ public class CollisionHandler { e.solidArea.x += e.worldX; e.solidArea.y += e.worldY; - parseSolidArea(entity); + parseSolidArea(entity, direction); if (entity.solidArea.intersects(e.solidArea) && e != entity) { entity.collisionOn = true; @@ -149,4 +157,13 @@ public class CollisionHandler { } } + private void parseSolidArea(Entity entity, Direction direction) { + switch (direction) { + case UP -> entity.solidArea.y -= entity.speed; + case DOWN -> entity.solidArea.y += entity.speed; + case LEFT -> entity.solidArea.x -= entity.speed; + case RIGHT -> entity.solidArea.x += entity.speed; + } + } + } diff --git a/src/de/miaurizius/jgame2d/core/handlers/KeyHandler.java b/src/de/miaurizius/jgame2d/core/handlers/KeyHandler.java index 87da264..786519f 100644 --- a/src/de/miaurizius/jgame2d/core/handlers/KeyHandler.java +++ b/src/de/miaurizius/jgame2d/core/handlers/KeyHandler.java @@ -33,6 +33,7 @@ public class KeyHandler implements KeyListener { switch (panel.ui.commandNum) { case 0: panel.gameState = GameState.PLAY; + panel.resetGame(true); panel.playMusic(0); break; case 1: diff --git a/src/de/miaurizius/jgame2d/data/SaveLoad.java b/src/de/miaurizius/jgame2d/data/SaveLoad.java index 26fdd7d..b42e149 100644 --- a/src/de/miaurizius/jgame2d/data/SaveLoad.java +++ b/src/de/miaurizius/jgame2d/data/SaveLoad.java @@ -118,7 +118,7 @@ public class SaveLoad { panel.obj[mapNum][i] = getObject(ds.mapObjectNames[mapNum][i]); panel.obj[mapNum][i].worldX = ds.mapObjectWorldX[mapNum][i]; panel.obj[mapNum][i].worldY = ds.mapObjectWorldY[mapNum][i]; - if (ds.mapObjectLootNames[mapNum][i] != null) panel.obj[mapNum][i].loot = getObject(ds.mapObjectLootNames[mapNum][i]); + if (ds.mapObjectLootNames[mapNum][i] != null) panel.obj[mapNum][i].setLoot(getObject(ds.mapObjectLootNames[mapNum][i])); panel.obj[mapNum][i].opened = ds.mapObjectOpened[mapNum][i]; panel.obj[mapNum][i].setDialogue(); if(panel.obj[mapNum][i].opened) panel.obj[mapNum][i].down1 = panel.obj[mapNum][i].image2; diff --git a/src/de/miaurizius/jgame2d/entity/Player.java b/src/de/miaurizius/jgame2d/entity/Player.java index 3f7f677..fb1e77d 100644 --- a/src/de/miaurizius/jgame2d/entity/Player.java +++ b/src/de/miaurizius/jgame2d/entity/Player.java @@ -367,7 +367,6 @@ public class Player extends Entity { inventory.add(currentWeapon); inventory.add(currentShield); inventory.add(new KeyObj(panel)); - inventory.add(new LanternObj(panel)); } public void setDialogue() { dialogue[0][0] = "You are level " + level + " now!\nYou feel stronger!";