尝试重置投射物音效

This commit is contained in:
17146 2025-05-24 17:30:22 +08:00 committed by Light_Quanta
parent 709d4b64ea
commit f49316cbc1
No known key found for this signature in database
GPG key ID: 11A39A1B8C890959
14 changed files with 88 additions and 97 deletions

View file

@ -3,6 +3,7 @@ package com.atsuishio.superbwarfare;
import com.atsuishio.superbwarfare.api.event.RegisterContainersEvent;
import com.atsuishio.superbwarfare.client.MouseMovementHandler;
import com.atsuishio.superbwarfare.client.renderer.molang.MolangVariable;
import com.atsuishio.superbwarfare.client.sound.FastProjectileSoundInstance;
import com.atsuishio.superbwarfare.client.sound.VehicleFireSoundInstance;
import com.atsuishio.superbwarfare.client.sound.VehicleSoundInstance;
import com.atsuishio.superbwarfare.compat.CompatHolder;
@ -11,6 +12,7 @@ import com.atsuishio.superbwarfare.component.ModDataComponents;
import com.atsuishio.superbwarfare.config.ClientConfig;
import com.atsuishio.superbwarfare.config.CommonConfig;
import com.atsuishio.superbwarfare.config.ServerConfig;
import com.atsuishio.superbwarfare.entity.projectile.FastThrowableProjectile;
import com.atsuishio.superbwarfare.entity.vehicle.A10Entity;
import com.atsuishio.superbwarfare.entity.vehicle.Hpj11Entity;
import com.atsuishio.superbwarfare.entity.vehicle.base.MobileVehicleEntity;
@ -135,6 +137,9 @@ public class Mod {
A10Entity.fireSound = vehicle -> Minecraft.getInstance().getSoundManager().play(new VehicleFireSoundInstance.A10FireSound(vehicle));
Hpj11Entity.fireSound = vehicle -> Minecraft.getInstance().getSoundManager().play(new VehicleFireSoundInstance.HPJ11CloseFireSound(vehicle));
FastThrowableProjectile.flySound = entity -> Minecraft.getInstance().getSoundManager().play(new FastProjectileSoundInstance.FlySound(entity));
FastThrowableProjectile.nearFlySound = entity -> Minecraft.getInstance().getSoundManager().play(new FastProjectileSoundInstance.NearFlySound(entity));
}
}

View file

@ -1,18 +0,0 @@
package com.atsuishio.superbwarfare.client.sound;
import com.atsuishio.superbwarfare.entity.LoudlyEntity;
import net.minecraft.client.Minecraft;
import net.minecraft.world.entity.Entity;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.api.distmarker.OnlyIn;
@OnlyIn(Dist.CLIENT)
public class ClientSoundHandler {
public static void playClientSoundInstance(Entity entity) {
if (entity instanceof LoudlyEntity) {
Minecraft.getInstance().getSoundManager().play(new LoudlyEntitySoundInstance.EntitySound(entity));
Minecraft.getInstance().getSoundManager().play(new LoudlyEntitySoundInstance.EntitySoundClose(entity));
}
}
}

View file

