WIP - begin work on client GUI

This commit is contained in:
Stephen Seo 2018-09-10 14:59:56 +09:00
parent 97cfdaf0f7
commit 0d5f18f997
6 changed files with 199 additions and 44 deletions

View file

@ -4,6 +4,7 @@ import java.time.Duration;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import com.seodisparate.TurnBasedMinecraft.client.BattleGui;
import com.seodisparate.TurnBasedMinecraft.common.Battle; import com.seodisparate.TurnBasedMinecraft.common.Battle;
import com.seodisparate.TurnBasedMinecraft.common.BattleManager; import com.seodisparate.TurnBasedMinecraft.common.BattleManager;
import com.seodisparate.TurnBasedMinecraft.common.Config; import com.seodisparate.TurnBasedMinecraft.common.Config;
@ -34,6 +35,7 @@ public class TurnBasedMinecraftMod
public static final String CONFIG_DIRECTORY = "config/TurnBasedMinecraft/"; public static final String CONFIG_DIRECTORY = "config/TurnBasedMinecraft/";
public static final String CONFIG_FILE_PATH = CONFIG_DIRECTORY + CONFIG_FILENAME; public static final String CONFIG_FILE_PATH = CONFIG_DIRECTORY + CONFIG_FILENAME;
public static final String CONFIG_INTERNAL_PATH = "/assets/TurnBasedMinecraft/" + CONFIG_FILENAME; public static final String CONFIG_INTERNAL_PATH = "/assets/TurnBasedMinecraft/" + CONFIG_FILENAME;
private static int CONFIG_FILE_VERSION = 0; private static int CONFIG_FILE_VERSION = 0;
private static Logger logger; private static Logger logger;
@ -43,7 +45,8 @@ public class TurnBasedMinecraftMod
public static int attackingDamage = 0; public static int attackingDamage = 0;
public static Config config; public static Config config;
public static Battle currentBattle; public static Battle currentBattle = null;
public static BattleGui currentBattleGui = null;
@EventHandler @EventHandler
public void preInit(FMLPreInitializationEvent event) public void preInit(FMLPreInitializationEvent event)
@ -102,7 +105,7 @@ public class TurnBasedMinecraftMod
@SubscribeEvent @SubscribeEvent
public void entityAttacked(LivingAttackEvent event) public void entityAttacked(LivingAttackEvent event)
{ {
if(battleManager == null) if(battleManager == null || event.getEntity().world.isRemote)
{ {
return; return;
} }

View file

@ -0,0 +1,82 @@
package com.seodisparate.TurnBasedMinecraft.client;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
import com.seodisparate.TurnBasedMinecraft.TurnBasedMinecraftMod;
import com.seodisparate.TurnBasedMinecraft.common.Battle;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
public class BattleGui extends GuiScreen
{
public AtomicInteger timeRemaining;
public long lastInstant;
public long elapsedTime;
public BattleGui()
{
timeRemaining = new AtomicInteger((int)TurnBasedMinecraftMod.BattleDecisionTime.getSeconds());
lastInstant = System.nanoTime();
elapsedTime = 0;
}
public void turnBegin()
{
TurnBasedMinecraftMod.currentBattle.setState(Battle.State.ACTION);
// TODO reset gui since decisions ended
}
public void turnEnd()
{
TurnBasedMinecraftMod.currentBattle.setState(Battle.State.DECISION);
timeRemaining.set((int)TurnBasedMinecraftMod.BattleDecisionTime.getSeconds());
}
@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();
elapsedTime += nextInstant - lastInstant;
lastInstant = nextInstant;
while(elapsedTime > 1000000000)
{
elapsedTime -= 1000000000;
timeRemaining.decrementAndGet();
}
}
}
@Override
protected void actionPerformed(GuiButton button) throws IOException
{
// TODO Auto-generated method stub
super.actionPerformed(button);
}
@Override
public void initGui()
{
// TODO Auto-generated method stub
super.initGui();
}
@Override
public void onGuiClosed()
{
// TODO Auto-generated method stub
super.onGuiClosed();
}
@Override
public boolean doesGuiPauseGame()
{
return false;
}
}

View file

