Subversion Repositories AndroidProjects

Rev

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

package com.gebauz.PonPonChun.game;

import com.gebauz.Bauzoid.game.Game;
import com.gebauz.Bauzoid.game.GameObject;
import com.gebauz.Bauzoid.math.MathUtil;
import com.gebauz.PonPonChun.game.entities.Background;
import com.gebauz.PonPonChun.game.entities.Block;
import com.gebauz.PonPonChun.game.entities.EntityManager;
import com.gebauz.PonPonChun.game.entities.FruitParticles;
import com.gebauz.PonPonChun.game.entities.GameOverScreen;
import com.gebauz.PonPonChun.game.entities.HeadsUpDisplay;
import com.gebauz.PonPonChun.game.entities.Mascot;
import com.gebauz.PonPonChun.game.entities.PlayField;
import com.gebauz.PonPonChun.game.entities.SpeedArrow;
import com.gebauz.PonPonChun.game.entities.SuccessScreen;
import com.gebauz.PonPonChun.game.entities.SuperPower;

/** Scoring
 * The scoring details of combos are as follows:
 * Combo:
 * 4 tiles: 20 points
 * 5 tiles: 30 points
 * 6 tiles: 50 points
 * 7 tiles: 60 points
 * 8 tiles: 70 points
 * 9 tiles: 80 points
 * 10 tiles: 100 points
 * 11 tiles: 140 points
 * 12 tiles: 170 points
 *
 * Chain:
 * x2 chain: 50 points
 * x3 chain: 80 points
 * x4 chain: 150 points
 * x5 chain: 300 points
 * x6 chain: 400 points
 * x7 chain: 500 points
 * x8 chain: 700 points
 * x9 chain: 900 points
 * x10 chain: 1100 points
 * x11 chain: 1300 points
 * x12 chain: 1500 points
 * x13 chain: 1800 points
 * x14 chain: 0 points
 * @author chris
 *
 */

public class GameLogic extends GameObject
{
       
       
        // Constants========================================================================================
       
        public static int BLOCK_CLEAR_SCORE = 10;
        public static int BLOCK_COMBO_SCORE[] =
                {
                        20,             // 4 block combo
                        30,             // 5 block combo
                        50,             // 6 block combo
                        60,             // 7 block combo
                        70,             // 8 block combo
                        80,             // 9 block combo
                        100,    // 10 block combo
                        140,    // 11 block combo
                        170             // 12 block combo
                };
        public static int BLOCK_CHAIN_SCORE[] =
                {
                        50,             // 2x chain
                        80,             // 3x chain
                        150,    // 4x chain
                        300,    // 5x chain
                        400,    // 6x chain
                        500,    // 7x chain
                        700,    // 8x chain
                        900,    // 9x chain
                        1100,   // 10x chain
                        1300,   // 11x chain
                        1500,   // 12x chain
                        1800,   // 13x chain
                };
       
        public static int CLEAR_BONUS_SCORE = 250;

        public static int APPLE_SCORE = 10;
        public static int CHERRY_SCORE = 20;
        public static int BANANA_SCORE = 30;
        public static int GRAPES_SCORE = 50;
       
        public static int APPLE_POWER_VALUE = 5;
        public static int CHERRY_POWER_VALUE = 10;
        public static int BANANA_POWER_VALUE = 15;
        public static int GRAPES_POWER_VALUE = 25;
       
        public static float TIME_FREEZE_AMOUNT = 3.0f;

        public static final int MAX_FRUITS = 6;
        public static final int MAX_CRYSTALS = 1;
        public static final int MAX_CRYSTALIZED_BLOCKS = 2;
        public static final int MAX_JOKERS = 3;
       
        public static final int MAX_SUPERPOWER = 100;
       
        // Embedded Types===================================================================================

        public enum GameMode
        {
                ENDLESS,
                PUZZLE,
                TIMETRIAL,
                TUTORIAL,
        }
       
        // Members==========================================================================================
       
        private EntityManager mEntityManager = new EntityManager();
       
        private Background mBackground = null;
        private PlayField mPlayField = null;
        private HeadsUpDisplay mHeadsUpDisplay = null;
        private SpeedArrow mSpeedArrow = null;
        private GameOverScreen mGameOverScreen = null;
        private SuccessScreen mSuccessScreen = null;
        private FruitParticles mFruits = null;
        private GameSounds mSounds = null;
        private Mascot mMascot = null;
        private SuperPower mSuperPower = null;
       
        private GameMode mGameMode = GameMode.ENDLESS;
        private GameModeLogic mGameModeLogic = null;
       
        private boolean mPaused = false;
       
        private int mScore = 0;
        private int mSuperPowerValue = 0;
        private float mSuperPowerValueAnimated = 0;

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

        public GameLogic(Game game)
        {
                super(game);
                mSounds = new GameSounds(this);
        }
       
        public void init(GameMode mode, String param)
        {
                mGameMode = mode;
                mSounds.init();
               
                mPlayField = mEntityManager.addEntity(new PlayField(this));
                mHeadsUpDisplay = mEntityManager.addEntity(new HeadsUpDisplay(this));
                mBackground = mEntityManager.addEntity(new Background(this));
                mSpeedArrow = mEntityManager.addEntity(new SpeedArrow(this));
                mGameOverScreen = mEntityManager.addEntity(new GameOverScreen(this));
                mSuccessScreen = mEntityManager.addEntity(new SuccessScreen(this));
                mFruits = mEntityManager.addEntity(new FruitParticles(this));
                mMascot = mEntityManager.addEntity(new Mascot(this));
                mSuperPower = mEntityManager.addEntity(new SuperPower(this));
               
                // initialize game mode
                switch (mode)
                {
                case ENDLESS:
                        mGameModeLogic = new GameModeLogicEndless(this);
                        break;
                case PUZZLE:
                        mGameModeLogic = new GameModeLogicPuzzle(this);
                        break;
                case TIMETRIAL:
                        mGameModeLogic = new GameModeLogicTimeTrial(this);
                        break;
                case TUTORIAL:
                        mGameModeLogic = new GameModeLogicTutorial(this);
                        break;                 
                }
               
                mEntityManager.init();
                mGameModeLogic.init(param);
        }
       
