Subversion Repositories AndroidProjects

Rev

Rev 913 | Rev 915 | 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.gebauz.bauzoid.graphics.sprite.AtlasSprite;
import com.gebauz.bauzoid.graphics.sprite.AtlasSpriteInstance;
import com.gebauz.bauzoid.graphics.sprite.Sprite;
import com.gebauz.bauzoid.graphics.sprite.SpriteParameters;
import com.gebauz.bauzoid.math.MathUtil;
import com.gebauz.bauzoid.math.Matrix4;
import com.gebauz.bauzoid.math.Vector2;
import com.gebauz.bauzoid.math.collision.Shape;
import com.gebauz.bauzoid.math.collision.ShapeInstance;
import com.gebauz.bauzoid.math.collision.ShapeUtil;
import com.gebauz.burutaru.game.GameLogic;
import com.gebauz.burutaru.game.entities.BlobEnemies.Instance;
import com.gebauz.burutaru.game.entities.PowerUps.Type;

public class CarrotEnemies extends Entity
{

        // Constants========================================================================================
       
        public static final float SPRITE_WIDTH = 110;
        public static final float SPRITE_HEIGHT = SPRITE_WIDTH/2;
        public static final float ANIM_DURATION = 1.2f;

        // Embedded Types===================================================================================
       
        public class Instance
        {
                public float x = 0;
                public float y = 0;
                public float lifeTime = 0;
                public boolean isAlive = false;
               
                private SpriteParameters mParams = new SpriteParameters();
               
                private ShapeInstance mShapeInstance = null;
               
                public Instance(float _x, float _y)
                {
                        x = _x; y = _y; isAlive = true;
                        lifeTime = 0.0f;
                       
                        mShapeInstance = new ShapeInstance(mCarrotShape, mParams);
                }
               
                public void update(float deltaTime)
                {
                        lifeTime += deltaTime;
                       
                        //x -= deltaTime * 400;
                }
               
                public void render()
                {
                        float t = (lifeTime % ANIM_DURATION) / ANIM_DURATION;
                        int frame = MathUtil.clamp((int)(t * mCarrotFrames.length), 0, mCarrotFrames.length-1);
                       
                        mParams.x = x;
                        mParams.y = y;
                        mParams.w = SPRITE_WIDTH;
                        mParams.h = SPRITE_HEIGHT;
                        mParams.pivotX = 0;
                        mParams.pivotY = mParams.h/2;
                        mParams.mirrorX = true;
                        //mParams.angle = (lifeTime * 90) % 360;
                        //mParams.angle = 34;
                        //mParams.angle = 90;
                       
               
                        mCarrotFrames[frame].param.apply(mParams);
                        mCarrotFrames[frame].render();
                       
                       
/*                      float pX = mParams.w/3;
                        float pY = mParams.h/3;
                       
                        Vector2 p = mCarrotFrames[frame].param.transformPoint(pX, pY);
                       
                        mTester.param.x = p.x;
                        mTester.param.y = p.y;
                        mTester.param.w = 4;
                        mTester.param.h = 4;                   
                        mTester.centerPivot();
                        mTester.render();*/

                       
                        /*getGraphics().getBatch().begin();
                        mParams.y += 100.0f;
                        mCarrotFrames[frame].param.apply(mParams);
                        getGraphics().getBatch().drawSprite(mCarrotFrames[frame]);
                        getGraphics().getBatch().end();*/

                }
               
                public boolean checkForHit(Shape shape, Matrix4 transform)
                {
                        return shape.intersects(mCarrotShape, mParams);
                }
               
                public boolean checkForHit(float _x, float _y, float w, float h)
                {
                        return (((_x) > (x - SPRITE_WIDTH/2)) &&
                                        ((_x) < (x + SPRITE_WIDTH/2)) &&
                                        ((_y) > (y - SPRITE_HEIGHT/2)) &&
                                        ((_y) < (y + SPRITE_HEIGHT/2)));
                        /*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)));*/

                }
               
        }

        // Fields===========================================================================================
       
        private Vector<Instance> mEnemies = new Vector<Instance>();
       
        private Shape mCarrotShape = null;
       
        private AtlasSprite mCarrotSprite = null;
        private AtlasSpriteInstance mCarrotFrames[] = null;
       
        private Sprite mTester = null;

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

        public CarrotEnemies(GameLogic gameLogic)
        {
                super(gameLogic);
                mCarrotSprite = new AtlasSprite(getGraphics(), "data/textures/carrot.png", "data/textures/carrot.txt");
                mTester = new Sprite(getGraphics(), "data/textures/tester.png");
        }
       
        @Override
        public void initAsync()
        {
                mCarrotSprite.initAsync();
                mTester.initAsync();
        }

        @Override
        public void init()
        {
                mCarrotSprite.init();
                mCarrotFrames = mCarrotSprite.createSpriteInstances();
               
                mTester.init();

                mCarrotShape = ShapeUtil.createShapeFromFile("data/textures/carrot.shape");
        }

        @Override
        public void exit()
        {
                mCarrotShape = null;
                mCarrotFrames = null;
               
                if (mTester != null)
                {
                        mTester.dispose();
                        mTester = null;
                }
               
                if (mCarrotSprite != null)
                {
                        mCarrotSprite.dispose();
                        mCarrotSprite = 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)
                        {
                                mEnemies.remove(i);
                                continue;
                        }
                       
                        ++i;
                }
        }

        @Override
        public void render()
        {
                int i = 0;
                while (i < mEnemies.size())
                {
                        Instance enemy = mEnemies.get(i);
                        if (enemy.isAlive)
                        {
                                enemy.render();
                        }
                       
                        ++i;
                }
        }
       
        public void spawn(float x, float y)
        {
                mEnemies.add(new Instance(x, y));
        }
       
        public boolean checkForHit(Shape shape, Matrix4 transform)
        {
                boolean hitFound = false;
               
                int i = 0;
                while (i < mEnemies.size())
                {
                        Instance enemy = mEnemies.get(i);
                        if (enemy.checkForHit(shape, transform))
                        {
                                getGameLogic().getExplosions().spawn(enemy.x, enemy.y);
                        }
                        ++i;
                }
               
                return hitFound;
        }
       
        // TODO: unify
        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)
                                {
                                        int rand = getGame().getRandomInt(PowerUps.Type.SPEED_UP.ordinal(), PowerUps.Type.MISSILE.ordinal());
                                        getGameLogic().getPowerUps().spawn(enemy.x, enemy.y, Type.values()[rand]);
                                }*/

                               
                                //mEnemies.remove(i);
                                //hitFound = true;
                                //continue;                            
                        }
                        ++i;
                }
               
                return hitFound;
        }
       
        // TODO: unify
        public boolean getNearestEnemy(float x, float y, Vector2 result, float previousDistanceSqr)
        {
                boolean found = false;
                float distanceSqr = previousDistanceSqr;
               
                for (int i = 0; i < mEnemies.size(); i++)
                {
                        Instance enemy = mEnemies.get(i);
                        if (enemy.isAlive)
                        {
                                float diffX = enemy.x - x;
                                float diffY = enemy.y - y;
                                float currentDistanceSqr = diffX*diffX + diffY*diffY;
                               
                                if ((distanceSqr < 0) || (currentDistanceSqr < distanceSqr))
                                {
                                        found = true;
                                        distanceSqr = currentDistanceSqr;
                                       
                                        result.x = enemy.x;
                                        result.y = enemy.y;
                                }
                        }
                }
               
                return found;
        }
       
        // Getters/Setters==================================================================================

}