Allow leaving battle gui temporarily, refactorings

This commit is contained in:
Stephen Seo 2024-10-22 16:54:52 +09:00
parent c553e24ad6
commit c5006f4c38
6 changed files with 46 additions and 8 deletions

View file

@ -28,6 +28,7 @@ public class BattleGui extends Screen {
private MenuState state; private MenuState state;
private boolean stateChanged; private boolean stateChanged;
private String info; private String info;
private Long waitMissingBattleTicks;
private enum MenuState { private enum MenuState {
MAIN_MENU(0), ATTACK_TARGET(1), ITEM_ACTION(2), WAITING(3), SWITCH_ITEM(4), USE_ITEM(5); MAIN_MENU(0), ATTACK_TARGET(1), ITEM_ACTION(2), WAITING(3), SWITCH_ITEM(4), USE_ITEM(5);
@ -91,6 +92,7 @@ public class BattleGui extends Screen {
elapsedTime = 0; elapsedTime = 0;
state = MenuState.MAIN_MENU; state = MenuState.MAIN_MENU;
stateChanged = true; stateChanged = true;
waitMissingBattleTicks = null;
} }
private void setState(MenuState state) { private void setState(MenuState state) {
@ -224,14 +226,31 @@ public class BattleGui extends Screen {
} }
} }
private int colorFromTicks(final Long ticks) {
if (ticks < 20 * 10) {
double value = (20 * 10 - ticks.intValue()) / (20.0 * 10.0);
return 0xFF0000FF | (((int)(value * 255.0)) << 8) | (((int)(value * 255.0)) << 16);
} else {
return 0xFF0000FF;
}
}
@Override @Override
public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTicks) { public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTicks) {
if (TurnBasedMinecraftMod.proxy.getLocalBattle() == null) { if (TurnBasedMinecraftMod.proxy.getLocalBattle() == null) {
if (waitMissingBattleTicks == null) {
waitMissingBattleTicks = 0L;
} else {
waitMissingBattleTicks += 1L;
}
// drawHoveringText("Waiting...", width / 2 - 50, height / 2); // drawHoveringText("Waiting...", width / 2 - 50, height / 2);
drawString(guiGraphics, "Waiting...", width / 2 - 50, height / 2, 0xFFFFFFFF); drawString(guiGraphics, "Waiting...", width / 2 - 50, height / 2, colorFromTicks(waitMissingBattleTicks));
super.render(guiGraphics, mouseX, mouseY, partialTicks); super.render(guiGraphics, mouseX, mouseY, partialTicks);
return; return;
} else {
waitMissingBattleTicks = null;
} }
if (TurnBasedMinecraftMod.proxy.getLocalBattle().getState() == Battle.State.DECISION if (TurnBasedMinecraftMod.proxy.getLocalBattle().getState() == Battle.State.DECISION
&& timeRemaining.get() > 0) { && timeRemaining.get() > 0) {
long nextInstant = System.nanoTime(); long nextInstant = System.nanoTime();
@ -352,9 +371,13 @@ public class BattleGui extends Screen {
} }
@Override @Override
public boolean keyPressed(int a, int b, int c) { public boolean keyPressed(int keyCode, int b, int c) {
if (getMinecraft().player.isCreative()) { if (getMinecraft().player.isCreative()) {
return super.keyPressed(a, b, c); return super.keyPressed(keyCode, b, c);
} else if (keyCode == 256) {
getMinecraft().setScreen(null);
TurnBasedMinecraftMod.proxy.displayString("Leaving GUI, but the battle continues!");
return true;
} }
return false; // TODO verify return value return false; // TODO verify return value
} }

View file

@ -370,6 +370,10 @@ public class ClientProxy extends CommonProxy {
break; break;
case TURN_BEGIN: case TURN_BEGIN:
TurnBasedMinecraftMod.proxy.displayString("The turn begins!"); TurnBasedMinecraftMod.proxy.displayString("The turn begins!");
if (TurnBasedMinecraftMod.proxy.getLocalBattle() == null || TurnBasedMinecraftMod.proxy.getLocalBattle().getId() != pkt.getAmount()) {
TurnBasedMinecraftMod.proxy.createLocalBattle(pkt.getAmount());
}
TurnBasedMinecraftMod.proxy.battleStarted();
TurnBasedMinecraftMod.proxy.battleGuiTurnBegin(); TurnBasedMinecraftMod.proxy.battleGuiTurnBegin();
break; break;
case TURN_END: case TURN_END:

View file

@ -49,6 +49,8 @@ public class Battle {
private ResourceKey<Level> dimension; private ResourceKey<Level> dimension;
private long infoNanos;
public enum State { public enum State {
DECISION(0), DECISION(0),
ACTION(1), ACTION(1),
@ -122,6 +124,7 @@ public class Battle {
undecidedCount = new AtomicInteger(0); undecidedCount = new AtomicInteger(0);
random = new Random(); random = new Random();
this.dimension = dimension; this.dimension = dimension;
infoNanos = 0;
if (sideA != null) { if (sideA != null) {
for (Entity e : sideA) { for (Entity e : sideA) {
EntityInfo entityInfo; EntityInfo entityInfo;
@ -447,7 +450,7 @@ public class Battle {
if (!isServer) { if (!isServer) {
return; return;
} }
PacketBattleInfo infoPacket = new PacketBattleInfo(getSideAIDs(), getSideBIDs(), timer, TurnBasedMinecraftMod.proxy.getConfig().getDecisionDurationNanos(), !TurnBasedMinecraftMod.proxy.getConfig().isBattleDecisionDurationForever()); PacketBattleInfo infoPacket = new PacketBattleInfo(getId(), getSideAIDs(), getSideBIDs(), timer, TurnBasedMinecraftMod.proxy.getConfig().getDecisionDurationNanos(), !TurnBasedMinecraftMod.proxy.getConfig().isBattleDecisionDurationForever());
for (Combatant p : players.values()) { for (Combatant p : players.values()) {
PacketDistributor.sendToPlayer((ServerPlayer)p.entity, infoPacket); PacketDistributor.sendToPlayer((ServerPlayer)p.entity, infoPacket);
} }
@ -651,6 +654,11 @@ public class Battle {
} }
private boolean update(final long dt) { private boolean update(final long dt) {
infoNanos += dt;
if (infoNanos >= 4000000000L) {
infoNanos = 0;
notifyPlayersBattleInfo();
}
if (battleEnded) { if (battleEnded) {
Collection<Combatant> combatants = new ArrayList<Combatant>(); Collection<Combatant> combatants = new ArrayList<Combatant>();
combatants.addAll(sideA.values()); combatants.addAll(sideA.values());

View file

@ -27,7 +27,6 @@ import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent;
import net.neoforged.fml.event.lifecycle.FMLCommonSetupEvent; import net.neoforged.fml.event.lifecycle.FMLCommonSetupEvent;
import net.neoforged.fml.event.lifecycle.FMLDedicatedServerSetupEvent; import net.neoforged.fml.event.lifecycle.FMLDedicatedServerSetupEvent;
import net.neoforged.fml.loading.FMLEnvironment; import net.neoforged.fml.loading.FMLEnvironment;
import net.neoforged.neoforge.client.gui.IConfigScreenFactory;
import net.neoforged.neoforge.common.NeoForge; import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.RegisterCommandsEvent; import net.neoforged.neoforge.event.RegisterCommandsEvent;
import net.neoforged.neoforge.event.server.ServerStartingEvent; import net.neoforged.neoforge.event.server.ServerStartingEvent;
@ -53,7 +52,7 @@ public class TurnBasedMinecraftMod {
public static final String MUSIC_SILLY = MUSIC_ROOT + "silly/"; public static final String MUSIC_SILLY = MUSIC_ROOT + "silly/";
public static final String MUSIC_BATTLE = MUSIC_ROOT + "battle/"; public static final String MUSIC_BATTLE = MUSIC_ROOT + "battle/";
private static final String PROTOCOL_VERSION = Integer.toString(3); private static final String PROTOCOL_VERSION = Integer.toString(4);
private static final ResourceLocation HANDLER_ID = ResourceLocation.fromNamespaceAndPath(MODID, "main_channel"); private static final ResourceLocation HANDLER_ID = ResourceLocation.fromNamespaceAndPath(MODID, "main_channel");
protected static Logger logger = LogManager.getLogger(); protected static Logger logger = LogManager.getLogger();

View file

@ -16,11 +16,13 @@ import org.jetbrains.annotations.NotNull;
import java.util.Collection; import java.util.Collection;
public record PacketBattleInfo(Collection<Integer> sideA, Collection<Integer> sideB, long decisionNanos, long maxDecisionNanos, boolean turnTimerEnabled) implements CustomPacketPayload public record PacketBattleInfo(int battleID, Collection<Integer> sideA, Collection<Integer> sideB, long decisionNanos, long maxDecisionNanos, boolean turnTimerEnabled) implements CustomPacketPayload
{ {
public static final CustomPacketPayload.Type<PacketBattleInfo> TYPE = new CustomPacketPayload.Type<>(ResourceLocation.fromNamespaceAndPath(TurnBasedMinecraftMod.MODID, "network_packetbattleinfo")); public static final CustomPacketPayload.Type<PacketBattleInfo> TYPE = new CustomPacketPayload.Type<>(ResourceLocation.fromNamespaceAndPath(TurnBasedMinecraftMod.MODID, "network_packetbattleinfo"));
public static final StreamCodec<ByteBuf, PacketBattleInfo> STREAM_CODEC = StreamCodec.composite( public static final StreamCodec<ByteBuf, PacketBattleInfo> STREAM_CODEC = StreamCodec.composite(
ByteBufCodecs.INT,
PacketBattleInfo::battleID,
CommonProxy.COLLECTION_INT_CODEC, CommonProxy.COLLECTION_INT_CODEC,
PacketBattleInfo::sideA, PacketBattleInfo::sideA,
CommonProxy.COLLECTION_INT_CODEC, CommonProxy.COLLECTION_INT_CODEC,
@ -45,7 +47,7 @@ public record PacketBattleInfo(Collection<Integer> sideA, Collection<Integer> si
ctx.enqueueWork(() -> { ctx.enqueueWork(() -> {
if(TurnBasedMinecraftMod.proxy.getLocalBattle() == null) if(TurnBasedMinecraftMod.proxy.getLocalBattle() == null)
{ {
return; TurnBasedMinecraftMod.proxy.createLocalBattle(pkt.battleID);
} }
TurnBasedMinecraftMod.proxy.getLocalBattle().clearCombatants(); TurnBasedMinecraftMod.proxy.getLocalBattle().clearCombatants();
for(Integer id : pkt.sideA) for(Integer id : pkt.sideA)
@ -64,6 +66,7 @@ public record PacketBattleInfo(Collection<Integer> sideA, Collection<Integer> si
TurnBasedMinecraftMod.proxy.getLocalBattle().addCombatantToSideB(e); TurnBasedMinecraftMod.proxy.getLocalBattle().addCombatantToSideB(e);
} }
} }
TurnBasedMinecraftMod.proxy.setBattleGuiAsGui();
TurnBasedMinecraftMod.proxy.setBattleGuiTime((int)(pkt.decisionNanos / 1000000000L)); TurnBasedMinecraftMod.proxy.setBattleGuiTime((int)(pkt.decisionNanos / 1000000000L));
TurnBasedMinecraftMod.proxy.setBattleGuiBattleChanged(); TurnBasedMinecraftMod.proxy.setBattleGuiBattleChanged();
TurnBasedMinecraftMod.proxy.setBattleGuiTurnTimerEnabled(pkt.turnTimerEnabled); TurnBasedMinecraftMod.proxy.setBattleGuiTurnTimerEnabled(pkt.turnTimerEnabled);

View file

@ -41,6 +41,7 @@ public record PacketBattleRequestInfo(int battleID) implements CustomPacketPaylo
return; return;
} }
ctx.reply(new PacketBattleInfo( ctx.reply(new PacketBattleInfo(
b.getId(),
b.getSideAIDs(), b.getSideAIDs(),
b.getSideBIDs(), b.getSideBIDs(),
b.getTimerNanos(), b.getTimerNanos(),