Subversion Repositories AndroidProjects

Rev

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

package com.gebauz.pingk.gamestates;

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.Bauzoid.math.Vector2;
import com.gebauz.pingk.game.GameConsts;

public class LogoState extends BaseGameState
{
        static private String mGebauzTextures[] =
        {
                "data/textures/logo_g.png",
                "data/textures/logo_e.png",
                "data/textures/logo_b.png",
                "data/textures/logo_a.png",
                "data/textures/logo_u.png",
                "data/textures/logo_z.png",
        };
       
        static private final int STATE_INITIAL = 0;
        static private final int STATE_ARRANGE_TO_PADDLE = 1;
        static private final int STATE_BALL_MOVE = 2;
        static private final int STATE_BALL_BOUNCE = 3;
        static private final int STATE_ARRANGE_TO_LOGO = 4;
        static private final int STATE_LOGO_WAIT = 5;
       
        static private final int NUM_SPRITES = mGebauzTextures.length;
       
        static private final float PADDLE_OFFSET_X = 12.0f;
        static private final float PADDLE_OFFSET_Y = 25.0f;
       
        static private final float CHAR_DISTANCE = 50.0f;
        static private final float LOGO_Z_SPEED = 1200.0f;
        static private final float LOGO_Z_SPEED_BOUNCE = 800.0f;
        static private final float LOGO_ACCEL = 7.0f;

        private Sprite mGebauzLogo[] = new Sprite[NUM_SPRITES];
        private Vector2 mFinalPositions[] = new Vector2[NUM_SPRITES];
        /*private Vector2 mPaddlePositions[] =
        {
                new Vector2(GameConsts.VIRTUAL_SCREEN_WIDTH / 4.0f,
                                GameConsts.VIRTUAL_SCREEN_HEIGHT / 2.0f - 2*PADDLE_OFFSET_Y),
                new Vector2(GameConsts.VIRTUAL_SCREEN_WIDTH / 4.0f - PADDLE_OFFSET_X,
                                GameConsts.VIRTUAL_SCREEN_HEIGHT / 2.0f - PADDLE_OFFSET_Y),
                new Vector2(GameConsts.VIRTUAL_SCREEN_WIDTH / 4.0f - 1.5f*PADDLE_OFFSET_X,
                                GameConsts.VIRTUAL_SCREEN_HEIGHT / 2.0f),
                new Vector2(GameConsts.VIRTUAL_SCREEN_WIDTH / 4.0f - PADDLE_OFFSET_X,
                                GameConsts.VIRTUAL_SCREEN_HEIGHT / 2.0f + PADDLE_OFFSET_Y),
                new Vector2(GameConsts.VIRTUAL_SCREEN_WIDTH / 4.0f,
                                GameConsts.VIRTUAL_SCREEN_HEIGHT / 2.0f + 2*PADDLE_OFFSET_Y),
                new Vector2(GameConsts.VIRTUAL_SCREEN_WIDTH + 64.0f,
                                GameConsts.VIRTUAL_SCREEN_HEIGHT / 2.0f),
        };*/

        private Vector2 mPaddlePositions[] =
        {
                new Vector2(GameConsts.VIRTUAL_SCREEN_WIDTH / 4.0f - PADDLE_OFFSET_X,
                                        GameConsts.VIRTUAL_SCREEN_HEIGHT / 2.0f + PADDLE_OFFSET_Y),
                new Vector2(GameConsts.VIRTUAL_SCREEN_WIDTH / 4.0f,
                                        GameConsts.VIRTUAL_SCREEN_HEIGHT / 2.0f + 2*PADDLE_OFFSET_Y),
                new Vector2(GameConsts.VIRTUAL_SCREEN_WIDTH / 4.0f - PADDLE_OFFSET_X,
                                GameConsts.VIRTUAL_SCREEN_HEIGHT / 2.0f - PADDLE_OFFSET_Y),
                new Vector2(GameConsts.VIRTUAL_SCREEN_WIDTH / 4.0f,
                                GameConsts.VIRTUAL_SCREEN_HEIGHT / 2.0f - 2*PADDLE_OFFSET_Y),
                new Vector2(GameConsts.VIRTUAL_SCREEN_WIDTH / 4.0f - 1.5f*PADDLE_OFFSET_X,
                                GameConsts.VIRTUAL_SCREEN_HEIGHT / 2.0f),
                new Vector2(GameConsts.VIRTUAL_SCREEN_WIDTH + 64.0f,
                                GameConsts.VIRTUAL_SCREEN_HEIGHT / 2.0f),
        };
       
