Subversion Repositories AndroidProjects

Rev

Rev 879 | Rev 883 | 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.graphics.Texture.TextureFilter;
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;
import com.gebauz.burutaru.game.entities.PowerUps.Type;

public class BlobEnemies extends Entity
{

        // Constants========================================================================================
       
        public static final int INITIAL_NUM_INSTANCES = 16;
        public static final int NUM_FRAMES_X = 4;
        public static final int NUM_FRAMES_Y = 4;
        public static final int NUM_FRAMES = NUM_FRAMES_X * NUM_FRAMES_Y;
        public static final int FRAME_SIZE = 128;
       
        public static final int SPRITE_SIZE = 80;
       
        public static final float ANIM_SPEED = 15;
       
        public static final float MOVE_SPEED_X_MIN = 125;
        public static final float MOVE_SPEED_Y_MIN = 175;
        public static final float MOVE_SPEED_X_MAX = 170;
        public static final float MOVE_SPEED_Y_MAX = 240;

        // Embedded Types===================================================================================
       
        public class Instance
        {
                public float x;
                public float y;        
                public float lifeTime = 0.0f;
               
                public float moveSpeedX = 300.0f;
                public float moveSpeedY = 450.0f;
                public float phaseFactor = 1.0f;
               
                public boolean isAlive = false;
               
                public Instance(float _x, float _y)
                {
                        x = _x; y = _y;                
                        lifeTime = 0.0f;
                        isAlive = true;
                       
                        moveSpeedX = getGame().getRandomFloat(MOVE_SPEED_X_MIN, MOVE_SPEED_X_MAX);
                        moveSpeedY = getGame().getRandomFloat(MOVE_SPEED_Y_MIN, MOVE_SPEED_Y_MAX);
                        phaseFactor = getGame().getRandomFloat(0.7f, 1.5f);
                }
               
                public void update(float deltaTime)
                {
                        lifeTime += deltaTime;
                       
                        float moveY = MathUtil.sin(lifeTime * 360.0f * phaseFactor) * moveSpeedY;
                       
                        y += moveY * deltaTime;
                        x -= moveSpeedX * deltaTime;
                       
                        // when reaching the left corner, the enemies are removed
                       
                        if (x < -100.0f)
                                isAlive = false;
                }
               
                public boolean checkForHit(float _x, float _y, float w, float h)
                {
                        return (((_x + w/2) > (x - SPRITE_SIZE/2)) &&
                                        ((_x - w/2) < (x + SPRITE_SIZE/2)) &&
                                        ((_y + h/2) > (y - SPRITE_SIZE/2)) &&
                                        ((_y - h/2) < (y + SPRITE_SIZE/2)));
                }
               
                public void render()
                {
                        int frame = MathUtil.clamp((int)((lifeTime * ANIM_SPEED) % NUM_FRAMES), 0, NUM_FRAMES-1);
                       
                        mBlobFrames[frame].x = x;
                        mBlobFrames[frame].y = y;
                        mBlobFrames[frame].w = SPRITE_SIZE;
                        mBlobFrames[frame].h = SPRITE_SIZE;
                        mBlobFrames[frame].centerPivot();
                        mBlobFrames[frame].render();
                }
        }      

        // Fields===========================================================================================
       
        private Vector<Instance> mEnemies = new Vector<Instance>();
       
        private AtlasSprite mBlobEnemySprite = null;
        private AtlasSpriteInstance mBlobFrames[] = null; //new AtlasSpriteInstance[NUM_FRAMES];

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

        public BlobEnemies(GameLogic gameLogic)
        {
                super(gameLogic);
               
                mBlobEnemySprite = new AtlasSprite(getGraphics(), "data/textures/blob_enemy.png", "data/textures/blob_enemy.txt");
        }
       
        @Override
        public void initAsync()
        {
                mBlobEnemySprite.initAsync();
        }

        @Override
        public void init()
        {
                mBlobEnemySprite.init();
                mBlobEnemySprite.getTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear);
                mBlobFrames = mBlobEnemySprite.createSpriteInstances();
        }

        @Override
        public void exit()
        {
                for (int i = 0; i < NUM_FRAMES; i++)
                        mBlobFrames[i] = null;
                mBlobFrames = null;
               
                if (mBlobEnemySprite != null)
                {
                        mBlobEnemySprite.dispose();
                        mBlobEnemySprite = null;
                }
        }

        @Override
        public void update(float deltaTime)
        {
                int i = 0;
                while (i < mEnemies.size())
                {
                        Instance enemy = mEnemies.get(i);
                        enemy.update(deltaTime);
                       
                        if (!enemy.isAlive)
                        {
                                // unspawn
                                mEnemies.remove(i);
                                continue;
                        }
                       
                        ++i;
                }
        }
       
        public boolean checkForHit(float x, float y, float w, float h)
        {
                boolean hitFound = false;
               
                int i = 0;
                while (i < mEnemies.size())
                {
                        Instance enemy = mEnemies.get(i);
                        if (enemy.checkForHit(x, y, w, h))
                        {
                                getGameLogic().getExplosions().spawn(enemy.x, enemy.y);
                               
                                // spawn powerup
                                // TEMP:
                                int n = getGame().getRandomInt(1, 10);
                                if (n == 5)
                                {
                                        getGameLogic().getPowerUps().spawn(enemy.x, enemy.y, Type.SPEED_UP);
                                }
                               
                                mEnemies.remove(i);
                                hitFound = true;
                                continue;                              
                        }
                        ++i;
                }
               
                return hitFound;
        }

        @Override
        public void render()
        {
                for (int i = 0; i < mEnemies.size(); i++)
                {
                        mEnemies.get(i).render();
                }
        }
       
        public void spawn(float x, float y)
        {
                mEnemies.add(new Instance(x, y));
        }

        // Getters/Setters==================================================================================

}