Subversion Repositories AndroidProjects

Rev

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

package com.gebauz.pingK.common.game.gamestates;

import java.util.Random;

import javax.microedition.khronos.opengles.GL10;

import com.gebauz.pingK.common.R;
import com.gebauz.pingK.common.framework.GLUtil;
import com.gebauz.pingK.common.framework.ResourceManager;
import com.gebauz.pingK.common.framework.Sprite2D;
import com.gebauz.pingK.common.game.BaseButton;
import com.gebauz.pingK.common.game.Font;
import com.gebauz.pingK.common.game.GameConsts;
import com.gebauz.pingK.common.game.GameServices;
import com.gebauz.pingK.common.game.HelpScreen;
import com.gebauz.pingK.common.game.MultitouchInput;
import com.gebauz.pingK.common.game.TextButton;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;


public class TitleState extends BaseGameState
{
        private float mTimer = 0.0f;
       
        private TextButton mGameStartButton;
        private TextButton mOptionsButton;
        private TextButton mHelpButton;
        private TextButton mCreditsButton;
       
        private Sprite2D mTitle = null;
       
        //private Sprite2D mTouchPoint = null;
       
        private boolean mSoundEnabled = true;
       
        private HelpScreen mHelp;
       
        int mSoundIds[] =
        {
                R.raw.paddleimpact,
        };
       
        BaseButton.TouchListener mGameStartListener = new BaseButton.TouchListener()
        {
                @Override
                public void onTouched(BaseButton sender, int tag)
                {
                        GameStateManager.getInstance().switchState("MainGameState");
                }
        };
       
        BaseButton.TouchListener mOptionsListener = new BaseButton.TouchListener()
        {
                @Override
                public void onTouched(BaseButton sender, int tag)
                {
                        TextButton tb = (TextButton)sender;
                        if (GameServices.getAudio().isMusicEnabled())
                        {
                                GameServices.getAudio().enableMusic(false);
                                tb.setText(ResourceManager.getResources().getString(R.string.title_music_off));
                        }
                        else
                        {
                                GameServices.getAudio().enableMusic(true);
                                tb.setText(ResourceManager.getResources().getString(R.string.title_music_on));                         
                        }
                }
        };
       
        BaseButton.TouchListener mHelpListener = new BaseButton.TouchListener()
        {
                @Override
                public void onTouched(BaseButton sender, int tag)
                {
                        mHelp.show();
                }
        };
       
        BaseButton.TouchListener mCreditsListener = new BaseButton.TouchListener()
        {
                @Override
                public void onTouched(BaseButton sender, int tag)
                {
                        GameStateManager.getInstance().switchState("CreditsState");
                }
        };
       
        public TitleState()
        {
               
        }
       
