refactor dialogue handling for various game entities
This commit is contained in:
@@ -294,6 +294,19 @@ public class UI {
|
||||
x += panel.tileSize;
|
||||
y += panel.tileSize;
|
||||
|
||||
if(tradingNPC.dialogue[tradingNPC.dialogueSet][tradingNPC.dialogueIndex] != null) {
|
||||
currentDialogue = tradingNPC.dialogue[tradingNPC.dialogueSet][tradingNPC.dialogueIndex];
|
||||
if(panel.keyH.spacePressed) {
|
||||
if(panel.gameState == GameState.DIALOGUE) {
|
||||
tradingNPC.dialogueIndex++;
|
||||
panel.keyH.spacePressed = false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
tradingNPC.dialogueIndex = 0;
|
||||
if(panel.gameState == GameState.DIALOGUE) panel.gameState = GameState.PLAY;
|
||||
}
|
||||
|
||||
for(String line : currentDialogue.split("\n")) {
|
||||
graphics2d.drawString(line, x, y);
|
||||
y += 40;
|
||||
@@ -419,6 +432,7 @@ public class UI {
|
||||
|
||||
// TRADING
|
||||
private void tradeSelect() {
|
||||
tradingNPC.dialogueSet = 0;
|
||||
drawDialogueScreen();
|
||||
|
||||
int x = panel.tileSize*15;
|
||||
@@ -449,9 +463,8 @@ public class UI {
|
||||
graphics2d.drawString(">", x-panel.tileSize/2, y);
|
||||
if(panel.keyH.spacePressed) {
|
||||
commandNum = 0;
|
||||
tradingNPC.startDialogue(tradingNPC,1);
|
||||
tradeState = TradeState.SELECT;
|
||||
panel.gameState = GameState.DIALOGUE;
|
||||
currentDialogue = "Come again, hehe!";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -488,9 +501,7 @@ public class UI {
|
||||
if(!panel.keyH.spacePressed) return;
|
||||
if(tradingNPC.inventory.get(itemIndex).price > panel.player.coins) {
|
||||
tradeState = TradeState.SELECT;
|
||||
panel.gameState = GameState.DIALOGUE;
|
||||
currentDialogue = "You need more coins to buy that!";
|
||||
drawDialogueScreen();
|
||||
tradingNPC.startDialogue(tradingNPC,2);
|
||||
return;
|
||||
}
|
||||
if(panel.player.canObtainItem(tradingNPC.inventory.get(itemIndex))) {
|
||||
@@ -498,9 +509,7 @@ public class UI {
|
||||
return;
|
||||
}
|
||||
tradeState = TradeState.SELECT;
|
||||
panel.gameState = GameState.DIALOGUE;
|
||||
currentDialogue = "Your inventory is full!";
|
||||
drawDialogueScreen();
|
||||
tradingNPC.startDialogue(tradingNPC,3);
|
||||
}
|
||||
}
|
||||
private void tradeSell() {
|
||||
@@ -537,8 +546,7 @@ public class UI {
|
||||
if(panel.player.inventory.get(itemIndex) == panel.player.currentWeapon || panel.player.inventory.get(itemIndex) == panel.player.currentShield) {
|
||||
commandNum = 0;
|
||||
tradeState = TradeState.SELECT;
|
||||
panel.gameState = GameState.DIALOGUE;
|
||||
currentDialogue = "You cannot sell an equipped item!";
|
||||
tradingNPC.startDialogue(tradingNPC,4);
|
||||
return;
|
||||
}
|
||||
if(panel.player.inventory.get(itemIndex).amt <= 1) panel.player.inventory.remove(itemIndex);
|
||||
|
||||
@@ -14,6 +14,7 @@ public class EventHandler {
|
||||
|
||||
GamePanel panel;
|
||||
EventRect[][][] eventRect;
|
||||
Entity eventMaster; //Like a "voice of god"
|
||||
public int prevEventX, prevEventY;
|
||||
boolean canTouchEvent = true;
|
||||
|
||||
@@ -23,6 +24,7 @@ public class EventHandler {
|
||||
|
||||
public EventHandler(GamePanel panel) {
|
||||
this.panel = panel;
|
||||
eventMaster = new Entity(panel);
|
||||
eventRect = new EventRect[Map.values().length][panel.maxWorldCol][panel.maxWorldRow];
|
||||
|
||||
int map = 0;
|
||||
@@ -46,8 +48,14 @@ public class EventHandler {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
setDialogue();
|
||||
}
|
||||
|
||||
public void setDialogue() {
|
||||
eventMaster.dialogue[0][0] = "You have fallen into a pit!";
|
||||
eventMaster.dialogue[1][0] = "You saved your progress!";
|
||||
}
|
||||
public void checkEvent() {
|
||||
// Check if the player char is more than 1 tile away from the last event
|
||||
int xDistance = Math.abs(panel.player.worldX - prevEventX); //return absolute number
|
||||
@@ -95,10 +103,9 @@ public class EventHandler {
|
||||
}
|
||||
|
||||
private void damagePit() {
|
||||
panel.gameState = GameState.DIALOGUE;
|
||||
panel.playSE(6);
|
||||
panel.ui.currentDialogue = "You have fallen into a pit!";
|
||||
panel.player.life -= 1;
|
||||
eventMaster.startDialogue(eventMaster,0);
|
||||
canTouchEvent = false;
|
||||
}
|
||||
private void healingPool() {
|
||||
@@ -106,7 +113,7 @@ public class EventHandler {
|
||||
panel.gameState = GameState.DIALOGUE;
|
||||
panel.player.attackCancel = true;
|
||||
panel.playSE(2);
|
||||
panel.ui.currentDialogue = "You saved your progress!";
|
||||
eventMaster.startDialogue(eventMaster,1);
|
||||
//panel.player.life = panel.player.maxLife;
|
||||
canTouchEvent = false;
|
||||
panel.assetSetter.setMonster();
|
||||
|
||||
@@ -134,7 +134,7 @@ public class KeyHandler implements KeyListener {
|
||||
private void handleDialogue(int code) {
|
||||
// EXIT STATE
|
||||
if (code == KeyEvent.VK_SPACE) {
|
||||
panel.gameState = GameState.PLAY;
|
||||
spacePressed = true;
|
||||
}
|
||||
}
|
||||
private void handleCharacter(int code) {
|
||||
|
||||
@@ -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 de.miaurizius.jgame2d.entity.particle.Particle;
|
||||
import de.miaurizius.jgame2d.entity.projectile.Projectile;
|
||||
|
||||
@@ -30,13 +31,14 @@ public class Entity {
|
||||
public Entity attacker;
|
||||
public int solidAreaDefaultX, solidAreaDefaultY;
|
||||
public boolean collision;
|
||||
protected String[] dialogue = new String[20];
|
||||
public String[][] dialogue = new String[20][20];
|
||||
|
||||
// STATE
|
||||
public int worldX, worldY;
|
||||
public Direction direction = Direction.DOWN;
|
||||
public int spriteNum = 1;
|
||||
int dialogueIndex;
|
||||
public int dialogueSet;
|
||||
public int dialogueIndex;
|
||||
public boolean collisionOn;
|
||||
public boolean invincible;
|
||||
public boolean transparent;
|
||||
@@ -267,10 +269,9 @@ public class Entity {
|
||||
panel.player.invincible = true;
|
||||
}
|
||||
public void speak() {
|
||||
if(dialogue[dialogueIndex] == null) dialogueIndex = 0;
|
||||
panel.ui.currentDialogue = dialogue[dialogueIndex];
|
||||
dialogueIndex++;
|
||||
|
||||
}
|
||||
public void facePlayer() {
|
||||
switch(panel.player.direction) {
|
||||
case UP -> direction = Direction.DOWN;
|
||||
case DOWN -> direction = Direction.UP;
|
||||
@@ -278,6 +279,11 @@ public class Entity {
|
||||
case RIGHT -> direction = Direction.LEFT;
|
||||
}
|
||||
}
|
||||
public void startDialogue(Entity entity, int setNum) {
|
||||
panel.gameState = GameState.DIALOGUE;
|
||||
panel.ui.tradingNPC = entity;
|
||||
dialogueSet = setNum;
|
||||
}
|
||||
public void dyingAnimation(Graphics2D graphics2d) {
|
||||
dyingCount++;
|
||||
int incr = 5;
|
||||
|
||||
@@ -46,6 +46,7 @@ public class Player extends Entity {
|
||||
getPlayerImage();
|
||||
getPlayerAttackImage();
|
||||
getGuardImages();
|
||||
setDialogue();
|
||||
}
|
||||
|
||||
// DEFAULT
|
||||
@@ -262,7 +263,6 @@ public class Player extends Entity {
|
||||
if(index == 999) return;
|
||||
//if(!panel.keyH.spacePressed) return; //Only uncomment if text should only appear if player hits space
|
||||
attackCancel = true;
|
||||
panel.gameState = GameState.DIALOGUE;
|
||||
panel.npc[panel.currentMap.getIndex()][index].speak();
|
||||
}
|
||||
public void speak() {
|
||||
@@ -281,9 +281,7 @@ public class Player extends Entity {
|
||||
attack = getAttack();
|
||||
defense = getDefense();
|
||||
panel.playSE(8);
|
||||
|
||||
panel.gameState = GameState.DIALOGUE;
|
||||
panel.ui.currentDialogue = "You are level " + level + " now!\nYou feel stronger!";
|
||||
startDialogue(this,0);
|
||||
}
|
||||
public void selectItem() {
|
||||
int itemIndex = panel.ui.getItemIndex(panel.ui.playerSlotCol, panel.ui.playerSlotRow);
|
||||
@@ -363,6 +361,9 @@ public class Player extends Entity {
|
||||
inventory.add(currentShield);
|
||||
inventory.add(new KeyObj(panel));
|
||||
}
|
||||
public void setDialogue() {
|
||||
dialogue[0][0] = "You are level " + level + " now!\nYou feel stronger!";
|
||||
}
|
||||
public int getAttack() {
|
||||
attackArea = currentWeapon.attackArea;
|
||||
return attack = strength * currentWeapon.attackValue;
|
||||
|
||||
@@ -17,19 +17,25 @@ public class KeyObj extends Entity {
|
||||
|
||||
price = 50;
|
||||
stackable = true;
|
||||
|
||||
setDialogue();
|
||||
}
|
||||
|
||||
public void setDialogue() {
|
||||
dialogue[0][0] = "What are you doing?\nThere is no door nearby.";
|
||||
dialogue[1][0] = "You used a " + name + "!\nThe door is now open.";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean 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.";
|
||||
startDialogue(this,0);
|
||||
return false;
|
||||
}
|
||||
panel.ui.currentDialogue = "You used a " + name + "!\nThe door is now open.";
|
||||
panel.playSE(3);
|
||||
panel.obj[panel.currentMap.getIndex()][objIndex] = null;
|
||||
startDialogue(this,1);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -22,12 +22,16 @@ public class PotionObj extends Entity {
|
||||
|
||||
price = 50;
|
||||
stackable = true;
|
||||
|
||||
setDialogue();
|
||||
}
|
||||
|
||||
public void setDialogue() {
|
||||
dialogue[0][0] = "You drank a " + name + "!\nYour life has been recovered by " + value + ".";
|
||||
}
|
||||
|
||||
public boolean use(Entity entity) {
|
||||
panel.gameState = GameState.DIALOGUE;
|
||||
panel.ui.currentDialogue = "You drank a " + name + "!\n" +
|
||||
"Your life has been recovered by " + value + ".";
|
||||
startDialogue(this,0);
|
||||
entity.life += value;
|
||||
if(panel.player.life > panel.player.maxLife) panel.player.life = panel.player.maxLife;
|
||||
panel.playSE(2);
|
||||
|
||||
@@ -23,7 +23,7 @@ public class MerchantNPC extends Entity {
|
||||
|
||||
@Override
|
||||
public void speak() {
|
||||
super.speak();
|
||||
facePlayer();
|
||||
panel.gameState = GameState.TRADE;
|
||||
panel.ui.tradingNPC = this;
|
||||
}
|
||||
@@ -40,7 +40,11 @@ public class MerchantNPC extends Entity {
|
||||
right2 = initEntitySprites("npc/merchant_down_2");
|
||||
}
|
||||
private void setDialogue() {
|
||||
dialogue[0] = "He he, so you found me. \nI have some good stuff. \nDo you want to trade?";
|
||||
dialogue[0][0] = "He he, so you found me. \nI have some good stuff. \nDo you want to trade?";
|
||||
dialogue[1][0] = "Come again, hehe!";
|
||||
dialogue[2][0] = "You need more coins to buy that!";
|
||||
dialogue[3][0] = "Your inventory is full!";
|
||||
dialogue[4][0] = "You cannot sell an equipped item!";
|
||||
}
|
||||
private void setItems() {
|
||||
inventory.add(new PotionObj(panel));
|
||||
|
||||
@@ -48,8 +48,9 @@ public class OldManNPC extends Entity {
|
||||
}
|
||||
@Override
|
||||
public void speak() {
|
||||
super.speak();
|
||||
super.onPath = true;
|
||||
super.facePlayer();
|
||||
super.startDialogue(this,super.dialogueSet);
|
||||
// super.onPath = true;
|
||||
}
|
||||
|
||||
// SETTING THINGS UP
|
||||
@@ -64,10 +65,16 @@ public class OldManNPC extends Entity {
|
||||
right2 = initEntitySprites("npc/oldman_right_2");
|
||||
}
|
||||
private void setDialogue() {
|
||||
dialogue[0] = "Hello, lad.";
|
||||
dialogue[1] = "So you've come to this island to \nfind the treasure?";
|
||||
dialogue[2] = "I used to be a great wizard but now... \nI'm a bit too old for taking an \nadventure.";
|
||||
dialogue[3] = "Well, good luck on you.";
|
||||
dialogue[4] = "I heard drinking the water of the \nholy lake makes you feel fine again...";
|
||||
dialogue[0][0] = "Hello, lad.";
|
||||
dialogue[0][1] = "So you've come to this island to \nfind the treasure?";
|
||||
dialogue[0][2] = "I used to be a great wizard but now... \nI'm a bit too old for taking an \nadventure.";
|
||||
dialogue[0][3] = "Well, good luck on you.";
|
||||
dialogue[0][4] = "I heard drinking the water of the \nholy lake makes you feel fine again...";
|
||||
|
||||
dialogue[1][0] = "If you become tired, reset at the water.";
|
||||
dialogue[1][1] = "However, the monsters reappear if you rest.\nI don't know why but that's how it works.";
|
||||
dialogue[1][2] = "In any case, don't push yourself too hard.";
|
||||
|
||||
dialogue[2][0] = "I wonder how to open that door...";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,31 +28,32 @@ public class ChestObj extends Entity {
|
||||
solidAreaDefaultY = solidArea.y;
|
||||
}
|
||||
|
||||
public void setDialogue() {
|
||||
dialogue[0][0] = "You open the chest and find a " + loot.name + "!\nBut your inventory is full...";
|
||||
dialogue[1][0] = "You open the chest and find a " + loot.name + "!\nYou obtain the " + loot.name + "!";
|
||||
dialogue[2][0] = "It's already empty...";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLoot(Entity loot) {
|
||||
this.loot = loot;
|
||||
setDialogue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void interact() {
|
||||
panel.gameState = GameState.DIALOGUE;
|
||||
|
||||
if(this.opened) {
|
||||
panel.ui.currentDialogue = "It's already empty...";
|
||||
startDialogue(this,2);
|
||||
return;
|
||||
}
|
||||
panel.playSE(3);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("You open the chest and find a ").append(loot.name).append("!");
|
||||
if(!panel.player.canObtainItem(loot)) {
|
||||
sb.append("\nBut your inventory is full...");
|
||||
panel.ui.currentDialogue = sb.toString();
|
||||
startDialogue(this,0);
|
||||
return;
|
||||
}
|
||||
sb.append("\nYou obtain the ").append(loot.name).append("!");
|
||||
down1 = image2;
|
||||
opened = true;
|
||||
panel.ui.currentDialogue = sb.toString();
|
||||
startDialogue(this,1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -23,12 +23,17 @@ public class DoorObj extends Entity {
|
||||
solidArea.height = 32;
|
||||
solidAreaDefaultX = solidArea.x;
|
||||
solidAreaDefaultY = solidArea.y;
|
||||
|
||||
setDialogue();
|
||||
}
|
||||
|
||||
public void setDialogue() {
|
||||
dialogue[0][0] = "You need a key to open this.";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void interact() {
|
||||
panel.gameState = GameState.DIALOGUE;
|
||||
panel.ui.currentDialogue = "You need a key to open this.";
|
||||
startDialogue(this,0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user