抄飞车奇匠,但是没成功(恼

This commit is contained in:
Atsuishio 2025-05-06 00:02:39 +08:00 committed by Light_Quanta
parent f4d0dd52bd
commit 5f554adc7c
No known key found for this signature in database
GPG key ID: 11A39A1B8C890959
6 changed files with 121 additions and 47 deletions

View file

@ -0,0 +1,19 @@
package com.atsuishio.superbwarfare;
import com.atsuishio.superbwarfare.client.VehicleSoundInstance;
import com.atsuishio.superbwarfare.entity.vehicle.base.MobileVehicleEntity;
import net.minecraft.client.Minecraft;
public class ModClient {
public static void init() {
initEntities();
}
public static void initEntities() {
MobileVehicleEntity.engineSound = mobileVehicle -> {
var client = Minecraft.getInstance();
client.getSoundManager().play(new VehicleSoundInstance.EngineSound(client, mobileVehicle));
};
}
}

View file

@ -1,46 +0,0 @@
package com.atsuishio.superbwarfare.client;
import com.atsuishio.superbwarfare.entity.vehicle.Tom6Entity;
import net.minecraft.client.resources.sounds.AbstractTickableSoundInstance;
import net.minecraft.client.resources.sounds.SoundInstance;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.util.Mth;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.api.distmarker.OnlyIn;
@OnlyIn(Dist.CLIENT)
public class ClientVehicleSoundsInstance extends AbstractTickableSoundInstance {
private final Tom6Entity tom6Entity;
public ClientVehicleSoundsInstance(Tom6Entity pTom6Entity) {
super(SoundEvents.ELYTRA_FLYING, SoundSource.PLAYERS, SoundInstance.createUnseededRandom());
this.tom6Entity = pTom6Entity;
this.looping = true;
this.delay = 0;
this.volume = 0.0F;
this.x = pTom6Entity.getX();
this.y = pTom6Entity.getY();
this.z = pTom6Entity.getZ();
}
public void tick() {
if (this.tom6Entity.isRemoved()) {
this.stop();
} else {
this.x = tom6Entity.getX();
this.y = tom6Entity.getY();
this.z = tom6Entity.getZ();
float $$0 = (float) tom6Entity.getDeltaMovement().horizontalDistance();
if ($$0 >= 0.01F) {
this.pitch = Mth.clamp(this.pitch + 0.0025F, 0.0F, 1.0F);
this.volume = Mth.lerp(Mth.clamp($$0, 0.0F, 0.5F), 0.0F, 0.7F);
} else {
this.pitch = 0.0F;
this.volume = 0.0F;
}
}
}
}

View file

@ -0,0 +1,89 @@
package com.atsuishio.superbwarfare.client;
import com.atsuishio.superbwarfare.entity.vehicle.base.MobileVehicleEntity;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.sounds.AbstractTickableSoundInstance;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundSource;
public abstract class VehicleSoundInstance extends AbstractTickableSoundInstance {
private final Minecraft client;
private final MobileVehicleEntity mobileVehicle;
private double lastDistance;
private int fade = 0;
private boolean die = false;
public VehicleSoundInstance(SoundEvent sound, Minecraft client, MobileVehicleEntity mobileVehicle) {
super(sound, SoundSource.AMBIENT, mobileVehicle.getCommandSenderWorld().getRandom());
this.client = client;
this.mobileVehicle = mobileVehicle;
this.looping = true;
this.delay = 0;
}
protected abstract boolean canPlay(MobileVehicleEntity mobileVehicle);
protected abstract float getPitch(MobileVehicleEntity mobileVehicle);
protected abstract float getVolume(MobileVehicleEntity mobileVehicle);
@Override
public void tick() {
var player = this.client.player;
if (mobileVehicle.isRemoved() || player == null) {
this.stop();
return;
} else if (!this.canPlay(mobileVehicle)) {
this.die = true;
}
if (this.die) {
if (this.fade > 0) this.fade--;
else if (this.fade == 0) {
this.stop();
return;
}
} else if (this.fade < 3) {
this.fade++;
}
this.volume = this.getVolume(this.mobileVehicle) * (float)fade / 3;
this.x = this.mobileVehicle.getX();
this.y = this.mobileVehicle.getY();
this.z = this.mobileVehicle.getZ();
this.pitch = this.getPitch(this.mobileVehicle);
if (player.getVehicle() != this.mobileVehicle) {
double distance = this.mobileVehicle.position().subtract(player.position()).length();
this.pitch += (float) (0.36 * Math.atan(lastDistance - distance));
this.lastDistance = distance;
} else {
this.lastDistance = 0;
}
}
public static class EngineSound extends VehicleSoundInstance {
public EngineSound(Minecraft client, MobileVehicleEntity mobileVehicle) {
super(mobileVehicle.getEngineSound(), client, mobileVehicle);
}
@Override
protected boolean canPlay(MobileVehicleEntity mobileVehicle) {
return true;
}
@Override
protected float getPitch(MobileVehicleEntity mobileVehicle) {
return 1;
}
@Override
protected float getVolume(MobileVehicleEntity mobileVehicle) {
return 1;
}
}
}

View file

@ -234,7 +234,7 @@ public class Tom6Entity extends MobileVehicleEntity implements GeoEntity {
@Override @Override
public SoundEvent getEngineSound() { public SoundEvent getEngineSound() {
return ModSounds.WHEEL_CHAIR_ENGINE.get(); return SoundEvents.ELYTRA_FLYING;
} }
protected void clampRotation(Entity entity) { protected void clampRotation(Entity entity) {

View file

@ -47,9 +47,11 @@ import org.joml.Vector3f;
import org.joml.Vector4f; import org.joml.Vector4f;
import java.util.List; import java.util.List;
import java.util.function.Consumer;
import java.util.stream.StreamSupport; import java.util.stream.StreamSupport;
public abstract class MobileVehicleEntity extends EnergyVehicleEntity implements ControllableVehicle { public abstract class MobileVehicleEntity extends EnergyVehicleEntity implements ControllableVehicle {
public static Consumer<MobileVehicleEntity> engineSound = e -> {};
public static final EntityDataAccessor<Integer> CANNON_RECOIL_TIME = SynchedEntityData.defineId(MobileVehicleEntity.class, EntityDataSerializers.INT); public static final EntityDataAccessor<Integer> CANNON_RECOIL_TIME = SynchedEntityData.defineId(MobileVehicleEntity.class, EntityDataSerializers.INT);
public static final EntityDataAccessor<Float> POWER = SynchedEntityData.defineId(MobileVehicleEntity.class, EntityDataSerializers.FLOAT); public static final EntityDataAccessor<Float> POWER = SynchedEntityData.defineId(MobileVehicleEntity.class, EntityDataSerializers.FLOAT);
@ -192,6 +194,8 @@ public abstract class MobileVehicleEntity extends EnergyVehicleEntity implements
super.baseTick(); super.baseTick();
engineSound.accept(this);
double direct = (90 - calculateAngle(this.getDeltaMovement(), this.getViewVector(1))) / 90; double direct = (90 - calculateAngle(this.getDeltaMovement(), this.getViewVector(1))) / 90;
setVelocity(Mth.lerp(0.4, getVelocity(), getDeltaMovement().horizontalDistance() * direct * 20)); setVelocity(Mth.lerp(0.4, getVelocity(), getDeltaMovement().horizontalDistance() * direct * 20));

View file

@ -1,5 +1,6 @@
package com.atsuishio.superbwarfare.event; package com.atsuishio.superbwarfare.event;
import com.atsuishio.superbwarfare.ModClient;
import com.atsuishio.superbwarfare.entity.projectile.SwarmDroneEntity; import com.atsuishio.superbwarfare.entity.projectile.SwarmDroneEntity;
import com.atsuishio.superbwarfare.entity.vehicle.*; import com.atsuishio.superbwarfare.entity.vehicle.*;
import com.atsuishio.superbwarfare.entity.vehicle.base.MobileVehicleEntity; import com.atsuishio.superbwarfare.entity.vehicle.base.MobileVehicleEntity;
@ -18,6 +19,7 @@ import net.minecraft.world.phys.Vec3;
import net.neoforged.api.distmarker.Dist; import net.neoforged.api.distmarker.Dist;
import net.neoforged.bus.api.SubscribeEvent; import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber; import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent;
import net.neoforged.neoforge.client.event.ClientTickEvent; import net.neoforged.neoforge.client.event.ClientTickEvent;
import org.joml.Math; import org.joml.Math;
@ -29,6 +31,12 @@ import static com.atsuishio.superbwarfare.entity.vehicle.base.MobileVehicleEntit
@EventBusSubscriber(bus = EventBusSubscriber.Bus.GAME, value = Dist.CLIENT) @EventBusSubscriber(bus = EventBusSubscriber.Bus.GAME, value = Dist.CLIENT)
public class ClientSoundHandler { public class ClientSoundHandler {
@SubscribeEvent
public static void initClient(FMLClientSetupEvent setup) {
ModClient.init();
}
@SubscribeEvent @SubscribeEvent
public static void handleClientTick(ClientTickEvent.Pre event) { public static void handleClientTick(ClientTickEvent.Pre event) {
LocalPlayer player = Minecraft.getInstance().player; LocalPlayer player = Minecraft.getInstance().player;