Skip to content

Commit

Permalink
fix: apply cockpit sound
Browse files Browse the repository at this point in the history
  • Loading branch information
HamaIndustries committed Nov 17, 2024
1 parent 9c21368 commit 4face8b
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
30 changes: 30 additions & 0 deletions src/generated/resources/data/armistice/model_bones/hull/null.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[
{
"autouv": 0,
"children": [
{
"autouv": 0,
"export": true,
"mirror_uv": false,
"name": "ordnance1",
"origin": [
0.0,
0.0,
0.0
],
"uuid": "045a875f-65a0-d37e-651f-ebd1cc54b12b",
"visibility": true
}
],
"export": true,
"mirror_uv": false,
"name": "root",
"origin": [
0.0,
1.0,
4.0
],
"uuid": "b9b05ff0-47ee-87bf-8e04-e83d5046d880",
"visibility": true
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package symbolics.division.armistice.client.sound;

import net.minecraft.client.resources.sounds.SimpleSoundInstance;
import net.minecraft.client.resources.sounds.TickableSoundInstance;
import net.minecraft.sounds.SoundSource;
import net.minecraft.util.RandomSource;
import net.minecraft.world.entity.player.Player;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.api.distmarker.OnlyIn;
import symbolics.division.armistice.mecha.MechaEntity;
import symbolics.division.armistice.registry.ArmisticeSoundEventRegistrar;

@OnlyIn(Dist.CLIENT)
public class CockpitSoundInstance extends SimpleSoundInstance implements TickableSoundInstance {
private final MechaEntity mecha;
private final Player pilot;

public CockpitSoundInstance(MechaEntity mecha, Player pilot, float volume, float pitch, RandomSource random) {
super(ArmisticeSoundEventRegistrar.ENTITY$MECHA$COCKPIT.getLocation(), SoundSource.HOSTILE, volume, pitch, random, true, 0, Attenuation.NONE, 0, 0, 0, true);
this.mecha = mecha;
this.pilot = pilot;
}

@Override
public boolean isStopped() {
return !mecha.hasPassenger(pilot);
}

@Override
public void tick() {
}
}
54 changes: 42 additions & 12 deletions src/main/java/symbolics/division/armistice/mecha/MechaCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,25 @@
import au.edu.federation.caliko.FabrikStructure3D;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.resources.sounds.SimpleSoundInstance;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.Vec3;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.api.distmarker.OnlyIn;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.VisibleForTesting;
import org.joml.Quaternionf;
import org.joml.Vector2f;
import org.joml.Vector2fc;
import org.joml.Vector3f;
import symbolics.division.armistice.client.sound.CockpitSoundInstance;
import symbolics.division.armistice.mecha.movement.ChassisLeg;
import symbolics.division.armistice.mecha.movement.Euclidean;
import symbolics.division.armistice.mecha.schematic.MechaSchematic;
Expand Down Expand Up @@ -46,6 +53,7 @@ public class MechaCore implements Part {
private MechaEntity entity = null;

private int soundCooldown;
private CockpitSoundInstance clientSound = null;

public static final StreamCodec<RegistryFriendlyByteBuf, MechaCore> TO_CLIENT_STREAM_CODEC = StreamCodec.of(
(buffer, value) -> {
Expand Down Expand Up @@ -78,10 +86,44 @@ public void init(MechaCore core) {
}

@Override
@OnlyIn(Dist.CLIENT)
public void clientTick(float tickDelta) {
Part.super.clientTick(tickDelta);

chassis.clientTick(tickDelta);

Player player = Minecraft.getInstance().player;

if (entity.hasPassenger(player)) {
// play interior sound
if (clientSound == null || clientSound.isStopped()) {
Minecraft.getInstance().getSoundManager().stop();
clientSound = new CockpitSoundInstance(entity, player, 0.35f, 1, entity.getRandom());
Minecraft.getInstance().getSoundManager().play(
clientSound
);
}
} else {
if (clientSound != null) {
clientSound = null;
}
soundCooldown--;
if (soundCooldown <= 0) {
if (player.distanceTo(entity) <= 40) {
Minecraft.getInstance().getSoundManager().play(
new SimpleSoundInstance(ArmisticeSoundEventRegistrar.AMBIENT$GEIGER, SoundSource.NEUTRAL, 2, entity.getRandom().nextFloat() * (1.25F - 0.75F) + 0.75F, entity.getRandom(), entity.blockPosition())
);
} else {
Minecraft.getInstance().getSoundManager().play(
new SimpleSoundInstance(
entity.getRandom().nextBoolean() ? ArmisticeSoundEventRegistrar.AMBIENT$MECHA1 : ArmisticeSoundEventRegistrar.AMBIENT$MECHA2,
SoundSource.NEUTRAL, 4, entity.getRandom().nextFloat() * (1.25F - 0.75F) + 0.75F, entity.getRandom(), entity.blockPosition())
);
}
soundCooldown = entity.getRandom().nextIntBetweenInclusive(20 * 20, 40 * 20);
}
}

}

@Override
Expand All @@ -90,18 +132,6 @@ public void serverTick() {

chassis.serverTick();
entity.setDeltaMovement(acceleration());

soundCooldown--;

if (soundCooldown <= 0) {
entity.playSound(
entity.getRandom().nextBoolean() ? ArmisticeSoundEventRegistrar.AMBIENT$MECHA1 : ArmisticeSoundEventRegistrar.AMBIENT$MECHA2,
1.0F,
entity.getRandom().nextFloat() * (1.25F - 0.75F) + 0.75F
);

soundCooldown = entity.getRandom().nextIntBetweenInclusive(20 * 20, 40 * 20);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"meta":{"format_version":"4.10","model_format":"free","box_uv":false},"name":"null","model_identifier":"","visible_box":[1,1,0],"variable_placeholders":"","variable_placeholder_buttons":[],"timeline_setups":[],"unhandled_root_fields":{},"resolution":{"width":16,"height":16},"elements":[],"outliner":[{"name":"root","origin":[0,1,4],"color":0,"uuid":"b9b05ff0-47ee-87bf-8e04-e83d5046d880","export":true,"mirror_uv":false,"isOpen":true,"locked":false,"visibility":true,"autouv":0,"children":[{"name":"ordnance1","origin":[0,0,0],"color":0,"uuid":"045a875f-65a0-d37e-651f-ebd1cc54b12b","export":true,"mirror_uv":false,"isOpen":true,"locked":false,"visibility":true,"autouv":0,"children":[]}]}],"textures":[]}
Binary file not shown.
12 changes: 12 additions & 0 deletions src/main/resources/data/armistice/armistice/hull/null.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"tier": 1,
"slots": [
1
],
"heat": {
"max": 100,
"delay": 20,
"decay": 5
},
"id": "armistice:null"
}

0 comments on commit 4face8b

Please sign in to comment.