        private Vector2 mBallTarget = new Vector2(GameConsts.VIRTUAL_SCREEN_WIDTH / 4.0f - 1.5f*PADDLE_OFFSET_X + PADDLE_OFFSET_Y,
                        GameConsts.VIRTUAL_SCREEN_HEIGHT / 2.5f);
        private Vector2 mBounceTarget = new Vector2(GameConsts.VIRTUAL_SCREEN_WIDTH / 2.0f,
                        GameConsts.VIRTUAL_SCREEN_HEIGHT / 1.0f);
       
        private int mCurrentState = STATE_INITIAL;
       
        private float mTimer = 0.0f;
       
        private Sound mBounce = null;
        private Sound mWoosh = null;

        public LogoState(Game game)
        {
                super(game);
                setFading(true, true);
        }

        @Override
        public void init()
        {
                for (int i = 0; i < NUM_SPRITES; i++)
                {
                        mGebauzLogo[i] = new Sprite(getGraphics(), new Texture(mGebauzTextures[i]));
                        mGebauzLogo[i].x = getGame().getRandomFloat(0, GameConsts.VIRTUAL_SCREEN_WIDTH);
                        mGebauzLogo[i].y = getGame().getRandomFloat(0, GameConsts.VIRTUAL_SCREEN_HEIGHT);
                       
                        if (mGebauzLogo[i].x >= (GameConsts.VIRTUAL_SCREEN_WIDTH / 2.0f))
                        {
                                mGebauzLogo[i].x = GameConsts.VIRTUAL_SCREEN_WIDTH + 64.0f;
                        }
                        else
                        {
                                mGebauzLogo[i].x = -64.0f;
                        }
                               
                        if (mGebauzLogo[i].y >= (GameConsts.VIRTUAL_SCREEN_HEIGHT / 2.0f))
                        {
                                mGebauzLogo[i].y = GameConsts.VIRTUAL_SCREEN_HEIGHT + 64.0f;
                        }
                        else
                        {
                                mGebauzLogo[i].y = -64.0f;
                        }
                       
                        mGebauzLogo[i].pivotX = 0.0f;
                        mGebauzLogo[i].pivotY = mGebauzLogo[i].h;
                        mGebauzLogo[i].color = GameConsts.PINGK_COLOR.copy();
                }
               
                float totalW = NUM_SPRITES * CHAR_DISTANCE;
                float posX = (GameConsts.VIRTUAL_SCREEN_WIDTH - totalW) / 2.0f;
                                               
                for (int i = 0; i < NUM_SPRITES; i++)
                {
                        mFinalPositions[i] = new Vector2(posX + i * CHAR_DISTANCE, GameConsts.VIRTUAL_SCREEN_HEIGHT / 2.0f);
                }
               
                mGebauzLogo[5].x = mPaddlePositions[5].x;
                mGebauzLogo[5].y = mPaddlePositions[5].y;
               
                mTimer = 0.0f;
               
                mBounce = getGame().getAudio().newManagedSound("data/sounds/paddleimpact.wav");
                mWoosh = getGame().getAudio().newManagedSound("data/sounds/woosh.wav");
        }

        @Override
        public void exit()
        {
                for (int i = 0; i < NUM_SPRITES; i++)
                {
                        if (mGebauzLogo[i] != null)
                        {
                                mGebauzLogo[i].dispose();
                                mGebauzLogo[i] = null;
                        }
                }
               
                getGame().getAudio().removeManagedSound(mBounce);
                mBounce = null;
               
                getGame().getAudio().removeManagedSound(mWoosh);
                mWoosh = null;
        }