@ -1,7 +1,5 @@
package com.seodisparate.TurnBasedMinecraft.common; package com.seodisparate.TurnBasedMinecraft.common;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayDeque; import java.util.ArrayDeque;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
@ -38,19 +36,46 @@ public class Battle
private Map<Integer, Combatant> players; private Map<Integer, Combatant> players;
private PriorityQueue<Combatant> turnOrderQueue; private PriorityQueue<Combatant> turnOrderQueue;
private Instant lastUpdated;
private State state; private State state;
private AtomicInteger playerCount; private AtomicInteger playerCount;
private AtomicInteger undecidedCount; private AtomicInteger undecidedCount;
private Duration timer; private long lastInstant;
private long timer;
private boolean isServer; private boolean isServer;
private boolean battleEnded; private boolean battleEnded;
public enum State public enum State
{ {
DECISION, DECISION(0),
ACTION ACTION(1),
DECISION_PLAYER_READY(2);
private int value;
private static Map<Integer, State> map = new HashMap<Integer, State>();
private State(int value)
{
this.value = value;
}
public int getValue()
{
return value;
}
static
{
for(State state : State.values())
{
map.put(state.value, state);
}
}
public static State valueOf(int stateType)
{
return map.get(stateType);
}
} }
public enum Decision public enum Decision
@ -148,10 +173,10 @@ public class Battle
sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, c.entity.getEntityId(), 0, id); sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, c.entity.getEntityId(), 0, id);
} }
lastUpdated = null; lastInstant = System.nanoTime();
state = State.DECISION; state = State.DECISION;
undecidedCount.set(playerCount.get()); undecidedCount.set(playerCount.get());
timer = TurnBasedMinecraftMod.BattleDecisionTime; timer = TurnBasedMinecraftMod.BattleDecisionTime.getSeconds() * 1000000000;
battleEnded = false; battleEnded = false;
notifyPlayersBattleInfo(); notifyPlayersBattleInfo();
@ -312,42 +337,29 @@ public class Battle
return state; return state;
} }
public void setState(State state)
{
this.state = state;
}
public long getTimerSeconds()
{
return timer / 1000000000;
}
protected void notifyPlayersBattleInfo() protected void notifyPlayersBattleInfo()
{ {
if(!isServer) if(!isServer)
{ {
return; return;
} }
PacketBattleInfo infoPacket = new PacketBattleInfo(getSideAIDs(), getSideBIDs()); PacketBattleInfo infoPacket = new PacketBattleInfo(getSideAIDs(), getSideBIDs(), timer);
for(Combatant p : players.values()) for(Combatant p : players.values())
{ {
PacketHandler.INSTANCE.sendTo(infoPacket, (EntityPlayerMP)p.entity); PacketHandler.INSTANCE.sendTo(infoPacket, (EntityPlayerMP)p.entity);
} }
} }
/**
* @return True if battle has ended
*/
public boolean update()
{
if(battleEnded)
{
return true;
}
if(lastUpdated == null)
{
lastUpdated = Instant.now();
return update(Duration.ZERO);
}
else
{
Instant now = Instant.now();
Duration dt = Duration.between(lastUpdated, now);
lastUpdated = now;
return update(dt);
}
}
private void sendMessageToAllPlayers(PacketBattleMessage.MessageType type, int from, int to, int amount) private void sendMessageToAllPlayers(PacketBattleMessage.MessageType type, int from, int to, int amount)
{ {
if(!isServer) if(!isServer)
@ -433,7 +445,22 @@ public class Battle
return didRemove; return didRemove;
} }
private boolean update(final Duration dt) /**
* @return True if battle has ended
*/
public boolean update()
{
if(battleEnded)
{
return true;
}
long nextInstant = System.nanoTime();
long dt = nextInstant - lastInstant;
lastInstant = nextInstant;
return update(dt);
}
private boolean update(final long dt)
{ {
if(battleEnded) if(battleEnded)
{ {
@ -442,8 +469,8 @@ public class Battle
switch(state) switch(state)
{ {
case DECISION: case DECISION:
timer = timer.minus(dt); timer -= dt;
if(timer.isNegative() || timer.isZero() || undecidedCount.get() <= 0) if(timer <= 0 || undecidedCount.get() <= 0)
{ {
for(Combatant c : sideA.values()) for(Combatant c : sideA.values())
{ {
@ -485,7 +512,8 @@ public class Battle
} }
} }
state = State.ACTION; state = State.ACTION;
timer = TurnBasedMinecraftMod.BattleDecisionTime; timer = TurnBasedMinecraftMod.BattleDecisionTime.getSeconds() * 1000000000;
sendMessageToAllPlayers(PacketBattleMessage.MessageType.TURN_BEGIN, 0, 0, 0);
turnOrderQueue.clear(); turnOrderQueue.clear();
for(Combatant c : sideA.values()) for(Combatant c : sideA.values())
{ {
@ -495,7 +523,7 @@ public class Battle
{ {
turnOrderQueue.add(c); turnOrderQueue.add(c);
} }
update(Duration.ZERO); update(0);
} }
else else
{ {
@ -791,9 +819,13 @@ public class Battle
} }
state = State.DECISION; state = State.DECISION;
healthCheck(); healthCheck();
sendMessageToAllPlayers(PacketBattleMessage.MessageType.TURN_END, 0, 0, 0);
break; break;
} // case ACTION } // case ACTION
default:
state = State.DECISION;
break;
} // switch(state) } // switch(state)
return battleEnded; return battleEnded;
} // update(final Duration dt) } // update(final long dt)
} }

View file

@ -15,17 +15,20 @@ public class PacketBattleInfo implements IMessage
{ {
private Collection<Integer> sideA; private Collection<Integer> sideA;
private Collection<Integer> sideB; private Collection<Integer> sideB;
private long decisionNanos;
public PacketBattleInfo() public PacketBattleInfo()
{ {
sideA = new ArrayList<Integer>(); sideA = new ArrayList<Integer>();
sideB = new ArrayList<Integer>(); sideB = new ArrayList<Integer>();
decisionNanos = TurnBasedMinecraftMod.BattleDecisionTime.getSeconds() * 1000000000;
} }
public PacketBattleInfo(Collection<Integer> sideA, Collection<Integer> sideB) public PacketBattleInfo(Collection<Integer> sideA, Collection<Integer> sideB, long decisionNanos)
{ {
this.sideA = sideA; this.sideA = sideA;
this.sideB = sideB; this.sideB = sideB;
this.decisionNanos = decisionNanos;
} }
@Override @Override
@ -41,6 +44,7 @@ public class PacketBattleInfo implements IMessage
{ {
sideB.add(buf.readInt()); sideB.add(buf.readInt());
} }
decisionNanos = buf.readLong();
} }
@Override @Override
@ -56,6 +60,7 @@ public class PacketBattleInfo implements IMessage
{ {
buf.writeInt(id); buf.writeInt(id);
} }
buf.writeLong(decisionNanos);
} }
public static class HandlerBattleInfo implements IMessageHandler<PacketBattleInfo, IMessage> public static class HandlerBattleInfo implements IMessageHandler<PacketBattleInfo, IMessage>
@ -76,6 +81,10 @@ public class PacketBattleInfo implements IMessage
{ {
TurnBasedMinecraftMod.currentBattle.addCombatantToSideB(Minecraft.getMinecraft().world.getEntityByID(id)); TurnBasedMinecraftMod.currentBattle.addCombatantToSideB(Minecraft.getMinecraft().world.getEntityByID(id));
} }
if(TurnBasedMinecraftMod.currentBattleGui != null)
{
TurnBasedMinecraftMod.currentBattleGui.timeRemaining.set((int)(message.decisionNanos / 1000000000));
}
return null; return null;
} }
} }

