From de48b8401c7af98ae98404c462d5c6ef1ce6acf9 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Tue, 11 Sep 2018 15:15:31 +0900 Subject: [PATCH] WIP - implemented enough GUI to start testing --- .../TurnBasedMinecraft/client/BattleGui.java | 256 +++++++++++++++++- .../client/EntitySelectionButton.java | 19 ++ .../client/ItemSelectionButton.java | 38 +++ .../TurnBasedMinecraft/common/Battle.java | 20 +- .../common/networking/PacketBattleInfo.java | 1 + .../networking/PacketBattleMessage.java | 7 +- 6 files changed, 330 insertions(+), 11 deletions(-) create mode 100644 src/main/java/com/seodisparate/TurnBasedMinecraft/client/EntitySelectionButton.java create mode 100644 src/main/java/com/seodisparate/TurnBasedMinecraft/client/ItemSelectionButton.java diff --git a/src/main/java/com/seodisparate/TurnBasedMinecraft/client/BattleGui.java b/src/main/java/com/seodisparate/TurnBasedMinecraft/client/BattleGui.java index 995a712..472d339 100644 --- a/src/main/java/com/seodisparate/TurnBasedMinecraft/client/BattleGui.java +++ b/src/main/java/com/seodisparate/TurnBasedMinecraft/client/BattleGui.java @@ -1,10 +1,15 @@ package com.seodisparate.TurnBasedMinecraft.client; import java.io.IOException; +import java.util.HashMap; +import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; import com.seodisparate.TurnBasedMinecraft.TurnBasedMinecraftMod; import com.seodisparate.TurnBasedMinecraft.common.Battle; +import com.seodisparate.TurnBasedMinecraft.common.Combatant; +import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleDecision; +import com.seodisparate.TurnBasedMinecraft.common.networking.PacketHandler; import net.minecraft.client.gui.GuiButton; import net.minecraft.client.gui.GuiScreen; @@ -14,32 +19,201 @@ public class BattleGui extends GuiScreen public AtomicInteger timeRemaining; public long lastInstant; public long elapsedTime; + private MenuState state; + private boolean stateChanged; + private String info; + + private enum MenuState + { + MAIN_MENU(0), + ATTACK_TARGET(1), + ITEM_ACTION(2), + WAITING(3), + SWITCH_ITEM(4), + USE_ITEM(5); + + private int value; + + private MenuState(int value) + { + this.value = value; + } + + public int getValue() + { + return value; + } + + private static Map map; + + static + { + map = new HashMap(); + for(MenuState state : MenuState.values()) + { + map.put(state.getValue(), state); + } + } + + public static MenuState valueOf(int value) + { + return map.get(value); + } + } + + private enum ButtonAction + { + ATTACK(0), + DEFEND(1), + ITEM(2), + FLEE(3), + ATTACK_TARGET(4), + SWITCH_HELD_ITEM(5), + DECIDE_USE_ITEM(6), + CANCEL(7), + DO_ITEM_SWITCH(8), + DO_USE_ITEM(9); + + private int value; + + private ButtonAction(int value) + { + this.value = value; + } + + public int getValue() + { + return value; + } + + private static Map map; + static + { + map = new HashMap(); + for(ButtonAction action : ButtonAction.values()) + { + map.put(action.getValue(), action); + } + } + + public static ButtonAction valueOf(int value) + { + return map.get(value); + } + } public BattleGui() { timeRemaining = new AtomicInteger((int)TurnBasedMinecraftMod.BattleDecisionTime.getSeconds()); lastInstant = System.nanoTime(); elapsedTime = 0; + state = MenuState.MAIN_MENU; + stateChanged = true; + } + + private void setState(MenuState state) + { + this.state = state; + stateChanged = true; } public void turnBegin() { TurnBasedMinecraftMod.currentBattle.setState(Battle.State.ACTION); - // TODO reset gui since decisions ended + setState(MenuState.WAITING); } public void turnEnd() { TurnBasedMinecraftMod.currentBattle.setState(Battle.State.DECISION); timeRemaining.set((int)TurnBasedMinecraftMod.BattleDecisionTime.getSeconds()); + setState(MenuState.MAIN_MENU); + } + + public void battleChanged() + { + stateChanged = true; + } + + public void updateState() + { + if(!stateChanged) + { + return; + } + + stateChanged = false; + buttonList.clear(); + switch(state) + { + case MAIN_MENU: + info = "What will you do?"; + buttonList.add(new GuiButton(ButtonAction.ATTACK.getValue(), width*2/7 - 40, height - 120, 80, 20, "Attack")); + buttonList.add(new GuiButton(ButtonAction.DEFEND.getValue(), width*3/7 - 40, height - 120, 80, 20, "Defend")); + buttonList.add(new GuiButton(ButtonAction.ITEM.getValue(), width*4/7 - 40, height - 120, 80, 20, "Item")); + buttonList.add(new GuiButton(ButtonAction.FLEE.getValue(), width*5/7 - 40, height - 120, 80, 20, "Flee")); + break; + case ATTACK_TARGET: + info = "Who will you attack?"; + int y = 50; + for(Map.Entry e : TurnBasedMinecraftMod.currentBattle.getSideAEntrySet()) + { + if(e.getValue().entity != null) + { + buttonList.add(new EntitySelectionButton(ButtonAction.ATTACK_TARGET.getValue(), width/4 - 100, y, e.getValue().entity.getName(), e.getKey())); + } + else + { + buttonList.add(new EntitySelectionButton(ButtonAction.ATTACK_TARGET.getValue(), width/4 - 100, y, "Unknown", e.getKey())); + } + y += 20; + } + y = 50; + for(Map.Entry e : TurnBasedMinecraftMod.currentBattle.getSideBEntrySet()) + { + if(e.getValue().entity != null) + { + buttonList.add(new EntitySelectionButton(ButtonAction.ATTACK_TARGET.getValue(), width*3/4 - 100, y, e.getValue().entity.getName(), e.getKey())); + } + else + { + buttonList.add(new EntitySelectionButton(ButtonAction.ATTACK_TARGET.getValue(), width*3/4 - 100, y, "Unknown", e.getKey())); + } + y += 20; + } + buttonList.add(new GuiButton(ButtonAction.CANCEL.getValue(), width/2 - 40, height - 120, 80, 20, "Cancel")); + break; + case ITEM_ACTION: + info = "What will you do with an item?"; + buttonList.add(new GuiButton(ButtonAction.SWITCH_HELD_ITEM.getValue(), width*1/4 - 40, height - 120, 80, 20, "Switch Held")); + buttonList.add(new GuiButton(ButtonAction.DECIDE_USE_ITEM.getValue(), width*2/4 - 40, height - 120, 80, 20, "Use")); + buttonList.add(new GuiButton(ButtonAction.CANCEL.getValue(), width*3/4 - 40, height - 120, 80, 20, "Cancel")); + break; + case WAITING: + info = "Waiting..."; + break; + case SWITCH_ITEM: + info = "To which item will you switch to?"; + for(int i = 0; i < 9; ++i) + { + buttonList.add(new ItemSelectionButton(ButtonAction.DO_ITEM_SWITCH.getValue(), width/2 - 88 + i * 20, height - 19, 16, 16, "", i)); + } + buttonList.add(new GuiButton(ButtonAction.CANCEL.getValue(), width/2 - 40, height - 120, 80, 20, "Cancel")); + break; + case USE_ITEM: + info = "Which item will you use?"; + for(int i = 0; i < 9; ++i) + { + buttonList.add(new ItemSelectionButton(ButtonAction.DO_USE_ITEM.getValue(), width/2 - 88 + i * 20, height - 19, 16, 16, "", i)); + } + buttonList.add(new GuiButton(ButtonAction.CANCEL.getValue(), width/2 - 40, height - 120, 80, 20, "Cancel")); + break; + } } @Override public void drawScreen(int mouseX, int mouseY, float partialTicks) { - // TODO Auto-generated method stub - super.drawScreen(mouseX, mouseY, partialTicks); - if(TurnBasedMinecraftMod.currentBattle.getState() == Battle.State.DECISION && timeRemaining.get() > 0) { long nextInstant = System.nanoTime(); @@ -51,26 +225,86 @@ public class BattleGui extends GuiScreen timeRemaining.decrementAndGet(); } } + + updateState(); + + super.drawScreen(mouseX, mouseY, partialTicks); + + drawHoveringText(info, width / 2 - 50, height - 50); } @Override protected void actionPerformed(GuiButton button) throws IOException { - // TODO Auto-generated method stub - super.actionPerformed(button); + switch(ButtonAction.valueOf(button.id)) + { + case ATTACK: + setState(MenuState.ATTACK_TARGET); + break; + case DEFEND: + PacketHandler.INSTANCE.sendToServer(new PacketBattleDecision(TurnBasedMinecraftMod.currentBattle.getId(), Battle.Decision.DEFEND, 0)); + setState(MenuState.WAITING); + break; + case ITEM: + setState(MenuState.ITEM_ACTION); + break; + case FLEE: + PacketHandler.INSTANCE.sendToServer(new PacketBattleDecision(TurnBasedMinecraftMod.currentBattle.getId(), Battle.Decision.FLEE, 0)); + setState(MenuState.WAITING); + break; + case ATTACK_TARGET: + if(button instanceof EntitySelectionButton) + { + PacketHandler.INSTANCE.sendToServer(new PacketBattleDecision(TurnBasedMinecraftMod.currentBattle.getId(), Battle.Decision.ATTACK, ((EntitySelectionButton)button).entityID)); + setState(MenuState.WAITING); + } + else + { + setState(MenuState.MAIN_MENU); + } + break; + case SWITCH_HELD_ITEM: + setState(MenuState.SWITCH_ITEM); + break; + case DECIDE_USE_ITEM: + setState(MenuState.USE_ITEM); + case CANCEL: + setState(MenuState.MAIN_MENU); + break; + case DO_ITEM_SWITCH: + if(button instanceof ItemSelectionButton) + { + PacketHandler.INSTANCE.sendToServer(new PacketBattleDecision(TurnBasedMinecraftMod.currentBattle.getId(), Battle.Decision.SWITCH_ITEM, ((ItemSelectionButton)button).itemStackID)); + setState(MenuState.WAITING); + } + else + { + setState(MenuState.MAIN_MENU); + } + break; + case DO_USE_ITEM: + if(button instanceof ItemSelectionButton) + { + PacketHandler.INSTANCE.sendToServer(new PacketBattleDecision(TurnBasedMinecraftMod.currentBattle.getId(), Battle.Decision.USE_ITEM, ((ItemSelectionButton)button).itemStackID)); + setState(MenuState.WAITING); + } + else + { + setState(MenuState.MAIN_MENU); + } + break; + } } @Override public void initGui() { - // TODO Auto-generated method stub super.initGui(); } @Override public void onGuiClosed() { - // TODO Auto-generated method stub super.onGuiClosed(); } @@ -79,4 +313,10 @@ public class BattleGui extends GuiScreen { return false; } + + @Override + protected void keyTyped(char typedChar, int keyCode) throws IOException + { + // left blank to prevent the player from exiting the gui + } } diff --git a/src/main/java/com/seodisparate/TurnBasedMinecraft/client/EntitySelectionButton.java b/src/main/java/com/seodisparate/TurnBasedMinecraft/client/EntitySelectionButton.java new file mode 100644 index 0000000..d63476b --- /dev/null +++ b/src/main/java/com/seodisparate/TurnBasedMinecraft/client/EntitySelectionButton.java @@ -0,0 +1,19 @@ +package com.seodisparate.TurnBasedMinecraft.client; + +import net.minecraft.client.gui.GuiButton; + +public class EntitySelectionButton extends GuiButton +{ + public int entityID; + public EntitySelectionButton(int buttonId, int x, int y, String buttonText, int entityID) + { + super(buttonId, x, y, buttonText); + this.entityID = entityID; + } + + public EntitySelectionButton(int buttonId, int x, int y, int widthIn, int heightIn, String buttonText, int entityID) + { + super(buttonId, x, y, widthIn, heightIn, buttonText); + this.entityID = entityID; + } +} diff --git a/src/main/java/com/seodisparate/TurnBasedMinecraft/client/ItemSelectionButton.java b/src/main/java/com/seodisparate/TurnBasedMinecraft/client/ItemSelectionButton.java new file mode 100644 index 0000000..f5fe250 --- /dev/null +++ b/src/main/java/com/seodisparate/TurnBasedMinecraft/client/ItemSelectionButton.java @@ -0,0 +1,38 @@ +package com.seodisparate.TurnBasedMinecraft.client; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.GuiButton; + +public class ItemSelectionButton extends GuiButton +{ + int itemStackID; + + public ItemSelectionButton(int buttonId, int x, int y, int widthIn, int heightIn, String buttonText, int itemStackID) + { + super(buttonId, x, y, widthIn, heightIn, buttonText); + this.itemStackID = itemStackID; + } + + public ItemSelectionButton(int buttonId, int x, int y, String buttonText, int itemStackID) + { + super(buttonId, x, y, buttonText); + this.itemStackID = itemStackID; + } + + @Override + public void drawButton(Minecraft mc, int mouseX, int mouseY, float partialTicks) + { + if(visible) + { + hovered = mouseX >= x && mouseY >= y && mouseX < x + width && mouseY < y + height; + if(hovered) + { + drawRect(x, y, x + width, y + height, 0x80FFFFFF); + } + else + { + drawRect(x, y, x + width, y + height, 0x20707070); + } + } + } +} diff --git a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/Battle.java b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/Battle.java index 217e7fd..570c9b3 100644 --- a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/Battle.java +++ b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/Battle.java @@ -8,6 +8,7 @@ import java.util.Hashtable; import java.util.Map; import java.util.PriorityQueue; import java.util.Queue; +import java.util.Set; import java.util.concurrent.atomic.AtomicInteger; import com.seodisparate.TurnBasedMinecraft.TurnBasedMinecraftMod; @@ -84,7 +85,8 @@ public class Battle ATTACK(1), DEFEND(2), FLEE(3), - USE_ITEM(4); + USE_ITEM(4), + SWITCH_ITEM(5); private int value; private static Map map = new HashMap(); @@ -279,6 +281,16 @@ public class Battle return sideB.values(); } + public Set> getSideAEntrySet() + { + return sideA.entrySet(); + } + + public Set> getSideBEntrySet() + { + return sideB.entrySet(); + } + public Collection getSideAIDs() { Collection sideAIDs = new ArrayList(sideA.size()); @@ -325,7 +337,7 @@ public class Battle { combatant.targetEntityID = targetIDOrItemID; } - else if(decision == Decision.USE_ITEM) + else if(decision == Decision.USE_ITEM || decision == Decision.SWITCH_ITEM) { combatant.itemToUse = targetIDOrItemID; } @@ -806,6 +818,10 @@ public class Battle sendMessageToAllPlayers(PacketBattleMessage.MessageType.USED_ITEM, next.entity.getEntityId(), 0, PacketBattleMessage.UsedItemAction.USED_INVALID.getValue(), targetItemStack.getDisplayName()); } break; + case SWITCH_ITEM: + ((EntityPlayer)next.entity).inventory.currentItem = next.itemToUse; + sendMessageToAllPlayers(PacketBattleMessage.MessageType.SWITCHED_ITEM, next.entity.getEntityId(), 0, 0); + break; } next = turnOrderQueue.poll(); } diff --git a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/networking/PacketBattleInfo.java b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/networking/PacketBattleInfo.java index f78acff..eca7752 100644 --- a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/networking/PacketBattleInfo.java +++ b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/networking/PacketBattleInfo.java @@ -84,6 +84,7 @@ public class PacketBattleInfo implements IMessage if(TurnBasedMinecraftMod.currentBattleGui != null) { TurnBasedMinecraftMod.currentBattleGui.timeRemaining.set((int)(message.decisionNanos / 1000000000)); + TurnBasedMinecraftMod.currentBattleGui.battleChanged(); } return null; } diff --git a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/networking/PacketBattleMessage.java b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/networking/PacketBattleMessage.java index a5d22a1..07d7ea2 100644 --- a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/networking/PacketBattleMessage.java +++ b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/networking/PacketBattleMessage.java @@ -35,7 +35,8 @@ public class PacketBattleMessage implements IMessage DID_NOTHING(9), USED_ITEM(10), TURN_BEGIN(11), - TURN_END(12); + TURN_END(12), + SWITCHED_ITEM(13); private int value; private static Map map = new HashMap(); @@ -325,6 +326,10 @@ public class PacketBattleMessage implements IMessage TurnBasedMinecraftMod.currentBattleGui.turnEnd(); } break; + case SWITCHED_ITEM: + Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString( + to + " switched to a different item!")); + break; } return null; }