优化开火动画的触发流程

This commit is contained in:
Atsuihsio 2024-09-30 03:28:12 +08:00
parent f01d4734d2
commit 203ed22b8d
7 changed files with 83 additions and 42 deletions

View file

@ -121,6 +121,7 @@ public class ModUtils {
addNetworkMessage(ModVariables.PlayerVariablesSyncMessage.class, ModVariables.PlayerVariablesSyncMessage::buffer, ModVariables.PlayerVariablesSyncMessage::new, ModVariables.PlayerVariablesSyncMessage::handler);
addNetworkMessage(ModVariables.PlayerVariablesSyncMessage.class, ModVariables.PlayerVariablesSyncMessage::buffer, ModVariables.PlayerVariablesSyncMessage::new, ModVariables.PlayerVariablesSyncMessage::handler);
addNetworkMessage(ShootMessage.class, ShootMessage::encode, ShootMessage::decode, ShootMessage::handler);
addNetworkMessage(ShootAnimationMessage.class, ShootAnimationMessage::encode, ShootAnimationMessage::decode, ShootAnimationMessage::handle, Optional.of(NetworkDirection.PLAY_TO_CLIENT));
event.enqueueWork(() -> BrewingRecipeRegistry.addRecipe(Ingredient.of(PotionUtils.setPotion(new ItemStack(Items.POTION), Potions.WATER)),
Ingredient.of(Items.LIGHTNING_ROD), PotionUtils.setPotion(new ItemStack(Items.POTION), ModPotion.SHOCK.get())));

View file

@ -36,9 +36,13 @@ import net.minecraftforge.client.event.ViewportEvent;
import net.minecraftforge.client.gui.overlay.VanillaGuiOverlay;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.LogicalSide;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.network.NetworkEvent;
import org.lwjgl.glfw.GLFW;
import java.util.function.Supplier;
import static net.mcreator.superbwarfare.entity.DroneEntity.ROT_X;
import static net.mcreator.superbwarfare.entity.DroneEntity.ROT_Z;
import static net.mcreator.superbwarfare.event.PlayerEventHandler.isProne;
@ -62,6 +66,7 @@ public class ClientEventHandler {
public static double[] turnRot = {0, 0, 0};
public static double[] cameraRot = {0, 0, 0};
public static double fireRecoilTime = 0;
public static double firePosTimer = 0;
public static double fireRotTimer = 0;
public static double firePos = 0;
@ -423,6 +428,12 @@ public class ClientEventHandler {
zoomPosZ = -Math.pow(2 * zoomTime - 1, 2) + 1;
}
public static void handleFireRecoilTimeMessage(double time, Supplier<NetworkEvent.Context> ctx) {
if (ctx.get().getDirection().getReceptionSide() == LogicalSide.CLIENT) {
fireRecoilTime = time;
}
}
private static void handleWeaponFire(ViewportEvent.ComputeCameraAngles event, LivingEntity entity) {
float times = 1.5f * Minecraft.getInstance().getDeltaFrameTime();
double yaw = event.getYaw();
@ -432,11 +443,12 @@ public class ClientEventHandler {
double amplitude = 15000 * stack.getOrCreateTag().getDouble("recoil_y") * stack.getOrCreateTag().getDouble("recoil_x");
var capability = entity.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null);
if (capability.orElse(new ModVariables.PlayerVariables()).firing > 0) {
if (fireRecoilTime > 0) {
firePosTimer = 0.001;
fireRotTimer = 0.001;
firePosZ = 0.1;
fireSpread += 0.2;
fireRecoilTime -= 7 * times;
}
fireSpread = Mth.clamp(fireSpread - 0.6 * (Math.pow(fireSpread, 2) * times), 0, 100);

View file

@ -409,7 +409,6 @@ public class PlayerEventHandler {
player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
capability.recoilHorizon = 2 * Math.random() - 1;
capability.recoil = 0.1;
capability.firing = 1;
capability.syncPlayerVariables(player);
});
tag.putBoolean("shoot", false);
@ -427,27 +426,11 @@ public class PlayerEventHandler {
player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
capability.recoilHorizon = 2 * Math.random() - 1;
capability.recoil = 0.1;
capability.firing = 1;
capability.syncPlayerVariables(player);
});
tag.putBoolean("shoot", false);
}
/*
开火动画计时器
*/
if ((player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new ModVariables.PlayerVariables())).firing > 0) {
player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
capability.firing = (player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new ModVariables.PlayerVariables())).firing - 0.1;
capability.syncPlayerVariables(player);
});
} else {
player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
capability.firing = 0;
capability.syncPlayerVariables(player);
});
}
/*
计算后坐力
*/

