Found a way to reduce the nasty sound delay when rapidly toggling sounds on and off.

This commit is contained in:
Ben Spiers 2014-08-30 01:08:55 +01:00
parent 3a0d0d2208
commit c4d1fd32f4
3 changed files with 31 additions and 16 deletions

View file

@ -11,7 +11,6 @@ import mekanism.api.IClientTicker;
import mekanism.api.gas.GasStack;
import mekanism.client.sound.FlamethrowerSound;
import mekanism.client.sound.GasMaskSound;
import mekanism.client.sound.JetpackSound;
import mekanism.common.KeySync;
import mekanism.common.Mekanism;
import mekanism.common.ObfuscatedNames;
@ -258,9 +257,9 @@ public class ClientTickHandler
if(player != null)
{
if(!MekanismClient.audioHandler.hasSound(player, JETPACK))
if(!MekanismClient.audioHandler.soundPlaying(player, JETPACK))
{
MekanismClient.audioHandler.addSound(player, JETPACK, new JetpackSound(player), false);
MekanismClient.audioHandler.addSound(player, JETPACK, true);
}
MekanismClient.audioHandler.playSound(player, JETPACK);
}
@ -272,9 +271,9 @@ public class ClientTickHandler
if(player != null)
{
if(!MekanismClient.audioHandler.hasSound(player, GASMASK))
if(!MekanismClient.audioHandler.soundPlaying(player, GASMASK))
{
MekanismClient.audioHandler.addSound(player, GASMASK, new GasMaskSound(player), false);
MekanismClient.audioHandler.addSound(player, GASMASK, true);
}
MekanismClient.audioHandler.playSound(player, GASMASK);
}
@ -284,9 +283,9 @@ public class ClientTickHandler
{
if(hasFlamethrower(player))
{
if(!MekanismClient.audioHandler.hasSound(player, FLAMETHROWER))
if(!MekanismClient.audioHandler.soundPlaying(player, FLAMETHROWER))
{
MekanismClient.audioHandler.addSound(player, FLAMETHROWER, new FlamethrowerSound(player), false);
MekanismClient.audioHandler.addSound(player, FLAMETHROWER, true);
}
MekanismClient.audioHandler.playSound(player, FLAMETHROWER);
}

View file

@ -17,6 +17,8 @@ public class FlamethrowerSound extends PlayerSound
super(player, new ResourceLocation("mekanism", "item.flamethrower.idle"));
onSound = new ResourceLocation("mekanism", "item.flamethrower.active");
offSound = new ResourceLocation("mekanism", "item.flamethrower.idle");
inUse = ClientTickHandler.isFlamethrowerOn(player);
sound = inUse ? onSound : offSound;
setFadeIn(0);
setFadeOut(0);
}

View file

@ -33,39 +33,54 @@ public class SoundHandler
public enum Channel
{
JETPACK("jetpack"),
GASMASK("gasMask"),
FLAMETHROWER("flamethrower");
JETPACK("jetpack", JetpackSound.class),
GASMASK("gasMask", GasMaskSound.class),
FLAMETHROWER("flamethrower", FlamethrowerSound.class);
String channelName;
Class<? extends PlayerSound> soundClass;
private Channel(String name)
private Channel(String name, Class<? extends PlayerSound> clazz)
{
channelName = name;
soundClass = clazz;
}
public String getName()
{
return channelName;
}
public PlayerSound getNewSound(EntityPlayer player)
{
try
{
return soundClass.getDeclaredConstructor(EntityPlayer.class).newInstance(player);
}
catch(Exception e)
{
return null;
}
}
}
public boolean hasSound(EntityPlayer player, Channel channel)
public boolean soundPlaying(EntityPlayer player, Channel channel)
{
String name = player.getCommandSenderName();
Map<String, IResettableSound> map = getMap(name);
IResettableSound sound = map.get(channel.getName());
return sound != null;
return !(sound == null || sound.isDonePlaying());
}
public void addSound(EntityPlayer player, Channel channel, IResettableSound newSound, boolean replace)
public void addSound(EntityPlayer player, Channel channel, boolean replace)
{
String name = player.getCommandSenderName();
Map<String, IResettableSound> map = getMap(name);
IResettableSound sound = map.get(channel.getName());
if(sound == null || replace)
{
PlayerSound newSound = channel.getNewSound(player);
map.put(channel.getName(), newSound);
}
}
@ -77,10 +92,9 @@ public class SoundHandler
IResettableSound sound = map.get(channel.getName());
if(sound != null)
{
if(sound.isDonePlaying() && !getSoundMap().containsKey(sound))
if(canRestartSound(sound))
{
sound.reset();
Mekanism.logger.info("Playing sound " + sound);
playSound(sound);
}
return true;