Subversion Repositories AndroidProjects

Rev

Rev 677 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.gebauz.PonPonChun.game;

import com.gebauz.Bauzoid.gamestates.GameStateManager;
import com.gebauz.Bauzoid.parser.ScanException;
import com.gebauz.Bauzoid.parser.Tokenizer;
import com.gebauz.PonPonChun.game.entities.Block;
import com.gebauz.PonPonChun.game.entities.TimeTrialResultScreen;

public class GameModeLogicTimeTrial extends GameModeLogic
{

        // Constants========================================================================================

        public static final int MAX_SPEED = 10;
        public static final int MAX_ROWS = 10;
        public static final int MIN_SPEED = 1;
        public static final int MIN_ROWS = 0;
        public static final int DEFAULT_SPEED = 1;
        public static final int DEFAULT_ROWS = 3;
       
        public static final int SPEEDUP_THRESHOLD = 1000;
        public static final float SPEEDUP_AMOUNT = 0.05f;
       
       
        // Embedded Types===================================================================================
       
        enum State
        {
                NORMAL,
                WAIT_ACTIVITY,
                RESULT,
        }

        // Members==========================================================================================
       
        private int mLastScore = 0;
        private int mBaseSpeedLevel = 1;
       
        private float mTime = 0;
        private int mMinutes = 0;
        private int mSeconds = 0;
       
        private TimeTrialResultScreen mResultScreen = null;
        private State mCurrentState = State.NORMAL;

        // Methods==========================================================================================

        public GameModeLogicTimeTrial(GameLogic gameLogic)
        {
                super(gameLogic, GameLogic.GameMode.ENDLESS);
               
                mResultScreen = new TimeTrialResultScreen(gameLogic);
        }

        @Override
        public void init(String param)
        {
                mResultScreen.init();
               
                mBlockRandomizer.addEntry(Block.TYPE_BLACK, 20);
                mBlockRandomizer.addEntry(Block.TYPE_WHITE, 20);
                mBlockRandomizer.addEntry(Block.TYPE_RED, 20);
                mBlockRandomizer.addEntry(Block.TYPE_GREEN, 20);
                mBlockRandomizer.addEntry(Block.TYPE_BLUE, 20);
                mBlockRandomizer.addEntry(Block.TYPE_YELLOW, 20);
                mBlockRandomizer.addEntry(Block.TYPE_BOMB, 5);
                mBlockRandomizer.addEntry(Block.TYPE_CRYSTAL, 1);
               
                // interpret param for level/stage/speed
                int speed = 3;
                int rows = 3;
                try
                {
                        Tokenizer t = new Tokenizer(param);
                        speed = (int)t.readNumber();
                        t.readToken(":");
                        rows = (int)t.readNumber();            
                }
                catch (ScanException ex)
                {
                        ex.log(GameConsts.LOG_TAG);
                }
               
                randomizeInitialLevel(rows);
               
                mBaseSpeedLevel = speed;
               
                //getGameLogic().getPlayField().loadPlayField(Gdx.files.internal("data/levels/level02.txt"));
                getGameLogic().getPlayField().setAdvanceSpeed(SPEEDUP_AMOUNT * mBaseSpeedLevel);
                getGameLogic().getHeadsUpDisplay().getLabelB().getTextElement().setCaption(String.format("LV. %02d", mBaseSpeedLevel));
               
                mLastScore = 0;
               
                // time in seconds
                mTime = 2 * 60;
                //mTime = 2;
                updateTimeHud();
        }
       
        @Override
        public void restart()
        {
                GameStateManager gs = getGameLogic().getGameStateManager();
                gs.switchTo(gs.getCurrentState().getClass(), gs.getCurrentParam());
        }
       
        @Override
        public void exit()
        {
                if (mResultScreen != null)
                {
                        mResultScreen.exit();
                        mResultScreen = null;
                }
        }

        @Override
        public void update(float deltaTime)
        {
                if (mCurrentState == State.NORMAL)
                {
                        // speed up at every SPEEDUP_THRESHOLD points
                        int scoreDelta = getGameLogic().getScore() - mLastScore;
                        if (scoreDelta > SPEEDUP_THRESHOLD)
                        {
                                mLastScore += SPEEDUP_THRESHOLD;
                                mBaseSpeedLevel++;
                                getGameLogic().getPlayField().setAdvanceSpeed(SPEEDUP_AMOUNT * mBaseSpeedLevel);
                                getGameLogic().getHeadsUpDisplay().getLabelB().getTextElement().setCaption(String.format("LV. %02d", mBaseSpeedLevel));
                        }
                       
                        updateTimeHud();
                       
                        mTime -= deltaTime;
                        if (mTime <= 0.0f)
                        {
                                mTime = 0.0f;
                                // stop -> disable playfield input and wait for activity
                                getGameLogic().getPlayField().getPlayFieldLogic().setInputEnabled(false);
                                updateTimeHud();
                                mCurrentState = State.WAIT_ACTIVITY;
                        }
                }
                else if (mCurrentState == State.WAIT_ACTIVITY)
                {
                        if (!isStillActivity())
                        {
                                mCurrentState = State.RESULT;
                                mResultScreen.showScreen();
                                getGameLogic().getSounds().playSound(GameSounds.SFX_SUCCESS);
                        }
                }
                else if (mCurrentState == State.RESULT)
                {
                        mResultScreen.update(deltaTime);
                }
        }
       
        public void updateTimeHud()
        {
                int minutes = (int)((mTime+0.999f) / 60.0f);
                int seconds = (int)((mTime+0.999f) % 60.0f);
               
                if ((mMinutes != minutes) || (mSeconds != seconds))
                        getGameLogic().getHeadsUpDisplay().getLabelC().getTextElement().setCaption(String.format("%02d:%02d", minutes, seconds));
               
                mMinutes = minutes;
                mSeconds = seconds;
        }

        @Override
        public void render()
        {
                mResultScreen.render();
               
                //if (getGameLogic().getPlayField().isGameOver())
                        //PonPonChunCustomServices.getInstance().getScoringFont().drawText("GAME OVER", 100, 300, GameConsts.WHITE_COLOR, 4);
        }
       
        // Getters/Setters==================================================================================
       
        public boolean isGameFinished() { return (mCurrentState == State.RESULT); }
       
        public int getDifficulty() { return mBaseSpeedLevel; }
}