Subversion Repositories AndroidProjects

Rev

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

package com.gebauz.pingk.game;

import java.util.Vector;

import com.gebauz.Bauzoid.app.Game;
import com.gebauz.Bauzoid.app.GameObject;
import com.gebauz.Bauzoid.graphics.Graphics;
import com.gebauz.pingk.entities.Ball;
import com.gebauz.pingk.entities.Entity;
import com.gebauz.pingk.entities.IReflectable;
import com.gebauz.pingk.entities.MenuBar;
import com.gebauz.pingk.entities.Paddle;
import com.gebauz.pingk.entities.PlayField;
import com.gebauz.pingk.entities.PowerUpSystem;

public class GameLogic extends GameObject
{
        private PlayField mPlayField = null;
        private MenuBar mMenuBar = null;
        private Paddle mPaddles[] = new Paddle[GameConsts.NUM_PADDLES];
        private Ball mBall = null;
        private PowerUpSystem mPowerUpSystem = null;
       
        private Vector<Entity> mEntities = new Vector<Entity>();
        private Vector<IReflectable> mReflectables = new Vector<IReflectable>();
       
        public GameLogic(Game game)
        {
                super(game);
        }
       
        public void init()
        {
                mPlayField = (PlayField)addEntity(new PlayField(this));
                mMenuBar = (MenuBar)addEntity(new MenuBar(this));
                mPaddles[Paddle.PLAYER_1] = (Paddle)addEntity(new Paddle(this, Paddle.PLAYER_1));
                mPaddles[Paddle.PLAYER_2]= (Paddle)addEntity(new Paddle(this, Paddle.PLAYER_2));
                mBall = (Ball)addEntity(new Ball(this));
                mPowerUpSystem = (PowerUpSystem)addEntity(new PowerUpSystem(this));
               
                for (int i = 0; i < mEntities.size(); i++)
                {
                        mEntities.get(i).init();
                }
        }
       
        public void exit()
        {
                mReflectables.clear();
                for (int i = mEntities.size() - 1; i >= 0; i--)
                {
                        mEntities.get(i).exit();
                }
                mEntities.clear();
               
                mPlayField = null;
               
                mPaddles[Paddle.PLAYER_1] = null;
                mPaddles[Paddle.PLAYER_2] = null;
        }
       
        public void update(float deltaTime)
        {
                mPlayField.update(deltaTime);
                mMenuBar.update(deltaTime);
       
                mPaddles[Paddle.PLAYER_1].update(deltaTime);
                mPaddles[Paddle.PLAYER_2].update(deltaTime);
               
                mBall.update(deltaTime);
        }
       
        public void render()
        {
                mPlayField.render();
                mMenuBar.render();
               
                mPaddles[Paddle.PLAYER_1].render();
                mPaddles[Paddle.PLAYER_2].render();
               
                mBall.render();
        }
       
        /** Player gains a point, game resets ball. */
        public void playerGainsScore(int playerIndex, int lastPaddleTouched)
        {
/*              int scoreGained = 1;
                if (mCrystalHitCounter.getCurrentHits() > 2)
                {
                        // X points
                        scoreGained = mCrystalHitCounter.getCurrentHits();                     
                }

                mScores[playerIndex] += scoreGained;

                mPlayPhase = PHASE_SCORE;
                if ((lastPaddleTouched == -1) || (lastPaddleTouched == playerIndex))
                {
                        // score normal
                        mScoreDisplay.startDisplay(playerIndex, ScoreDisplay.MODE_SCORE, scoreGained);
                }
                else
                {
                        // own goal
                        mScoreDisplay.startDisplay(playerIndex, ScoreDisplay.MODE_OWNGOAL, scoreGained);
                }
               
                mScreenFlash.startFlash(GameConsts.SCREENFLASH_SCORE_TIME);*/

        }
       
        /** Adds an entity to the list and returns it. */
        public Entity addEntity(Entity entity)
        {
                mEntities.add(entity);
                return entity;
        }
       
        /** Add an entity to the reflectables list. */
        public void addReflectable(IReflectable reflectable)
        {
                mReflectables.add(reflectable);
        }

        /** Get i-th reflectable. */
        public IReflectable getReflectable(int i )
        {
                return mReflectables.get(i);
        }
       
        /** Get number of reflectables. */
        public int getNumReflectables()
        {
                return mReflectables.size();
        }
       
        public final Graphics getGraphics()
        {
                return getGame().getGraphics();
        }
       
        public final PlayField getPlayField() { return mPlayField; }
        public final MenuBar getMenuBar() { return mMenuBar; }
        public final Ball getBall() { return mBall; }
        public final Paddle getPaddle(int playerIndex) { return mPaddles[playerIndex]; }
        public final PowerUpSystem getPowerUpSystem() { return mPowerUpSystem; }
}