Subversion Repositories AndroidProjects

Rev

Blame | Last modification | View Log | RSS feed

package com.gebauz.pingK.game;

import java.util.Vector;

import com.gebauz.framework.util.MathUtil;
import com.gebauz.framework.util.Sprite2D;
import com.gebauz.pingK.R;

public class FlowerEffect
{
        public class Particle
        {      
                private float mSize = GameConsts.POWERUP_FLOWER_PARTICLE_SIZE;
                private float x;
                private float y;
                private float mLifeTime;
               
                public Particle(float posX, float posY)
                {
                        x = posX;
                        y = posY;                      
                        mLifeTime = 0.0f;
                }
               
                public void update(float deltaTime)
                {              
                        mSize += GameConsts.POWERUP_FLOWER_PARTICLE_GROWTH * deltaTime;
                        mLifeTime += deltaTime;
                }
               
                public void render()
                {
                        mFlower.x = x;
                        mFlower.y = y;
                        mFlower.w = mSize;
                        mFlower.h = mSize;
                        mFlower.pivotX = mFlower.w/2.0f;
                        mFlower.pivotY = mFlower.h/2.0f;
                       

                        float alpha = 1.0f;
                        if (mLifeTime > (GameConsts.POWERUP_FLOWER_PARTICLE_LIFETIME - GameConsts.POWERUP_FLOWER_PARTICLE_FADE_OUT))
                                alpha = MathUtil.clamp(GameConsts.POWERUP_FLOWER_PARTICLE_LIFETIME - (mLifeTime / GameConsts.POWERUP_FLOWER_PARTICLE_FADE_OUT), 0.0f, 1.0f);
                        else if (mLifeTime < GameConsts.POWERUP_FLOWER_PARTICLE_FADE_IN)
                                alpha = MathUtil.clamp(mLifeTime/GameConsts.POWERUP_FLOWER_PARTICLE_FADE_IN, 0.0f, 1.0f);
                        mFlower.setColor(GameConsts.PINGK_COLOR.x, GameConsts.PINGK_COLOR.y, GameConsts.PINGK_COLOR.z, alpha);

                        mFlower.render();
                }
               
                public boolean isDone()
                {
                        return (mLifeTime > GameConsts.POWERUP_FLOWER_PARTICLE_LIFETIME);
                }
        }
       
        private GameLogic mGameLogic = null;
        private Sprite2D mFlower = new Sprite2D();
        private float mFlowerTimer = 0.0f;
       
        private Vector<Particle> mParticles = new Vector<Particle>();
       
        public FlowerEffect(GameLogic gameLogic)
        {
                mGameLogic = gameLogic;
               
                mFlower.init(R.drawable.flower);
                mFlower.setColor(GameConsts.PINGK_COLOR);
        }
       
        public void update(float deltaTime)
        {
                if (mGameLogic.getPowerUpEffect().isFlowerActive())
                {
                        // spawn flowers
                        mFlowerTimer -= deltaTime;
                        while (mFlowerTimer < 0.0f)
                        {
                                mFlowerTimer += GameConsts.POWERUP_FLOWER_PARTICLE_INTERVAL;
                               
                                spawnParticle(mGameLogic.getBall().getX(), mGameLogic.getBall().getY());
                        }
                }
               
                int i = 0;
                while (i < mParticles.size())
                {
                        Particle p = mParticles.get(i);
                        p.update(deltaTime);
                       
                        if (p.isDone())
                        {
                                mParticles.remove(i);
                                continue;
                        }
                       
                        i++;
                }
        }
       
        public void render()
        {
                for (int i = 0; i < mParticles.size(); i++)
                {
                        mParticles.get(i).render();
                }
        }
       
        public void spawnParticle(float x, float y)
        {
                if (mParticles.size() >= GameConsts.POWERUP_FLOWER_MAX_PARTICLES)
                        return;
               
                if (mGameLogic.getPlayPhase() == GameLogic.PHASE_SCORE)
                        return;
               
                float posX = x + mGameLogic.getRandomFloat(-GameConsts.POWERUP_FLOWER_PARTICLE_SPREAD, GameConsts.POWERUP_FLOWER_PARTICLE_SPREAD);
                float posY = y + mGameLogic.getRandomFloat(-GameConsts.POWERUP_FLOWER_PARTICLE_SPREAD, GameConsts.POWERUP_FLOWER_PARTICLE_SPREAD);
               
                Particle p = new Particle(posX, posY);
                mParticles.add(p);
        }
}