Mekanism-tilera-Edition/common/mekanism/client/sound/Sound.java

160 lines
3.6 KiB
Java
Raw Normal View History

package mekanism.client.sound;
import java.net.URL;
import mekanism.client.MekanismClient;
2013-07-20 18:10:14 +02:00
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.world.World;
import universalelectricity.core.vector.Vector3;
public abstract class Sound
{
/** The bundled path where the sound is */
public String soundPath;
/** A unique identifier for this sound */
public String identifier;
/** Whether or not this sound is playing */
public boolean isPlaying = false;
private Object objRef;
2013-12-09 03:40:00 +01:00
protected Minecraft mc = Minecraft.getMinecraft();
/**
* A sound that runs off of the PaulsCode sound system.
* @param id - unique identifier
* @param sound - bundled path to the sound
* @param tileentity - the tile this sound is playing from.
*/
public Sound(String id, String sound, Object obj, Vector3 loc)
{
2013-12-06 21:26:44 +01:00
if(MekanismClient.audioHandler.getFrom(obj) != null)
{
return;
}
synchronized(MekanismClient.audioHandler.sounds)
{
soundPath = sound;
identifier = id;
objRef = obj;
2013-07-20 18:10:14 +02:00
URL url = getClass().getClassLoader().getResource("assets/mekanism/sound/" + sound);
if(url == null)
{
System.out.println("[Mekanism] Invalid sound file: " + sound);
}
2013-07-20 18:10:14 +02:00
if(SoundHandler.getSoundSystem() != null)
{
SoundHandler.getSoundSystem().newSource(false, id, url, sound, true, (float)loc.x, (float)loc.y, (float)loc.z, 0, 16F);
2013-12-09 03:40:00 +01:00
updateVolume();
2013-07-20 18:10:14 +02:00
SoundHandler.getSoundSystem().activate(id);
}
2013-02-25 21:02:05 +01:00
MekanismClient.audioHandler.sounds.put(obj, this);
}
}
/**
* Start looping the sound effect
*/
public void play()
{
synchronized(MekanismClient.audioHandler.sounds)
{
if(isPlaying)
{
return;
}
2013-07-20 18:10:14 +02:00
if(SoundHandler.getSoundSystem() != null)
{
2013-12-09 03:40:00 +01:00
updateVolume();
2013-07-20 18:10:14 +02:00
SoundHandler.getSoundSystem().play(identifier);
}
isPlaying = true;
}
}
/**
* Stop looping the sound effect
*/
public void stopLoop()
{
synchronized(MekanismClient.audioHandler.sounds)
{
if(!isPlaying)
{
return;
}
2013-07-20 18:10:14 +02:00
if(SoundHandler.getSoundSystem() != null)
{
2013-12-09 03:40:00 +01:00
updateVolume();
2013-07-20 18:10:14 +02:00
SoundHandler.getSoundSystem().stop(identifier);
}
isPlaying = false;
}
}
/**
* Remove the sound effect from the PaulsCode SoundSystem
*/
public void remove()
{
synchronized(MekanismClient.audioHandler.sounds)
{
if(isPlaying)
{
stopLoop();
}
MekanismClient.audioHandler.sounds.remove(objRef);
2013-07-20 18:10:14 +02:00
if(SoundHandler.getSoundSystem() != null)
{
2013-12-09 03:40:00 +01:00
updateVolume();
2013-07-20 18:10:14 +02:00
SoundHandler.getSoundSystem().removeSource(identifier);
}
}
}
public abstract boolean update(World world);
public abstract Vector3 getLocation();
public abstract float getMultiplier();
/**
* Updates the volume based on how far away the player is from the machine.
* @param entityplayer - player who is near the machine, always Minecraft.thePlayer
*/
2013-12-09 03:40:00 +01:00
public void updateVolume()
{
synchronized(MekanismClient.audioHandler.sounds)
{
try {
float multiplier = getMultiplier();
float volume = 0;
float masterVolume = MekanismClient.audioHandler.masterVolume;
2013-12-09 03:40:00 +01:00
double distance = mc.thePlayer.getDistance(getLocation().x, getLocation().y, getLocation().z);
volume = (float)Math.min(Math.max(masterVolume-((distance*.08F)*masterVolume), 0)*multiplier, 1);
volume *= Math.max(0, Math.min(1, MekanismClient.baseSoundVolume));
2013-07-20 18:10:14 +02:00
if(SoundHandler.getSoundSystem() != null)
{
2013-07-20 18:10:14 +02:00
SoundHandler.getSoundSystem().setVolume(identifier, volume);
}
} catch(Exception e) {}
}
}
}