Subversion Repositories AndroidProjects

Rev

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

package com.gebauz.pingk.entities;

import java.util.Vector;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Texture;
import com.gebauz.Bauzoid.graphics.sprite.Sprite;
import com.gebauz.Bauzoid.math.RandomChooser;
import com.gebauz.pingk.game.GameConsts;
import com.gebauz.pingk.game.GameLogic;

public class PowerUpSystem extends Entity
{
        public class SpawnParticle
        {
                private float x;
                private float y;
                private float dx;
                private float dy;
                private float s;
                private float growth;
               
                public SpawnParticle(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 float mSpawnTimer = 3.0f;// getNextSpawnTime();
       
        private Vector<PowerUp> mPowerUps = new Vector<PowerUp>();
        private Vector<SpawnParticle> mParticles = new Vector<SpawnParticle>();
       
        private Sprite mBubble = null;
       
        private PowerUpLogic mPowerUpEffect = null;
       
        private RandomChooser mRandomChooser = new RandomChooser();
        private RandomChooser mRandomChooserNoBarriers = new RandomChooser();
       
        private Sound mPickup = null;
        private Sound mPowerUpAppears = null;
       
        public PowerUpSystem(GameLogic gameLogic)
        {
                super(gameLogic);
        }

        @Override
        public void init()
        {
                mPowerUpEffect = new PowerUpLogic(getGameLogic());
                mPowerUpEffect.init();
               
                mBubble = new Sprite(getGraphics(), new Texture(Gdx.files.internal("data/textures/bubble.png")));
                mBubble.color = GameConsts.PINGK_COLOR.copy();
               
                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);
               
                mRandomChooserNoBarriers.addEntry(PowerUp.Type.POWERUP_CONCAVE.ordinal(), GameConsts.POWERUP_PROBABILITY_CONCAVE);
                mRandomChooserNoBarriers.addEntry(PowerUp.Type.POWERUP_MOSAIC.ordinal(), GameConsts.POWERUP_PROBABILITY_MOSAIC);
                mRandomChooserNoBarriers.addEntry(PowerUp.Type.POWERUP_FLOWER.ordinal(), GameConsts.POWERUP_PROBABILITY_FLOWER);
                mRandomChooserNoBarriers.addEntry(PowerUp.Type.POWERUP_HOURGLASS.ordinal(), GameConsts.POWERUP_PROBABILITY_HOURGLASS);
                mRandomChooserNoBarriers.addEntry(PowerUp.Type.POWERUP_CRYSTAL.ordinal(), GameConsts.POWERUP_PROBABILITY_CRYSTAL);
                mRandomChooserNoBarriers.addEntry(PowerUp.Type.POWERUP_BOMB.ordinal(), GameConsts.POWERUP_PROBABILITY_BOMB);
               
                mPickup = getAudio().newManagedSound("data/sounds/pickup.wav");
                mPowerUpAppears = getAudio().newManagedSound("data/sounds/powerup_appears.wav");
        }

        @Override
        public void exit()
        {
                mPowerUpEffect.exit();
               
                for (int i = 0; i < mPowerUps.size(); i++)
                {
                        mPowerUps.get(i).exit();                               
                }
                mPowerUps.clear();
               
                mParticles.clear();
               
                if (mBubble != null)
                {
                        mBubble.dispose();
                        mBubble = null;
                }
               
                getAudio().removeManagedSound(mPickup);
                mPickup = null;
               
                getAudio().removeManagedSound(mPowerUpAppears);
                mPowerUpAppears = null;
        }

        @Override
        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 = getGameLogic().getBall();
                        if (powerUp.isInside(ball.getX(), ball.getY()))
                        {
                                //mGameLogic.playSound(R.raw.pickup);  
                                getAudio().playSound(mPickup);
                                activatePowerUp(powerUp);
                                mPowerUps.get(i).exit();
                                mPowerUps.remove(i);
                                continue;
                        }
                       
                        i++;
                }
               
