Subversion Repositories AndroidProjects

Rev

Blame | 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.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.Rect;

import com.gebauz.pingK.GameMain;
import com.gebauz.pingK.PowerUps;
import com.gebauz.pingK.util.Vector2D;
import com.gebauz.pingK.R;

public class PowerUp extends BaseEntity {
       
        int mType = 0;
        Vector2D mPosition = new Vector2D();
        float mLiveTime = 0;
       
        public PowerUp(GameMain gameMain) {
                super(gameMain);
        }
       
        public static PowerUp spawnPowerUp(GameMain gameMain) {
                PowerUp newPowerUp = new PowerUp(gameMain);
                newPowerUp.mType = (int)(Math.random() * (double)PowerUps.POWERUP_COUNT);
                newPowerUp.mPosition.x = 20.0f + (float)Math.random() * (gameMain.getWidth() - 20.0f*2);
                newPowerUp.mPosition.y = 150.0f + (float)Math.random() * (gameMain.getHeight() - 150.0f*2);
                return newPowerUp;
        }

        @Override
        public void init() {
               
        }
       
        @Override
        public void destroy() {
               
        }
       
        @Override
        public void update(float deltaTime) {
                if ((mLiveTime < 0.5f) && ((mLiveTime + deltaTime) >= 0.5f))
                        mGameMain.playSound(R.raw.powerup_appears);

                mLiveTime += deltaTime;
        }
       
        @Override
        public void render(Canvas canvas) {
                Bitmap powerUpIcon = mGameMain.getPowerUps().getPowerUpIcon(mType);
                if (powerUpIcon == null)
                        return;

                int alpha = 255;
                if (mLiveTime < 0.5f)
                        alpha = (int)(mLiveTime * 2.0f * 255.0f);
                Paint paint = new Paint();
                int color = Color.argb(alpha, 255, 0, 153);
                paint.setColor(color);
                paint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.MULTIPLY));

                Matrix matrix = new Matrix();
                matrix.setTranslate(mPosition.x - powerUpIcon.getWidth()/2, mPosition.y - powerUpIcon.getHeight()/2);
               
                canvas.drawBitmap(powerUpIcon, matrix, paint);
               
                Bitmap bubble = mGameMain.getPowerUps().getBubbleIcon();
                // bubble wobble
                matrix.preScale(1.05f + 0.05f * (float)Math.cos(mLiveTime*5.0f), 1.05f + 0.05f * (float)Math.sin(mLiveTime*5.0f), bubble.getWidth()/2, bubble.getHeight()/2);
               
                canvas.drawBitmap(bubble, matrix, paint);
        }
       
        public Vector2D getPosition() {
                return mPosition;
        }
       
        public int getType() {
                return mType;
        }
}