        @Override
        public void update(float deltaTime)
        {
                if (Gdx.input.justTouched())
                {
                        switchTo(com.gebauz.pingk.gamestates.TitleState.class);
                }
               
                switch (mCurrentState)
                {
                case STATE_INITIAL:
                        getGame().getAudio().playSound(mWoosh);
                        mCurrentState = STATE_ARRANGE_TO_PADDLE;
                        break;
                case STATE_ARRANGE_TO_PADDLE:
                        updateArrangeToPaddle(deltaTime);
                        break;
                case STATE_BALL_MOVE:
                        updateBallMove(deltaTime);
                        break;
                case STATE_BALL_BOUNCE:
                        updateBallBounce(deltaTime);
                        break;
                case STATE_ARRANGE_TO_LOGO:
                        updateArrangeToLogo(deltaTime);
                        break;
                case STATE_LOGO_WAIT:
                        updateLogoWait(deltaTime);
                        break;
                }
        }
       
        public void updateArrangeToPaddle(float deltaTime)
        {
                boolean canSwitchState = true;
               
                for (int i = 0; i < NUM_SPRITES-1; i++)
                {
                        float diffX = mPaddlePositions[i].x - mGebauzLogo[i].x;
                        mGebauzLogo[i].x += (diffX * deltaTime * LOGO_ACCEL);
                       
                        float diffY = mPaddlePositions[i].y - mGebauzLogo[i].y;
                        mGebauzLogo[i].y += (diffY * deltaTime * LOGO_ACCEL);
                       
                        if (diffX > 1.0f)
                                canSwitchState = false;
                        if (diffY > 1.0f)
                                canSwitchState = false;
                }
               
                if (canSwitchState)
                {
                        mCurrentState = STATE_BALL_MOVE;
                }
        }
       
        public void updateBallMove(float deltaTime)
        {
                if (mGebauzLogo[5].x > mBallTarget.x)
                {
                        mGebauzLogo[5].x -= LOGO_Z_SPEED * deltaTime;
                        if (mGebauzLogo[5].x < mBallTarget.x)
                        {
                                mGebauzLogo[5].x = mBallTarget.x;
                        }
                }
                else
                {
                        getGame().getAudio().playSound(mBounce);
                        mCurrentState = STATE_BALL_BOUNCE;
                }
        }
       
        public void updateBallBounce(float deltaTime)
        {
                if (mGebauzLogo[5].x < mBounceTarget.x)
                {
                        mGebauzLogo[5].x += LOGO_Z_SPEED_BOUNCE * deltaTime;
                        if (mGebauzLogo[5].x > mBounceTarget.x)
                        {
                                mGebauzLogo[5].x = mBounceTarget.x;
                        }
                }
                else
                {
                        getGame().getAudio().playSound(mWoosh);
                        mCurrentState = STATE_ARRANGE_TO_LOGO;
                }
        }

        public void updateArrangeToLogo(float deltaTime)
        {
                boolean canSwitchState = true;
               
                for (int i = 0; i < NUM_SPRITES; i++)
                {
                        float diffX = mFinalPositions[i].x - mGebauzLogo[i].x;
                        mGebauzLogo[i].x += (diffX * deltaTime * LOGO_ACCEL);
                       
                        float diffY = mFinalPositions[i].y - mGebauzLogo[i].y;
                        mGebauzLogo[i].y += (diffY * deltaTime * LOGO_ACCEL);
                       
                        if (diffX > 1.0f)
                                canSwitchState = false;
                        if (diffY > 1.0f)
                                canSwitchState = false;
                }
               
                if (canSwitchState)
                {
                        mCurrentState = STATE_LOGO_WAIT;
                }
        }

        public void updateLogoWait(float deltaTime)
        {
                mTimer += deltaTime;
               
                if (mTimer > GameConsts.LOGO_TIMEOUT)
                {
                        switchTo(com.gebauz.pingk.gamestates.TitleState.class);
                }
        }

        @Override
        public void render()
        {
                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
                        );

                for (int i = 0; i < NUM_SPRITES; i++)
                {
                        mGebauzLogo[i].render();
                }
        }

}