Package transientlibs.bindedobjects.gamecontent

Source Code of transientlibs.bindedobjects.gamecontent.SoundsGDX

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package transientlibs.bindedobjects.gamecontent;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Sound;
import transientlibs.objects.general.Node;
import transientlibs.objects.primitives.TBoolean;
import transientlibs.processors.misc.Detonator;

import transientlibs.tex.StringAnalyst;
import java.io.File;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import transientlibs.slick2d.util.Log;
import transientlibs.bindedobjects.core.Binding;
import transientlibs.preui.objects.gui.interfaces.ISound;

/**
*
* @author kibertoad
*/
public class SoundsGDX extends Node implements ISound {

    public static ArrayList<SoundsGDX> soundList = new ArrayList<SoundsGDX>();
    public static float volume = (float) 0.08;
    public static TBoolean soundEnabled = new TBoolean(true);
    public Sound sound;
    public static SoundsGDX lastSound;
    public static SoundsGDX currentSound = null;

    public static void loadSounds() {
        boolean validFile = (new File(Detonator.INSTANCE.gameDataDir + "sound.dat")).exists();
        if (validFile) {
            StringAnalyst reader = new StringAnalyst();


            reader.openFile("sound.dat");

            while (!reader.eof) {


                reader.readRow();
                analyzeStringForSounds(reader);
            }

            reader.closeFile();
        }
    }

    public static void analyzeStringForSounds(StringAnalyst analyst) {
        analyzeStringForSounds("-NO-", analyst);
    }

    public static Sound loadSoundFile(String theFile) {
        return Gdx.audio.newSound(Gdx.files.internal(theFile));
    }

    public static void analyzeStringForSounds(String getString, StringAnalyst analyst) {

        if (!"-NO-".equals(getString)) {
            analyst.fullStr = getString;
        }

        analyst.getNextString();
        String nowStr = analyst.lastStr;

        boolean withResult = false;

        if (nowStr.equals("sound")) {

            soundList.add(new SoundsGDX());
            lastSound = soundList.get(soundList.size() - 1);
            lastSound.ID = analyst.getBinding(Binding.soundBinding);

            //ReqList.lastReqList = lastSounds.trainReqs;

            withResult = true;

        }


        if (nowStr.equals("file")) {

            String path = Detonator.INSTANCE.gameDataDir + "sounds/" + analyst.getNextString();
            Log.info("Sounds: Looking for " + path);
            boolean validFile = (new File(path)).exists();
            if (validFile) {
                //lastMusic.LName = analyst.getNextString();
                lastSound.sound = loadSoundFile(path);
            } else {
                Log.info("Sound: can't find " + path);
            }
            withResult = true;
        }




        if ((withResult == false) && (nowStr.length() > 2) && (!nowStr.startsWith("//"))) {
            Log.error("Unknown string: " + nowStr);
        }

    }

    public static void stop() {
        if (currentSound != null) {
            currentSound.sound.stop();
        }
    }

    public static void setVolume(float toVolume) {
        volume = toVolume / 100;
    }

    public static void play(String whichOne) {
        if (soundEnabled.value == true) {
            play(getSoundByCode(whichOne));
        }
    }

    public static void play(SoundsGDX whichOne) {

        if (soundEnabled.value == true) {

            Log.info("Sounds playing: " + whichOne.ID);
            if (whichOne.sound != null) {
                if (currentSound != whichOne) {
                    currentSound = whichOne;
                }
                //currentSound.sound.play();
                //currentSound.sound.play(1, volume);               
                currentSound.sound.play(volume);
            }

        }
    }

    public static SoundsGDX getSoundByID(int getCode) {
        return soundList.get(getCode);
    }

    public static SoundsGDX getSoundByCode(String getCode) {

        return soundList.get(Binding.readBinding(Binding.soundBinding, getCode.toLowerCase()));
    }
}
TOP

Related Classes of transientlibs.bindedobjects.gamecontent.SoundsGDX

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.