Subversion Repositories AndroidProjects

Rev

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

package com.gebauz.burutaru.game.entities;

import java.util.Vector;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Sound;
import com.gebauz.bauzoid.graphics.sprite.AtlasSprite;
import com.gebauz.bauzoid.graphics.sprite.AtlasSpriteFrame;
import com.gebauz.bauzoid.math.MathUtil;
import com.gebauz.bauzoid.math.RandomChooser;
import com.gebauz.burutaru.game.GameLogic;

public class PowerUps extends Entity
{
        // Constants========================================================================================
       
        public static final int POWERUP_SIZE = 40;
        public static final int POWERUP_ICON_SIZE = POWERUP_SIZE/2;
       
        public static final int NUM_ANIM_FRAMES = 4;
        public static final float ANIM_FRAME_DURATION = 0.15f;
        public static final float ANIM_DURATION = ANIM_FRAME_DURATION * NUM_ANIM_FRAMES;

        public static final int ICON_FRAME_START = 4;
       
        public static final float POWERUP_RADIUS_SQR = (POWERUP_SIZE*POWERUP_SIZE);
       
        // Embedded Types===================================================================================
       
        public enum Type
        {
                SPEED_UP,
                MISSILE,
                BOMB,
                BEAM_SHOT,
                SPREAD_SHOT,
                NAPALM_SHOT,           
                FORCE_FIELD                            
        }
       
        public class Instance
        {
                private Type mType;
               
                public float x = 0;
                public float y = 0;
                public boolean isAlive = false;
               
                public Instance(float _x, float _y, Type type)
                {
                        x =_x; y = _y; isAlive = true;
                        mType = type;
                }
               
                public void update(float deltaTime)
                {
                        // check if ship picked up
                        float shipX = getGameLogic().getShip().getPositionX();
                        float shipY = getGameLogic().getShip().getPositionY();
                        /*float shipW = Ship.SHIP_WIDTH;
                        float shipH = getGameLogic().getShip().getPositionY();
                       
                        if (((shipX + shipW) > (x - POWERUP_SIZE/2)) && ((shipX - shipW)< (x + POWERUP_SIZE/2)) &&
                                ((shipY + shipH) > (y - POWERUP_SIZE/2)) && ((shipY - shipH)< (y + POWERUP_SIZE/2)))*/

                       
                        x += getGameLogic().getGlobalScrollX() * deltaTime;
                        y += getGameLogic().getGlobalScrollY() * deltaTime;
                       
                        float diffX = x - shipX;
                        float diffY = y - shipY;
                       
                        float distanceSqr = diffX*diffX + diffY*diffY;
                       
                        if (distanceSqr < (Ship.SHIP_RADIUS_SQR + POWERUP_ICON_SIZE))
                        {
                                // picked up
                                pickPowerUp(mType);
                                isAlive = false;
                        }
                }
               
                public void render()
                {
                        float t = (getGame().getTime() % ANIM_DURATION) / ANIM_DURATION;
                        int frame = MathUtil.clamp((int)(t * NUM_ANIM_FRAMES), 0, NUM_ANIM_FRAMES-1);                  
                       
                        mSpriteFrames[frame].param.x = x;
                        mSpriteFrames[frame].param.y = y;
                        mSpriteFrames[frame].param.w = POWERUP_SIZE;
                        mSpriteFrames[frame].param.h = POWERUP_SIZE;
                        mSpriteFrames[frame].centerPivot();
                        mSpriteFrames[frame].render();
                       
                        mSpriteFrames[ICON_FRAME_START + mType.ordinal()].param.x = x;
                        mSpriteFrames[ICON_FRAME_START + mType.ordinal()].param.y = y;
                        mSpriteFrames[ICON_FRAME_START + mType.ordinal()].param.w = POWERUP_ICON_SIZE;
                        mSpriteFrames[ICON_FRAME_START + mType.ordinal()].param.h = POWERUP_ICON_SIZE;
                        mSpriteFrames[ICON_FRAME_START + mType.ordinal()].centerPivot();
                        mSpriteFrames[ICON_FRAME_START + mType.ordinal()].render();
                }              
               
