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;
|
2013-07-20 18:10:14 +02:00
|
|
|
import net.minecraft.client.Minecraft;
|
2012-12-20 22:53:39 +01:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
2013-04-14 17:55:51 +02:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2013-04-18 04:40:11 +02:00
|
|
|
import cpw.mods.fml.client.FMLClientHandler;
|
|
|
|
import cpw.mods.fml.relauncher.Side;
|
|
|
|
import cpw.mods.fml.relauncher.SideOnly;
|
2012-10-22 03:29:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-04-13 16:33:37 +02:00
|
|
|
@SideOnly(Side.CLIENT)
|
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;
|
|
|
|
|
2013-04-14 17:55:51 +02:00
|
|
|
/** The TileEntity this sound is associated with. */
|
|
|
|
public TileEntity tileEntity;
|
2013-04-13 16:33:37 +02:00
|
|
|
|
2012-10-22 03:29:26 +02:00
|
|
|
/** Whether or not this sound is playing */
|
|
|
|
public boolean isPlaying = false;
|
|
|
|
|
2013-04-14 17:55:51 +02:00
|
|
|
/**
|
|
|
|
* A sound that runs off of the PaulsCode sound system.
|
2012-10-22 03:29:26 +02:00
|
|
|
* @param id - unique identifier
|
|
|
|
* @param sound - bundled path to the sound
|
2013-04-14 17:55:51 +02:00
|
|
|
* @param tileentity - the tile this sound is playing from.
|
2012-10-22 03:29:26 +02:00
|
|
|
*/
|
2013-04-14 17:55:51 +02:00
|
|
|
public Sound(String id, String sound, TileEntity tileentity)
|
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;
|
2013-04-14 17:55:51 +02:00
|
|
|
tileEntity = tileentity;
|
2012-12-30 22:34:45 +01:00
|
|
|
|
2013-07-20 18:10:14 +02:00
|
|
|
URL url = getClass().getClassLoader().getResource("assets/mekanism/sound/" + sound);
|
2013-05-14 17:34:26 +02:00
|
|
|
|
2012-12-30 22:34:45 +01:00
|
|
|
if(url == null)
|
|
|
|
{
|
|
|
|
System.out.println("[Mekanism] Invalid sound file: " + sound);
|
|
|
|
}
|
|
|
|
|
2013-07-20 18:10:14 +02:00
|
|
|
if(SoundHandler.getSoundSystem() != null)
|
2013-01-21 02:15:59 +01:00
|
|
|
{
|
2013-07-20 18:10:14 +02:00
|
|
|
SoundHandler.getSoundSystem().newSource(false, id, url, sound, true, tileEntity.xCoord, tileEntity.yCoord, tileEntity.zCoord, 0, 16F);
|
|
|
|
updateVolume(Minecraft.getMinecraft().thePlayer);
|
|
|
|
SoundHandler.getSoundSystem().activate(id);
|
2013-01-21 02:15:59 +01:00
|
|
|
}
|
2013-02-25 21:02:05 +01:00
|
|
|
|
2013-04-14 17:55:51 +02:00
|
|
|
Mekanism.audioHandler.sounds.put(tileEntity, this);
|
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-07-20 18:10:14 +02:00
|
|
|
if(SoundHandler.getSoundSystem() != null)
|
2013-01-21 02:15:59 +01:00
|
|
|
{
|
2013-07-20 18:10:14 +02:00
|
|
|
updateVolume(Minecraft.getMinecraft().thePlayer);
|
|
|
|
SoundHandler.getSoundSystem().play(identifier);
|
2013-01-21 02:15:59 +01:00
|
|
|
}
|
2013-05-14 17:34:26 +02:00
|
|
|
|
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-07-20 18:10:14 +02:00
|
|
|
if(SoundHandler.getSoundSystem() != null)
|
2013-01-21 02:15:59 +01:00
|
|
|
{
|
2013-07-20 18:10:14 +02:00
|
|
|
updateVolume(Minecraft.getMinecraft().thePlayer);
|
|
|
|
SoundHandler.getSoundSystem().stop(identifier);
|
2013-01-21 02:15:59 +01:00
|
|
|
}
|
2013-05-14 17:34:26 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-04-14 17:55:51 +02:00
|
|
|
Mekanism.audioHandler.sounds.remove(tileEntity);
|
2013-01-21 02:15:59 +01:00
|
|
|
|
2013-07-20 18:10:14 +02:00
|
|
|
if(SoundHandler.getSoundSystem() != null)
|
2013-01-21 02:15:59 +01:00
|
|
|
{
|
2013-07-20 18:10:14 +02:00
|
|
|
updateVolume(Minecraft.getMinecraft().thePlayer);
|
|
|
|
SoundHandler.getSoundSystem().removeSource(identifier);
|
2013-01-21 02:15:59 +01:00
|
|
|
}
|
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-25 21:02:05 +01:00
|
|
|
public void updateVolume(EntityPlayer entityplayer)
|
2012-10-22 03:29:26 +02:00
|
|
|
{
|
2012-12-30 22:34:45 +01:00
|
|
|
synchronized(Mekanism.audioHandler.sounds)
|
|
|
|
{
|
2013-04-22 05:43:04 +02:00
|
|
|
if(entityplayer != null && tileEntity != null && entityplayer.worldObj == tileEntity.worldObj)
|
2013-04-13 16:33:37 +02:00
|
|
|
{
|
2013-05-07 01:42:03 +02:00
|
|
|
float multiplier = ((IHasSound)tileEntity).getVolumeMultiplier();
|
2013-04-13 16:33:37 +02:00
|
|
|
float volume = 0;
|
2013-05-07 01:42:03 +02:00
|
|
|
float masterVolume = Mekanism.audioHandler.masterVolume;
|
2013-04-13 16:33:37 +02:00
|
|
|
|
2013-05-07 01:42:03 +02:00
|
|
|
double distance = entityplayer.getDistance(tileEntity.xCoord, tileEntity.yCoord, tileEntity.zCoord);
|
|
|
|
volume = (float)Math.min(Math.max(masterVolume-((distance*.08F)*masterVolume), 0)*multiplier, 1);
|
2013-04-13 16:33:37 +02:00
|
|
|
|
2013-07-20 18:10:14 +02:00
|
|
|
if(SoundHandler.getSoundSystem() != null)
|
2013-04-13 16:33:37 +02:00
|
|
|
{
|
2013-07-20 18:10:14 +02:00
|
|
|
SoundHandler.getSoundSystem().setVolume(identifier, volume);
|
2013-04-13 16:33:37 +02:00
|
|
|
}
|
|
|
|
}
|
2012-12-30 22:34:45 +01:00
|
|
|
}
|
2012-10-22 03:29:26 +02:00
|
|
|
}
|
|
|
|
}
|