使用毫秒精度计时器重写开火逻辑

This commit is contained in:
Light_Quanta 2024-09-20 22:04:59 +08:00
parent c753d9234e
commit dbfddf7440
No known key found for this signature in database
GPG key ID: 11A39A1B8C890959
3 changed files with 55 additions and 23 deletions

View file

@ -8,6 +8,7 @@ import net.mcreator.superbwarfare.init.ModMobEffects;
import net.mcreator.superbwarfare.init.ModTags; import net.mcreator.superbwarfare.init.ModTags;
import net.mcreator.superbwarfare.network.ModVariables; import net.mcreator.superbwarfare.network.ModVariables;
import net.mcreator.superbwarfare.network.message.ShootMessage; import net.mcreator.superbwarfare.network.message.ShootMessage;
import net.mcreator.superbwarfare.tools.MillisTimer;
import net.minecraft.client.CameraType; import net.minecraft.client.CameraType;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel; import net.minecraft.client.multiplayer.ClientLevel;
@ -73,7 +74,7 @@ public class ClientEventHandler {
public static double bowPos = 0; public static double bowPos = 0;
public static double handPos = 0; public static double handPos = 0;
public static double ClientTimer = 0; public static MillisTimer clientTimer = new MillisTimer();
@SubscribeEvent @SubscribeEvent
public static void handleWeaponTurn(RenderHandEvent event) { public static void handleWeaponTurn(RenderHandEvent event) {
@ -109,8 +110,7 @@ public class ClientEventHandler {
if (notInGame()) return; if (notInGame()) return;
ClientLevel level = Minecraft.getInstance().level; ClientLevel level = Minecraft.getInstance().level;
Player player = Minecraft.getInstance().player; Player player = Minecraft.getInstance().player;
if (player != null && level != null && player.getMainHandItem().is(ModTags.Items.NORMAL_GUN)) { if (GLFW.glfwGetMouseButton(Minecraft.getInstance().getWindow().getWindow(), GLFW.GLFW_MOUSE_BUTTON_LEFT) == GLFW.GLFW_PRESS && player != null && level != null && player.getMainHandItem().is(ModTags.Items.NORMAL_GUN)) {
ItemStack stack = player.getMainHandItem(); ItemStack stack = player.getMainHandItem();
double customRpm = 0; double customRpm = 0;
@ -120,15 +120,28 @@ public class ClientEventHandler {
} }
double rpm = stack.getOrCreateTag().getDouble("rpm") + customRpm; double rpm = stack.getOrCreateTag().getDouble("rpm") + customRpm;
if (rpm == 0) {
rpm = 600;
}
double rps = rpm / 60;
ClientTimer = Mth.clamp(ClientTimer - 25 * Minecraft.getInstance().getDeltaFrameTime(), 0, Double.POSITIVE_INFINITY); // cooldown in ms
double cooldown = 1000 / rps;
if (ClientTimer == 0 && GLFW.glfwGetMouseButton(Minecraft.getInstance().getWindow().getWindow(), GLFW.GLFW_MOUSE_BUTTON_LEFT) == GLFW.GLFW_PRESS) { if (!clientTimer.started()) {
ModUtils.PACKET_HANDLER.sendToServer(new ShootMessage(0)); clientTimer.start();
ClientTimer = 60 / (rpm == 0 ? 600 : rpm) * 1000; // 首发瞬间发射
clientTimer.setProgress((long) (cooldown + 1));
} }
player.displayClientMessage(Component.literal(new java.text.DecimalFormat("####").format(ClientTimer)), true); if (clientTimer.getProgress() >= cooldown) {
ModUtils.PACKET_HANDLER.sendToServer(new ShootMessage(0));
clientTimer.setProgress((long) (clientTimer.getProgress() - cooldown));
}
player.displayClientMessage(Component.literal(new java.text.DecimalFormat("####").format(clientTimer.getProgress())), true);
} else {
clientTimer.stop();
} }
} }

View file

@ -1,15 +0,0 @@
package net.mcreator.superbwarfare.tools;
public class AnimationTicker {
public static long startTime;
public static void reset() {
startTime = System.currentTimeMillis();
}
public static long getProgress() {
return System.currentTimeMillis() - startTime;
}
}

View file

@ -0,0 +1,34 @@
package net.mcreator.superbwarfare.tools;
public class MillisTimer {
public long startTime;
private boolean started = false;
public void start() {
if (!started) {
started = true;
startTime = System.currentTimeMillis();
}
}
public boolean started() {
return started;
}
public void stop() {
started = false;
}
public long getProgress() {
if (!started) {
return 0;
}
return System.currentTimeMillis() - startTime;
}
public void setProgress(long progress) {
startTime = System.currentTimeMillis() - progress;
}
}