Subversion Repositories AndroidProjects

Rev

Rev 93 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.gebauz.pingK.game;

import java.util.Vector;

import android.util.Log;

import com.gebauz.framework.util.MathUtil;
import com.gebauz.framework.util.RandomChooser;
import com.gebauz.framework.util.Sprite2D;
import com.gebauz.pingK.R;
import com.gebauz.pingK.game.PowerUp.Type;

public class PowerUpLogic
{
        public class Particle
        {
                private float x;
                private float y;
                private float dx;
                private float dy;
                private float s;
                private float growth;
               
                public Particle(float posX, float posY, float dirX, float dirY, float size, float sizeDelta)
                {
                        x = posX;
                        y = posY;
                        dx = dirX;
                        dy = dirY;
                        s = size;
                        growth = sizeDelta;
                }
               
                public void update(float deltaTime)
                {
                        x += dx * deltaTime;
                        y += dy * deltaTime;
                       
                        s -= growth * deltaTime;
                }
               
                public void render()
                {
                        if (isDone())
                                return;
                       
                        mBubble.x = x;
                        mBubble.y = y;
                       
                        mBubble.w = s;
                        mBubble.h = s;
                       
                        mBubble.pivotX = mBubble.w/2.0f;
                        mBubble.pivotY = mBubble.h/2.0f;
                       
                        mBubble.render();
                }
               
                public boolean isDone()
                {
                        return (s < 0.0f);
                }
        }
       
        private GameLogic mGameLogic = null;
        private float mSpawnTimer = 3.0f;// getNextSpawnTime();
       
        private Vector<PowerUp> mPowerUps = new Vector<PowerUp>();
        private Vector<Particle> mParticles = new Vector<Particle>();
       
        private Sprite2D mBubble = new Sprite2D();
       
        private PowerUpEffect mPowerUpEffect = null;
       
        private RandomChooser mRandomChooser = new RandomChooser();
       
        public PowerUpLogic(GameLogic gameLogic)
        {
                mGameLogic = gameLogic;
               
                mPowerUpEffect = new PowerUpEffect(mGameLogic);
               
                mBubble.init(R.drawable.bubble);
                mBubble.setColor(GameConsts.PINGK_COLOR);
               
                mRandomChooser.addEntry(PowerUp.Type.POWERUP_CONCAVE.ordinal(), GameConsts.POWERUP_PROBABILITY_CONCAVE);
                mRandomChooser.addEntry(PowerUp.Type.POWERUP_MOSAIC.ordinal(), GameConsts.POWERUP_PROBABILITY_MOSAIC);
                mRandomChooser.addEntry(PowerUp.Type.POWERUP_FLOWER.ordinal(), GameConsts.POWERUP_PROBABILITY_FLOWER);
                mRandomChooser.addEntry(PowerUp.Type.POWERUP_HOURGLASS.ordinal(), GameConsts.POWERUP_PROBABILITY_HOURGLASS);
                mRandomChooser.addEntry(PowerUp.Type.POWERUP_CRYSTAL.ordinal(), GameConsts.POWERUP_PROBABILITY_CRYSTAL);
                mRandomChooser.addEntry(PowerUp.Type.POWERUP_BOMB.ordinal(), GameConsts.POWERUP_PROBABILITY_BOMB);
                mRandomChooser.addEntry(PowerUp.Type.POWERUP_BARRIER_HORIZONTAL.ordinal(), GameConsts.POWERUP_PROBABILITY_BARRIER_HORIZONTAL);
                mRandomChooser.addEntry(PowerUp.Type.POWERUP_BARRIER_VERTICAL.ordinal(), GameConsts.POWERUP_PROBABILITY_BARRIER_VERTICAL);
        }
       
        public void update(float deltaTime)
        {
                mPowerUpEffect.update(deltaTime);
               
                mSpawnTimer -= deltaTime;
               
                if (mSpawnTimer < 0.0f)
                {
                        // spawn
                        spawnPowerUp();                
                        mSpawnTimer = getNextSpawnTime();
                }

                int i = 0;
                while (i < mPowerUps.size())
                {
                        PowerUp powerUp = mPowerUps.get(i);
                        powerUp.update(deltaTime);
                       
                        // check if ball is taking a powerup and activate it
                        Ball ball = mGameLogic.getBall();
                        if (powerUp.isInside(ball.getX(), ball.getY()))
                        {
                                mGameLogic.playSound(R.raw.pickup);                            
                                activatePowerUp(powerUp.getType());                            
                                mPowerUps.remove(i);
                                continue;
                        }
                       
                        i++;
                }
               
                updateParticles(deltaTime);
        }
       
        public void updateParticles(float deltaTime)
        {              
                int i = 0;
                while (i < mParticles.size())
                {
                        Particle particle = mParticles.get(i);
                        particle.update(deltaTime);
                        if (particle.isDone())
                        {
                                mParticles.remove(i);
                                continue;
                        }
                       
                        i++;
                }              
               
                mPowerUpEffect.render();
        }
       
