Skip to content

Commit

Permalink
Added... Music and Sound control feature...
Browse files Browse the repository at this point in the history
  • Loading branch information
Iteranya committed Nov 14, 2024
1 parent 9212d0c commit 8977b0d
Show file tree
Hide file tree
Showing 9 changed files with 471 additions and 337 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ private void processAction(Map<String, Object> action) {
this.variables.put("unlocked_events", events);
this.currentState.incrementAndGet();
break;
case "play_sound":
updateSound(this, (String) action.get("sound"));
case "play_music":
if(action.get("music")!=null){
updateMusic(this, (String) action.get("music"));
}else{
stopMusic(this);
}

case "next":
processNext(action,this);
this.currentState.incrementAndGet();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.arsparadox.mobtalkerredux.vn.controller.vnmodules;

public class SoundHandler {
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,15 @@ public static void changeStateByLabel(String label,AtomicLong currentState, List
currentState.set(findLabelId(label,gameData));
}

public static void updateMusic(VisualNovelEngine vn,String music){
vn.state.setMusic(music);
}

public static void stopMusic(VisualNovelEngine vn){
vn.state.setMusic(null);
}

public static void updateSound(VisualNovelEngine vn, String sound){
vn.state.setSound(sound);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class DialogueState {
private String content;
private String background;
private String command;
private String music;
private String sound;
private List <SpriteState> sprites = new ArrayList<>();
private List<Map<String, Object>> choices;

Expand Down Expand Up @@ -62,4 +64,18 @@ public String getCommand(){
public void setCommand(String action) {
this.command = action;
}

public String getMusic(){return this.music;}

public void setMusic(String music) {
this.music = music;
}

public String getSound() {
return sound;
}

public void setSound(String sound) {
this.sound = sound;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class DialogueScreen extends Screen{

private String background;
private String command;
private String music;
private String sound;



Expand Down Expand Up @@ -62,6 +64,46 @@ public void update(){
// if(player.server.isSingleplayer()){
// //ForgeCommandRunner.runCommand(player.server,command);
// }

if(state.getSound()!=null){
if(!state.getSound().equals(sound)){
sound = state.getSound();
playSound(sound);
System.out.println("Current Sound: "+state.getSound());
}
}
if(state.getMusic()!=null){
if(!state.getMusic().equals(music)){
music = state.getMusic();
playMusic(music);
System.out.println("Current Music: "+music);
}
}else{
playMusic(music);
music = state.getMusic();
}


}

public void playMusic(String music){
if(music!=null){
ResourceLocation musicPath = new ResourceLocation("mobtalkerredux","music."+music);
SoundUtils.playMusic(musicPath);
System.out.println("Playing: "+music);
}else{
SoundUtils.stopMusic();
}

}
public void playSound(String sound){
if(sound!=null){
ResourceLocation soundPath = new ResourceLocation("mobtalkerredux","sound/"+sound);
SoundUtils.playSound(soundPath);
System.out.println("Playing: "+sound);
}else{
SoundUtils.stopSound();
}
}

public void updateSprites(DialogueState state){
Expand Down
118 changes: 118 additions & 0 deletions src/main/java/org/arsparadox/mobtalkerredux/vn/view/SoundUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package org.arsparadox.mobtalkerredux.vn.view;

import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.sounds.SimpleSoundInstance;
import net.minecraft.client.resources.sounds.SoundInstance;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundSource;

public class SoundUtils {

private static SimpleSoundInstance currentMusic = null;
private static SimpleSoundInstance currentSound = null;

/**
* Plays a sound effect once at full volume
* Stops any currently playing sound effect
* @param sound The ResourceLocation of the sound to play
*/
public static void playSound(ResourceLocation sound) {
if (currentSound != null) {
Minecraft.getInstance().getSoundManager().stop(currentSound);
}
currentSound = SimpleSoundInstance.forUI(
net.minecraft.sounds.SoundEvent.createVariableRangeEvent(sound),
1.0F, // Volume
1.0F // Pitch
);
Minecraft.getInstance().getSoundManager().play(currentSound);
}

/**
* Plays a sound effect with custom volume and pitch
* Stops any currently playing sound effect
* @param sound The ResourceLocation of the sound to play
* @param volume Volume from 0.0 to 1.0
* @param pitch Pitch from 0.5 to 2.0
*/
public static void playSound(ResourceLocation sound, float volume, float pitch) {
if (currentSound != null) {
Minecraft.getInstance().getSoundManager().stop(currentSound);
}
currentSound = SimpleSoundInstance.forUI(
net.minecraft.sounds.SoundEvent.createVariableRangeEvent(sound),
volume,
pitch
);
Minecraft.getInstance().getSoundManager().play(currentSound);
}

/**
* Plays music that will loop continuously
* Automatically stops any currently playing music
* @param music The ResourceLocation of the music to play
*/
public static void playMusic(ResourceLocation music) {
stopMusic();
currentMusic = new SimpleSoundInstance(
music,
SoundSource.MUSIC,
1.0F, // Volume
1.0F, // Pitch
SoundInstance.createUnseededRandom(),
true, // Loop
0, // Delay
SoundInstance.Attenuation.NONE,
0.0D, // x
0.0D, // y
0.0D, // z
true // Relative
);
Minecraft.getInstance().getSoundManager().play(currentMusic);
}

/**
* Plays music with custom volume that will loop continuously
* Automatically stops any currently playing music
* @param music The ResourceLocation of the music to play
* @param volume Volume from 0.0 to 1.0
*/
public static void playMusic(ResourceLocation music, float volume) {
stopMusic();
currentMusic = new SimpleSoundInstance(
music,
SoundSource.MUSIC,
volume,
1.0F,
SoundInstance.createUnseededRandom(),
true,
0,
SoundInstance.Attenuation.NONE,
0.0D,
0.0D,
0.0D,
true
);
Minecraft.getInstance().getSoundManager().play(currentMusic);
}

/**
* Stops any currently playing music
*/
public static void stopMusic() {
if (currentMusic != null) {
Minecraft.getInstance().getSoundManager().stop(currentMusic);
currentMusic = null;
}
}

/**
* Stops any currently playing sound effect
*/
public static void stopSound() {
if (currentSound != null) {
Minecraft.getInstance().getSoundManager().stop(currentSound);
currentSound = null;
}
}
}
Loading

0 comments on commit 8977b0d

Please sign in to comment.