WIP - implemented enough GUI to start testing

This commit is contained in:
Stephen Seo 2018-09-11 15:15:31 +09:00
parent 0d5f18f997
commit de48b8401c
6 changed files with 330 additions and 11 deletions

View file

@ -1,10 +1,15 @@
package com.seodisparate.TurnBasedMinecraft.client; package com.seodisparate.TurnBasedMinecraft.client;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import com.seodisparate.TurnBasedMinecraft.TurnBasedMinecraftMod; import com.seodisparate.TurnBasedMinecraft.TurnBasedMinecraftMod;
import com.seodisparate.TurnBasedMinecraft.common.Battle; 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.GuiButton;
import net.minecraft.client.gui.GuiScreen; import net.minecraft.client.gui.GuiScreen;
@ -14,32 +19,201 @@ public class BattleGui extends GuiScreen
public AtomicInteger timeRemaining; public AtomicInteger timeRemaining;
public long lastInstant; public long lastInstant;
public long elapsedTime; 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<Integer, MenuState> map;
static
{
map = new HashMap<Integer, MenuState>();
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<Integer, ButtonAction> map;
static
{
map = new HashMap<Integer, ButtonAction>();
for(ButtonAction action : ButtonAction.values())
{
map.put(action.getValue(), action);
}
}
public static ButtonAction valueOf(int value)
{
return map.get(value);
}
}
public BattleGui() public BattleGui()
{ {
timeRemaining = new AtomicInteger((int)TurnBasedMinecraftMod.BattleDecisionTime.getSeconds()); timeRemaining = new AtomicInteger((int)TurnBasedMinecraftMod.BattleDecisionTime.getSeconds());
lastInstant = System.nanoTime(); lastInstant = System.nanoTime();
elapsedTime = 0; elapsedTime = 0;
state = MenuState.MAIN_MENU;
stateChanged = true;
}
private void setState(MenuState state)
{
this.state = state;
stateChanged = true;
} }
public void turnBegin() public void turnBegin()
{ {
TurnBasedMinecraftMod.currentBattle.setState(Battle.State.ACTION); TurnBasedMinecraftMod.currentBattle.setState(Battle.State.ACTION);
// TODO reset gui since decisions ended setState(MenuState.WAITING);
} }
public void turnEnd() public void turnEnd()
{ {
TurnBasedMinecraftMod.currentBattle.setState(Battle.State.DECISION); TurnBasedMinecraftMod.currentBattle.setState(Battle.State.DECISION);
timeRemaining.set((int)TurnBasedMinecraftMod.BattleDecisionTime.getSeconds()); 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<Integer, Combatant> 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<Integer, Combatant> 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 @Override
public void drawScreen(int mouseX, int mouseY, float partialTicks) 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) if(TurnBasedMinecraftMod.currentBattle.getState() == Battle.State.DECISION && timeRemaining.get() > 0)
{ {
long nextInstant = System.nanoTime(); long nextInstant = System.nanoTime();
@ -51,26 +225,86 @@ public class BattleGui extends GuiScreen
timeRemaining.decrementAndGet(); timeRemaining.decrementAndGet();
} }
} }
updateState();
super.drawScreen(mouseX, mouseY, partialTicks);
drawHoveringText(info, width / 2 - 50, height - 50);
} }
@Override @Override
protected void actionPerformed(GuiButton button) throws IOException protected void actionPerformed(GuiButton button) throws IOException
{ {
// TODO Auto-generated method stub switch(ButtonAction.valueOf(button.id))
super.actionPerformed(button); {
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 @Override
public void initGui() public void initGui()
{ {
// TODO Auto-generated method stub
super.initGui(); super.initGui();
} }
@Override @Override
public void onGuiClosed() public void onGuiClosed()
{ {
// TODO Auto-generated method stub
super.onGuiClosed(); super.onGuiClosed();
} }
@ -79,4 +313,10 @@ public class BattleGui extends GuiScreen
{ {
return false; return false;
} }
@Override
protected void keyTyped(char typedChar, int keyCode) throws IOException
{
// left blank to prevent the player from exiting the gui
}
} }

View file

@ -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;
}
}

