2012-11-05 20:29:04 +01:00
|
|
|
package mekanism.client;
|
2012-10-22 03:29:26 +02:00
|
|
|
|
|
|
|
import java.net.URL;
|
|
|
|
|
2012-11-07 21:01:46 +01:00
|
|
|
import mekanism.common.Mekanism;
|
2012-12-20 22:53:39 +01:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.world.World;
|
2012-10-22 03:29:26 +02:00
|
|
|
import paulscode.sound.SoundSystem;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sound -- an object that is created in SoundHandler. A 'Sound' object runs off of
|
|
|
|
* PaulsCode's SoundSystem. It has several methods; play(), for looping the clip,
|
|
|
|
* stop(), for stopping the loop, remove(), for removing the sound from PaulsCode,
|
|
|
|
* and updateVolume() for updating the volume based on where the player is.
|
|
|
|
* @author AidanBrady
|
|
|
|
*
|
|
|
|
*/
|
2013-02-14 19:26:13 +01:00
|
|
|
public class Sound
|
2012-10-22 03:29:26 +02:00
|
|
|
{
|
|
|
|
/** The bundled path where the sound is */
|
|
|
|
public String soundPath;
|
2012-12-30 22:34:45 +01:00
|
|
|
|
2012-10-22 03:29:26 +02:00
|
|
|
/** A unique identifier for this sound */
|
|
|
|
public String identifier;
|
|
|
|
|
|
|
|
/** X coordinate of this sound effect */
|
|
|
|
public int xCoord;
|
2012-12-30 22:34:45 +01:00
|
|
|
|
2012-10-22 03:29:26 +02:00
|
|
|
/** Y coordinate of this sound effect */
|
|
|
|
public int yCoord;
|
2012-12-30 22:34:45 +01:00
|
|
|
|
2012-10-22 03:29:26 +02:00
|
|
|
/** Z coordinate of this sound effect */
|
|
|
|
public int zCoord;
|
|
|
|
|
|
|
|
/** Whether or not this sound is playing */
|
|
|
|
public boolean isPlaying = false;
|
|
|
|
|
|
|
|
/** A sound, an object that runs off of the PaulsCode sound system.
|
|
|
|
*
|
|
|
|
* @param system - PaulsCode SoundSystem
|
|
|
|
* @param id - unique identifier
|
|
|
|
* @param sound - bundled path to the sound
|
|
|
|
* @param world - world the sound is playing
|
|
|
|
* @param x - x coord
|
|
|
|
* @param y - y coord
|
|
|
|
* @param z - z coord
|
|
|
|
*/
|
2012-12-30 22:34:45 +01:00
|
|
|
public Sound(String id, String sound, World world, int x, int y, int z)
|
2012-10-22 03:29:26 +02:00
|
|
|
{
|
2012-12-30 22:34:45 +01:00
|
|
|
synchronized(Mekanism.audioHandler.sounds)
|
2012-10-22 03:29:26 +02:00
|
|
|
{
|
2012-12-30 22:34:45 +01:00
|
|
|
soundPath = sound;
|
|
|
|
identifier = id;
|
|
|
|
xCoord = x;
|
|
|
|
yCoord = y;
|
|
|
|
zCoord = z;
|
|
|
|
|
|
|
|
URL url = getClass().getClassLoader().getResource("resources/mekanism/sound/" + sound);
|
|
|
|
if(url == null)
|
|
|
|
{
|
|
|
|
System.out.println("[Mekanism] Invalid sound file: " + sound);
|
|
|
|
}
|
|
|
|
|
|
|
|
Mekanism.audioHandler.sounds.add(this);
|
2013-01-21 02:15:59 +01:00
|
|
|
|
|
|
|
if(Mekanism.audioHandler.soundSystem != null)
|
|
|
|
{
|
|
|
|
Mekanism.audioHandler.soundSystem.newSource(false, id, url, sound, true, x, y, z, 0, 16F);
|
|
|
|
Mekanism.audioHandler.soundSystem.activate(id);
|
|
|
|
}
|
2012-10-22 03:29:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-09 06:24:27 +01:00
|
|
|
/**
|
|
|
|
* Start looping the sound effect
|
|
|
|
*/
|
2012-10-22 03:29:26 +02:00
|
|
|
public void play()
|
|
|
|
{
|
2012-12-30 22:34:45 +01:00
|
|
|
synchronized(Mekanism.audioHandler.sounds)
|
2012-10-22 03:29:26 +02:00
|
|
|
{
|
2012-12-30 22:34:45 +01:00
|
|
|
if(isPlaying)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-21 02:15:59 +01:00
|
|
|
if(Mekanism.audioHandler.soundSystem != null)
|
|
|
|
{
|
|
|
|
Mekanism.audioHandler.soundSystem.play(identifier);
|
|
|
|
}
|
2012-12-30 22:34:45 +01:00
|
|
|
isPlaying = true;
|
2012-10-22 03:29:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-09 06:24:27 +01:00
|
|
|
/**
|
|
|
|
* Stop looping the sound effect
|
|
|
|
*/
|
2013-02-14 19:26:13 +01:00
|
|
|
public void stopLoop()
|
2012-10-22 03:29:26 +02:00
|
|
|
{
|
2012-12-30 22:34:45 +01:00
|
|
|
synchronized(Mekanism.audioHandler.sounds)
|
2012-10-22 03:29:26 +02:00
|
|
|
{
|
2012-12-30 22:34:45 +01:00
|
|
|
if(!isPlaying)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-21 02:15:59 +01:00
|
|
|
if(Mekanism.audioHandler.soundSystem != null)
|
|
|
|
{
|
|
|
|
Mekanism.audioHandler.soundSystem.stop(identifier);
|
|
|
|
}
|
2012-12-30 22:34:45 +01:00
|
|
|
isPlaying = false;
|
2012-10-22 03:29:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-09 06:24:27 +01:00
|
|
|
/**
|
|
|
|
* Remove the sound effect from the PaulsCode SoundSystem
|
|
|
|
*/
|
2012-10-22 03:29:26 +02:00
|
|
|
public void remove()
|
|
|
|
{
|
2012-12-30 22:34:45 +01:00
|
|
|
synchronized(Mekanism.audioHandler.sounds)
|
2012-10-22 03:29:26 +02:00
|
|
|
{
|
2012-12-30 22:34:45 +01:00
|
|
|
if(isPlaying)
|
|
|
|
{
|
2013-02-14 19:26:13 +01:00
|
|
|
stopLoop();
|
2012-12-30 22:34:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Mekanism.audioHandler.sounds.remove(this);
|
2013-01-21 02:15:59 +01:00
|
|
|
|
|
|
|
if(Mekanism.audioHandler.soundSystem != null)
|
|
|
|
{
|
|
|
|
Mekanism.audioHandler.soundSystem.removeSource(identifier);
|
|
|
|
}
|
2012-10-22 03:29:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 19:26:13 +01:00
|
|
|
/**
|
|
|
|
* Updates the volume based on how far away the player is from the machine.
|
2012-12-30 22:34:45 +01:00
|
|
|
* @param entityplayer - player who is near the machine, always Minecraft.thePlayer
|
2012-10-22 03:29:26 +02:00
|
|
|
*/
|
2013-02-14 19:26:13 +01:00
|
|
|
public void distanceUpdate(EntityPlayer entityplayer)
|
2012-10-22 03:29:26 +02:00
|
|
|
{
|
2012-12-30 22:34:45 +01:00
|
|
|
synchronized(Mekanism.audioHandler.sounds)
|
|
|
|
{
|
|
|
|
float volume = 0;
|
|
|
|
|
|
|
|
if (!isPlaying)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
double distanceVolume = entityplayer.getDistanceSq(xCoord, yCoord, zCoord)*0.01;
|
|
|
|
volume = (float)Math.max(Mekanism.audioHandler.masterVolume-distanceVolume, 0);
|
|
|
|
|
2013-01-21 02:15:59 +01:00
|
|
|
if(Mekanism.audioHandler.soundSystem != null)
|
|
|
|
{
|
|
|
|
Mekanism.audioHandler.soundSystem.setVolume(identifier, volume);
|
|
|
|
}
|
2012-12-30 22:34:45 +01:00
|
|
|
}
|
2012-10-22 03:29:26 +02:00
|
|
|
}
|
|
|
|
}
|