package com.gebauz.pingK.entities;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import com.gebauz.pingK.util.Vector2D;
import com.gebauz.pingK.GameMain;
import com.gebauz.pingK.R;
public class Paddle
extends BaseEntity
{
public static final float PADDLE_IMPACT_DURATION = 0.5f
;
public static final int PADDLE_TOP =
0;
public static final int PADDLE_BOTTOM =
1;
public static final float PADDLE_INPUT_ACCEL = 15.0f
;
Bitmap mPaddle
;
int mMode
;
Bitmap mPaddleImpact
;
Vector2D position =
new Vector2D
();
Vector2D lastPosition =
new Vector2D
();
float targetX
;
float lastDeltaTime
;
float impactTimer = -
1;
public static final float offset = 120.0f
;
float width = 120.0f
;
public Paddle
(GameMain gameMain,
int mode
) {
super(gameMain
);
mMode = mode
;
}
@
Override
public void init
() {
mPaddle = BitmapFactory.
decodeResource(getResources
(), R.
drawable.
paddle);
mPaddleImpact = BitmapFactory.
decodeResource(getResources
(), R.
drawable.
paddleimpact);
width = mPaddle.
getWidth();
reset
();
}
public void reset
() {
if (mMode == PADDLE_TOP
) {
position.
x = mGameMain.
getWidth() / 2.0f
;
position.
y = offset
;
} else {
position.
x = mGameMain.
getWidth() / 2.0f
;
position.
y = mGameMain.
getHeight() - offset
;
}
targetX = position.
x;
}
@
Override
public void destroy
() {
}
public void handleInput
(float X,
float Y
) {
targetX = X
;
}
public void clearInput
() {
targetX = position.
x;
}
@
Override
public void update
(float deltaTime
) {
lastDeltaTime = deltaTime
;
lastPosition.
x = position.
x;
impactTimer -= deltaTime
;
if (targetX
> position.
x) {
float diff = targetX - position.
x;
position.
x +=
(diff
* deltaTime
* PADDLE_INPUT_ACCEL
);
if (position.
x > targetX
)
position.
x = targetX
;
} else if (targetX
< position.
x) {
float diff = position.
x - targetX
;
position.
x -=
(diff
* deltaTime
* PADDLE_INPUT_ACCEL
);
if (position.
x < targetX
)
position.
x = targetX
;
}
}
@
Override
public void render
(Canvas canvas
) {
Paint paint =
new Paint();
Matrix matrix =
new Matrix
();
matrix.
setTranslate(position.
x - width/
2, position.
y - width/
2);
if (mMode == PADDLE_BOTTOM
) {
Matrix mirror =
new Matrix
();
mirror.
postRotate(180.0f, width/
2, width/
2);
matrix.
reset();
matrix.
postConcat(mirror
);
Matrix trans =
new Matrix
();
trans.
setTranslate(position.
x - width/
2, position.
y - width/
2);
matrix.
postConcat(trans
);
}
canvas.
drawBitmap(mPaddle, matrix, paint
);
//canvas.drawBitmap(mPaddle, null, new Rect((int)positionX, (int)positionY, (int)(positionX+width), (int)(positionY+width)), paint);
if (impactTimer
> 0.0f
) {
paint.
setColor(Color.
argb((int)((float)impactTimer
* 255.0f
),
255,
255,
255));
canvas.
drawBitmap(mPaddleImpact, matrix, paint
);
}
}
public void startImpactEffect
() {
impactTimer = PADDLE_IMPACT_DURATION
;
}
public int getMode
() {
return mMode
;
}
public float getWidth
() {
return width
;
}
public float getMovementSpeed
() {
// timer:=sx.timer.getTimer;
// result:=(settings.pos.y-paddle.lastpos)/timer;
return (position.
x - lastPosition.
x) / lastDeltaTime
;
}
}