package com.gebauz.pingK.game;
import java.util.Random;
import java.util.Vector;
import android.util.Log;
import com.gebauz.framework.util.GLUtil;
import com.gebauz.pingK.gamestates.GameStateManager;
public class GameLogic
{
public static final int PHASE_GAMEPLAY =
0;
public static final int PHASE_SCORE =
1;
private int mPlayPhase = PHASE_GAMEPLAY
;
private Background mBackground
;
private PlayField mPlayField
;
private MenuBar mMenuBar
;
private Paddle mPaddles
[] =
new Paddle
[2];
private PaddleImpact mPaddleImpact
;
private Ball mBall
;
private BallTrail mBallTrail
;
private ScoreDisplay mScoreDisplay
;
private Font mFont
;
private Random mRandomizer =
new Random();
private int mScores
[] =
new int[2];
private float mVirtualScoreBarHeight =
0;
private float mDipToPixelScale = 1.0f
;
private Vector<IReflectable
> mReflectables =
new Vector<IReflectable
>();
public GameLogic
()
{
mScores
[0] =
0;
mScores
[1] =
0;
}
public void playerGainsScore
(int playerIndex,
int lastPaddleTouched
)
{
mScores
[playerIndex
]++
;
mPlayPhase = PHASE_SCORE
;
if ((lastPaddleTouched == -
1) ||
(lastPaddleTouched == playerIndex
))
{
// score normal
mScoreDisplay.
startDisplay(playerIndex, ScoreDisplay.
MODE_SCORE);
}
else
{
// own goal
mScoreDisplay.
startDisplay(playerIndex, ScoreDisplay.
MODE_OWNGOAL);
}
}
/* public void resetGame()
{
// switch state to same gamestate
mScores[0] = 0;
mScores[1] = 0;
}*/
public void init
()
{
mDipToPixelScale = GameStateManager.
getInstance().
getContext().
getResources().
getDisplayMetrics().
density;
//pixels = (Constants.GRID_BUTTON_SIZE_DIP * scale + 0.5f);
mVirtualScoreBarHeight =
(GameConsts.
SCORE_BAR_HEIGHT * mDipToPixelScale + 0.5f
) * (GameConsts.
VIRTUAL_SCREEN_HEIGHT /
(float)GLUtil.
getInstance().
getHeight());
Log.
v(GameConsts.
LOG_TAG,
"VirtualScoreBarHeight=" + mVirtualScoreBarHeight +
" H=" + GLUtil.
getInstance().
getHeight());
mFont =
new Font();
mScoreDisplay =
new ScoreDisplay
(this);
mBackground =
new Background
(this);
mPlayField =
new PlayField
(this);
mMenuBar =
new MenuBar(this);
mPaddles
[Paddle.
PLAYER_1] =
new Paddle
(this, Paddle.
PLAYER_1);
mPaddles
[Paddle.
PLAYER_2]=
new Paddle
(this, Paddle.
PLAYER_2);
mPaddleImpact =
new PaddleImpact
(this);
mBall =
new Ball
(this);
mBallTrail =
new BallTrail
(mBall
);
mPlayField.
init();
mPaddles
[Paddle.
PLAYER_1].
init();
mPaddles
[Paddle.
PLAYER_2].
init();
mBall.
init();
mPlayPhase = PHASE_GAMEPLAY
;
mScores
[0] =
0;
mScores
[1] =
0;}
public void exit
()
{
mBall.
exit();
mPaddles
[Paddle.
PLAYER_1].
exit();
mPaddles
[Paddle.
PLAYER_2].
exit();
mPlayField.
exit();
mMenuBar =
null;
mBackground =
null;
mPlayField =
null;
mPaddles
[Paddle.
PLAYER_1] =
null;
mPaddles
[Paddle.
PLAYER_2] =
null;
mPaddleImpact =
null;
mBall =
null;
mBallTrail =
null;
mFont =
null;
}
public void update
(float deltaTime
)
{
mBackground.
update(deltaTime
);
mPlayField.
update(deltaTime
);
mMenuBar.
update(deltaTime
);
mPaddles
[Paddle.
PLAYER_1].
update(deltaTime
);
mPaddles
[Paddle.
PLAYER_2].
update(deltaTime
);
mPaddleImpact.
update(deltaTime
);
if (mPlayPhase == PHASE_GAMEPLAY
)
{
mBall.
update(deltaTime
);
mBallTrail.
update(deltaTime
);
}
else
{
mBallTrail.
updateFade(deltaTime
);
}
mScoreDisplay.
update(deltaTime
);
if ((mPlayPhase == PHASE_SCORE
) && (mScoreDisplay.
isDone()))
{
mPlayPhase = PHASE_GAMEPLAY
;
mBall.
reset((mScoreDisplay.
getLastPlayerScored() ==
0) ? 1 :
0);
}
mFont.
update(deltaTime
);
}
public void render
()
{
mBackground.
render();
mPaddles
[Paddle.
PLAYER_1].
render();
mPaddles
[Paddle.
PLAYER_2].
render();
mPaddleImpact.
render();
mBallTrail.
render();
if (mPlayPhase == PHASE_GAMEPLAY
)
{
mBall.
render();
}
mPlayField.
render();
mScoreDisplay.
render();
mMenuBar.
render();
mFont.
render();
// render scores
renderScores
();
}
public void renderScores
()
{
float scale = 2.0f
* getDipToPixelScale
();
String score1 =
String.
format("%02d", mScores
[0]);
mFont.
drawText(score1, GameConsts.
SCOREBAR_MARGIN_X, getVirtualPlayFieldHeight
() + GameConsts.
SCOREBAR_MARGIN_Y, scale
);
String score2 =
String.
format("%02d", mScores
[1]);
mFont.
drawText(score2, GameConsts.
VIRTUAL_SCREEN_WIDTH - GameConsts.
SCOREBAR_MARGIN_X -
(score2.
length() * Font.
CHARACTER_SIZE * scale
), getVirtualPlayFieldHeight
() + GameConsts.
SCOREBAR_MARGIN_Y, scale
);
}
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 Background getBackground
() { return mBackground
; }
public PlayField getPlayField
() { return mPlayField
; }
public Paddle getPaddle
(int index
) { return mPaddles
[index
]; }
public PaddleImpact getPaddleImpact
() { return mPaddleImpact
; }
public Ball getBall
() { return mBall
; }
public BallTrail getBallTrail
() { return mBallTrail
; }
public Random getRandomizer
() { return mRandomizer
; }
public float getVirtualScoreBarHeight
() { return mVirtualScoreBarHeight
; }
public float getVirtualPlayFieldHeight
() { return (GameConsts.
VIRTUAL_SCREEN_HEIGHT - mVirtualScoreBarHeight
); }
public float getDipToPixelScale
() { return mDipToPixelScale
; }
public float dipToVirtual
(float dip
)
{
return (dip
* mDipToPixelScale + 0.5f
) * (GameConsts.
VIRTUAL_SCREEN_HEIGHT /
(float)GLUtil.
getInstance().
getHeight());
}
public float getRandomFloat
(float min,
float max
)
{
return min + mRandomizer.
nextFloat() * (max-min
);
}
}