Subversion Repositories AndroidProjects

Rev

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

package com.gebauz.pingK.common.game;

import java.util.Vector;

import com.gebauz.pingK.common.R;
import com.gebauz.pingK.common.framework.MathUtil;
import com.gebauz.pingK.common.framework.Sprite2D;
import com.gebauz.pingK.common.framework.Vector2;
import com.gebauz.pingK.common.game.Crystals.Particle;

public class Barriers
{
        public class Particle
        {
                private float x;
                private float y;
                private boolean mIsHorizontal = false;
               
                private float mLifeTime = 0.0f;
                private int mLives = 3;
                private float mFlickerTimer = -1.0f;
               
                public Particle(float _x, float _y, boolean _horizontal)
                {
                        mIsHorizontal = _horizontal;
                        x = _x;
                        y = _y;
                }
               
                public void update(float deltaTime)
                {
                        mLifeTime += deltaTime;
                        if (mFlickerTimer > 0.0f)
                                mFlickerTimer -= deltaTime;
                       
                        Ball ball = mGameLogic.getBall();
                       
                        float growth = MathUtil.ease(MathUtil.clamp(mLifeTime / GameConsts.POWERUP_BARRIERS_GROWTH_TIME, 0.0f, 1.0f));
                        float length = GameConsts.POWERUP_BARRIERS_LENGTH * growth;
                       
                        Vector2 v = new Vector2(ball.getX() - ball.getLastX(), ball.getY() - ball.getLastY());
               
                        if (mIsHorizontal)
                        {
                                if (v.y != 0)
                                {
                                        float t = (y - ball.getLastY()) / v.y;
                                        float sx = ball.getLastX() + t * v.x;
                               
                                        if ((sx >= (x - length/2.0f)) && (sx <= (x + length/2.0f)))
                                        {
                                                if ((ball.getLastY() < y) && (ball.getY() >= y))
                                                {
                                                        // ball going down - reflect up
                                                        collideWithBall(sx, y, 180.0f);
                                                       
                                                        if (!isAlive())
                                                        {
                                                                createHorizontalShards(x, y, sx, length, GameServices.getRandomFloat(30.0f, 50.0f));
                                                        }
                                                }
                                                else if ((ball.getLastY() > y) && (ball.getY() <= y))
                                                {
                                                        // ball going up - reflect down
                                                        collideWithBall(sx, y, 0.0f);
                                                       
                                                        if (!isAlive())
                                                        {
                                                                createHorizontalShards(x, y, sx, length, -GameServices.getRandomFloat(30.0f, 50.0f));
                                                        }
                                                }
                                        }
                                }
                        }
                        else
                        {
                                if (v.x != 0)
                                {
                                        float t = (x - ball.getLastX()) / v.x;
                                        float sy = ball.getLastY() + t * v.y;
                                       
                                        if ((sy >= (y - length/2.0f)) && (sy <= (y + length/2.0f)))
                                        {
                                                if ((ball.getLastX() < x) && (ball.getX() >= x))
                                                {
                                                        // ball going right - reflect left
                                                        collideWithBall(x, sy, 270.0f);
                                                       
                                                        if (!isAlive())
                                                        {
                                                                createVerticalShards(x, y, sy, length, GameServices.getRandomFloat(30.0f, 50.0f));
                                                        }
                                                       
                                                        //mShards
                                                }
                                                else if ((ball.getLastX() > x) && (ball.getX() <= x))
                                                {
                                                        // ball going left - reflect right
                                                        collideWithBall(x, sy, 90.0f);
                                                       
                                                        if (!isAlive())
                                                        {
                                                                createVerticalShards(x, y, sy, length, -GameServices.getRandomFloat(30.0f, 50.0f));
                                                        }                                                      
                                                }
                                        }
                                }
                               
                        }
                }
               
                public void render()
                {
                        float growth = MathUtil.ease(MathUtil.clamp(mLifeTime / GameConsts.POWERUP_BARRIERS_GROWTH_TIME, 0.0f, 1.0f));
                       
                        mBarrier.x = x;
                        mBarrier.y = y;
                        mBarrier.w = GameConsts.POWERUP_BARRIERS_LENGTH * growth;
                        mBarrier.h = GameConsts.POWERUP_BARRIERS_WIDTH;
                       
                        if (mIsHorizontal)
                        {
                                mBarrier.angle = 0.0f;
                        }
                        else
                        {
                                mBarrier.angle = 90.0f;
                        }

                        mBarrier.pivotX = mBarrier.w / 2.0f;
                        mBarrier.pivotY = mBarrier.h / 2.0f;
               
                        if (mFlickerTimer > 0.0f)
                        {
                                mBarrier.setColor(GameConsts.PINGK_COLOR.x, GameConsts.PINGK_COLOR.y, GameConsts.PINGK_COLOR.z, GameServices.getRandomFloat(0.4f, 0.9f));
                                mBarrier.x += GameServices.getRandomFloat(-GameConsts.POWERUP_BARRIERS_WAGGLE, GameConsts.POWERUP_BARRIERS_WAGGLE);
                                mBarrier.y += GameServices.getRandomFloat(-GameConsts.POWERUP_BARRIERS_WAGGLE, GameConsts.POWERUP_BARRIERS_WAGGLE);
                        }
                        else
                        {
                                mBarrier.setColor(GameConsts.PINGK_COLOR);
                        }
               
                        mBarrier.render();
                }
               
