Skip to content

Commit

Permalink
Implemented voicelines. Trying to implement skidding sounds
Browse files Browse the repository at this point in the history
  • Loading branch information
floral-qua-floral committed Oct 24, 2024
1 parent 9ab2978 commit b2232f5
Show file tree
Hide file tree
Showing 28 changed files with 310 additions and 101 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/floralquafloral/MarioPackets.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public static void registerCommon() {
SyncUseCharacterStatsS2CPayload.register();
MarioDataPackets.registerCommon();
StompHandler.registerPackets();
VoiceLine.registerPackets();

ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> {
MarioQuaMario.LOGGER.info("");
Expand All @@ -31,6 +32,7 @@ public static void registerClient() {
SyncUseCharacterStatsS2CPayload.registerReceiver();
MarioDataPackets.registerClient();
StompHandler.registerPacketsClient();
VoiceLine.registerPacketsClient();
}

public static PlayerEntity getPlayerFromInt(ClientPlayNetworking.Context context, int playerID) {
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/com/floralquafloral/MarioQuaMario.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.floralquafloral.mariodata.MarioDataManager;
import com.floralquafloral.mariodata.MarioDataPackets;
import com.floralquafloral.registries.RegistryManager;
import com.floralquafloral.registries.states.powerup.ParsedPowerUp;
import com.floralquafloral.util.ModConfig;
import com.floralquafloral.util.MarioSFX;
import me.shedaniel.autoconfig.AutoConfig;
Expand All @@ -20,7 +19,6 @@
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.Identifier;
import net.minecraft.world.GameRules;
import org.slf4j.Logger;
Expand Down
78 changes: 77 additions & 1 deletion src/main/java/com/floralquafloral/VoiceLine.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
package com.floralquafloral;

import com.floralquafloral.mariodata.MarioData;
import com.floralquafloral.registries.RegistryManager;
import com.floralquafloral.registries.states.character.ParsedCharacter;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.sound.PositionedSoundInstance;
import net.minecraft.client.sound.SoundManager;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.codec.PacketCodecs;
import net.minecraft.network.packet.CustomPayload;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvent;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.random.Random;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public enum VoiceLine {
Expand Down Expand Up @@ -40,10 +54,72 @@ public enum VoiceLine {
SOUND_EVENTS = new HashMap<>();

for(ParsedCharacter character : RegistryManager.CHARACTERS) {
Identifier id = Identifier.of(character.ID.getNamespace(), "voice." + character.ID.getPath() + "." + this.name().toLowerCase());
Identifier id = Identifier.of(character.ID.getNamespace(), "voice." + character.ID.getPath() + "." + this.name().toLowerCase(Locale.ROOT));
MarioQuaMario.LOGGER.info("Automatically registering VoiceLine sound event {}...", id);
SoundEvent event = SoundEvent.of(id);
SOUND_EVENTS.put(character, event);
Registry.register(Registries.SOUND_EVENT, id, event);
}
}

public void play(MarioData data, long seed) {
MarioQuaMario.LOGGER.info("UWU: {}", SOUND_EVENTS.get(data.getCharacter()));

PlayerEntity mario = data.getMario();

if(mario.getWorld().isClient) {
PositionedSoundInstance prevVoiceSound = PLAYER_VOICE_LINES.get(mario);
SoundManager soundManager = MinecraftClient.getInstance().getSoundManager();
soundManager.stop(prevVoiceSound);

PositionedSoundInstance voiceSound = new PositionedSoundInstance(
SOUND_EVENTS.get(data.getCharacter()),
SoundCategory.VOICE,
1.0F,
1.0F,
Random.create(seed),
data.getMario().getX(),
data.getMario().getY(),
data.getMario().getZ()
);
soundManager.play(voiceSound);
PLAYER_VOICE_LINES.put(mario, voiceSound);
}
else {
MarioQuaMario.LOGGER.info("Send voiceline packet!!!");
MarioPackets.sendPacketToTrackers((ServerPlayerEntity) data.getMario(), new PlayVoiceLineS2CPayload(data.getMario(), this, seed));
}
}

public static void registerPackets() {
PlayVoiceLineS2CPayload.register();
}
public static void registerPacketsClient() {
PlayVoiceLineS2CPayload.registerReceiver();
}

private record PlayVoiceLineS2CPayload(int player, int voiceLineOrdinal, long seed) implements CustomPayload {
public static final Id<PlayVoiceLineS2CPayload> ID = new Id<>(Identifier.of(MarioQuaMario.MOD_ID, "play_voice_line"));
public static final PacketCodec<RegistryByteBuf, PlayVoiceLineS2CPayload> CODEC = PacketCodec.tuple(
PacketCodecs.INTEGER, PlayVoiceLineS2CPayload::player,
PacketCodecs.INTEGER, PlayVoiceLineS2CPayload::voiceLineOrdinal,
PacketCodecs.VAR_LONG, PlayVoiceLineS2CPayload::seed,
PlayVoiceLineS2CPayload::new
);
public PlayVoiceLineS2CPayload(PlayerEntity player, VoiceLine voiceLine, long seed) {
this(player.getId(), voiceLine.ordinal(), seed);
}
public static void registerReceiver() {
ClientPlayNetworking.registerGlobalReceiver(ID, (payload, context) -> {

});
}

@Override public Id<? extends CustomPayload> getId() {
return ID;
}
public static void register() {
PayloadTypeRegistry.playS2C().register(ID, CODEC);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static void sendAllData(ServerPlayerEntity toWho, PlayerEntity aboutWho)
// am I supposed to send one packet with all this data or is this fine????
ServerPlayNetworking.send(toWho, new SetEnabledS2CPayload(aboutWho, data.isEnabled()));
ServerPlayNetworking.send(toWho, new SetActionS2CPayload(aboutWho, data.getAction(), true, 0));
ServerPlayNetworking.send(toWho, new SetPowerUpS2CPayload(aboutWho, data.getPowerUp()));
ServerPlayNetworking.send(toWho, new SetPowerUpS2CPayload(aboutWho, data.getPowerUp(), true));
ServerPlayNetworking.send(toWho, new SetCharacterS2CPayload(aboutWho, data.getCharacter()));
}

Expand Down Expand Up @@ -93,7 +93,7 @@ public static String forceSetMarioAction(ServerPlayerEntity mario, ParsedAction

public static String setMarioPowerUp(ServerPlayerEntity mario, ParsedPowerUp powerUp) {
getMarioData(mario).setPowerUp(powerUp);
MarioPackets.sendPacketToTrackers(mario, new SetPowerUpS2CPayload(mario, powerUp));
MarioPackets.sendPacketToTrackers(mario, new SetPowerUpS2CPayload(mario, powerUp, false));

return(mario.getName().getString() + "'s Power-up has been set to " + powerUp.ID);
}
Expand Down Expand Up @@ -159,15 +159,16 @@ public static void register() {
}
}

private record SetPowerUpS2CPayload(int player, int powerUp) implements CustomPayload {
private record SetPowerUpS2CPayload(int player, int powerUp, boolean transitionless) implements CustomPayload {
public static final Id<SetPowerUpS2CPayload> ID = new Id<>(Identifier.of(MarioQuaMario.MOD_ID, "set_power_up"));
public static final PacketCodec<RegistryByteBuf, SetPowerUpS2CPayload> CODEC = PacketCodec.tuple(
PacketCodecs.INTEGER, SetPowerUpS2CPayload::player,
PacketCodecs.INTEGER, SetPowerUpS2CPayload::powerUp,
PacketCodecs.BOOL, SetPowerUpS2CPayload::transitionless,
SetPowerUpS2CPayload::new
);
public SetPowerUpS2CPayload(PlayerEntity player, ParsedPowerUp powerUp) {
this(player.getId(), RegistryManager.POWER_UPS.getRawIdOrThrow(powerUp));
public SetPowerUpS2CPayload(PlayerEntity player, ParsedPowerUp powerUp, boolean transitionless) {
this(player.getId(), RegistryManager.POWER_UPS.getRawIdOrThrow(powerUp), transitionless);
}
public static void registerReceiver() {
ClientPlayNetworking.registerGlobalReceiver(ID, (payload, context) ->
Expand Down
88 changes: 87 additions & 1 deletion src/main/java/com/floralquafloral/mariodata/MarioPlayerData.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@
import com.floralquafloral.registries.states.character.ParsedCharacter;
import com.floralquafloral.registries.states.powerup.ParsedPowerUp;
import com.floralquafloral.util.CPMIntegration;
import com.floralquafloral.util.MarioSFX;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.sound.*;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvent;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.math.random.Random;
import org.joml.Vector2d;

public class MarioPlayerData implements MarioData {
private boolean enabled;
Expand Down Expand Up @@ -150,12 +157,91 @@ public void tick() {
@Override public boolean getSneakProhibited() {
return useMarioPhysics() && getAction().SNEAK_LEGALITY.prohibitSneak();
}

private class SkidSoundInstance extends MovingSoundInstance {
private final PlayerEntity MARIO;
private final boolean IS_WALL;
private final SkidMaterial MATERIAL;
private int ticks;

protected SkidSoundInstance(boolean isWall, SkidMaterial material) {
super(isWall ? MarioSFX.SKID_WALL : MarioSFX.SKID_BLOCK, SoundCategory.PLAYERS, SoundInstance.createRandom());
this.MARIO = getMario();
this.IS_WALL = isWall;
this.MATERIAL = material;
this.updatePos();
this.repeat = true;
this.repeatDelay = 0;
this.volume = 1.0F;
this.ticks = 0;
}

private static SkidMaterial getFloorSkidMaterial(PlayerEntity mario) {

return SkidMaterial.BASIC;
}

private enum SkidMaterial {
BASIC(MarioSFX.SKID_BLOCK);

public final SoundEvent EVENT;
SkidMaterial(SoundEvent event) {
this.EVENT = event;
}
}

@Override public void tick() {
this.ticks++;
MarioQuaMario.LOGGER.info("Ticking sound!");
if(this.IS_WALL || this.MARIO.isOnGround()) {
SkidMaterial newMaterial = getFloorSkidMaterial(this.MARIO);
if(newMaterial != this.MATERIAL && !this.IS_WALL) {
MarioQuaMario.LOGGER.info("Switching skid sound effect!");
this.kill();
// SkidSoundInstance.create(this.DATA, false);
}
else this.updatePos();
}
else this.volume = 0.0F;
}

private void updatePos() {
this.x = this.MARIO.getX();
this.y = this.MARIO.getY();
this.z = this.MARIO.getZ();
MarioQuaMario.LOGGER.info("X: " + this.x);
if(this.IS_WALL) return;
float slidingSpeed = (float) this.MARIO.getVelocity().horizontalLengthSquared();
this.volume = Math.min(1.0F, ((float) ticks) / 3.0F) * Math.min(1.0F, 0.7F * slidingSpeed);
this.pitch = 1.0F + Math.min(0.15F, 0.5F * slidingSpeed);
}

@Override public boolean shouldAlwaysPlay() {
return true;
}

private void kill() {
skidSFX = null;
this.setDone();
}
}
private SkidSoundInstance skidSFX;

@Override public void setAction(ParsedAction action, long seed) {
getAction().transitionTo(this, action, seed);
this.setActionTransitionless(action);
}
@Override public void setActionTransitionless(ParsedAction action) {
if(!this.mario.getWorld().isClient) {
if(this.mario.getWorld().isClient) {
// Skid SFX
if(this.skidSFX != null) this.skidSFX.kill();
if(action.SLIDING_STATUS.doSlideSfx() || action.SLIDING_STATUS.doWallSlideSfx()) {
this.skidSFX = new SkidSoundInstance(action.SLIDING_STATUS.doWallSlideSfx(), SkidSoundInstance.getFloorSkidMaterial(getMario()));
MinecraftClient.getInstance().getSoundManager().play(this.skidSFX);
MarioQuaMario.LOGGER.info("SFX? " + this.skidSFX);
}
}
else {
if(this.action.ANIMATION != null)
CPMIntegration.commonAPI.playAnimation(PlayerEntity.class, this.mario, this.action.ANIMATION, 0);
if(action.ANIMATION != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ static void update(ClientPlayerEntity mario) {

JUMP.update(mario.input.jumping);

DUCK.update(mario.input.sneaking, mario.input.sneaking || mario.isInSneakingPose());
DUCK.update(mario.input.sneaking || mario.isInSneakingPose(), mario.input.sneaking);

SPIN.update(false);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.floralquafloral.registries.states;

import com.floralquafloral.CharaStat;
import com.floralquafloral.stats.BaseStats;

import java.util.EnumMap;

public interface MarioMajorStateDefinition extends MarioStateDefinition {
void populateStatFactors(EnumMap<CharaStat, Double> statFactorMap);
void populateStatFactors(EnumMap<BaseStats, Double> statFactorMap);

float getWidthFactor();
float getHeightFactor();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package com.floralquafloral.registries.states;

import com.floralquafloral.CharaStat;
import com.floralquafloral.stats.BaseStats;

import java.util.EnumMap;

public abstract class ParsedMajorMarioState extends ParsedMarioState {
public final float WIDTH_FACTOR;
public final float HEIGHT_FACTOR;
public final EnumMap<CharaStat, Double> STAT_FACTORS;
public final EnumMap<BaseStats, Double> STAT_FACTORS;

protected ParsedMajorMarioState(MarioMajorStateDefinition definition) {
super(definition);

this.WIDTH_FACTOR = definition.getWidthFactor();
this.HEIGHT_FACTOR = definition.getHeightFactor();
this.STAT_FACTORS = new EnumMap<>(CharaStat.class);
this.STAT_FACTORS = new EnumMap<>(BaseStats.class);
definition.populateStatFactors(this.STAT_FACTORS);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.floralquafloral.registries.states.action;

import com.floralquafloral.VoiceLine;
import com.floralquafloral.mariodata.MarioData;
import com.floralquafloral.mariodata.client.Input;
import com.floralquafloral.mariodata.client.MarioClientData;
Expand Down Expand Up @@ -29,6 +30,7 @@ protected abstract static class GroundedTransitions {
(data, isSelf, seed) -> {
// Play duck voiceline
data.getMario().playSound(MarioSFX.DUCK);
VoiceLine.DUCK.play(data, seed);
LOGGER.info("Ducking voiceline with seed {}", seed);
},
(data, seed) -> {
Expand Down
Loading

0 comments on commit b2232f5

Please sign in to comment.