package com.gebauz.pingK;
import com.gebauz.pingK.entities.Ball;
import com.gebauz.pingK.entities.Paddle;
import com.gebauz.pingK.entities.PlayField;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
public class GameMain
{
public static final float SCORED_COUNT_DOWN = 2.0f
;
Resources mResources
;
TextRender mTextRender
;
ScoreDisplay mScoreDisplay
;
PlayField mPlayField =
null;
Ball mBall =
null;
Paddle mPaddles
[] =
new Paddle
[2];
int mWidth =
0;
int mHeight =
0;
int lastPlayerScored = -
1;
float scoredCountdown = 0.0f
;
boolean wasOwnGoal =
false;
int mScores
[] =
new int[2];
float mVolumeControlStripSize = 32.0f
;
Sounds mSounds
;
int soundIds
[] =
{
R.
raw.
paddleimpact,
R.
raw.
wallimpact,
R.
raw.
score,
R.
raw.
newball
};
public void init
(Context context, Resources resources,
int w,
int h
) {
mResources = resources
;
mWidth = w
;
mHeight = h
;
mTextRender =
new TextRender
(resources
);
mScoreDisplay =
new ScoreDisplay
(resources
);
mSounds =
new Sounds
(soundIds
);
mSounds.
init(context
);
mPlayField =
new PlayField
(this);
mBall =
new Ball
(this);
mPaddles
[Paddle.
PADDLE_TOP] =
new Paddle
(this, Paddle.
PADDLE_TOP);
mPaddles
[Paddle.
PADDLE_BOTTOM] =
new Paddle
(this, Paddle.
PADDLE_BOTTOM);
mPlayField.
init();
mBall.
init();
mPaddles
[Paddle.
PADDLE_TOP].
init();
mPaddles
[Paddle.
PADDLE_BOTTOM].
init();
lastPlayerScored =
3;
scoredCountdown = -1.0f
;
}
public void destroy
() {
mPlayField.
destroy();
mBall.
destroy();
mPaddles
[Paddle.
PADDLE_TOP].
destroy();
mPaddles
[Paddle.
PADDLE_BOTTOM].
destroy();
mSounds.
destroy();
}
public void handleInput
(GameView.
TouchPoint touchPoints
[]) {
// find first touch point in upper half
for (int i =
0; i
< touchPoints.
length; i++
) {
if (touchPoints
[i
].
isActive && (touchPoints
[i
].
Y < ((mHeight - mVolumeControlStripSize
) /
2))) {
mPaddles
[Paddle.
PADDLE_TOP].
handleInput(touchPoints
[i
].
X, touchPoints
[i
].
Y);
break;
}
// reaching last -> no input
if (i == touchPoints.
length-
1)
mPaddles
[Paddle.
PADDLE_TOP].
clearInput();
}
// find first touch point in lower half
for (int i =
0; i
< touchPoints.
length; i++
) {
if (touchPoints
[i
].
isActive && (touchPoints
[i
].
Y > ((mHeight + mVolumeControlStripSize
) /
2))) {
mPaddles
[Paddle.
PADDLE_BOTTOM].
handleInput(touchPoints
[i
].
X, touchPoints
[i
].
Y);
break;
}
// reaching last -> no input
if (i == touchPoints.
length-
1)
mPaddles
[Paddle.
PADDLE_BOTTOM].
clearInput();
}
// find first touch point in volume control strip
for (int i =
0; i
< touchPoints.
length; i++
) {
if (touchPoints
[i
].
isActive &&
(touchPoints
[i
].
Y < ((mHeight + mVolumeControlStripSize
) /
2)) &&
(touchPoints
[i
].
Y > ((mHeight - mVolumeControlStripSize
) /
2))) {
// switch sound on/off
// TODO:
break;
}
}
}
public void update
(float deltaTime
) {
mPlayField.
update(deltaTime
);
mPaddles
[Paddle.
PADDLE_TOP].
update(deltaTime
);
mPaddles
[Paddle.
PADDLE_BOTTOM].
update(deltaTime
);
if (lastPlayerScored
!= -
1) {
// play scored text!
mScoreDisplay.
update(deltaTime
);
scoredCountdown -= deltaTime
;
if (scoredCountdown
< 0.0f
) {
mBall.
reset(lastPlayerScored+
1);
lastPlayerScored = -
1;
scoredCountdown = 0.0f
;
wasOwnGoal =
false;
playSound
(R.
raw.
newball);
}
return;
}
// only update ball if not displaying "scored" text
mBall.
update(deltaTime
);
}
public void playerGainsScore
(int scoringPlayer,
int lastPaddleTouched
) {
if (scoringPlayer
!= lastPaddleTouched
)
{
// own goal!
wasOwnGoal =
true;
}
lastPlayerScored = scoringPlayer
;
scoredCountdown = SCORED_COUNT_DOWN
;
mScores
[lastPlayerScored
]++
;
playSound
(R.
raw.
score);
// start animation for winning
mScoreDisplay.
startAnimation(wasOwnGoal, SCORED_COUNT_DOWN, scoringPlayer
);
}
public void render
(Canvas canvas
) {
mBall.
render(canvas
);
mPaddles
[Paddle.
PADDLE_TOP].
render(canvas
);
mPaddles
[Paddle.
PADDLE_BOTTOM].
render(canvas
);
// play field clips the rest
mPlayField.
render(canvas
);
if (lastPlayerScored
!= -
1) {
/* Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.RED);
paint.setTextSize(30.0f);
if (wasOwnGoal)
canvas.drawText("OWN GOAL!", (canvas.getWidth() - 200.0f) / 2.0f, (canvas.getHeight() - 20.0f) / 2.0f, paint);
else
canvas.drawText("SCORE!", (canvas.getWidth() - 200.0f) / 2.0f, (canvas.getHeight() - 20.0f) / 2.0f, paint);*/
mScoreDisplay.
render(canvas
);
}
mTextRender.
setColor(Color.
argb(255,
255,
0,
153));
mTextRender.
setRotation(180.0f
);
mTextRender.
drawText(canvas,
Integer.
toString(mScores
[Paddle.
PADDLE_TOP]), getWidth
() - 10.0f, 40.0f, 2.0f
);
mTextRender.
setRotation(0.0f
);
mTextRender.
drawText(canvas,
Integer.
toString(mScores
[Paddle.
PADDLE_BOTTOM]),
10, getHeight
() - 40.0f, 2.0f
);
}
public void setDimensions
(int w,
int h
) {
mWidth = w
;
mHeight = h
;
}
public Resources getResources
() {
return mResources
;
}
public float getWidth
() {
return mWidth
;
}
public float getHeight
() {
return mHeight
;
}
public Paddle
[] getPaddles
() {
return mPaddles
;
}
public TextRender getTextRender
() {
return mTextRender
;
}
public void playSound
(int id
) {
mSounds.
playSound(id
);
}
}