View file

@ -73,7 +73,6 @@ public class ModVariables {
clone.holdFire = original.holdFire;
clone.recoil = original.recoil;
clone.recoilHorizon = original.recoilHorizon;
clone.firing = original.firing;
clone.rifleAmmo = original.rifleAmmo;
clone.handgunAmmo = original.handgunAmmo;
clone.shotgunAmmo = original.shotgunAmmo;
@ -264,7 +263,6 @@ public class ModVariables {
public boolean holdFire = false;
public double recoil = 0;
public double recoilHorizon = 0;
public double firing = 0;
public int cannonRecoil = 0;
public int rifleAmmo = 0;
@ -292,7 +290,6 @@ public class ModVariables {
nbt.putBoolean("holdFire", holdFire);
nbt.putDouble("recoil", recoil);
nbt.putDouble("recoil_horizon", recoilHorizon);
nbt.putDouble("firing", firing);
nbt.putInt("cannonRecoil", cannonRecoil);
nbt.putInt("rifle_ammo", rifleAmmo);
nbt.putInt("handgun_ammo", handgunAmmo);
@ -317,7 +314,6 @@ public class ModVariables {
holdFire = nbt.getBoolean("holdFire");
recoil = nbt.getDouble("recoil");
recoilHorizon = nbt.getDouble("recoil_horizon");
firing = nbt.getDouble("firing");
cannonRecoil = nbt.getInt("cannonRecoil");
rifleAmmo = nbt.getInt("rifle_ammo");
handgunAmmo = nbt.getInt("handgun_ammo");
@ -373,7 +369,6 @@ public class ModVariables {
variables.holdFire = message.data.holdFire;
variables.recoil = message.data.recoil;
variables.recoilHorizon = message.data.recoilHorizon;
variables.firing = message.data.firing;
variables.cannonRecoil = message.data.cannonRecoil;
variables.rifleAmmo = message.data.rifleAmmo;
variables.handgunAmmo = message.data.handgunAmmo;

View file

@ -32,6 +32,7 @@ import net.minecraft.world.item.Items;
import net.minecraft.world.level.Level;
import net.minecraftforge.common.capabilities.ForgeCapabilities;
import net.minecraftforge.network.NetworkEvent;
import net.minecraftforge.network.PacketDistributor;
import org.joml.Vector3d;
import java.util.concurrent.atomic.AtomicBoolean;
@ -263,8 +264,6 @@ public class FireMessage {
player.playSound(ModSounds.BOCEK_ZOOM_FIRE_3P.get(), 2, 1);
}
} else {
stack.getOrCreateTag().putBoolean("shoot", true);
var perk = PerkHelper.getPerkByType(stack, Perk.Type.AMMO);
for (int index0 = 0; index0 < (perk == ModPerks.HE_BULLET.get() ? 1 : 10); index0++) {
@ -287,12 +286,6 @@ public class FireMessage {
}
}
player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
capability.recoil = 0.1;
capability.firing = 1;
capability.syncPlayerVariables(player);
});
player.getCooldowns().addCooldown(player.getMainHandItem().getItem(), 7);
player.getMainHandItem().getOrCreateTag().putInt("arrow_empty", 7);
player.getMainHandItem().getOrCreateTag().putDouble("power", 0);
@ -308,6 +301,11 @@ public class FireMessage {
if (count == 0 && !player.isCreative()) {
player.getInventory().clearOrCountMatchingItems(p -> Items.ARROW == p.getItem(), 1, player.inventoryMenu.getCraftSlots());
}
stack.getOrCreateTag().putBoolean("shoot", true);
if (player.level() instanceof ServerLevel && player instanceof ServerPlayer serverPlayer) {
ModUtils.PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> serverPlayer), new ShootAnimationMessage(10));
}
}
}
@ -409,14 +407,17 @@ public class FireMessage {
level.addFreshEntity(taserBulletProjectile);
}
stack.getOrCreateTag().putBoolean("shoot", true);
stack.getOrCreateTag().putInt("fire_animation", 4);
stack.getOrCreateTag().putInt("ammo", (stack.getOrCreateTag().getInt("ammo") - 1));
stack.getCapability(ForgeCapabilities.ENERGY).ifPresent(
energy -> energy.extractEnergy(2000 + 200 * perkLevel, false)
);
stack.getOrCreateTag().putBoolean("shoot", true);
if (player.level() instanceof ServerLevel && player instanceof ServerPlayer serverPlayer) {
ModUtils.PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> serverPlayer), new ShootAnimationMessage(10));
}
}
}
}
@ -463,10 +464,13 @@ public class FireMessage {
serverPlayer.level().playSound(null, serverPlayer.getOnPos(), ModSounds.M_79_VERYFAR.get(), SoundSource.PLAYERS, 10, 1);
}
stack.getOrCreateTag().putBoolean("shoot", true);
stack.getOrCreateTag().putInt("fire_animation", 2);
stack.getOrCreateTag().putInt("ammo", (stack.getOrCreateTag().getInt("ammo") - 1));
stack.getOrCreateTag().putBoolean("shoot", true);
if (player.level() instanceof ServerLevel && player instanceof ServerPlayer serverPlayer) {
ModUtils.PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> serverPlayer), new ShootAnimationMessage(10));
}
}
}
}
@ -520,10 +524,13 @@ public class FireMessage {
serverPlayer.level().playSound(null, serverPlayer.getOnPos(), ModSounds.RPG_VERYFAR.get(), SoundSource.PLAYERS, 10, 1);
}
tag.putBoolean("shoot", true);
tag.putInt("fire_animation", 2);
tag.putInt("ammo", tag.getInt("ammo") - 1);
stack.getOrCreateTag().putBoolean("shoot", true);
if (player.level() instanceof ServerLevel && player instanceof ServerPlayer serverPlayer) {
ModUtils.PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> serverPlayer), new ShootAnimationMessage(10));
}
}
}
@ -578,9 +585,12 @@ public class FireMessage {
serverPlayer.level().playSound(null, serverPlayer.getOnPos(), ModSounds.JAVELIN_FAR.get(), SoundSource.PLAYERS, 10, 1);
}
tag.putBoolean("shoot", true);
tag.putInt("fire_animation", 2);
tag.putInt("ammo", tag.getInt("ammo") - 1);
stack.getOrCreateTag().putBoolean("shoot", true);
if (player.level() instanceof ServerLevel && player instanceof ServerPlayer serverPlayer) {
ModUtils.PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> serverPlayer), new ShootAnimationMessage(10));
}
}
}