                public Type getType()
                {
                        return mType;
                }
        }

        // Fields===========================================================================================

        private AtlasSprite mSprite = null;
        private AtlasSpriteFrame mSpriteFrames[] = null;
       
        private Vector<Instance> mPowerUps = new Vector<Instance>();
       
        private Sound mPickUpSfx = null;

        // Methods==========================================================================================

        public PowerUps(GameLogic gameLogic)
        {
                super(gameLogic);
                mSprite = new AtlasSprite(getGraphics(), "data/textures/powerup.png",
                                "data/textures/powerup.txt");
        }

        @Override
        public void initAsync()
        {
                mSprite.initAsync();
        }

        @Override
        public void init()
        {
                mPickUpSfx = getAudio().newManagedSound("data/sounds/pickup.wav");
               
                mSprite.init();
                mSpriteFrames = mSprite.createSpriteInstances();
               
                spawn(400, 200, Type.SPEED_UP);
                //spawn(460, 150, Type.MISSILE);
                /*spawn(560, 300, Type.SPEED_UP);
                spawn(700, 260, Type.SPEED_UP);
                spawn(300, 220, Type.SPEED_UP);*/

        }

        @Override
        public void exit()
        {
                if (mSpriteFrames != null)
                        mSpriteFrames = null;

                if (mSprite != null)
                {
                        mSprite.dispose();
                        mSprite = null;
                }
               
                if (mPickUpSfx != null)
                {
                        getAudio().removeManagedSound(mPickUpSfx);
                        mPickUpSfx = null;
                }
        }

        @Override
        public void update(float deltaTime)
        {
                int i = 0;
                while (i < mPowerUps.size())
                {
                        Instance powerup = mPowerUps.get(i);
                        powerup.update(deltaTime);
                        if (!powerup.isAlive)
                        {
                                mPowerUps.remove(i);
                                continue;
                        }
                        ++i;
                }
        }

        @Override
        public void render()
        {
                for (int i = 0; i < mPowerUps.size(); i++)
                {
                        mPowerUps.get(i).render();
                }              
        }
       
        public void spawn(float x, float y, Type type)
        {
                mPowerUps.add(new Instance(x, y, type));
        }
       
        public void randomSpawn(float x, float y)
        {
                RandomChooser r = new RandomChooser();
                r.addEntry(Type.SPEED_UP.ordinal(), 5);
                r.addEntry(Type.MISSILE.ordinal(), 5);
                r.addEntry(Type.BOMB.ordinal(), 5);
                r.addEntry(Type.BEAM_SHOT.ordinal(), 5);
                /*
                r.addEntry(Type.FORCE_FIELD.ordinal(), 5);
                r.addEntry(Type.THREE_WAY_SHOT.ordinal(), 5);
                r.addEntry(Type.NAPALM_SHOT.ordinal(), 5);
                */

               
                mPowerUps.add(new Instance(x, y, Type.values()[r.chooseEntry()]));
        }
       
        public void pickPowerUp(Type type)
        {
                switch (type)
                {
                case SPEED_UP:
                        getGameLogic().getShip().boostEngine();
                        getGameLogic().getShipParameters().upgradeSpeed();
                        break;
                case MISSILE:
                        mPickUpSfx.play(0.5f);
                        getGameLogic().getShipParameters().upgradeMissile();
                        break;
                case BOMB:
                        mPickUpSfx.play(0.5f);
                        getGameLogic().getShipParameters().upgradeBomb();
                        break;
                case BEAM_SHOT:
                        mPickUpSfx.play(0.5f);
                        getGameLogic().getShipParameters().activateBeamShot();
                        break;
                case SPREAD_SHOT:
                        mPickUpSfx.play(0.5f);
                        break;
                case NAPALM_SHOT:
                        mPickUpSfx.play(0.5f);
                        break;
                case FORCE_FIELD:
                        mPickUpSfx.play(0.5f);
                        break;
                }
        }
       
        // Getters/Setters==================================================================================

}