Subversion Repositories AndroidProjects

Rev

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

package com.gebauz.pingK.entities;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
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 int PADDLE_TOP = 0;
        public static final int PADDLE_BOTTOM = 1;
       
        public static final float PADDLE_INPUT_ACCEL = 15.0f;
       
        Bitmap mPaddle;
        int mMode;
       
        Vector2D position = new Vector2D();
        Vector2D lastPosition = new Vector2D();
        float targetX;
        float lastDeltaTime;
       
        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);
               
                width = mPaddle.getWidth();
               
                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;
               
                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);
        }
       
        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;
        }
}