made item buyable
This commit is contained in:
@@ -2,6 +2,7 @@ package de.miaurizius.jgame2d.core;
|
||||
|
||||
import de.miaurizius.jgame2d.core.enums.GameState;
|
||||
import de.miaurizius.jgame2d.entity.Entity;
|
||||
import de.miaurizius.jgame2d.entity.item.CoinObj;
|
||||
import de.miaurizius.jgame2d.entity.item.HeartObj;
|
||||
|
||||
import java.awt.*;
|
||||
@@ -16,15 +17,20 @@ public class UI {
|
||||
GamePanel panel;
|
||||
Graphics2D graphics2d;
|
||||
Font font;
|
||||
BufferedImage heart_full, heart_half, heart_blank;
|
||||
BufferedImage heart_full, heart_half, heart_blank, coin;
|
||||
ArrayList<String> messages = new ArrayList<>();
|
||||
ArrayList<Integer> messageCounter = new ArrayList<>();
|
||||
public OptionState optionState;
|
||||
public String currentDialogue;
|
||||
public Entity tradingNPC;
|
||||
public int commandNum;
|
||||
public int slotCol, slotRow;
|
||||
public int playerSlotCol, playerSlotRow;
|
||||
public int npcSlotCol, npcSlotRow;
|
||||
private int transCounter;
|
||||
|
||||
// SUB-STATES
|
||||
public TradeState tradeState = TradeState.SELECT;
|
||||
public OptionState optionState;
|
||||
|
||||
public UI(GamePanel panel) {
|
||||
this.panel = panel;
|
||||
font = new Font("Arial", Font.PLAIN, 40);
|
||||
@@ -39,6 +45,8 @@ public class UI {
|
||||
heart_full = heart.image;
|
||||
heart_half = heart.image2;
|
||||
heart_blank = heart.image3;
|
||||
Entity coin = new CoinObj(panel);
|
||||
this.coin = coin.down1;
|
||||
}
|
||||
|
||||
public void draw(Graphics2D graphics2d) {
|
||||
@@ -55,6 +63,7 @@ public class UI {
|
||||
case CHARACTER -> drawCharacterScreen();
|
||||
case GAMEOVER -> drawGameOverScreen();
|
||||
case TRANSITION -> drawTransitionScreen();
|
||||
case TRADE -> drawTradeScreen();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -152,12 +161,26 @@ public class UI {
|
||||
textY += panel.tileSize;
|
||||
graphics2d.drawImage(panel.player.currentShield.down1, tailX - panel.tileSize, textY-14, null);
|
||||
}
|
||||
private void drawInventory() {
|
||||
private void drawInventory(Entity entity, boolean cursor) {
|
||||
int frameX, frameY, frameWidth, frameHeight;
|
||||
int slotCol, slotRow;
|
||||
|
||||
// DRAW FRAME
|
||||
int frameX = panel.tileSize*12;
|
||||
int frameY = panel.tileSize;
|
||||
int frameWidth = panel.tileSize*6;
|
||||
int frameHeight = panel.tileSize*5;
|
||||
if(entity == panel.player) {
|
||||
frameX = panel.tileSize*12;
|
||||
frameY = panel.tileSize;
|
||||
frameWidth = panel.tileSize*6;
|
||||
frameHeight = panel.tileSize*5;
|
||||
slotCol = playerSlotCol;
|
||||
slotRow = playerSlotRow;
|
||||
} else {
|
||||
frameX = panel.tileSize*2;
|
||||
frameY = panel.tileSize;
|
||||
frameWidth = panel.tileSize*6;
|
||||
frameHeight = panel.tileSize*5;
|
||||
slotCol = npcSlotCol;
|
||||
slotRow = npcSlotRow;
|
||||
}
|
||||
drawSubWindow(frameX, frameY, frameWidth, frameHeight);
|
||||
|
||||
// SLOT
|
||||
@@ -167,15 +190,15 @@ public class UI {
|
||||
int slotY = slotYStart;
|
||||
int slotSize = panel.tileSize+3;
|
||||
|
||||
// DRAW PLAYER ITEMS
|
||||
for(int i = 0; i < panel.player.inventory.size(); i++) {
|
||||
// DRAW ITEMS
|
||||
for(int i = 0; i < entity.inventory.size(); i++) {
|
||||
|
||||
// EQUIP CURSOR
|
||||
if(panel.player.inventory.get(i) == panel.player.currentWeapon || panel.player.inventory.get(i) == panel.player.currentShield) {
|
||||
if(entity.inventory.get(i) == entity.currentWeapon || entity.inventory.get(i) == entity.currentShield) {
|
||||
graphics2d.setColor(new Color(240, 190,90));
|
||||
graphics2d.fillRoundRect(slotX, slotY, panel.tileSize, panel.tileSize, 10, 10);
|
||||
}
|
||||
graphics2d.drawImage(panel.player.inventory.get(i).down1, slotX, slotY, null);
|
||||
graphics2d.drawImage(entity.inventory.get(i).down1, slotX, slotY, null);
|
||||
slotX += slotSize;
|
||||
if (i == 4 || i == 9 || i == 14) {
|
||||
slotX = slotXStart;
|
||||
@@ -184,17 +207,17 @@ public class UI {
|
||||
}
|
||||
|
||||
// CURSOR
|
||||
if(!cursor) return;
|
||||
int curserX = slotXStart + (slotSize*slotCol);
|
||||
int curserY = slotYStart + (slotSize*slotRow);
|
||||
int curserHeight = panel.tileSize;
|
||||
int curserWidth = panel.tileSize;
|
||||
|
||||
graphics2d.setColor(Color.white);
|
||||
graphics2d.setStroke(new BasicStroke(3));
|
||||
graphics2d.drawRoundRect(curserX, curserY, curserWidth, curserHeight, 10, 10);
|
||||
|
||||
// DESCRIPTION FRAME
|
||||
int itemIndex = getItemIndex();
|
||||
int itemIndex = getItemIndex(slotCol, slotRow);
|
||||
int dFrameX = frameX;
|
||||
int dFrameY = frameY + frameHeight+2;
|
||||
int dFrameWidth = frameWidth;
|
||||
@@ -204,9 +227,9 @@ public class UI {
|
||||
int textX = dFrameX + 20;
|
||||
int textY = dFrameY + panel.tileSize;
|
||||
graphics2d.setFont(graphics2d.getFont().deriveFont(28F));
|
||||
if(itemIndex < panel.player.inventory.size()) {
|
||||
if(itemIndex < entity.inventory.size()) {
|
||||
drawSubWindow(dFrameX, dFrameY, dFrameWidth, dFrameHeight);
|
||||
for(String line : panel.player.inventory.get(itemIndex).description.split("\n")) {
|
||||
for(String line : entity.inventory.get(itemIndex).description.split("\n")) {
|
||||
graphics2d.drawString(line, textX, textY);
|
||||
textY += 32;
|
||||
}
|
||||
@@ -291,7 +314,7 @@ public class UI {
|
||||
}
|
||||
private void drawCharacterScreen() {
|
||||
drawCharStats();
|
||||
drawInventory();
|
||||
drawInventory(panel.player, true);
|
||||
}
|
||||
private void drawPlayScreen() {
|
||||
drawPlayerLife();
|
||||
@@ -342,6 +365,105 @@ public class UI {
|
||||
panel.eventH.prevEventX = panel.player.worldX;
|
||||
panel.eventH.prevEventY = panel.player.worldY;
|
||||
}
|
||||
private void drawTradeScreen() {
|
||||
switch(tradeState) {
|
||||
case SELECT -> tradeSelect();
|
||||
case BUY -> tradeBuy();
|
||||
case SELL -> tradeSell();
|
||||
}
|
||||
panel.keyH.spacePressed = false;
|
||||
}
|
||||
|
||||
// TRADING
|
||||
private void tradeSelect() {
|
||||
drawDialogueScreen();
|
||||
|
||||
int x = panel.tileSize*15;
|
||||
int y = panel.tileSize*4;
|
||||
int width = panel.tileSize*3;
|
||||
int height = (int)(panel.tileSize*3.5);
|
||||
drawSubWindow(x, y, width, height);
|
||||
|
||||
// DRAW TEXTS
|
||||
x += panel.tileSize;
|
||||
y += panel.tileSize;
|
||||
graphics2d.drawString("Buy",x , y);
|
||||
if(commandNum == 0) {
|
||||
graphics2d.drawString(">", x-panel.tileSize/2, y);
|
||||
if(panel.keyH.spacePressed) tradeState = TradeState.BUY;
|
||||
}
|
||||
y += panel.tileSize;
|
||||
|
||||
graphics2d.drawString("Sell",x , y);
|
||||
if(commandNum == 1) {
|
||||
graphics2d.drawString(">", x-panel.tileSize/2, y);
|
||||
if(panel.keyH.spacePressed) tradeState = TradeState.SELL;
|
||||
}
|
||||
y += panel.tileSize;
|
||||
|
||||
graphics2d.drawString("Leave",x , y);
|
||||
if(commandNum == 2) {
|
||||
graphics2d.drawString(">", x-panel.tileSize/2, y);
|
||||
if(panel.keyH.spacePressed) {
|
||||
commandNum = 0;
|
||||
tradeState = TradeState.SELECT;
|
||||
panel.gameState = GameState.DIALOGUE;
|
||||
currentDialogue = "Come again, hehe!";
|
||||
}
|
||||
}
|
||||
}
|
||||
private void tradeBuy() {
|
||||
drawInventory(panel.player, false);
|
||||
drawInventory(tradingNPC, true);
|
||||
|
||||
// HINT WINDOW
|
||||
int x = panel.tileSize*2;
|
||||
int y = panel.tileSize*9;
|
||||
int width = panel.tileSize*6;
|
||||
int height = panel.tileSize*2;
|
||||
drawSubWindow(x, y, width, height);
|
||||
graphics2d.drawString("[ESC] Back", x+panel.tileSize, y+60);
|
||||
|
||||
// PLAYER COINS
|
||||
x = panel.tileSize*12;
|
||||
drawSubWindow(x, y, width, height);
|
||||
graphics2d.drawString("Your Coins: " + panel.player.coins, x+panel.tileSize, y+60);
|
||||
|
||||
// PRICE WINDOW
|
||||
int itemIndex = getItemIndex(npcSlotCol, npcSlotRow);
|
||||
if(itemIndex < tradingNPC.inventory.size()) {
|
||||
x = (int)(panel.tileSize*5.5);
|
||||
y = (int)(panel.tileSize*5.5);
|
||||
width = (int)(panel.tileSize*2.5);
|
||||
height = panel.tileSize;
|
||||
drawSubWindow(x, y, width, height);
|
||||
graphics2d.drawImage(coin, x+10, y+8, 32, 32, null);
|
||||
String price = String.valueOf(tradingNPC.inventory.get(itemIndex).price);
|
||||
graphics2d.drawString(price, getAlignedToRightX(price, panel.tileSize*8-20), y+34);
|
||||
|
||||
// BUY
|
||||
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();
|
||||
return;
|
||||
}
|
||||
if(panel.player.inventory.size() == panel.player.maxInvSize) {
|
||||
tradeState = TradeState.SELECT;
|
||||
panel.gameState = GameState.DIALOGUE;
|
||||
currentDialogue = "Your inventory is full!";
|
||||
drawDialogueScreen();
|
||||
return;
|
||||
}
|
||||
panel.player.coins -= tradingNPC.inventory.get(itemIndex).price;
|
||||
panel.player.inventory.add(tradingNPC.inventory.get(itemIndex));
|
||||
}
|
||||
}
|
||||
private void tradeSell() {
|
||||
|
||||
}
|
||||
|
||||
// UTILITY
|
||||
private void drawSubWindow(int x, int y, int width, int height) {
|
||||
@@ -357,8 +479,8 @@ public class UI {
|
||||
private int getAlignedToRightX(String text, int tailX) {
|
||||
return tailX - (int) graphics2d.getFontMetrics().getStringBounds(text, graphics2d).getWidth();
|
||||
}
|
||||
public int getItemIndex() {
|
||||
return slotCol + slotRow*5;
|
||||
public int getItemIndex(int slotCol, int slotRow) {
|
||||
return slotCol + slotRow *5;
|
||||
}
|
||||
public void addMessage(String text) {
|
||||
messages.add(text);
|
||||
@@ -541,5 +663,9 @@ public class UI {
|
||||
CONTROLS,
|
||||
QUITNF;
|
||||
}
|
||||
|
||||
public enum TradeState {
|
||||
SELECT,
|
||||
BUY,
|
||||
SELL;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user