Some work on flamethrower sounds, also refactored sound system to allow for multiple sounds from a single object reference (to allow flamethrower and jetpack sounds to work simultaneously)

This commit is contained in:
Aidan C. Brady 2014-07-16 16:59:45 -04:00
parent 6e5eca9d4e
commit be7ea115b6
9 changed files with 181 additions and 76 deletions

View file

@ -80,8 +80,8 @@ import mekanism.client.render.tileentity.RenderRotaryCondensentrator;
import mekanism.client.render.tileentity.RenderSalinationController; import mekanism.client.render.tileentity.RenderSalinationController;
import mekanism.client.render.tileentity.RenderSeismicVibrator; import mekanism.client.render.tileentity.RenderSeismicVibrator;
import mekanism.client.render.tileentity.RenderTeleporter; import mekanism.client.render.tileentity.RenderTeleporter;
import mekanism.client.sound.Sound;
import mekanism.client.sound.SoundHandler; import mekanism.client.sound.SoundHandler;
import mekanism.client.sound.SoundMap;
import mekanism.common.CommonProxy; import mekanism.common.CommonProxy;
import mekanism.common.IElectricChest; import mekanism.common.IElectricChest;
import mekanism.common.IInvConfiguration; import mekanism.common.IInvConfiguration;
@ -185,13 +185,13 @@ public class ClientProxy extends CommonProxy
} }
@Override @Override
public void registerSound(Object obj) public void registerSound(TileEntity tileEntity)
{ {
if(MekanismClient.enableSounds && MekanismClient.audioHandler != null) if(MekanismClient.enableSounds && MekanismClient.audioHandler != null)
{ {
synchronized(MekanismClient.audioHandler.sounds) synchronized(MekanismClient.audioHandler.soundMaps)
{ {
MekanismClient.audioHandler.register(obj); MekanismClient.audioHandler.registerTileSound(tileEntity);
} }
} }
} }
@ -201,11 +201,11 @@ public class ClientProxy extends CommonProxy
{ {
if(MekanismClient.enableSounds && MekanismClient.audioHandler != null) if(MekanismClient.enableSounds && MekanismClient.audioHandler != null)
{ {
synchronized(MekanismClient.audioHandler.sounds) synchronized(MekanismClient.audioHandler.soundMaps)
{ {
if(MekanismClient.audioHandler.getFrom(tileEntity) != null) if(MekanismClient.audioHandler.getMap(tileEntity) != null)
{ {
MekanismClient.audioHandler.getFrom(tileEntity).remove(); MekanismClient.audioHandler.getMap(tileEntity).kill();
} }
} }
} }
@ -504,17 +504,17 @@ public class ClientProxy extends CommonProxy
{ {
if(MekanismClient.audioHandler != null) if(MekanismClient.audioHandler != null)
{ {
synchronized(MekanismClient.audioHandler.sounds) synchronized(MekanismClient.audioHandler.soundMaps)
{ {
HashMap<Object, Sound> sounds = new HashMap<Object, Sound>(); HashMap<Object, SoundMap> mapsCopy = new HashMap<Object, SoundMap>();
sounds.putAll(MekanismClient.audioHandler.sounds); mapsCopy.putAll(MekanismClient.audioHandler.soundMaps);
for(Sound sound : sounds.values()) for(SoundMap map : mapsCopy.values())
{ {
sound.remove(); map.kill();
} }
MekanismClient.audioHandler.sounds.clear(); MekanismClient.audioHandler.soundMaps.clear();
} }
} }
} }

View file

@ -387,22 +387,26 @@ public class ClientTickHandler
{ {
for(String username : Mekanism.jetpackOn) for(String username : Mekanism.jetpackOn)
{ {
if(mc.theWorld.getPlayerEntityByName(username) != null) EntityPlayer player = mc.theWorld.getPlayerEntityByName(username);
if(player != null)
{ {
if(MekanismClient.audioHandler.getFrom(mc.theWorld.getPlayerEntityByName(username)) == null) if(MekanismClient.audioHandler.getMap(player) == null)
{ {
new JetpackSound(MekanismClient.audioHandler.getIdentifier(), mc.theWorld.getPlayerEntityByName(username)); new JetpackSound(MekanismClient.audioHandler.getIdentifier(player), player);
} }
} }
} }
for(String username : Mekanism.gasmaskOn) for(String username : Mekanism.gasmaskOn)
{ {
if(mc.theWorld.getPlayerEntityByName(username) != null) EntityPlayer player = mc.theWorld.getPlayerEntityByName(username);
if(player != null)
{ {
if(MekanismClient.audioHandler.getFrom(mc.theWorld.getPlayerEntityByName(username)) == null) if(MekanismClient.audioHandler.getMap(player) == null)
{ {
new GasMaskSound(MekanismClient.audioHandler.getIdentifier(), mc.theWorld.getPlayerEntityByName(username)); new GasMaskSound(MekanismClient.audioHandler.getIdentifier(player), player);
} }
} }
} }
@ -411,7 +415,10 @@ public class ClientTickHandler
{ {
if(hasFlamethrower(player)) if(hasFlamethrower(player))
{ {
new FlamethrowerSound(MekanismClient.audioHandler.getIdentifier(), player); if(MekanismClient.audioHandler.getMap(player) == null)
{
new FlamethrowerSound(MekanismClient.audioHandler.getIdentifier(player), player);
}
} }
} }
} }
@ -546,7 +553,7 @@ public class ClientTickHandler
public static boolean hasFlamethrower(EntityPlayer player) public static boolean hasFlamethrower(EntityPlayer player)
{ {
if(player.getCurrentEquippedItem() != null && player.getCurrentEquippedItem().getItem() instanceof ItemJetpack) if(player.getCurrentEquippedItem() != null && player.getCurrentEquippedItem().getItem() instanceof ItemFlamethrower)
{ {
ItemFlamethrower flamethrower = (ItemFlamethrower)player.getCurrentEquippedItem().getItem(); ItemFlamethrower flamethrower = (ItemFlamethrower)player.getCurrentEquippedItem().getItem();
@ -563,7 +570,7 @@ public class ClientTickHandler
{ {
if(MekanismClient.audioHandler != null) if(MekanismClient.audioHandler != null)
{ {
synchronized(MekanismClient.audioHandler.sounds) synchronized(MekanismClient.audioHandler.soundMaps)
{ {
MekanismClient.audioHandler.onTick(); MekanismClient.audioHandler.onTick();
} }

View file

@ -2,7 +2,6 @@ package mekanism.client.sound;
import mekanism.client.ClientTickHandler; import mekanism.client.ClientTickHandler;
import mekanism.common.item.ItemFlamethrower; import mekanism.common.item.ItemFlamethrower;
import mekanism.common.item.ItemJetpack;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.world.World; import net.minecraft.world.World;
@ -12,7 +11,7 @@ public class FlamethrowerSound extends PlayerSound
public FlamethrowerSound(String id, EntityPlayer entity) public FlamethrowerSound(String id, EntityPlayer entity)
{ {
super(id, "Flamethrower.ogg", entity); super(id, getSound(getInUse(entity)), entity);
inUse = getInUse(entity); inUse = getInUse(entity);
} }
@ -35,16 +34,22 @@ public class FlamethrowerSound extends PlayerSound
if(isPlaying) if(isPlaying)
{ {
System.out.println(getLocation());
ticksSincePlay++; ticksSincePlay++;
} }
return true; return true;
} }
private boolean getInUse(EntityPlayer player) private static boolean getInUse(EntityPlayer player)
{ {
ItemFlamethrower flamethrower = (ItemFlamethrower)player.getCurrentEquippedItem().getItem(); ItemFlamethrower flamethrower = (ItemFlamethrower)player.getCurrentEquippedItem().getItem();
return flamethrower.getInUse(player.getCurrentEquippedItem()); return flamethrower.getInUse(player.getCurrentEquippedItem());
} }
private static String getSound(boolean inUse)
{
return inUse ? "FlamethrowerActive.ogg" : "FlamethrowerIdle.ogg";
}
} }

View file

@ -33,12 +33,12 @@ public abstract class Sound
*/ */
public Sound(String id, String sound, Object obj, Pos3D loc) public Sound(String id, String sound, Object obj, Pos3D loc)
{ {
if(MekanismClient.audioHandler.getFrom(obj) != null) if(MekanismClient.audioHandler.getMap(obj) != null)
{ {
return; return;
} }
synchronized(MekanismClient.audioHandler.sounds) synchronized(MekanismClient.audioHandler.soundMaps)
{ {
prevSoundPath = sound; prevSoundPath = sound;
identifier = id; identifier = id;
@ -58,7 +58,7 @@ public abstract class Sound
SoundHandler.getSoundSystem().activate(id); SoundHandler.getSoundSystem().activate(id);
} }
MekanismClient.audioHandler.sounds.put(obj, this); MekanismClient.audioHandler.registerSound(objRef, prevSoundPath, this);
} }
} }
@ -67,7 +67,7 @@ public abstract class Sound
*/ */
public void play() public void play()
{ {
synchronized(MekanismClient.audioHandler.sounds) synchronized(MekanismClient.audioHandler.soundMaps)
{ {
if(isPlaying) if(isPlaying)
{ {
@ -91,7 +91,7 @@ public abstract class Sound
*/ */
public void stopLoop() public void stopLoop()
{ {
synchronized(MekanismClient.audioHandler.sounds) synchronized(MekanismClient.audioHandler.soundMaps)
{ {
if(!isPlaying) if(!isPlaying)
{ {
@ -113,14 +113,14 @@ public abstract class Sound
*/ */
public void remove() public void remove()
{ {
synchronized(MekanismClient.audioHandler.sounds) synchronized(MekanismClient.audioHandler.soundMaps)
{ {
if(isPlaying) if(isPlaying)
{ {
stopLoop(); stopLoop();
} }
MekanismClient.audioHandler.sounds.remove(objRef); MekanismClient.audioHandler.removeSound(objRef, prevSoundPath);
if(SoundHandler.getSoundSystem() != null) if(SoundHandler.getSoundSystem() != null)
{ {
@ -158,7 +158,7 @@ public abstract class Sound
*/ */
public void updateVolume() public void updateVolume()
{ {
synchronized(MekanismClient.audioHandler.sounds) synchronized(MekanismClient.audioHandler.soundMaps)
{ {
try { try {
float multiplier = getMultiplier(); float multiplier = getMultiplier();

View file

@ -44,7 +44,7 @@ import cpw.mods.fml.relauncher.SideOnly;
public class SoundHandler public class SoundHandler
{ {
/** All the sound references in the Minecraft game. */ /** All the sound references in the Minecraft game. */
public Map<Object, Sound> sounds = Collections.synchronizedMap(new HashMap<Object, Sound>()); public Map<Object, SoundMap> soundMaps = Collections.synchronizedMap(new HashMap<Object, SoundMap>());
public static Minecraft mc = Minecraft.getMinecraft(); public static Minecraft mc = Minecraft.getMinecraft();
@ -173,7 +173,7 @@ public class SoundHandler
*/ */
public void onTick() public void onTick()
{ {
synchronized(sounds) synchronized(soundMaps)
{ {
if(getSoundSystem() != null) if(getSoundSystem() != null)
{ {
@ -184,7 +184,9 @@ public class SoundHandler
if(FMLClientHandler.instance().getClient().thePlayer != null && world != null) if(FMLClientHandler.instance().getClient().thePlayer != null && world != null)
{ {
for(Sound sound : sounds.values()) for(SoundMap map : soundMaps.values())
{
for(Sound sound : map)
{ {
if(!sound.update(world)) if(!sound.update(world))
{ {
@ -197,6 +199,7 @@ public class SoundHandler
sound.updateVolume(); sound.updateVolume();
} }
} }
}
for(Sound sound : soundsToRemove) for(Sound sound : soundsToRemove)
{ {
@ -205,12 +208,9 @@ public class SoundHandler
} }
} }
else { else {
for(Sound sound : sounds.values()) for(SoundMap map : soundMaps.values())
{ {
if(sound.isPlaying) map.stopLoops();
{
sound.stopLoop();
}
} }
} }
} }
@ -220,39 +220,67 @@ public class SoundHandler
} }
} }
public void removeSound(Object ref, String path)
{
if(soundMaps.get(ref) == null)
{
return;
}
soundMaps.get(ref).remove(path);
}
public void registerSound(Object ref, String path, Sound sound)
{
if(soundMaps.get(ref) == null)
{
soundMaps.put(ref, new SoundMap(ref, path, sound));
return;
}
soundMaps.get(ref).add(path, sound);
}
/** /**
* Gets a sound object from a specific TileEntity, null if there is none. * Gets a sound object from a specific TileEntity, null if there is none.
* @param tileEntity - the holder of the sound * @param tileEntity - the holder of the sound
* @return Sound instance * @return Sound instance
*/ */
public Sound getFrom(Object obj) public SoundMap getMap(Object obj)
{ {
synchronized(sounds) synchronized(soundMaps)
{ {
return sounds.get(obj); return soundMaps.get(obj);
} }
} }
public Sound getSound(Object obj, String path)
{
if(soundMaps.get(obj) == null)
{
return null;
}
return soundMaps.get(obj).getSound(path);
}
/** /**
* Create and return an instance of a Sound. * Create and return an instance of a Sound.
* @param tileEntity - the holder of this sound. * @param tileEntity - the holder of this sound.
* @return Sound instance * @return Sound instance
*/ */
public void register(Object obj) public void registerTileSound(TileEntity tile)
{ {
if(obj instanceof TileEntity && !(obj instanceof IHasSound)) if(!(tile instanceof IHasSound))
{ {
return; return;
} }
synchronized(sounds) synchronized(soundMaps)
{ {
if(getFrom(obj) == null) if(getMap(tile) == null)
{ {
if(obj instanceof TileEntity) new TileSound(getIdentifier(tile), HolidayManager.filterSound(((IHasSound)tile).getSoundPath()), tile);
{
new TileSound(getIdentifier(), HolidayManager.filterSound(((IHasSound)obj).getSoundPath()), (TileEntity)obj);
}
} }
} }
} }
@ -263,19 +291,11 @@ public class SoundHandler
* number between 0 and 10,000. Example: "Mekanism_6_6123" * number between 0 and 10,000. Example: "Mekanism_6_6123"
* @return unique identifier * @return unique identifier
*/ */
public String getIdentifier() public String getIdentifier(Object obj)
{ {
synchronized(sounds) synchronized(soundMaps)
{ {
String toReturn = "Mekanism_" + sounds.size() + "_" + new Random().nextInt(10000); String toReturn = "Mekanism_" + soundMaps.size() + "_" + new Random().nextInt(10000);
for(Sound sound : sounds.values())
{
if(sound.identifier.equals(toReturn))
{
return getIdentifier();
}
}
return toReturn; return toReturn;
} }
@ -350,11 +370,11 @@ public class SoundHandler
if(tileEntity instanceof IHasSound) if(tileEntity instanceof IHasSound)
{ {
if(getFrom(tileEntity) != null) if(getMap(tileEntity) != null)
{ {
if(sounds.containsKey(tileEntity)) if(soundMaps.containsKey(tileEntity))
{ {
getFrom(tileEntity).remove(); getMap(tileEntity).kill();
} }
} }
} }

View file

@ -0,0 +1,73 @@
package mekanism.client.sound;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import mekanism.client.MekanismClient;
public class SoundMap implements Iterable<Sound>
{
public Map<String, Sound> soundMap = new HashMap<String, Sound>();
public Object objRef;
public SoundMap(Object obj)
{
objRef = obj;
}
public SoundMap(Object obj, String path, Sound sound)
{
this(obj);
soundMap.put(path, sound);
}
@Override
public Iterator<Sound> iterator()
{
return soundMap.values().iterator();
}
public Sound getSound(String path)
{
return soundMap.get(path);
}
public boolean hasSound(String path)
{
return soundMap.containsKey(path);
}
public void remove(String path)
{
soundMap.remove(path);
}
public void add(String path, Sound sound)
{
soundMap.put(path, sound);
}
public void stopLoops()
{
for(Sound sound : soundMap.values())
{
if(sound.isPlaying)
{
sound.stopLoop();
}
}
}
public void kill()
{
for(Sound sound : soundMap.values())
{
sound.remove();
}
MekanismClient.audioHandler.soundMaps.remove(objRef);
}
}

View file

@ -142,9 +142,9 @@ public class CommonProxy
/** /**
* Registers a client-side sound, assigned to a TileEntity. * Registers a client-side sound, assigned to a TileEntity.
* @param obj - TileEntity who is registering the sound * @param tile - TileEntity who is registering the sound
*/ */
public void registerSound(Object obj) {} public void registerSound(TileEntity tileEntity) {}
/** /**
* Unregisters a client-side sound, assigned to a TileEntity; * Unregisters a client-side sound, assigned to a TileEntity;