        public void exit()
        {
                mGameModeLogic.exit();         
                mEntityManager.exit();
               
                mSounds.exit();        
        }
       
        /** Update the Game Logic.
         * NOTE: The EntityManager does not update entities because that is up to the game
         * implementation (especially if the game implementation requires a certain order).
         */

        public void update(float deltaTime)
        {
                float diff = (float)mSuperPowerValue - (float)mSuperPowerValueAnimated;
                mSuperPowerValueAnimated += (diff*deltaTime*10.0f);
               
                if (!mPlayField.isGameOver())
                {
                        if (!mGameModeLogic.isGameFinished())
                        {
                                if (!mGameModeLogic.isGameStopped())
                                        mPlayField.update(deltaTime);

                                mHeadsUpDisplay.update(deltaTime);
                                mBackground.update(deltaTime);
                                mSpeedArrow.update(deltaTime);
                                mMascot.update(deltaTime);
                                mFruits.update(deltaTime);
                                mSuperPower.update(deltaTime);
                        }
                }
                else
                {
                        mGameOverScreen.update(deltaTime);
                }
                mGameModeLogic.update(deltaTime);
        }
       
        /** Render the Game Logic.
         * NOTE: The EntityManager does not render entities because that is up to the game implementation
         * (especially if the game implementation requires a certain order).
         */

        public void render()
        {
               
                //getGraphics().renderStates.activate();
                //getGraphics().renderStates.scissor.setScissor(0, 0, 480, 400);
                //getGraphics().renderStates.scissor.setEnabled(true);
               
                mBackground.render();

                if (!mPlayField.isGameOver())
                {
                        mSpeedArrow.render();
                }
               
                //getGraphics().renderStates.pushViewMatrix();
                //getGraphics().renderStates.view.setTranslation(PlayField.PLAYFIELD_LEFT, PlayField.PLAYFIELD_TOP, 0.0f);
                mPlayField.render();
                //getGraphics().renderStates.popViewMatrix();
               
               
                mHeadsUpDisplay.render();
                mMascot.render();
                mFruits.render();
                mHeadsUpDisplay.renderOnTop();
                mSuperPower.render();
               
                mGameModeLogic.render();
               
                if (mPlayField.isGameOver())
                {
                        mGameOverScreen.render();
                }
               
                //getGraphics().renderStates.deactivate();
        }
       
        /** Restart current game. */
        public void restart()
        {
                mGameModeLogic.restart();
                mScore = 0;
                mSuperPowerValue = 0;
        }
       
        /** Mark game over. */
        public void gameOver()
        {
                getSounds().playSound(GameSounds.SFX_GAMEOVER);
               
                // start game over animation
                mGameOverScreen.startGameOverScreen();
                mMascot.sad();
        }
       
        public static int getFruitScore(int fruitType)
        {
                switch(fruitType)
                {
                case Block.TYPE_CHERRY:
                        return CHERRY_SCORE;
                case Block.TYPE_BANANA:
                        return BANANA_SCORE;
                case Block.TYPE_APPLE:
                        return APPLE_SCORE;
                case Block.TYPE_GRAPES:
                        return GRAPES_SCORE;
                }
                return 0;
        }
       
        // Getters/Setters==================================================================================
       
        public final GameMode getGameMode()
        {
                return mGameMode;
        }
       
        public final GameModeLogic getGameModeLogic()
        {
                return mGameModeLogic;
        }
       
        public boolean isPaused()
        {
                return mPaused;
        }
       
        public void setPaused(boolean paused)
        {
                mPaused = paused;
                mHeadsUpDisplay.doPause(paused);
        }
       
        public void setScore(int score) { mScore = score; }
        public void addScore(int score) { mScore += score; }
        public int getScore() { return mScore; }
       
        public void setSuperPower(int superpower) {     mSuperPowerValue = MathUtil.clamp(superpower, 0, MAX_SUPERPOWER); }
        public void addSuperPower(int superpower) { setSuperPower(mSuperPowerValue+superpower); }
        public int getSuperPowerValue() { return mSuperPowerValue; }
        public float getSuperPowerValueAnimated() { return mSuperPowerValueAnimated; } 
       
        public GameSounds getSounds() { return mSounds; }

        // Entity getters
        public final PlayField getPlayField() { return mPlayField; }
        public final HeadsUpDisplay getHeadsUpDisplay() { return mHeadsUpDisplay; }
        public final Background getBackground() { return mBackground; }
        public final SpeedArrow getSpeedArrow() { return mSpeedArrow; }
        public final GameOverScreen getGameOverScreen() { return mGameOverScreen; }
        public final SuccessScreen getSuccessScreen() { return mSuccessScreen; }
        public final FruitParticles getFruitParticles() { return mFruits; }
        public final Mascot getMascot() { return mMascot; }
        public final SuperPower getSuperPower() { return mSuperPower; }
}