Subversion Repositories AndroidProjects

Rev

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

package com.gebauz.pingK.game;

import java.util.Vector;

import com.gebauz.framework.util.GLUtil;

public class GameLogic
{
        private PlayField mPlayField;
        private Paddle mPaddles[] = new Paddle[2];
        private Ball mBall;
        private int mScores[] = new int[2];
       
        private float mVirtualScoreBarHeight = 0;
       
        private Vector<IReflectable> mReflectables = new Vector<IReflectable>();
       
        public GameLogic()
        {
               
        }
       
       
        public void startBall()
        {
                mBall.reset(Ball.DIRECTION_LEFT);
        }
       
        public void score(int playerIndex)
        {
                mScores[playerIndex]++;
        }
       
        public void resetGame()
        {
                // switch state to same gamestate
        }      
       
        public void init()
        {
                mVirtualScoreBarHeight = GameConsts.SCORE_BAR_HEIGHT * (GameConsts.VIRTUAL_SCREEN_HEIGHT / (float)GLUtil.getInstance().getHeight());
               
                mPlayField = new PlayField(this);
                mPaddles[Paddle.PLAYER_1] = new Paddle(this, Paddle.PLAYER_1);
                mPaddles[Paddle.PLAYER_2]= new Paddle(this, Paddle.PLAYER_2);
                mBall = new Ball(this);
               
                mPlayField.init();
                mPaddles[Paddle.PLAYER_1].init();
                mPaddles[Paddle.PLAYER_2].init();
                mBall.init();
        }
       
        public void exit()
        {
                mBall.exit();
                mPaddles[Paddle.PLAYER_1].exit();
                mPaddles[Paddle.PLAYER_2].exit();
                mPlayField.exit();

               
                mPlayField = null;
                mPaddles[Paddle.PLAYER_1] = null;
                mPaddles[Paddle.PLAYER_2] = null;
                mBall = null;
        }
       
        public void update(float deltaTime)
        {
                mPlayField.update(deltaTime);
                mPaddles[Paddle.PLAYER_1].update(deltaTime);
                mPaddles[Paddle.PLAYER_2].update(deltaTime);
                mBall.update(deltaTime);
        }
       
        public void render()
        {
                mPaddles[Paddle.PLAYER_1].render();
                mPaddles[Paddle.PLAYER_2].render();
                mBall.render();
                mPlayField.render();
        }
       
        public void addReflectable(IReflectable reflectable)
        {
                mReflectables.add(reflectable);
        }
       
        public void reflect(Ball ball, float deltaTime)
        {
                for (int i = 0; i < mReflectables.size(); i++)
                {
                        IReflectable item = mReflectables.get(i);
                        item.reflect(ball, deltaTime);
                }
        }
       
       
        public PlayField getPlayField() { return mPlayField; }
        public Paddle getPaddle(int index) { return mPaddles[index]; }
        public Ball getBall() { return mBall; }
        public float getVirtualScoreBarHeight() { return mVirtualScoreBarHeight; }
        public float getVirtualPlayFieldHeight() { return (GameConsts.VIRTUAL_SCREEN_HEIGHT - mVirtualScoreBarHeight); }
       
}