Subversion Repositories AndroidProjects

Rev

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

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

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.content.SharedPreferences;

import com.gebauz.pingK.common.R;
import com.gebauz.pingK.common.framework.GLUtil;
import com.gebauz.pingK.common.framework.MathUtil;
import com.gebauz.pingK.common.framework.ResourceManager;
import com.gebauz.pingK.common.framework.Sprite2D;
import com.gebauz.pingK.common.game.GameConsts;
import com.gebauz.pingK.common.game.GameServices;
import com.gebauz.pingK.common.game.MultitouchInput;
import com.gebauz.pingK.common.game.MultitouchInput.Finger;

public class LogoState extends BaseGameState
{              
        static private float UPDATE_INTERVAL = 0.3f;

        public class Bar
        {
                private float x;
                private float y;
                private float w;
                private float h;
                private float alpha;
               
                private float mCurrentHeight = 0.0f;
                private float mTargetHeight = 0.0f;
                private float mUpdateTimer = GameServices.getRandomFloat(0.0f, UPDATE_INTERVAL);
               
                private float mMoveDelta;
               
                public Bar(float _x, float _y, float _w, float _h, float _alpha, float _move)
                {
                        x = _x;
                        y = _y;
                        w = _w;
                        h = _h;
                        alpha = _alpha;
                       
                        mTargetHeight = GameServices.getRandomFloat(0.2f*h, h);
                        mCurrentHeight = _h;
                        mMoveDelta = _move;
                }
               
                public void update(float deltaTime)
                {
                        mUpdateTimer += deltaTime;
                        if (mUpdateTimer > UPDATE_INTERVAL)
                        {
                                mTargetHeight = GameServices.getRandomFloat(0.15f*h, h);
                                mUpdateTimer = 0.0f;
                        }
                       
                        //float diff = mTargetHeight - mCurrentHeight;
                        //mCurrentHeight += (diff * deltaTime * 10.0f);
                        if (mTargetHeight > mCurrentHeight)
                        {
                                float diff = mTargetHeight - mCurrentHeight;
                                mCurrentHeight += (diff * deltaTime * 15.0f);
                        }
                        else if (mTargetHeight < mCurrentHeight)
                        {
                                mCurrentHeight -= 300.0f * deltaTime;
                                if (mCurrentHeight < mTargetHeight)
                                        mCurrentHeight = mTargetHeight;
                                /*float diff = mTargetHeight - mCurrentHeight;
                                mCurrentHeight += (diff * deltaTime * 3.0f);*/

                        }
                       
                        x -= mMoveDelta * deltaTime;
                       
                        if (x < (-w))
                        {
                                x = GameConsts.VIRTUAL_SCREEN_WIDTH + (w);
                        }
                }
               
                public void render()
                {
                        mBar.x = x;
                        mBar.y = y;
                        mBar.w = w;
                        mBar.h = mCurrentHeight;
                        //mBar.h = h;
                        mBar.pivotX = mBar.w/2.0f;
                        mBar.pivotY = mBar.h;
                       
                        mBar.setColor(1.0f, 1.0f, 1.0f, alpha);
                       
                        mBar.render();
                }
        }
       
        private int NUM_BARS = 24;
       
        private float mTimer = 0.0f;
       
        private Sprite2D mBackground;
        private Sprite2D mBar;
        private Sprite2D mLogo;
       
        private boolean mSoundEnabled = true;
       
        private Bar[] mBars = new Bar[NUM_BARS];
       
        private float mWidths[] =
        {
                80.0f,
                100.0f,
                150.0f,
                250.0f
        };
       
        private float mHeights[] =
        {
                400.0f,
                380.0f,
                350.0f,
                300.0f,
        };
       
        private float mAlphas[] =
        {
                0.3f,
                0.5f,
                0.7f,
                0.9f,
        };
       
        private float mSpeeds[] =
        {
                50.0f,
                100.0f,
                250.0f,
                700.0f,
        };
       
        public LogoState()
        {
        }
       