        @Override
        public void init()
        {
                GameServices.showAd(true);
                GameServices.getAudio().init(mSoundIds);
               
            SharedPreferences myPrefs = ResourceManager.getInstance().getCurrentContext().getSharedPreferences(GameConsts.PREF_NAME, Context.MODE_PRIVATE);
        boolean soundsEnabled = myPrefs.getBoolean(GameConsts.PREF_AUDIO_ENABLED, true);
        enableSound(soundsEnabled);      
                       
                mTitle = new Sprite2D();
                mTitle.init(R.drawable.logo, GameConsts.VIRTUAL_SCREEN_WIDTH/2.0f, GameConsts.TITLE_POSITION_Y, GameConsts.TITLE_WIDTH, GameConsts.TITLE_HEIGHT);
                mTitle.pivotX = GameConsts.TITLE_WIDTH / 2.0f;
                mTitle.pivotY = 0.0f;
               
                /*mTouchPoint = new Sprite2D();
                mTouchPoint.init(R.drawable.touchpoint, 0.0f, 0.0f, 64.0f, 64.0f);
                mTouchPoint.pivotX = mTouchPoint.w / 2.0f;
                mTouchPoint.pivotY = mTouchPoint.h / 2.0f;*/


                mGameStartButton = new TextButton(GameServices.getFont(), ResourceManager.getInstance().getCurrentContext().getString(R.string.title_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 musicEnabled = ResourceManager.getInstance().getCurrentContext().getString(R.string.title_music_on);
                if (!GameServices.getAudio().isMusicEnabled())
                        musicEnabled = ResourceManager.getInstance().getCurrentContext().getString(R.string.title_music_off);
                mOptionsButton = new TextButton(GameServices.getFont(), musicEnabled);
                mOptionsButton.x = GameConsts.VIRTUAL_SCREEN_WIDTH / 2.0f;
                mOptionsButton.y = GameConsts.TITLE_POSITION_Y + GameConsts.TITLE_HEIGHT + 40.0f;
                mOptionsButton.scale = GameConsts.TITLE_MENU_TEXT_SCALE;
                mOptionsButton.setAlignment(TextButton.HorizontalAlignment.CENTER, TextButton.VerticalAlignment.MIDDLE);
                mOptionsButton.setTouchListener(mOptionsListener);
               
                mHelpButton = new TextButton(GameServices.getFont(), ResourceManager.getInstance().getCurrentContext().getString(R.string.title_help));
                mHelpButton.x = GameConsts.VIRTUAL_SCREEN_WIDTH / 2.0f;
                mHelpButton.y = GameConsts.TITLE_POSITION_Y + GameConsts.TITLE_HEIGHT + 80.0f;
                mHelpButton.scale = GameConsts.TITLE_MENU_TEXT_SCALE;
                mHelpButton.setAlignment(TextButton.HorizontalAlignment.CENTER, TextButton.VerticalAlignment.MIDDLE);
                mHelpButton.setTouchListener(mHelpListener);
               
                mCreditsButton = new TextButton(GameServices.getFont(), ResourceManager.getInstance().getCurrentContext().getString(R.string.title_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);
               
                mHelp = new HelpScreen(GameConsts.VIRTUAL_SCREEN_HEIGHT);
        }
       
        @Override
        public void exit()
        {
                mGameStartButton = null;
                mOptionsButton = null;
                mCreditsButton = null;
                mHelpButton = null;
               
                GameServices.getAudio().exit();
        }
       
        public void enableSound(boolean enable)
        {
                mSoundEnabled = enable;
                GameServices.getAudio().setSoundsOn(mSoundEnabled);
               
                // store in prefs
                SharedPreferences myPrefs = ResourceManager.getInstance().getCurrentContext().getSharedPreferences(GameConsts.PREF_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor prefsEditor = myPrefs.edit();
        prefsEditor.putBoolean(GameConsts.PREF_AUDIO_ENABLED, enable);
        //prefsEditor.putBoolean(GameConsts.PREF_MUSIC_ENABLED), enable);
        prefsEditor.commit();
        }
       
        @Override
        public void onSurfaceChanged(int width, int height)
        {
                GLUtil.getGL().glViewport(0, 0, width, height);
        }
       
        @Override
        public void update(float deltaTime)
        {
                mTimer += deltaTime;
                mTitle.update(deltaTime);
               
/*              Finger finger = MultitouchInput.getInstance().getTouchPointInside(0.0f, 0.0f, GameConsts.VIRTUAL_SCREEN_WIDTH, GameConsts.VIRTUAL_SCREEN_HEIGHT);
                if (finger != null)
                {
                        // switch state
                        GameStateManager.getInstance().switchState("MainGameState");
                }*/

               
                if (!mHelp.isActive())
                {
                        mGameStartButton.update(deltaTime);
                        mOptionsButton.update(deltaTime);
                        mHelpButton.update(deltaTime);
                        mCreditsButton.update(deltaTime);
                }
               
                mHelp.update(deltaTime);
        }
       
        @Override
        public void render()
        {
        GL10 gl = GLUtil.getGL();

        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

                gl.glMatrixMode(GL10.GL_PROJECTION);
                gl.glLoadIdentity();
        gl.glOrthof(0, GameConsts.VIRTUAL_SCREEN_WIDTH-1.0f, GameConsts.VIRTUAL_SCREEN_HEIGHT-1.0f, 0, 0, 1);

                float alpha = GameServices.getRandomFloat(0.7f, 1.0f);
                mTitle.setColor(GameConsts.PINGK_COLOR.x, GameConsts.PINGK_COLOR.y, GameConsts.PINGK_COLOR.z, 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();
                mOptionsButton.render();
                mHelpButton.render();
                mCreditsButton.render();
                mHelp.render();
               
               
/*              MultitouchInput mt = MultitouchInput.getInstance();
                for (int i = 0; i < mt.getNumFingers(); i++)
                {
                        MultitouchInput.Finger finger = mt.getFinger(i);
                       
                        mTouchPoint.x = finger.x;
                        mTouchPoint.y = finger.y;
                       
                        mTouchPoint.render();                  
                }*/

        }
       
/*      public void renderText(int id, float posY, float scale)
        {
                String text = GameStateManager.getInstance().getContext().getString(id);
               
                float w = text.length() * Font.CHARACTER_SIZE * scale;
               
                mFont.drawText(text, (GameConsts.VIRTUAL_SCREEN_WIDTH - w) / 2.0f, posY, scale);
        }
        */

        @Override
        public boolean doFadeIn()
        {
                return true;
        }
       
        @Override
        public boolean doFadeOut()
        {
                return true;
        }
}