org.blockartistry.mod.DynSurround.client.footsteps.engine.implem.BasicAcoustic.java Source code

Java tutorial

Introduction

Here is the source code for org.blockartistry.mod.DynSurround.client.footsteps.engine.implem.BasicAcoustic.java

Source

/*
 * This file is part of Dynamic Surroundings, licensed under the MIT License (MIT).
 *
 * Copyright (c) OreCruncher
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package org.blockartistry.mod.DynSurround.client.footsteps.engine.implem;

import java.util.Random;

import org.apache.commons.lang3.StringUtils;
import org.blockartistry.mod.DynSurround.client.footsteps.engine.interfaces.IAcoustic;
import org.blockartistry.mod.DynSurround.client.footsteps.engine.interfaces.EventType;
import org.blockartistry.mod.DynSurround.client.footsteps.engine.interfaces.IOptions;
import org.blockartistry.mod.DynSurround.client.footsteps.engine.interfaces.IOptions.Option;
import org.blockartistry.mod.DynSurround.client.footsteps.engine.interfaces.ISoundPlayer;

import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

@SideOnly(Side.CLIENT)
public class BasicAcoustic implements IAcoustic {
    protected String soundName;
    protected float volMin = 1f;
    protected float volMax = 1f;
    protected float pitchMin = 1f;
    protected float pitchMax = 1f;

    protected IOptions outputOptions;

    @Override
    public void playSound(final ISoundPlayer player, final Object location, final EventType event,
            final IOptions inputOptions) {
        // Special case for intentionally empty sounds (as opposed to fall back
        // sounds)
        if (StringUtils.isEmpty(this.soundName))
            return;

        float volume = generateVolume(player.getRNG());
        float pitch = generatePitch(player.getRNG());
        if (inputOptions != null) {
            if (inputOptions.hasOption(Option.GLIDING_VOLUME)) {
                volume = this.volMin
                        + (this.volMax - this.volMin) * (Float) inputOptions.getOption(Option.GLIDING_VOLUME);
            }
            if (inputOptions.hasOption(Option.GLIDING_PITCH)) {
                pitch = this.pitchMin
                        + (this.pitchMax - this.pitchMin) * (Float) inputOptions.getOption(Option.GLIDING_PITCH);
            }
        }

        player.playSound(location, this.soundName, volume, pitch, this.outputOptions);
    }

    private float generateVolume(final Random rng) {
        return randAB(rng, this.volMin, this.volMax);
    }

    private float generatePitch(final Random rng) {
        return randAB(rng, this.pitchMin, this.pitchMax);
    }

    private float randAB(final Random rng, final float a, final float b) {
        if (a >= b)
            return a;

        return a + rng.nextFloat() * (b - a);
    }

    //

    public void setSoundName(final String soundName) {
        this.soundName = soundName;
    }

    public void setVolMin(final float volMin) {
        this.volMin = volMin;
    }

    public void setVolMax(final float volMax) {
        this.volMax = volMax;
    }

    public void setPitchMin(final float pitchMin) {
        this.pitchMin = pitchMin;
    }

    public void setPitchMax(final float pitchMax) {
        this.pitchMax = pitchMax;
    }

}