Subversion Repositories AndroidProjects

Rev

Rev 526 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.gebauz.pingk.gamestates;

import com.badlogic.gdx.Application.ApplicationType;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Texture;
import com.gebauz.Bauzoid.app.Game;
import com.gebauz.Bauzoid.gamestates.BaseGameState;
import com.gebauz.Bauzoid.graphics.sprite.Sprite;
import com.gebauz.Bauzoid.math.Matrix4;
import com.gebauz.pingk.PingkCustomServices;
import com.gebauz.pingk.game.GameConsts;
import com.gebauz.pingk.game.TextConsts;
import com.gebauz.pingk.ui.BaseButton;
import com.gebauz.pingk.ui.TextButton;

public class TitleState extends BaseGameState
{
        private TextButton mGameStartButton = null;
        private TextButton mSoundButton = null;
        private TextButton mMusicButton = null;
        private TextButton mCreditsButton = null;
        private TextButton mExitButton = null;
       
        private Sprite mTitle = null;
       
        private Sound mButtonPress = null;
       
        BaseButton.TouchListener mGameStartListener = new BaseButton.TouchListener()
        {
                @Override
                public void onTouched(BaseButton sender, int tag)
                {
                        switchTo(com.gebauz.pingk.gamestates.MainGameState.class);
                }
        };
       
        BaseButton.TouchListener mMusicListener = new BaseButton.TouchListener()
        {
                @Override
                public void onTouched(BaseButton sender, int tag)
                {
                        TextButton tb = (TextButton)sender;
                        if (getGame().getAudio().isMusicEnabled())
                        {
                                getGame().getAudio().setMusicEnabled(false);
                                tb.setText(TextConsts.MUSIC_OFF);
                        }
                        else
                        {
                                getGame().getAudio().setMusicEnabled(true);
                                tb.setText(TextConsts.MUSIC_ON);                               
                        }
                }
        };
       
        BaseButton.TouchListener mSoundListener = new BaseButton.TouchListener()
        {
                @Override
                public void onTouched(BaseButton sender, int tag)
                {
                        TextButton tb = (TextButton)sender;
                        if (getGame().getAudio().isSoundEnabled())
                        {
                                getGame().getAudio().setSoundEnabled(false);
                                tb.setText(TextConsts.SOUND_OFF);
                        }
                        else
                        {
                                getGame().getAudio().setSoundEnabled(true);
                                tb.setText(TextConsts.SOUND_ON);                               
                        }
                }
        };
       
        BaseButton.TouchListener mCreditsListener = new BaseButton.TouchListener()
        {
                @Override
                public void onTouched(BaseButton sender, int tag)
                {
                        //GameStateManager.getInstance().switchState("CreditsState");
                        switchTo(com.gebauz.pingk.gamestates.CreditsState.class);
                }
        };
       
        BaseButton.TouchListener mExitListener = new BaseButton.TouchListener()
        {
                @Override
                public void onTouched(BaseButton sender, int tag)
                {
                        //GameStateManager.getInstance().switchState("CreditsState");
                        Gdx.app.exit();
                        //System.exit(0);
                }
        };
       
        public TitleState(Game game)
        {
                super(game);
                setFading(true, true);
        }

