Package transientlibs.bindedobjects.gamecontent

Source Code of transientlibs.bindedobjects.gamecontent.MusicTrackGDX

/*
* 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.Music;
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 transientlibs.slick2d.util.Log;
import transientlibs.bindedobjects.core.Binding;
import transientlibs.objects.primitives.Int;

/**
*
* @author kibertoad
*/
public class MusicTrackGDX extends Node {

    public Music music;
    public static ArrayList<MusicTrackGDX> musicList = new ArrayList<MusicTrackGDX>();
    public static TBoolean musicEnabled = new TBoolean(true);
    public static MusicTrackGDX lastMusic;
    public static MusicTrackGDX currentTrack = null;
    private static float VOLUME = (float) 0.75;
    public static MusicTrackGDX lastPlayedTrack;
    public static Int volumeVar = new Int(75);
    public boolean doLoop = true;

    /**
     * Get the global music volume.
     *
     * @return the global music volume
     */
    public static float getVolume() {
        return VOLUME;
    }

    /**
     * Set the global music volume, including on any currently playing track.
     * Value should be between or including 0.0 and 1.0.
     *
     * @param volume the volume to set.
     */
    public static void setVolume(float volume) {
        VOLUME = volume / 100;
        if (currentTrack != null) {
            currentTrack.music.setVolume(volume / 100);
        }
    }

    /**
     * Set the global music volume, including on any currently playing track.
     * Value should be between or including 0.0 and 1.0. Converts your double
     * into a float.
     *
     * @param volume the volume to set.
     */
    public static void setVolume(double volume) {
        setVolume((float) volume);
    }

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

            musicEnabled = Detonator.INSTANCE.getBindedTrigger("musicenabled");
            Detonator.INSTANCE.musicIsOn = Detonator.INSTANCE.getBindedTrigger("musicenabled");
            Detonator.INSTANCE.soundIsOn = Detonator.INSTANCE.getBindedTrigger("soundenabled");

            Log.info("Enable music: " + musicEnabled.value);



            reader.openFile("music.dat");

            while (!reader.eof) {

                reader.readRow();
                analyzeStringForMusics(reader);
            }

            reader.closeFile();
        }
    }

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

    public static Music loadMusicFile(String fromFile) {
        return Gdx.audio.newMusic(Gdx.files.internal(fromFile));
    }

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

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

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

        boolean withResult = false;

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

            musicList.add(new MusicTrackGDX());
            lastMusic = musicList.get(musicList.size() - 1);
            lastMusic.ID = analyst.getBinding(Binding.musicBinding);

            //ReqList.lastReqList = lastMusic.trainReqs;

           
            withResult = true;

        }

        if (nowStr.equals("noloop")) {
            lastMusic.doLoop = false;
            lastMusic.music.setLooping(false);
        }
       
        if (nowStr.equals("file")) {

            //lastMusic.LName = analyst.getNextString();
            //-woe, who learned a very importent lesson: don't reuse code by copy/pasting, reuse it by stuffing it in a variable
            String path = Detonator.INSTANCE.gameDataDir + "music/" + analyst.getNextString();
            Log.info("Music: Looking for " + path);
            boolean validFile = (new File(path)).exists();
            if (validFile) {
                lastMusic.music = loadMusicFile(path);
                lastMusic.music.setLooping(true);
                // Log.info("lastMusic.music = " +lastMusic.music.toString());
            } else {
                //  lastMusic.music = new Music ("NULL");
                Log.info("Music: Can't find " + path);
            }

            withResult = true;

        }



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

    }

    public static void play(String byCode) {
        if (musicEnabled.value == true) {
            play(getMusicByCode(byCode));
        }
    }

    public static void play(int byCode) {
        if (musicEnabled.value == true) {
            play(getMusicByID(byCode));
        }
    }
   
   
    public static void stop() {
        if (currentTrack != null) {
            currentTrack.music.stop();
        }
    }

    public static void playLastTrack() {
        if (musicEnabled.value == true) {
            //lastPlayedTrack.music.loop(1, VOLUME);
            lastPlayedTrack.music.play();
        }
    }

    public static void play(MusicTrackGDX whichOne) {

        //Log.info("musi: "+musicEnabled.value);

        if (musicEnabled.value == true) {


            //Log.info("time to play");
            //Log.info("try playing "+whichOne.ID);

            if ((whichOne != null) && (whichOne.music != null)) {
                Log.info("MusicTrack music: " + whichOne.ID);
                if (currentTrack != whichOne) {

                    if (currentTrack != null) {
                        currentTrack.music.stop();
                    }

                    currentTrack = whichOne;
                    currentTrack.music.setVolume(VOLUME);
                    //currentTrack.music.loop();
                    lastPlayedTrack = currentTrack;
                    currentTrack.music.play();

                }
            }
        }
    }

    public static MusicTrackGDX getMusicByID(int getCode) {
        if ((getCode != -1) && (!(musicList.isEmpty()))) {
            return musicList.get(getCode);
        } else {
            return null;
        }
    }

    public static MusicTrackGDX getMusicByCode(String getCode) {

        //Log.info("code: "+getCode);
        //Log.info("List size:" + musicList.size());

        int setCode = Binding.readBinding(Binding.musicBinding, getCode.toLowerCase());

        //Log.info("got code: "+setCode);

        if ((setCode != -1) && (musicList.size() >= setCode)) {
            return musicList.get(setCode);
        } else {
            return (null);
        }

    }
}
TOP

Related Classes of transientlibs.bindedobjects.gamecontent.MusicTrackGDX

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.