added some sample events

This commit is contained in:
2025-11-28 21:52:28 +01:00
parent 4c50f99e67
commit 7616b4daff
5 changed files with 68 additions and 7 deletions

View File

@@ -1,10 +1,7 @@
package de.miaurizius.jgame2d.core;
import de.miaurizius.jgame2d.core.enums.GameState;
import de.miaurizius.jgame2d.core.handlers.AssetSetter;
import de.miaurizius.jgame2d.core.handlers.CollisionHandler;
import de.miaurizius.jgame2d.core.handlers.KeyHandler;
import de.miaurizius.jgame2d.core.handlers.Sound;
import de.miaurizius.jgame2d.core.handlers.*;
import de.miaurizius.jgame2d.entity.Entity;
import de.miaurizius.jgame2d.entity.Player;
import de.miaurizius.jgame2d.object.SuperObject;
@@ -43,6 +40,7 @@ public class GamePanel extends JPanel implements Runnable {
public CollisionHandler collisionH = new CollisionHandler(this);
public AssetSetter assetSetter = new AssetSetter(this);
public UI ui = new UI(this);
public EventHandler eventH = new EventHandler(this);
Thread gameThread;
// ENTITY AND OBJECT

View File

@@ -5,6 +5,6 @@ public enum Direction {
UP,
DOWN,
LEFT,
RIGHT;
RIGHT
}

View File

@@ -0,0 +1,59 @@
package de.miaurizius.jgame2d.core.handlers;
import de.miaurizius.jgame2d.core.GamePanel;
import de.miaurizius.jgame2d.core.enums.Direction;
import de.miaurizius.jgame2d.core.enums.GameState;
import java.awt.*;
public class EventHandler {
GamePanel panel;
Rectangle eventRect;
int eventRectDefaultX, eventRectDefaultY;
public EventHandler(GamePanel panel) {
this.panel = panel;
eventRect = new Rectangle();
eventRect.x = 23;
eventRect.y = 23;
eventRect.width = 2;
eventRect.height = 2;
eventRectDefaultX = eventRect.x;
eventRectDefaultY = eventRect.y;
}
public void checkEvent() {
if(hit(27,16, Direction.RIGHT)) damagePit(GameState.DIALOGUE);
if(hit(23,12, Direction.UP)) healingPool(GameState.DIALOGUE);
}
public boolean hit(int eventCol, int eventRow, Direction reqDirection) {
boolean hit = false;
panel.player.solidArea.x = panel.player.worldX + panel.player.solidArea.x;
panel.player.solidArea.y = panel.player.worldY + panel.player.solidArea.y;
eventRect.x = eventCol*panel.tileSize + eventRect.x;
eventRect.y = eventRow*panel.tileSize + eventRect.y;
if(panel.player.solidArea.intersects(eventRect)) if(reqDirection == null || panel.player.direction == reqDirection) hit = true;
panel.player.solidArea.x = panel.player.solidAreaDefaultX;
panel.player.solidArea.y = panel.player.solidAreaDefaultY;
eventRect.x = eventRectDefaultX;
eventRect.y = eventRectDefaultY;
return hit;
}
public void damagePit(GameState gameState) {
panel.gameState = gameState;
panel.ui.currentDialogue = "You have fallen into a pit!";
panel.player.life -= 1;
}
public void healingPool(GameState gameState) {
if(!panel.keyH.spacePressed) return;
panel.gameState = gameState;
panel.ui.currentDialogue = "You drank the holy water.\nYour life has been recovered!";
panel.player.life = panel.player.maxLife;
}
}

View File

@@ -75,6 +75,11 @@ public class Player extends Entity {
int npcIndex = panel.collisionH.checkEntity(this, panel.npc);
interactNPC(npcIndex);
// CHECK EVENT
panel.eventH.checkEvent();
panel.keyH.spacePressed = false;
if(!collisionOn) {
switch (direction) {
case UP -> worldY -= speed;
@@ -103,7 +108,6 @@ public class Player extends Entity {
//if(!panel.keyH.spacePressed) return; //Only uncomment if text should only appear if player hits space
panel.gameState = GameState.DIALOGUE;
panel.npc[index].speak();
panel.keyH.spacePressed = false;
}
public void draw(Graphics2D graphics2d) {