        @Override
        public void init()
        {
                mButtonPress = getGame().getAudio().newManagedSound("data/sounds/paddleimpact.wav");
               
                mTitle = new Sprite(getGraphics(), new Texture(Gdx.files.internal("data/textures/logo.png")));
                mTitle.x = GameConsts.VIRTUAL_SCREEN_WIDTH/2.0f;
                mTitle.y = GameConsts.TITLE_POSITION_Y;
                mTitle.w = GameConsts.TITLE_WIDTH;
                mTitle.h = GameConsts.TITLE_HEIGHT;
                mTitle.pivotX = GameConsts.TITLE_WIDTH / 2.0f;
                mTitle.pivotY = 0.0f;
                mTitle.color = GameConsts.PINGK_COLOR.copy();

                mGameStartButton = new TextButton(getGame(), TextConsts.START_GAME);
                mGameStartButton.x = GameConsts.VIRTUAL_SCREEN_WIDTH / 2.0f;
                mGameStartButton.y = GameConsts.TITLE_POSITION_Y + GameConsts.TITLE_HEIGHT + 0.0f;
                mGameStartButton.scale = GameConsts.TITLE_MENU_TEXT_SCALE;
                mGameStartButton.setAlignment(TextButton.HorizontalAlignment.CENTER, TextButton.VerticalAlignment.MIDDLE);
                mGameStartButton.setTouchListener(mGameStartListener);
               
                String soundEnabled = TextConsts.SOUND_ON;
                if (!getGame().getAudio().isSoundEnabled())
                        soundEnabled = TextConsts.SOUND_OFF;
                mSoundButton = new TextButton(getGame(), soundEnabled);
                mSoundButton.x = GameConsts.VIRTUAL_SCREEN_WIDTH / 2.0f;
                mSoundButton.y = GameConsts.TITLE_POSITION_Y + GameConsts.TITLE_HEIGHT + 40.0f;
                mSoundButton.scale = GameConsts.TITLE_MENU_TEXT_SCALE;
                mSoundButton.setAlignment(TextButton.HorizontalAlignment.CENTER, TextButton.VerticalAlignment.MIDDLE);
                mSoundButton.setTouchListener(mSoundListener);
               
                String musicEnabled = TextConsts.MUSIC_ON;
                if (!getGame().getAudio().isMusicEnabled())
                        musicEnabled = TextConsts.MUSIC_OFF;
                mMusicButton = new TextButton(getGame(), musicEnabled);
                mMusicButton.x = GameConsts.VIRTUAL_SCREEN_WIDTH / 2.0f;
                mMusicButton.y = GameConsts.TITLE_POSITION_Y + GameConsts.TITLE_HEIGHT + 80.0f;
                mMusicButton.scale = GameConsts.TITLE_MENU_TEXT_SCALE;
                mMusicButton.setAlignment(TextButton.HorizontalAlignment.CENTER, TextButton.VerticalAlignment.MIDDLE);
                mMusicButton.setTouchListener(mMusicListener);
               
                mCreditsButton = new TextButton(getGame(), TextConsts.CREDITS);
                mCreditsButton.x = GameConsts.VIRTUAL_SCREEN_WIDTH / 2.0f;
                mCreditsButton.y = GameConsts.TITLE_POSITION_Y + GameConsts.TITLE_HEIGHT + 120.0f;
                mCreditsButton.scale = GameConsts.TITLE_MENU_TEXT_SCALE;
                mCreditsButton.setAlignment(TextButton.HorizontalAlignment.CENTER, TextButton.VerticalAlignment.MIDDLE);
                mCreditsButton.setTouchListener(mCreditsListener);
               
               
                if (Gdx.app.getType() == ApplicationType.Desktop)
                {
                        mExitButton = new TextButton(getGame(), TextConsts.EXIT);
                        mExitButton.x = GameConsts.VIRTUAL_SCREEN_WIDTH / 2.0f;
                        mExitButton.y = GameConsts.TITLE_POSITION_Y + GameConsts.TITLE_HEIGHT + 160.0f;
                        mExitButton.scale = GameConsts.TITLE_MENU_TEXT_SCALE;
                        mExitButton.setAlignment(TextButton.HorizontalAlignment.CENTER, TextButton.VerticalAlignment.MIDDLE);
                        mExitButton.setTouchListener(mExitListener);
                }
               
                //mHelp = new HelpScreen(GameConsts.VIRTUAL_SCREEN_HEIGHT);
               
                //getGame().getAudio().playMusic("data/music/music1.ogg");
                PingkCustomServices.getInstance().getMusicPlayer().start();            
        }

        @Override
        public void exit()
        {
                if (mTitle != null)
                {
                        mTitle.dispose();
                        mTitle = null;
                }
               
                if (mGameStartButton != null)
                {
                        mGameStartButton.exit();
                        mGameStartButton = null;
                }
                if (mSoundButton != null)
                {
                        mSoundButton.exit();
                        mSoundButton = null;
                }
                if (mCreditsButton != null)
                {
                        mCreditsButton.exit();
                        mCreditsButton = null; 
                }
                if (mMusicButton != null)
                {
                        mMusicButton.exit();
                        mMusicButton = null;
                }
                if (mExitButton != null)
                {
                        mExitButton.exit();
                        mExitButton = null;
                }
        }

        @Override
        public void update(float deltaTime)
        {
                mTitle.update(deltaTime);
               
//              if (!mHelp.isActive())
                {
                        mGameStartButton.update(deltaTime);
                        mSoundButton.update(deltaTime);
                        mMusicButton.update(deltaTime);
                        mCreditsButton.update(deltaTime);
                       
                        if (mExitButton != null)
                                mExitButton.update(deltaTime);
                }
               
                //mHelp.update(deltaTime);
        }

        @Override
        public void render()
        {
                //Gdx.gl.glClearColor(0.05f, 0.05f, 0.05f, 0.0f);
                //Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
                getGraphics().clear(0.05f, 0.05f, 0.05f, 0.0f);
               
                getRenderStates().projection = Matrix4.createOrtho(
                                0.0f,
                                GameConsts.VIRTUAL_SCREEN_WIDTH-1,
                                GameConsts.VIRTUAL_SCREEN_HEIGHT-1,
                                0.0f,
                                0.0f,
                                1.0f
                        );
               
                //PingkCustomServices.getInstance().getFont().drawText("TEST", 20, 20, GameConsts.PINGK_COLOR);
               
                float alpha = getGame().getRandomFloat(0.7f, 1.0f);
                mTitle.color.w = alpha;
        mTitle.render();
               
/*              if (Math.cos(mTimer * GameConsts.TITLE_FLASH_INTERVAL) > 0.0f)
                {
                        Log.v(GameConsts.LOG_TAG, "t=" + mTimer);
                        renderText(R.string.title_touch_to_start, 300.0f, 3.0f);
                }*/

               
        mGameStartButton.render();
                mSoundButton.render();
                mMusicButton.render();
                mCreditsButton.render();
                if (mExitButton != null)
                        mExitButton.render();
                //mHelp.render();
        }

}