Subversion Repositories AndroidProjects

Rev

Rev 879 | Rev 891 | Go to most recent revision | 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.gebauz.bauzoid.graphics.sprite.AtlasSprite;
import com.gebauz.bauzoid.graphics.sprite.AtlasSpriteInstance;
import com.gebauz.bauzoid.math.MathUtil;
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 SPEED_UP_FRAME = 4;
       
        public static final float POWERUP_RADIUS_SQR = (POWERUP_SIZE*POWERUP_SIZE);
       
        // Embedded Types===================================================================================
       
        public enum Type
        {
                SPEED_UP,
                MISSILE,
                BOMB,
                FORCE_FIELD,
                THREE_WAY_SHOT,
                NAPALM_SHOT,
                BEAM_SHOT              
        }
       
        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)))*/

                       
                        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].x = x;
                        mSpriteFrames[frame].y = y;
                        mSpriteFrames[frame].w = POWERUP_SIZE;
                        mSpriteFrames[frame].h = POWERUP_SIZE;
                        mSpriteFrames[frame].centerPivot();
                        mSpriteFrames[frame].render();
                       
                        mSpriteFrames[SPEED_UP_FRAME].x = x;
                        mSpriteFrames[SPEED_UP_FRAME].y = y;
                        mSpriteFrames[SPEED_UP_FRAME].w = POWERUP_ICON_SIZE;
                        mSpriteFrames[SPEED_UP_FRAME].h = POWERUP_ICON_SIZE;
                        mSpriteFrames[SPEED_UP_FRAME].centerPivot();
                        mSpriteFrames[SPEED_UP_FRAME].render();
                }              
               
                public Type getType()
                {
                        return mType;
                }
        }

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

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

        // 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()
        {
                mSprite.init();
                mSpriteFrames = mSprite.createSpriteInstances();
               
                spawn(400, 200, Type.SPEED_UP);
                /*spawn(460, 150, Type.SPEED_UP);
                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;
                }
        }

        @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 pickPowerUp(Type type)
        {
                switch (type)
                {
                case SPEED_UP:
                        getGameLogic().getShip().boostEngine();
                        getGameLogic().getShipParameters().upgradeSpeed();
                        break;
                case MISSILE:
                        break;
                case BOMB:
                        break;
                case FORCE_FIELD:
                        break;
                case THREE_WAY_SHOT:
                        break;
                case NAPALM_SHOT:
                        break;
                case BEAM_SHOT:
                        break;
                }
        }
       
        // Getters/Setters==================================================================================

}