Subversion Repositories AndroidProjects

Rev

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

package com.gebauz.darts2go.game;

import com.gebauz.bauzoid.game.Game;
import com.gebauz.bauzoid.game.GameObject;
import com.gebauz.bauzoid.game.entities.EntityManager;
import com.gebauz.darts2go.game.entities.Background;
import com.gebauz.darts2go.game.entities.Board;
import com.gebauz.darts2go.game.entities.Dart;
import com.gebauz.darts2go.game.entities.DartController;
import com.gebauz.darts2go.game.entities.ScoreDisplay;

public class GameLogic extends GameObject
{

        // Constants========================================================================================
       
        public static final float DART_OUTSIDE_THRESHOLD = 200.0f;
       
        public static final float ANIMATION_SPEED_MULTIPLIER = 1.5f;

        // Embedded Types===================================================================================
       
        public enum GamePhase
        {
                INPUT_READY,
                DART_ANIMATION,
                SCORING,
        }

        // Fields===========================================================================================
       
        private EntityManager mEntities = new EntityManager();
       
       
        private Background mBackground = null;
        private Dart mDart = null;
        private DartController mDartController = null;
        private Board mDartBoard = null;
        private ScoreDisplay mScoreDisplay = null;
       
        private GamePhase mGamePhase = GamePhase.INPUT_READY;
        private float mPhaseTimer = 0.0f;
       
        private int mLastScore = 0;
       
        // Methods==========================================================================================

        public GameLogic(Game game)
        {
                super(game);
        }
       
        public void initAsync()
        {
                createEntities();
               
                mEntities.initAsync();
        }
       
        public void init()
        {
                mEntities.init();
               
                resetGame();
        }
       
        public void createEntities()
        {
                mBackground = mEntities.addEntity(new Background(this));               
                mDart = mEntities.addEntity(new Dart(this));
                mDartController = mEntities.addEntity(new DartController(this));
                mDartBoard = mEntities.addEntity(new Board(this));
                mScoreDisplay = mEntities.addEntity(new ScoreDisplay(this));
        }
       
        public void exit()
        {
                mEntities.exit();
        }
       
        public void update(float deltaTime)
        {
                mPhaseTimer += deltaTime;
                switch (mGamePhase)
                {
                case INPUT_READY:
                        updateInputReady(deltaTime);
                        break;
                case DART_ANIMATION:
                        updateDartAnimation(deltaTime);
                        break;
                case SCORING:
                        updateScoring(deltaTime);
                        break;
                }

        }
       
        public void updateInputReady(float deltaTime)
        {
                mBackground.update(deltaTime);
                mDart.update(deltaTime);
                mDartBoard.update(deltaTime);
                mScoreDisplay.update(deltaTime);
               
                mDartController.update(deltaTime);
        }
       
        public void updateDartAnimation(float deltaTime)
        {
                mBackground.update(deltaTime);
                mDart.update(deltaTime * ANIMATION_SPEED_MULTIPLIER);
                mDartBoard.update(deltaTime);
                mDart.update(deltaTime*2.0f);
               
                if (mDart.getPositionY() < -DART_OUTSIDE_THRESHOLD)
                {
                        switchPhase(GamePhase.SCORING);
                }
        }
       
        public void updateScoring(float deltaTime)
        {
                mBackground.update(deltaTime);
                mDart.update(deltaTime);
                mDartBoard.update(deltaTime);
               
                if (mPhaseTimer > 1.0f)
                {
                        resetGame();
                }
        }
       
        public void render()
        {
                mBackground.render();
                mDartBoard.render();
                mScoreDisplay.render();
               
                mDart.render();
        }
       
        /** Switch game phase. */
        public void switchPhase(GamePhase phase)
        {
                mGamePhase = phase;
                mPhaseTimer = 0.0f;
               
                switch (phase)
                {

                case DART_ANIMATION:
                        break;
                case INPUT_READY:
                        break;
                case SCORING:
                        mLastScore = mDartBoard.getHitScore(mDart.getPositionX(), mDart.getPositionY());
                        mScoreDisplay.nextThrow(mLastScore);                                           
                        break;
                default:
                        break;
                }
        }      
       
        /** Reset the game to input ready phase. */
        public void resetGame()
        {
                mGamePhase = GamePhase.INPUT_READY;
                mDart.reset();
        }
       
        // Getters/Setters==================================================================================

        public final Background getBackground() { return mBackground; }
        public final Dart getDart() { return mDart; }
        public final DartController getDartController() { return mDartController; }
        public final Board getDartBoard() { return mDartBoard; }
        public final ScoreDisplay getScoreDisplay() { return mScoreDisplay; }
       
        public final GamePhase getGamePhase() { return mGamePhase; }
       
}