diff --git a/projects/pswg/src/main/java/com/parzivail/pswg/Client.java b/projects/pswg/src/main/java/com/parzivail/pswg/Client.java index e41935153..427acfd95 100644 --- a/projects/pswg/src/main/java/com/parzivail/pswg/Client.java +++ b/projects/pswg/src/main/java/com/parzivail/pswg/Client.java @@ -23,6 +23,7 @@ import com.parzivail.pswg.client.screen.CrateGenericSmallScreen; import com.parzivail.pswg.client.screen.CrateOctagonScreen; import com.parzivail.pswg.client.screen.MoistureVaporatorScreen; +import com.parzivail.pswg.client.sound.EnvironmentSoundManager; import com.parzivail.pswg.container.*; import com.parzivail.pswg.entity.ship.ShipEntity; import com.parzivail.pswg.features.blasters.BlasterItem; @@ -160,6 +161,7 @@ public void onInitializeClient() ClientTickEvents.START_CLIENT_TICK.register(KeyHandler::tick); ClientTickEvents.START_CLIENT_TICK.register(BlasterRecoilManager::tick); + ClientTickEvents.START_CLIENT_TICK.register(EnvironmentSoundManager::tick); ClientTickEvents.END_CLIENT_TICK.register(BlasterZoomHandler::tick); ClientTickEvents.END_CLIENT_TICK.register(DebugUtil::tick); diff --git a/projects/pswg/src/main/java/com/parzivail/pswg/client/sound/EnvironmentSoundManager.java b/projects/pswg/src/main/java/com/parzivail/pswg/client/sound/EnvironmentSoundManager.java new file mode 100644 index 000000000..686bbba81 --- /dev/null +++ b/projects/pswg/src/main/java/com/parzivail/pswg/client/sound/EnvironmentSoundManager.java @@ -0,0 +1,43 @@ +package com.parzivail.pswg.client.sound; + +import com.parzivail.pswg.entity.BlasterBoltEntity; +import net.minecraft.client.MinecraftClient; +import net.minecraft.sound.SoundCategory; +import net.minecraft.sound.SoundEvents; +import net.minecraft.util.math.Box; + +public class EnvironmentSoundManager +{ + public static void tick(MinecraftClient mc) + { + if (mc.player == null || mc.world == null) + return; + + var pos = mc.player.getEyePos(); + + var box = Box.of(mc.player.getPos(), 16, 16, 16); + for (var entity : mc.world.getEntitiesByClass(BlasterBoltEntity.class, box, e -> true)) + { + var entityDirection = entity.getVelocity(); + + var newEntityPos = entity.getLerpedPos(1); + + var oldPlayerDelta = entity.getLerpedPos(0).subtract(pos); + var newPlayerDelta = newEntityPos.subtract(pos); + + var oldPlayerDot = entityDirection.dotProduct(oldPlayerDelta); + var newPlayerDot = entityDirection.dotProduct(newPlayerDelta); + + if (oldPlayerDot >= 0 && newPlayerDot < 0) + { + // Entity transitioned from moving towards the + // player (i.e. pos-player vector and direction + // vector face the same direction) to away from + // the player (i.e. vectors point opposite + // directions) + + mc.world.playSound(newEntityPos.x, newEntityPos.y, newEntityPos.z, SoundEvents.ENTITY_GHAST_SCREAM, SoundCategory.PLAYERS, 1, 1, true); + } + } + } +} diff --git a/projects/pswg/src/main/java/com/parzivail/pswg/client/sound/SoundHelper.java b/projects/pswg/src/main/java/com/parzivail/pswg/client/sound/SoundHelper.java index dac83d700..8b9fc22e5 100644 --- a/projects/pswg/src/main/java/com/parzivail/pswg/client/sound/SoundHelper.java +++ b/projects/pswg/src/main/java/com/parzivail/pswg/client/sound/SoundHelper.java @@ -1,9 +1,7 @@ package com.parzivail.pswg.client.sound; -import com.parzivail.pswg.features.blasters.client.BlasterBoltHissSoundInstance; -import com.parzivail.pswg.entity.BlasterBoltEntity; -import com.parzivail.pswg.features.lightsabers.client.ThrownLightsaberEntity; import com.parzivail.pswg.entity.ship.ShipEntity; +import com.parzivail.pswg.features.lightsabers.client.ThrownLightsaberEntity; import net.minecraft.client.MinecraftClient; import net.minecraft.client.sound.SoundInstance; import net.minecraft.sound.SoundEvent; @@ -16,12 +14,6 @@ public static void playThrownLightsaberSound(ThrownLightsaberEntity entity) minecraft.getSoundManager().play(new LightsaberThrownSoundInstance(entity)); } - public static void playBlasterBoltHissSound(BlasterBoltEntity entity) - { - var minecraft = MinecraftClient.getInstance(); - minecraft.getSoundManager().play(new BlasterBoltHissSoundInstance(entity)); - } - public static void playShipExteriorSound(ShipEntity entity, SoundEvent sound) { var minecraft = MinecraftClient.getInstance(); diff --git a/projects/pswg/src/main/java/com/parzivail/pswg/entity/BlasterBoltEntity.java b/projects/pswg/src/main/java/com/parzivail/pswg/entity/BlasterBoltEntity.java index b4ce9ffa3..87c86c547 100644 --- a/projects/pswg/src/main/java/com/parzivail/pswg/entity/BlasterBoltEntity.java +++ b/projects/pswg/src/main/java/com/parzivail/pswg/entity/BlasterBoltEntity.java @@ -1,7 +1,6 @@ package com.parzivail.pswg.entity; import com.parzivail.pswg.Resources; -import com.parzivail.pswg.client.sound.SoundHelper; import com.parzivail.pswg.container.SwgDamageTypes; import com.parzivail.pswg.container.SwgPackets; import com.parzivail.pswg.container.SwgParticles; @@ -123,7 +122,6 @@ public Packet createSpawnPacket() public void onSpawnPacket(EntitySpawnS2CPacket packet) { super.onSpawnPacket(packet); - SoundHelper.playBlasterBoltHissSound(this); if (packet instanceof PreciseEntitySpawnS2CPacket pes) { diff --git a/projects/pswg/src/main/java/com/parzivail/pswg/features/blasters/client/BlasterBoltHissSoundInstance.java b/projects/pswg/src/main/java/com/parzivail/pswg/features/blasters/client/BlasterBoltHissSoundInstance.java deleted file mode 100644 index 3e83d3147..000000000 --- a/projects/pswg/src/main/java/com/parzivail/pswg/features/blasters/client/BlasterBoltHissSoundInstance.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.parzivail.pswg.features.blasters.client; - -import com.parzivail.pswg.container.SwgSounds; -import com.parzivail.pswg.entity.BlasterBoltEntity; -import com.parzivail.util.sound.DopplerSoundInstance; -import net.fabricmc.api.EnvType; -import net.fabricmc.api.Environment; -import net.minecraft.client.sound.SoundInstance; -import net.minecraft.sound.SoundCategory; - -@Environment(EnvType.CLIENT) -public class BlasterBoltHissSoundInstance extends DopplerSoundInstance -{ - public BlasterBoltHissSoundInstance(BlasterBoltEntity entity) - { - super(entity, SwgSounds.Blaster.HISS, SoundCategory.PLAYERS, SoundInstance.createRandom()); - this.repeat = true; - this.repeatDelay = 0; - this.volume = 0.0f; - } - - @Override - public boolean canPlay() - { - return !this.source.isSilent(); - } - - @Override - public boolean shouldAlwaysPlay() - { - return true; - } - - @Override - public void tick() - { - super.tick(); - - if (this.source.isRemoved()) - { - this.setDone(); - return; - } - - this.volume = 1; - this.x = (float)this.source.getX(); - this.y = (float)this.source.getY(); - this.z = (float)this.source.getZ(); - } -} diff --git a/projects/pswg/src/main/java/com/parzivail/util/math/Vec3fUtil.java b/projects/pswg/src/main/java/com/parzivail/util/math/Vec3fUtil.java deleted file mode 100644 index 9f93d23ac..000000000 --- a/projects/pswg/src/main/java/com/parzivail/util/math/Vec3fUtil.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.parzivail.util.math; - -public class Vec3fUtil -{ - -}