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 com.seodisparate.TurnBasedMinecraft.client.BattleGui;
import com.seodisparate.TurnBasedMinecraft.common.Battle;
import com.seodisparate.TurnBasedMinecraft.common.BattleManager;
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_FILE_PATH = CONFIG_DIRECTORY + CONFIG_FILENAME;
public static final String CONFIG_INTERNAL_PATH = "/assets/TurnBasedMinecraft/" + CONFIG_FILENAME;
private static int CONFIG_FILE_VERSION = 0;
private static Logger logger;
@ -43,7 +45,8 @@ public class TurnBasedMinecraftMod
public static int attackingDamage = 0;
public static Config config;
public static Battle currentBattle;
public static Battle currentBattle = null;
public static BattleGui currentBattleGui = null;
@EventHandler
public void preInit(FMLPreInitializationEvent event)
@ -102,7 +105,7 @@ public class TurnBasedMinecraftMod
@SubscribeEvent
public void entityAttacked(LivingAttackEvent event)
{
if(battleManager == null)
if(battleManager == null || event.getEntity().world.isRemote)
{
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;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
@ -38,19 +36,46 @@ public class Battle
private Map<Integer, Combatant> players;
private PriorityQueue<Combatant> turnOrderQueue;
private Instant lastUpdated;
private State state;
private AtomicInteger playerCount;
private AtomicInteger undecidedCount;
private Duration timer;
private long lastInstant;
private long timer;
private boolean isServer;
private boolean battleEnded;
public enum State
{
DECISION,
ACTION
DECISION(0),
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
@ -148,10 +173,10 @@ public class Battle
sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, c.entity.getEntityId(), 0, id);
}
lastUpdated = null;
lastInstant = System.nanoTime();
state = State.DECISION;
undecidedCount.set(playerCount.get());
timer = TurnBasedMinecraftMod.BattleDecisionTime;
timer = TurnBasedMinecraftMod.BattleDecisionTime.getSeconds() * 1000000000;
battleEnded = false;
notifyPlayersBattleInfo();
@ -312,42 +337,29 @@ public class Battle
return state;
}
public void setState(State state)
{
this.state = state;
}
public long getTimerSeconds()
{
return timer / 1000000000;
}
protected void notifyPlayersBattleInfo()
{
if(!isServer)
{
return;
}
PacketBattleInfo infoPacket = new PacketBattleInfo(getSideAIDs(), getSideBIDs());
PacketBattleInfo infoPacket = new PacketBattleInfo(getSideAIDs(), getSideBIDs(), timer);
for(Combatant p : players.values())
{
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)
{
if(!isServer)
@ -433,7 +445,22 @@ public class Battle
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)
{
@ -442,8 +469,8 @@ public class Battle
switch(state)
{
case DECISION:
timer = timer.minus(dt);
if(timer.isNegative() || timer.isZero() || undecidedCount.get() <= 0)
timer -= dt;
if(timer <= 0 || undecidedCount.get() <= 0)
{
for(Combatant c : sideA.values())
{
@ -485,7 +512,8 @@ public class Battle
}
}
state = State.ACTION;
timer = TurnBasedMinecraftMod.BattleDecisionTime;
timer = TurnBasedMinecraftMod.BattleDecisionTime.getSeconds() * 1000000000;
sendMessageToAllPlayers(PacketBattleMessage.MessageType.TURN_BEGIN, 0, 0, 0);
turnOrderQueue.clear();
for(Combatant c : sideA.values())
{
@ -495,7 +523,7 @@ public class Battle
{
turnOrderQueue.add(c);
}
update(Duration.ZERO);
update(0);
}
else
{
@ -791,9 +819,13 @@ public class Battle
}
state = State.DECISION;
healthCheck();
sendMessageToAllPlayers(PacketBattleMessage.MessageType.TURN_END, 0, 0, 0);
break;
} // case ACTION
default:
state = State.DECISION;
break;
} // switch(state)
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> sideB;
private long decisionNanos;
public PacketBattleInfo()
{
sideA = 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.sideB = sideB;
this.decisionNanos = decisionNanos;
}
@Override
@ -41,6 +44,7 @@ public class PacketBattleInfo implements IMessage
{
sideB.add(buf.readInt());
}
decisionNanos = buf.readLong();
}
@Override
@ -56,6 +60,7 @@ public class PacketBattleInfo implements IMessage
{
buf.writeInt(id);
}
buf.writeLong(decisionNanos);
}
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));
}
if(TurnBasedMinecraftMod.currentBattleGui != null)
{
TurnBasedMinecraftMod.currentBattleGui.timeRemaining.set((int)(message.decisionNanos / 1000000000));
}
return null;
}
}

View file

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

View file

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