                updateParticles(deltaTime);
        }
       
        public void updateParticles(float deltaTime)
        {              
                int i = 0;
                while (i < mParticles.size())
                {
                        SpawnParticle particle = mParticles.get(i);
                        particle.update(deltaTime);
                        if (particle.isDone())
                        {
                                mParticles.remove(i);
                                continue;
                        }
                       
                        i++;
                }              
               
                mPowerUpEffect.render();
        }

        @Override
        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 = mRandomChooser.chooseEntry();

                if ((getGameLogic().getBarriers().getNumBarriers() >= GameConsts.POWERUP_BARRIERS_MAX_PARTICLES) &&
                    ((whichType == PowerUp.Type.POWERUP_BARRIER_HORIZONTAL.ordinal()) || (whichType == PowerUp.Type.POWERUP_BARRIER_VERTICAL.ordinal()))
                    )
                {
                        // do not spawn too many barriers
                        whichType = mRandomChooserNoBarriers.chooseEntry();
                       
                        if (whichType == -1)
                                return;
                }
               
                if ((whichType == PowerUp.Type.POWERUP_BOMB.ordinal()) && (getGameLogic().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 = getGameLogic().getGame().getRandomFloat(rangeLeftX, rangeLeftX + GameConsts.POWERUP_RANGE_X);
                float y = getGameLogic().getGame().getRandomFloat(GameConsts.POWERUP_BUBBLE_SIZE*2.0f,
                                getGameLogic().getPlayField().getVirtualHeight() - GameConsts.POWERUP_BUBBLE_SIZE*2.0f);
                               
                PowerUp powerup = new PowerUp(getGameLogic(), PowerUp.Type.values()[whichType], x, y);
                powerup.init();
                mPowerUps.add(powerup);
               
                startParticlesAt(powerup.getX(), powerup.getY());
               
                //mGameLogic.playSound(R.raw.powerup_appears);
                getAudio().playSound(mPowerUpAppears);
               
                //Log.v(GameConsts.LOG_TAG, "SPAWNED");
        }
       
        public float getNextSpawnTime()
        {
                return getGameLogic().getGame().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 = getGameLogic().getGame().getRandomFloat(x - GameConsts.POWERUP_SPARKLE_SPREAD, x + GameConsts.POWERUP_SPARKLE_SPREAD);
                        float posY = getGameLogic().getGame().getRandomFloat(y - GameConsts.POWERUP_SPARKLE_SPREAD, y + GameConsts.POWERUP_SPARKLE_SPREAD);
                       
                        float moveX = getGameLogic().getGame().getRandomFloat(-GameConsts.POWERUP_SPARKLE_MOVEMENT, GameConsts.POWERUP_SPARKLE_MOVEMENT);
                        float moveY = getGameLogic().getGame().getRandomFloat(-GameConsts.POWERUP_SPARKLE_MOVEMENT, GameConsts.POWERUP_SPARKLE_MOVEMENT);
                       
                        float size = getGameLogic().getGame().getRandomFloat(GameConsts.POWERUP_SPARKLE_MIN_SIZE, GameConsts.POWERUP_SPARKLE_MAX_SIZE);
                        float sizeDelta = getGameLogic().getGame().getRandomFloat(GameConsts.POWERUP_SPARKLE_GROWTH_MIN, GameConsts.POWERUP_SPARKLE_GROWTH_MIN);
                       
                        mParticles.add(new SpawnParticle(posX, posY, moveX, moveY, size, sizeDelta));
                }
        }
       
        public void activatePowerUp(PowerUp powerUp)
        {
                switch(powerUp.getType())
                {
                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:
                        getGameLogic().getCrystals().destroyCrystals();
                        break;
                case POWERUP_BARRIER_HORIZONTAL:
                        getGameLogic().getBarriers().spawnBarrier(powerUp.getX(), powerUp.getY(), true);
                        break;
                case POWERUP_BARRIER_VERTICAL:
                        getGameLogic().getBarriers().spawnBarrier(powerUp.getX(), powerUp.getY(), false);                      
                        break;
                }
        }

        public PowerUpLogic getPowerUpLogic()
        {
                return mPowerUpEffect;
        }
}