View file

@ -0,0 +1,31 @@
package net.mcreator.superbwarfare.network.message;
import net.mcreator.superbwarfare.event.ClientEventHandler;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.network.NetworkEvent;
import java.util.function.Supplier;
public class ShootAnimationMessage {
public double time;
public ShootAnimationMessage(double time) {
this.time = time;
}
public static void encode(ShootAnimationMessage message, FriendlyByteBuf buffer) {
buffer.writeDouble(message.time);
}
public static ShootAnimationMessage decode(FriendlyByteBuf buffer) {
return new ShootAnimationMessage(buffer.readDouble());
}
public static void handle(ShootAnimationMessage message, Supplier<NetworkEvent.Context> context) {
context.get().enqueueWork(() -> DistExecutor.unsafeRunWhenOn(Dist.CLIENT,
() -> () -> ClientEventHandler.handleFireRecoilTimeMessage(message.time, context)));
context.get().setPacketHandled(true);
}
}

View file

@ -1,5 +1,6 @@
package net.mcreator.superbwarfare.network.message;
import net.mcreator.superbwarfare.ModUtils;
import net.mcreator.superbwarfare.init.ModItems;
import net.mcreator.superbwarfare.init.ModPerks;
import net.mcreator.superbwarfare.init.ModSounds;
@ -20,6 +21,7 @@ import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraftforge.common.capabilities.ForgeCapabilities;
import net.minecraftforge.network.NetworkEvent;
import net.minecraftforge.network.PacketDistributor;
import java.util.function.Supplier;
@ -159,6 +161,11 @@ public class ShootMessage {
playGunSounds(player);
stack.getOrCreateTag().putBoolean("shoot", true);
if (player.level() instanceof ServerLevel && player instanceof ServerPlayer serverPlayer) {
ModUtils.PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> serverPlayer), new ShootAnimationMessage(10));
}
}
} else if (stack.is(ModItems.MINIGUN.get())) {
var tag = stack.getOrCreateTag();
@ -194,8 +201,6 @@ public class ShootMessage {
}
}
stack.getOrCreateTag().putBoolean("shoot", true);
for (int index0 = 0; index0 < (perk == ModPerks.HE_BULLET.get() ? 1 : (int) stack.getOrCreateTag().getDouble("projectile_amount")); index0++) {
gunShoot(player, spared);
}
@ -208,8 +213,12 @@ public class ShootMessage {
}
tag.putInt("fire_animation", 2);
}
stack.getOrCreateTag().putBoolean("shoot", true);
if (player.level() instanceof ServerLevel && player instanceof ServerPlayer serverPlayer) {
ModUtils.PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> serverPlayer), new ShootAnimationMessage(10));
}
}
}
}
}