View file

@ -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);
}
}
}
}

View file

@ -8,6 +8,7 @@ import java.util.Hashtable;
import java.util.Map; import java.util.Map;
import java.util.PriorityQueue; import java.util.PriorityQueue;
import java.util.Queue; import java.util.Queue;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import com.seodisparate.TurnBasedMinecraft.TurnBasedMinecraftMod; import com.seodisparate.TurnBasedMinecraft.TurnBasedMinecraftMod;
@ -84,7 +85,8 @@ public class Battle
ATTACK(1), ATTACK(1),
DEFEND(2), DEFEND(2),
FLEE(3), FLEE(3),
USE_ITEM(4); USE_ITEM(4),
SWITCH_ITEM(5);
private int value; private int value;
private static Map<Integer, Decision> map = new HashMap<Integer, Decision>(); private static Map<Integer, Decision> map = new HashMap<Integer, Decision>();
@ -279,6 +281,16 @@ public class Battle
return sideB.values(); return sideB.values();
} }
public Set<Map.Entry<Integer, Combatant>> getSideAEntrySet()
{
return sideA.entrySet();
}
public Set<Map.Entry<Integer, Combatant>> getSideBEntrySet()
{
return sideB.entrySet();
}
public Collection<Integer> getSideAIDs() public Collection<Integer> getSideAIDs()
{ {
Collection<Integer> sideAIDs = new ArrayList<Integer>(sideA.size()); Collection<Integer> sideAIDs = new ArrayList<Integer>(sideA.size());
@ -325,7 +337,7 @@ public class Battle
{ {
combatant.targetEntityID = targetIDOrItemID; combatant.targetEntityID = targetIDOrItemID;
} }
else if(decision == Decision.USE_ITEM) else if(decision == Decision.USE_ITEM || decision == Decision.SWITCH_ITEM)
{ {
combatant.itemToUse = targetIDOrItemID; 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()); sendMessageToAllPlayers(PacketBattleMessage.MessageType.USED_ITEM, next.entity.getEntityId(), 0, PacketBattleMessage.UsedItemAction.USED_INVALID.getValue(), targetItemStack.getDisplayName());
} }
break; 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(); next = turnOrderQueue.poll();
} }

View file

@ -84,6 +84,7 @@ public class PacketBattleInfo implements IMessage
if(TurnBasedMinecraftMod.currentBattleGui != null) if(TurnBasedMinecraftMod.currentBattleGui != null)
{ {
TurnBasedMinecraftMod.currentBattleGui.timeRemaining.set((int)(message.decisionNanos / 1000000000)); TurnBasedMinecraftMod.currentBattleGui.timeRemaining.set((int)(message.decisionNanos / 1000000000));
TurnBasedMinecraftMod.currentBattleGui.battleChanged();
} }
return null; return null;
} }

View file

@ -35,7 +35,8 @@ public class PacketBattleMessage implements IMessage
DID_NOTHING(9), DID_NOTHING(9),
USED_ITEM(10), USED_ITEM(10),
TURN_BEGIN(11), TURN_BEGIN(11),
TURN_END(12); TURN_END(12),
SWITCHED_ITEM(13);
private int value; private int value;
private static Map<Integer, MessageType> map = new HashMap<Integer, MessageType>(); private static Map<Integer, MessageType> map = new HashMap<Integer, MessageType>();
@ -325,6 +326,10 @@ public class PacketBattleMessage implements IMessage
TurnBasedMinecraftMod.currentBattleGui.turnEnd(); TurnBasedMinecraftMod.currentBattleGui.turnEnd();
} }
break; break;
case SWITCHED_ITEM:
Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString(
to + " switched to a different item!"));
break;
} }
return null; return null;
} }