Skip to content

Commit

Permalink
[#103] Refactor sound types; added PERIODIC emitter
Browse files Browse the repository at this point in the history
  • Loading branch information
OreCruncher committed Mar 1, 2016
1 parent 3070469 commit 7100717
Show file tree
Hide file tree
Showing 14 changed files with 141 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@

package org.blockartistry.mod.DynSurround.client.sound;

import java.util.Random;

import org.blockartistry.mod.DynSurround.client.sound.SoundEffect.SoundType;
import org.blockartistry.mod.DynSurround.util.XorShiftRandom;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.client.Minecraft;
Expand All @@ -38,6 +43,8 @@
@SideOnly(Side.CLIENT)
class Emitter {

protected static final Random RANDOM = new XorShiftRandom();

protected final SoundEffect effect;
protected PlayerSound activeSound;

Expand All @@ -49,38 +56,43 @@ public Emitter(final SoundEffect sound) {

public void update() {
final SoundHandler handler = Minecraft.getMinecraft().getSoundHandler();
if (activeSound != null) {
if (handler.isSoundPlaying(activeSound))
if (this.activeSound != null) {
if (handler.isSoundPlaying(this.activeSound))
return;
activeSound.fadeAway();
activeSound = null;
if (this.effect.repeatDelay > 0) {
this.repeatDelay = this.effect.repeatDelay;
this.activeSound.fadeAway();
this.activeSound = null;
this.repeatDelay = this.effect.getRepeat(RANDOM);
if (this.repeatDelay > 0)
return;
}
} else if (this.repeatDelay > 0) {
if (--this.repeatDelay > 0)
return;
}

// If the volume is turned off don't send
// down a sound.
if(SoundSystemConfig.getMasterGain() <= 0)
if (SoundSystemConfig.getMasterGain() <= 0)
return;

this.activeSound = new PlayerSound(effect);

final PlayerSound theSound = new PlayerSound(effect);
if (this.effect.type == SoundType.PERIODIC) {
this.repeatDelay = this.effect.getRepeat(RANDOM);
} else {
this.activeSound = theSound;
}

try {
handler.playSound(this.activeSound);
handler.playSound(theSound);
} catch (final Throwable t) {
;
}
}

public void setVolume(final float volume) {
if(this.activeSound != null)
if (this.activeSound != null)
this.activeSound.setVolume(volume);
}

public float getVolume() {
return this.activeSound != null ? this.activeSound.getVolume() : 0.0F;
}
Expand All @@ -90,4 +102,3 @@ public void fade() {
this.activeSound.fadeAway();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,31 @@ public final class SoundEffect {

private static final float[] pitchDelta = { -0.2F, 0.0F, 0.0F, 0.2F, 0.2F, 0.2F };

public static enum SoundType {
BACKGROUND, SPOT, STEP, PERIODIC;

public static SoundType getType(final String soundType) {
if (soundType == null)
return BACKGROUND;

try {
return SoundType.valueOf(soundType.toUpperCase());
} catch (final Throwable ex) {
ex.printStackTrace();
}
return BACKGROUND;
}
}

public final String sound;
public final String conditions;
private final Pattern pattern;
public final SoundType type;
public float volume;
public final float pitch;
public final int weight;
public final boolean isSpot;
public final boolean variable;
public final int repeatDelayRandom;
public final int repeatDelay;

public SoundEffect(final String sound) {
Expand All @@ -67,20 +84,22 @@ public SoundEffect(final String sound, final float volume, final float pitch, fi
this.conditions = ".*";
this.pattern = null;
this.weight = 1;
this.isSpot = true;
this.type = SoundType.SPOT;
this.variable = variable;
this.repeatDelayRandom = 0;
this.repeatDelay = repeatDelay;
}

public SoundEffect(final SoundEffect effect) {
this.sound = effect.sound;
this.volume = effect.volume;
this.pitch = effect.pitch;
this.conditions = effect.conditions;
this.pattern = effect.pattern;
this.weight = effect.weight;
this.isSpot = effect.isSpot;
this.type = effect.type;
this.variable = effect.variable;
this.repeatDelayRandom = effect.repeatDelayRandom;
this.repeatDelay = effect.repeatDelay;
}

Expand All @@ -91,15 +110,28 @@ public SoundEffect(final SoundConfig record) {
this.pitch = record.pitch == null ? 1.0F : record.pitch.floatValue();
this.pattern = Pattern.compile(this.conditions);
this.weight = record.weight == null ? 10 : record.weight.intValue();
this.isSpot = record.spotSound != null && record.spotSound.booleanValue();
this.variable = record.variable != null && record.variable.booleanValue();
this.repeatDelayRandom = record.repeatDelayRandom == null ? 0 : record.repeatDelayRandom.intValue();
this.repeatDelay = record.repeatDelay == null ? 0 : record.repeatDelay.intValue();

if (record.soundType != null) {
this.type = SoundType.getType(record.soundType);
} else {
if (record.repeatDelay != null && record.repeatDelay.intValue() > 0)
this.type = SoundType.PERIODIC;
else if (record.step != null && record.step.booleanValue())
this.type = SoundType.STEP;
else if (record.spotSound != null && record.spotSound.booleanValue())
this.type = SoundType.SPOT;
else
this.type = SoundType.BACKGROUND;
}
}

public boolean matches(final String conditions) {
return pattern.matcher(conditions).matches();
}

public float getVolume() {
return this.volume;
}
Expand All @@ -110,8 +142,13 @@ public float getPitch(final Random rand) {
return this.pitch;
}

public void doEffect(final Block block, final World world, final BlockPos pos,
final Random random) {
public int getRepeat(final Random rand) {
if (this.repeatDelayRandom <= 0)
return this.repeatDelay;
return this.repeatDelay + rand.nextInt(this.repeatDelayRandom);
}

public void doEffect(final Block block, final World world, final BlockPos pos, final Random random) {
SoundManager.playSoundAt(pos, this, 0);
}

Expand All @@ -124,12 +161,12 @@ public boolean equals(final Object anObj) {
final SoundEffect s = (SoundEffect) anObj;
return this.sound.equals(s.sound);
}

@Override
public int hashCode() {
return this.sound.hashCode();
}

public static SoundEffect scaleVolume(final SoundEffect sound, final float scale) {
final SoundEffect newEffect = new SoundEffect(sound);
newEffect.volume *= scale;
Expand All @@ -143,8 +180,11 @@ public String toString() {
builder.append('(').append(this.conditions).append(')');
builder.append(", v:").append(this.volume);
builder.append(", p:").append(this.pitch);
if (this.isSpot)
builder.append(", t:").append(this.type);
if (this.type == SoundType.SPOT)
builder.append(", w:").append(this.weight);
if (this.repeatDelay != 0 || this.repeatDelayRandom != 0)
builder.append(", d:").append(this.repeatDelay).append('+').append(this.repeatDelayRandom);
builder.append(']');
return builder.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.blockartistry.mod.DynSurround.ModOptions;
import org.blockartistry.mod.DynSurround.Module;
import org.blockartistry.mod.DynSurround.client.sound.SoundEffect;
import org.blockartistry.mod.DynSurround.client.sound.SoundEffect.SoundType;
import org.blockartistry.mod.DynSurround.data.config.BiomeConfig;
import org.blockartistry.mod.DynSurround.data.config.SoundConfig;
import org.blockartistry.mod.DynSurround.event.RegistryReloadEvent;
Expand Down Expand Up @@ -343,7 +344,7 @@ private static void process(final BiomeConfig config) {
if (SoundRegistry.isSoundBlocked(sr.sound))
continue;
final SoundEffect s = new SoundEffect(sr);
if (sr.spotSound != null && sr.spotSound.booleanValue())
if (s.type == SoundType.SPOT)
biomeEntry.spotSounds.add(s);
else
biomeEntry.sounds.add(s);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.blockartistry.mod.DynSurround.client.fx.BlockEffect;
import org.blockartistry.mod.DynSurround.client.fx.JetEffect;
import org.blockartistry.mod.DynSurround.client.sound.SoundEffect;
import org.blockartistry.mod.DynSurround.client.sound.SoundEffect.SoundType;
import org.blockartistry.mod.DynSurround.data.config.BlockConfig;
import org.blockartistry.mod.DynSurround.data.config.BlockConfig.Effect;
import org.blockartistry.mod.DynSurround.data.config.SoundConfig;
Expand Down Expand Up @@ -221,10 +222,8 @@ private static void process(final BlockConfig config) {

for (final SoundConfig sr : entry.sounds) {
if (sr.sound != null && !SoundRegistry.isSoundBlocked(sr.sound)) {
// Block sounds are always spot sounds
sr.spotSound = true;
final SoundEffect eff = new SoundEffect(sr);
if (sr.step != null && sr.step.booleanValue())
if (eff.type == SoundType.STEP)
blockData.stepSounds.add(eff);
else
blockData.sounds.add(eff);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,26 @@ public class SoundConfig {
public String sound = null;
@SerializedName("conditions")
public String conditions = ".*";
@SerializedName("soundType")
public String soundType = null;
@SerializedName("volume")
public Float volume = null;
@SerializedName("pitch")
public Float pitch = null;
@SerializedName("spot")
public Boolean spotSound = null;
@SerializedName("weight")
public Integer weight = null;
@SerializedName("variable")
public Boolean variable = null;
@SerializedName("repeatDelayRandom")
public Integer repeatDelayRandom = null;
@SerializedName("repeatDelay")
public Integer repeatDelay = null;

// Deprecation list
@SerializedName("skipFade")
public Boolean skipFade = null;
@SerializedName("spot")
public Boolean spotSound = null;
@SerializedName("step")
public Boolean step = null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
"sounds":[
{
"sound":"dsurround:floorsqueak",
"soundType":"step",
"volume":0.2,
"variable":true,
"step":true
"variable":true
}

]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
"sounds":[
{
"sound":"dsurround:floorsqueak",
"soundType":"step",
"volume":0.2,
"variable":true,
"step":true
"variable":true
}

]
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/assets/dsurround/data/Forestry_blocks.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
"sounds":[
{
"sound":"dsurround:floorsqueak",
"soundType":"step",
"volume":0.2,
"variable":true,
"step":true
"variable":true
}

]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
"sounds":[
{
"sound":"dsurround:floorsqueak",
"soundType":"step",
"volume":0.2,
"variable":true,
"step":true
"variable":true
}

]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
"sounds":[
{
"sound":"dsurround:floorsqueak",
"soundType":"step",
"volume":0.2,
"variable":true,
"step":true
"variable":true
}

]
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/assets/dsurround/data/blocks.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@
"sounds":[
{
"sound":"dsurround:floorsqueak",
"soundType":"step",
"volume":0.2,
"variable":true,
"step":true
"variable":true
}

]
Expand Down
Loading

0 comments on commit 7100717

Please sign in to comment.