        public void render()
        {
                for (int i = 0; i < mPowerUps.size(); i++)
                {
                        mPowerUps.get(i).render();                     
                }
               
                for (int i = 0; i < mParticles.size(); i++)
                {
                        mParticles.get(i).render();
                }
        }
       
        public void spawnPowerUp()
        {
                if (mPowerUps.size() >= GameConsts.POWERUP_MAX_SPAWNED)
                        return;
               
                //int whichType = mGameLogic.getRandomizer().nextInt(PowerUp.Type.values().length);
                int whichType = mRandomChooser.chooseEntry();
               
                if ((whichType == PowerUp.Type.POWERUP_BOMB.ordinal()) && (mGameLogic.getCrystals().getNumCrystals() == 0))
                {
                        // don't spawn a bomb when no crystals left
                        whichType = PowerUp.Type.POWERUP_CRYSTAL.ordinal();
                }
               
                float rangeLeftX = (GameConsts.VIRTUAL_SCREEN_WIDTH - GameConsts.POWERUP_RANGE_X) / 2.0f;
                float x = mGameLogic.getRandomFloat(rangeLeftX, rangeLeftX + GameConsts.POWERUP_RANGE_X);
                float y = mGameLogic.getRandomFloat(GameConsts.POWERUP_BUBBLE_SIZE*2.0f, mGameLogic.getVirtualPlayFieldHeight() - GameConsts.POWERUP_BUBBLE_SIZE*2.0f);
                               
                PowerUp powerup = new PowerUp(mGameLogic, PowerUp.Type.values()[whichType], x, y);
                mPowerUps.add(powerup);
               
                startParticlesAt(powerup.getX(), powerup.getY());
               
                mGameLogic.playSound(R.raw.powerup_appears);
               
                //Log.v(GameConsts.LOG_TAG, "SPAWNED");
        }
       
        public float getNextSpawnTime()
        {
                return mGameLogic.getRandomFloat(GameConsts.POWERUP_SPAWN_TIME_MINIMUM, GameConsts.POWERUP_SPAWN_TIME_MAXIMUM);
        }
       
        public void startParticlesAt(float x, float y)
        {
                for (int i = 0; i < GameConsts.POWERUP_SPARKLE_MAX_BUBBLES; i++)
                {
                        float posX = mGameLogic.getRandomFloat(x - GameConsts.POWERUP_SPARKLE_SPREAD, x + GameConsts.POWERUP_SPARKLE_SPREAD);
                        float posY = mGameLogic.getRandomFloat(y - GameConsts.POWERUP_SPARKLE_SPREAD, y + GameConsts.POWERUP_SPARKLE_SPREAD);
                       
                        float moveX = mGameLogic.getRandomFloat(-GameConsts.POWERUP_SPARKLE_MOVEMENT, GameConsts.POWERUP_SPARKLE_MOVEMENT);
                        float moveY = mGameLogic.getRandomFloat(-GameConsts.POWERUP_SPARKLE_MOVEMENT, GameConsts.POWERUP_SPARKLE_MOVEMENT);
                       
                        float size = mGameLogic.getRandomFloat(GameConsts.POWERUP_SPARKLE_MIN_SIZE, GameConsts.POWERUP_SPARKLE_MAX_SIZE);
                        float sizeDelta = mGameLogic.getRandomFloat(GameConsts.POWERUP_SPARKLE_GROWTH_MIN, GameConsts.POWERUP_SPARKLE_GROWTH_MIN);
                       
                        mParticles.add(new Particle(posX, posY, moveX, moveY, size, sizeDelta));
                }
        }
       
        public void activatePowerUp(PowerUp.Type type)
        {
                switch(type)
                {
                case POWERUP_CONCAVE:
                        mPowerUpEffect.activateConcave();
                        break;
                case POWERUP_MOSAIC:
                        mPowerUpEffect.activateMosaic();
                        break;
                case POWERUP_FLOWER:
                        mPowerUpEffect.activateFlower();
                        break;
                case POWERUP_HOURGLASS:
                        mPowerUpEffect.activateSlowMo();
                        break;
                case POWERUP_CRYSTAL:
                        mPowerUpEffect.activateCrystal();
                        break;
                case POWERUP_BOMB:
                        mGameLogic.getCrystals().destroyCrystals();
                        break;
                case POWERUP_BARRIER_HORIZONTAL:
                        mPowerUpEffect.activateBarrier(true);
                        break;
                case POWERUP_BARRIER_VERTICAL:
                        mPowerUpEffect.activateBarrier(false);
                        break;
                }
        }

        public PowerUpEffect getPowerUpEffect()
        {
                return mPowerUpEffect;
        }
       
}