@ -1,21 +1,23 @@
package com.atsuishio.superbwarfare.client.sound;
import com.atsuishio.superbwarfare.entity.LoudlyEntity;
import com.atsuishio.superbwarfare.entity.projectile.FastThrowableProjectile;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.sounds.AbstractTickableSoundInstance;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.entity.Entity;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.api.distmarker.OnlyIn;
public abstract class LoudlyEntitySoundInstance extends AbstractTickableSoundInstance {
@OnlyIn(Dist.CLIENT)
public abstract class FastProjectileSoundInstance extends AbstractTickableSoundInstance {
private final Minecraft client;
private final Entity entity;
private final FastThrowableProjectile entity;
private double lastDistance;
private int fade = 0;
private boolean die = false;
public LoudlyEntitySoundInstance(SoundEvent sound, Minecraft client, Entity entity) {
public FastProjectileSoundInstance(SoundEvent sound, Minecraft client, FastThrowableProjectile entity) {
super(sound, SoundSource.AMBIENT, entity.getCommandSenderWorld().getRandom());
this.client = client;
this.entity = entity;
@ -23,11 +25,11 @@ public abstract class LoudlyEntitySoundInstance extends AbstractTickableSoundIns
this.delay = 0;
}
protected abstract boolean canPlay(Entity entity);
protected abstract boolean canPlay(FastThrowableProjectile entity);
protected abstract float getPitch(Entity entity);
protected abstract float getPitch(FastThrowableProjectile entity);
protected abstract float getVolume(Entity entity);
protected abstract float getVolume(FastThrowableProjectile entity);
@Override
public void tick() {
@ -67,51 +69,49 @@ public abstract class LoudlyEntitySoundInstance extends AbstractTickableSoundIns
}
}
public static class EntitySound extends LoudlyEntitySoundInstance {
public EntitySound(Entity entity) {
super(entity instanceof LoudlyEntity loudlyEntity ? loudlyEntity.getSound() : null, Minecraft.getInstance(), entity);
@OnlyIn(Dist.CLIENT)
public static class FlySound extends FastProjectileSoundInstance {
public FlySound(FastThrowableProjectile entity) {
super(entity.getSound(), Minecraft.getInstance(), entity);
}
@Override
protected boolean canPlay(Entity entity) {
return true;
protected boolean canPlay(FastThrowableProjectile entity) {
return entity.isFastMoving();
}
@Override
protected float getPitch(Entity entity) {
protected float getPitch(FastThrowableProjectile entity) {
return 1;
}
@Override
protected float getVolume(Entity entity) {
if (entity instanceof LoudlyEntity loudlyEntity) {
return (float) Math.min(loudlyEntity.getVolume() * 0.1 * entity.getDeltaMovement().length(), 1.5);
}
return 0;
protected float getVolume(FastThrowableProjectile entity) {
return (float) Math.min(entity.getVolume() * 0.1 * entity.getDeltaMovement().length(), 1.5);
}
}
public static class EntitySoundClose extends LoudlyEntitySoundInstance {
public EntitySoundClose(Entity entity) {
super(entity instanceof LoudlyEntity loudlyEntity ? loudlyEntity.getCloseSound() : null, Minecraft.getInstance(), entity);
@OnlyIn(Dist.CLIENT)
public static class NearFlySound extends FastProjectileSoundInstance {
public NearFlySound(FastThrowableProjectile entity) {
super(entity.getCloseSound(), Minecraft.getInstance(), entity);
}
@Override
protected boolean canPlay(Entity entity) {
return true;
protected boolean canPlay(FastThrowableProjectile entity) {
return entity.isFastMoving();
}
@Override
protected float getPitch(Entity entity) {
protected float getPitch(FastThrowableProjectile entity) {
return 1;
}
@Override
protected float getVolume(Entity entity) {
if (entity instanceof LoudlyEntity loudlyEntity) {
return (float) Math.min(loudlyEntity.getVolume() * 0.1 * entity.getDeltaMovement().length(), 1.5);
}
return 0;
protected float getVolume(FastThrowableProjectile entity) {
return (float) Math.min(entity.getVolume() * 0.1 * entity.getDeltaMovement().length(), 1.5);
}
}
}

View file

@ -1,18 +0,0 @@
package com.atsuishio.superbwarfare.entity;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
import org.jetbrains.annotations.NotNull;
public interface LoudlyEntity {
@NotNull
default SoundEvent getCloseSound() {
return SoundEvents.EMPTY;
}
@NotNull
SoundEvent getSound();
float getVolume();
}

View file

@ -1,7 +1,6 @@
package com.atsuishio.superbwarfare.entity.projectile;
import com.atsuishio.superbwarfare.config.server.ExplosionConfig;
import com.atsuishio.superbwarfare.entity.LoudlyEntity;
import com.atsuishio.superbwarfare.entity.vehicle.base.VehicleEntity;
import com.atsuishio.superbwarfare.init.ModDamageTypes;
import com.atsuishio.superbwarfare.init.ModEntities;
@ -41,6 +40,7 @@ import net.minecraft.world.phys.Vec3;
import net.neoforged.neoforge.event.EventHooks;
import net.neoforged.neoforge.network.PacketDistributor;
import org.jetbrains.annotations.NotNull;
import org.joml.Math;
import software.bernie.geckolib.animatable.GeoEntity;
import software.bernie.geckolib.animatable.instance.AnimatableInstanceCache;
import software.bernie.geckolib.animation.*;
@ -48,7 +48,8 @@ import software.bernie.geckolib.util.GeckoLibUtil;
import java.util.List;
public class Agm65Entity extends FastThrowableProjectile implements GeoEntity, DestroyableProjectileEntity, LoudlyEntity, ExplosiveProjectile {
public class Agm65Entity extends FastThrowableProjectile implements GeoEntity, DestroyableProjectileEntity, ExplosiveProjectile {
public static final EntityDataAccessor<Float> HEALTH = SynchedEntityData.defineId(Agm65Entity.class, EntityDataSerializers.FLOAT);
public static final EntityDataAccessor<String> TARGET_UUID = SynchedEntityData.defineId(Agm65Entity.class, EntityDataSerializers.STRING);
private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);

View file

@ -1,7 +1,6 @@
package com.atsuishio.superbwarfare.entity.projectile;
import com.atsuishio.superbwarfare.config.server.ExplosionConfig;
import com.atsuishio.superbwarfare.entity.LoudlyEntity;
import com.atsuishio.superbwarfare.entity.vehicle.base.VehicleEntity;
import com.atsuishio.superbwarfare.init.*;
import com.atsuishio.superbwarfare.network.message.receive.ClientIndicatorMessage;
@ -42,7 +41,8 @@ import software.bernie.geckolib.util.GeckoLibUtil;
import java.util.HashSet;
import java.util.Set;
public class CannonShellEntity extends FastThrowableProjectile implements GeoEntity, LoudlyEntity, ExplosiveProjectile {
public class CannonShellEntity extends FastThrowableProjectile implements GeoEntity, ExplosiveProjectile {
private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);
private float damage = 0;

View file

@ -2,6 +2,8 @@ package com.atsuishio.superbwarfare.entity.projectile;
import com.atsuishio.superbwarfare.network.message.receive.ClientMotionSyncMessage;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.projectile.ThrowableItemProjectile;
@ -9,11 +11,20 @@ import net.minecraft.world.level.Level;
import net.minecraft.world.phys.Vec3;
import net.neoforged.neoforge.entity.IEntityWithComplexSpawn;
import net.neoforged.neoforge.network.PacketDistributor;
import org.jetbrains.annotations.NotNull;
import javax.annotation.Nullable;
import java.util.function.Consumer;
public abstract class FastThrowableProjectile extends ThrowableItemProjectile implements CustomSyncMotionEntity, IEntityWithComplexSpawn {
public static Consumer<FastThrowableProjectile> flySound = projectile -> {
};
public static Consumer<FastThrowableProjectile> nearFlySound = projectile -> {
};
private boolean isFastMoving = false;
public FastThrowableProjectile(EntityType<? extends ThrowableItemProjectile> pEntityType, Level pLevel) {
super(pEntityType, pLevel);
}
@ -34,6 +45,12 @@ public abstract class FastThrowableProjectile extends ThrowableItemProjectile im
public void tick() {
super.tick();
if (!this.isFastMoving && this.isFastMoving() && this.level().isClientSide) {
flySound.accept(this);
nearFlySound.accept(this);
}
this.isFastMoving = this.isFastMoving();
Vec3 vec3 = this.getDeltaMovement();
float friction;
if (this.isInWater()) {
@ -62,6 +79,10 @@ public abstract class FastThrowableProjectile extends ThrowableItemProjectile im
}
}
public boolean isFastMoving() {
return this.getDeltaMovement().lengthSqr() >= 2500;
}
public boolean shouldSyncMotion() {
return false;
}
@ -78,4 +99,18 @@ public abstract class FastThrowableProjectile extends ThrowableItemProjectile im
public void readSpawnData(RegistryFriendlyByteBuf additionalData) {
this.setDeltaMovement(additionalData.readFloat(), additionalData.readFloat(), additionalData.readFloat());
}
@NotNull
public SoundEvent getCloseSound() {
return SoundEvents.EMPTY;
}
@NotNull
public SoundEvent getSound() {
return SoundEvents.EMPTY;
}
public float getVolume() {
return 0.5f;
}
}

View file

@ -1,7 +1,6 @@
package com.atsuishio.superbwarfare.entity.projectile;
import com.atsuishio.superbwarfare.config.server.ExplosionConfig;
import com.atsuishio.superbwarfare.entity.LoudlyEntity;
import com.atsuishio.superbwarfare.init.ModDamageTypes;
import com.atsuishio.superbwarfare.init.ModEntities;
import com.atsuishio.superbwarfare.init.ModItems;
@ -35,7 +34,7 @@ import software.bernie.geckolib.util.GeckoLibUtil;
import javax.annotation.Nullable;
public class HeliRocketEntity extends FastThrowableProjectile implements GeoEntity, LoudlyEntity, ExplosiveProjectile {
public class HeliRocketEntity extends FastThrowableProjectile implements GeoEntity, ExplosiveProjectile {
private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);
private float damage = 140f;
@ -59,14 +58,8 @@ public class HeliRocketEntity extends FastThrowableProjectile implements GeoEnti
this.explosionRadius = explosionRadius;
}
// TODO 音效
// public HeliRocketEntity(PlayMessages.SpawnEntity spawnEntity, Level level) {
// this(ModEntities.HELI_ROCKET.get(), level);
// DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> ClientSoundHandler.playClientSoundInstance(this));
// }
@Override
public void addAdditionalSaveData(CompoundTag pCompound) {
public void addAdditionalSaveData(@NotNull CompoundTag pCompound) {
super.addAdditionalSaveData(pCompound);
pCompound.putFloat("Damage", this.damage);
pCompound.putFloat("ExplosionDamage", this.explosionDamage);
@ -74,7 +67,7 @@ public class HeliRocketEntity extends FastThrowableProjectile implements GeoEnti
}
@Override
public void readAdditionalSaveData(CompoundTag pCompound) {
public void readAdditionalSaveData(@NotNull CompoundTag pCompound) {
super.readAdditionalSaveData(pCompound);
if (pCompound.contains("Damage")) {
this.damage = pCompound.getFloat("Damage");

View file

@ -1,7 +1,6 @@
package com.atsuishio.superbwarfare.entity.projectile;
import com.atsuishio.superbwarfare.config.server.ExplosionConfig;
import com.atsuishio.superbwarfare.entity.LoudlyEntity;
import com.atsuishio.superbwarfare.entity.vehicle.base.VehicleEntity;
import com.atsuishio.superbwarfare.init.ModDamageTypes;
import com.atsuishio.superbwarfare.init.ModEntities;
@ -50,7 +49,8 @@ import software.bernie.geckolib.util.GeckoLibUtil;
import java.util.List;
public class JavelinMissileEntity extends FastThrowableProjectile implements GeoEntity, DestroyableProjectileEntity, LoudlyEntity, ExplosiveProjectile {
public class JavelinMissileEntity extends FastThrowableProjectile implements GeoEntity, DestroyableProjectileEntity, ExplosiveProjectile {
public static final EntityDataAccessor<Float> HEALTH = SynchedEntityData.defineId(JavelinMissileEntity.class, EntityDataSerializers.FLOAT);
public static final EntityDataAccessor<String> TARGET_UUID = SynchedEntityData.defineId(JavelinMissileEntity.class, EntityDataSerializers.STRING);
public static final EntityDataAccessor<Boolean> TOP = SynchedEntityData.defineId(JavelinMissileEntity.class, EntityDataSerializers.BOOLEAN);

View file

@ -1,7 +1,6 @@
package com.atsuishio.superbwarfare.entity.projectile;
import com.atsuishio.superbwarfare.config.server.ExplosionConfig;
import com.atsuishio.superbwarfare.entity.LoudlyEntity;
import com.atsuishio.superbwarfare.init.ModEntities;
import com.atsuishio.superbwarfare.init.ModItems;
import com.atsuishio.superbwarfare.init.ModSounds;
@ -27,7 +26,8 @@ import software.bernie.geckolib.animatable.instance.AnimatableInstanceCache;
import software.bernie.geckolib.animation.*;
import software.bernie.geckolib.util.GeckoLibUtil;
public class Mk82Entity extends FastThrowableProjectile implements GeoEntity, DestroyableProjectileEntity, LoudlyEntity, AerialBombEntity {
public class Mk82Entity extends FastThrowableProjectile implements GeoEntity, DestroyableProjectileEntity, AerialBombEntity {
public static final EntityDataAccessor<Float> HEALTH = SynchedEntityData.defineId(Mk82Entity.class, EntityDataSerializers.FLOAT);
private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);
@ -152,11 +152,6 @@ public class Mk82Entity extends FastThrowableProjectile implements GeoEntity, De
return this.cache;
}
@Override
public @NotNull SoundEvent getCloseSound() {
return LoudlyEntity.super.getCloseSound();
}
@Override
public @NotNull SoundEvent getSound() {
return ModSounds.SHELL_FLY.get();

View file

@ -1,7 +1,6 @@
package com.atsuishio.superbwarfare.entity.projectile;
import com.atsuishio.superbwarfare.config.server.ExplosionConfig;
import com.atsuishio.superbwarfare.entity.LoudlyEntity;
import com.atsuishio.superbwarfare.init.ModDamageTypes;
import com.atsuishio.superbwarfare.init.ModEntities;
import com.atsuishio.superbwarfare.init.ModItems;
@ -47,7 +46,8 @@ import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
public class MortarShellEntity extends FastThrowableProjectile implements GeoEntity, LoudlyEntity, ExplosiveProjectile {
public class MortarShellEntity extends FastThrowableProjectile implements GeoEntity, ExplosiveProjectile {
private float damage = 50;
private float explosionDamage = ExplosionConfig.MORTAR_SHELL_EXPLOSION_DAMAGE.get();
private int life = 600;

View file

@ -1,7 +1,6 @@
package com.atsuishio.superbwarfare.entity.projectile;
import com.atsuishio.superbwarfare.config.server.ExplosionConfig;
import com.atsuishio.superbwarfare.entity.LoudlyEntity;
import com.atsuishio.superbwarfare.init.ModDamageTypes;
import com.atsuishio.superbwarfare.init.ModEntities;
import com.atsuishio.superbwarfare.init.ModItems;
@ -37,7 +36,7 @@ import software.bernie.geckolib.animatable.instance.AnimatableInstanceCache;
import software.bernie.geckolib.animation.*;
import software.bernie.geckolib.util.GeckoLibUtil;
public class RpgRocketEntity extends FastThrowableProjectile implements GeoEntity, LoudlyEntity, ExplosiveProjectile {
public class RpgRocketEntity extends FastThrowableProjectile implements GeoEntity, ExplosiveProjectile {
private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);

View file

@ -1,7 +1,6 @@
package com.atsuishio.superbwarfare.entity.projectile;
import com.atsuishio.superbwarfare.config.server.ExplosionConfig;
import com.atsuishio.superbwarfare.entity.LoudlyEntity;
import com.atsuishio.superbwarfare.init.ModDamageTypes;
import com.atsuishio.superbwarfare.init.ModEntities;
import com.atsuishio.superbwarfare.init.ModItems;
@ -45,7 +44,7 @@ import software.bernie.geckolib.util.GeckoLibUtil;
import javax.annotation.Nullable;
import java.util.List;
public class SwarmDroneEntity extends FastThrowableProjectile implements GeoEntity, DestroyableProjectileEntity, LoudlyEntity, ExplosiveProjectile {
public class SwarmDroneEntity extends FastThrowableProjectile implements GeoEntity, DestroyableProjectileEntity, ExplosiveProjectile {
public static final EntityDataAccessor<String> TARGET_UUID = SynchedEntityData.defineId(SwarmDroneEntity.class, EntityDataSerializers.STRING);
public static final EntityDataAccessor<Float> TARGET_X = SynchedEntityData.defineId(SwarmDroneEntity.class, EntityDataSerializers.FLOAT);

View file

@ -1,7 +1,6 @@
package com.atsuishio.superbwarfare.entity.projectile;
import com.atsuishio.superbwarfare.config.server.ExplosionConfig;
import com.atsuishio.superbwarfare.entity.LoudlyEntity;
import com.atsuishio.superbwarfare.entity.vehicle.base.VehicleEntity;
import com.atsuishio.superbwarfare.init.ModDamageTypes;
import com.atsuishio.superbwarfare.init.ModEntities;
@ -44,7 +43,8 @@ import software.bernie.geckolib.util.GeckoLibUtil;
import javax.annotation.Nullable;
public class WgMissileEntity extends FastThrowableProjectile implements GeoEntity, DestroyableProjectileEntity, LoudlyEntity, ExplosiveProjectile {
public class WgMissileEntity extends FastThrowableProjectile implements GeoEntity, DestroyableProjectileEntity, ExplosiveProjectile {
public static final EntityDataAccessor<Float> HEALTH = SynchedEntityData.defineId(WgMissileEntity.class, EntityDataSerializers.FLOAT);
private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);