View file

@ -4,6 +4,7 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.seodisparate.TurnBasedMinecraft.TurnBasedMinecraftMod; import com.seodisparate.TurnBasedMinecraft.TurnBasedMinecraftMod;
import com.seodisparate.TurnBasedMinecraft.client.BattleGui;
import com.seodisparate.TurnBasedMinecraft.common.Battle; import com.seodisparate.TurnBasedMinecraft.common.Battle;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
@ -32,7 +33,9 @@ public class PacketBattleMessage implements IMessage
MISS(7), MISS(7),
DEFENDING(8), DEFENDING(8),
DID_NOTHING(9), DID_NOTHING(9),
USED_ITEM(10); USED_ITEM(10),
TURN_BEGIN(11),
TURN_END(12);
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>();
@ -228,6 +231,13 @@ public class PacketBattleMessage implements IMessage
{ {
TurnBasedMinecraftMod.currentBattle = new Battle(message.amount, null, null, false); TurnBasedMinecraftMod.currentBattle = new Battle(message.amount, null, null, false);
} }
if(TurnBasedMinecraftMod.currentBattleGui == null)
{
Minecraft.getMinecraft().addScheduledTask(() -> {
TurnBasedMinecraftMod.currentBattleGui = new BattleGui();
Minecraft.getMinecraft().displayGuiScreen(TurnBasedMinecraftMod.currentBattleGui);
});
}
break; break;
case FLEE: case FLEE:
if(message.amount != 0) if(message.amount != 0)
@ -249,7 +259,10 @@ public class PacketBattleMessage implements IMessage
Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString( Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString(
"Battle has ended!")); "Battle has ended!"));
TurnBasedMinecraftMod.currentBattle = null; TurnBasedMinecraftMod.currentBattle = null;
// TODO kick player out of battle Minecraft.getMinecraft().addScheduledTask(() -> {
TurnBasedMinecraftMod.currentBattleGui = null;
Minecraft.getMinecraft().setIngameFocus();
});
break; break;
case ATTACK: case ATTACK:
Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString( Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString(
@ -296,6 +309,22 @@ public class PacketBattleMessage implements IMessage
break; break;
} }
break; break;
case TURN_BEGIN:
Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString(
"The turn begins!"));
if(TurnBasedMinecraftMod.currentBattleGui != null)
{
TurnBasedMinecraftMod.currentBattleGui.turnBegin();
}
break;
case TURN_END:
Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString(
"The turn ended!"));
if(TurnBasedMinecraftMod.currentBattleGui != null)
{
TurnBasedMinecraftMod.currentBattleGui.turnEnd();
}
break;
} }
return null; return null;
} }

View file

@ -41,7 +41,7 @@ public class PacketBattleRequestInfo implements IMessage
{ {
return null; return null;
} }
PacketBattleInfo battleInfo = new PacketBattleInfo(b.getSideAIDs(), b.getSideBIDs()); PacketBattleInfo battleInfo = new PacketBattleInfo(b.getSideAIDs(), b.getSideBIDs(), b.getTimerSeconds());
return battleInfo; return battleInfo;
} }
} }