                public void collideWithBall(float x, float y, float angle)
                {
                        Ball ball = mGameLogic.getBall();
                       
                        ball.setX(x);
                        ball.setY(y);
                        ball.reflectAlongNormal(angle);
                       
                        mFlickerTimer = GameConsts.POWERUP_BARRIERS_FLICKER_TIME;
                        mLives--;
                       
                        if (isAlive())
                                mGameLogic.playSound(R.raw.barrier_hit);
                        else
                                mGameLogic.playSound(R.raw.barrier_shatter);                           
                       
                        // add hit
                        mGameLogic.getCrystalHitCounter().addHit(x, y);
                }
               
                public boolean isAlive()
                {
                        return (mLives > 0);
                }
        }
       
        public class Shard
        {
                private float x;
                private float y;
                private float w;
                private float h;
                private float mPivotX;
                private float mPivotY;
                private float mAngle;
                private float mTargetAngle;
                private float mLifeTime = 0.0f;
               
                public Shard(float _x, float _y, float _w, float _h, float _pX, float _pY, float _angle, float _targetAngle)
                {
                        x = _x;
                        y = _y;
                        w = _w;
                        h = _h;
                        mPivotX = _pX;
                        mPivotY = _pY;
                        mAngle = _angle;
                        mTargetAngle = _targetAngle;
                }
               
                public void update(float deltaTime)
                {
                        float diff = mTargetAngle - mAngle;
                        mAngle += (diff * deltaTime * 3.0f);
                       
                        mLifeTime += deltaTime;
                }
               
                public void render()
                {
                        mBarrier.x = x;
                        mBarrier.y = y;
                        mBarrier.w = w;
                        mBarrier.h = h;
                       
                        mBarrier.angle = mAngle;
                        mBarrier.pivotX = mPivotX;
                        mBarrier.pivotY = mPivotY;
                       
                        float alpha = 1.0f - MathUtil.clamp(mLifeTime / 1.0f, 0.0f, 1.0f);
               
                        mBarrier.setColor(GameConsts.PINGK_COLOR.x, GameConsts.PINGK_COLOR.y, GameConsts.PINGK_COLOR.z, alpha);
                        mBarrier.render();
                }              
               
                public boolean isAlive()
                {
                        return (mLifeTime < 1.0f);
                }
        }
       
        private GameLogic mGameLogic = null;
       
        private Vector<Particle> mParticles = new Vector<Particle>();
        private Vector<Shard> mShards = new Vector<Shard>();
        private Sprite2D mBarrier = new Sprite2D();
       
        public Barriers(GameLogic gameLogic)
        {
                mGameLogic = gameLogic;
               
                mBarrier.init(R.drawable.barrier);
                mBarrier.setColor(GameConsts.PINGK_COLOR);
        }
       
        public void update(float deltaTime)
        {
                int i = 0;
                while (i < mParticles.size())
                {
                        Particle p = mParticles.get(i);
                        p.update(deltaTime);
                       
                        if (!p.isAlive())
                        {
                                mParticles.remove(i);
                                continue;
                        }
                       
                        i++;                   
                }
               
                i = 0;
                while (i < mShards.size())
                {
                        Shard s = mShards.get(i);
                        s.update(deltaTime);
                       
                        if (!s.isAlive())
                        {
                                mShards.remove(i);
                                continue;
                        }
                       
                        i++;
                }
        }
       
        public void render()
        {
                for (int i = mParticles.size()-1; i >= 0; i--)
                {
                        mParticles.get(i).render();
                }
               
                for (int i = mShards.size()-1; i >= 0; i--)
                {
                        mShards.get(i).render();
                }
        }

        public void spawnCrystal(float posX, float posY, boolean horizontal)
        {
                Particle p = new Particle(posX, posY, horizontal);
                mParticles.add(p);
        }
       
        public int getNumBarriers()
        {
                return mParticles.size();
        }
       
        public void createVerticalShards(float x, float y, float sy, float length, float angleDirection)
        {
                mShards.add(new Shard(
                                x,
                                y - length/2.0f,
                                sy - (y - length/2.0f),
                                GameConsts.POWERUP_BARRIERS_WIDTH,
                                0,
                                GameConsts.POWERUP_BARRIERS_WIDTH/2.0f,
                                90.0f,
                                90.0f - angleDirection
                                ));
                       
                        mShards.add(new Shard(
                                x,
                                y + length/2.0f,
                                length/2.0f - sy + y,
                                GameConsts.POWERUP_BARRIERS_WIDTH,
                                length/2.0f - sy + y,
                                GameConsts.POWERUP_BARRIERS_WIDTH/2.0f,
                                90.0f,
                                90.0f + angleDirection
                                ));
        }
       
        public void createHorizontalShards(float x, float y, float sx, float length, float angleDirection)
        {
                mShards.add(new Shard(
                                x - length/2.0f,
                                y,
                                sx - (x - length/2.0f),
                                GameConsts.POWERUP_BARRIERS_WIDTH,
                                0,
                                GameConsts.POWERUP_BARRIERS_WIDTH/2.0f,
                                0.0f,
                                angleDirection
                                ));
               
                mShards.add(new Shard(
                                x + length/2.0f,
                                y,
                                length/2.0f - sx + x,
                                GameConsts.POWERUP_BARRIERS_WIDTH,
                                length/2.0f - sx + x,
                                GameConsts.POWERUP_BARRIERS_WIDTH/2.0f,
                                0.0f,
                                -angleDirection
                                ));
        }
       
}