        @Override
        public void init()
        {
                GameServices.showAd(false);
               
            SharedPreferences myPrefs = ResourceManager.getInstance().getCurrentContext().getSharedPreferences(GameConsts.PREF_NAME, Context.MODE_PRIVATE);
        boolean soundsEnabled = myPrefs.getBoolean(GameConsts.PREF_AUDIO_ENABLED, true);
        enableSound(soundsEnabled);  
               
                mBackground = new Sprite2D();
                mBackground.init(R.drawable.logo_bg, 0.0f, 0.0f, GameConsts.VIRTUAL_SCREEN_WIDTH, GameConsts.VIRTUAL_SCREEN_HEIGHT);
                mBackground.pivotX = 0.0f;
                mBackground.pivotY = 0.0f;
               
                mBar = new Sprite2D();
                mBar.init(R.drawable.logo_bars);
               
                mLogo = new Sprite2D();
                mLogo.init(R.drawable.logo_gebauz, GameConsts.VIRTUAL_SCREEN_WIDTH/2.0f, GameConsts.VIRTUAL_SCREEN_HEIGHT/2.0f, GameConsts.LOGO_GEBAUZ_WIDTH, GameConsts.LOGO_GEBAUZ_HEIGHT);
                mLogo.pivotX = mLogo.w/2.0f;
                mLogo.pivotY = mLogo.h/2.0f;
               
                for (int i = 0; i < NUM_BARS; i++)
                {
                        int z = GameServices.getRandomInt(0, 3);
                       
                        // the smaller, the farther in the back
                        mBars[i] = new Bar(GameServices.getRandomFloat(-mWidths[z], GameConsts.VIRTUAL_SCREEN_WIDTH + mWidths[z]), GameConsts.VIRTUAL_SCREEN_HEIGHT,
                                        GameServices.getRandomFloat(mWidths[z] * 0.8f, mWidths[z] * 1.2f),
                                        GameServices.getRandomFloat(mHeights[z] * 0.4f, mHeights[z] * 1.1f),
                                        GameServices.getRandomFloat(mAlphas[z] * 0.75f, mAlphas[z] * 1.0f),
                                        GameServices.getRandomFloat(mSpeeds[z] * 0.75f, mSpeeds[z] * 1.5f));
                }
               
                GameServices.getAudio().playMusic(R.raw.music1);
        }
       
        @Override
        public void exit()
        {
                mBackground = null;
        }
       
        @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) || (mTimer > GameConsts.LOGO_TIMEOUT))
                {
                        // switch state
                        GameStateManager.getInstance().switchState("TitleState");
                }
               
                mBackground.update(deltaTime);
                for (int i = 0; i < NUM_BARS; i++)
                {
                        mBars[i].update(deltaTime);
                }
               
/*                      if (!mHelp.isActive())
                        {
                                mGameStartButton.update(deltaTime);
                                mOptionsButton.update(deltaTime);
                                mHelpButton.update(deltaTime);
                        }
                       
                        mHelp.update(deltaTime);*/

        }
       
        @Override
        public void render()
        {
        GL10 gl = GLUtil.getGL();

        gl.glClearColor(1.0f, 1.0f, 1.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);
       
        gl.glMatrixMode(GL10.GL_MODELVIEW);
       
        mBackground.render();
       
                for (int i = 0; i < NUM_BARS; i++)
                {
                        mBars[i].render();
                }
               
                if (mTimer > GameConsts.LOGO_GEBAUZ_SHOW)
                {
                        float alpha = MathUtil.clamp((mTimer - GameConsts.LOGO_GEBAUZ_SHOW) / GameConsts.LOGO_GEBAUZ_FADE_TIME, 0.0f, 1.0f);
                        mLogo.setColor(1.0f, 1.0f, 1.0f, alpha);
                        mLogo.render();
                }

/*                      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();
                       
                mGameStartButton.render();
                        mOptionsButton.render();
                        mHelpButton.render();
                        mHelp.render();*/

        }
       
        @Override
        public boolean doFadeIn()
        {
                return true;
        }
       
        @Override
        public boolean doFadeOut()
        {
                return true;
        }
       
        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();
        }

}