Subversion Repositories AndroidProjects

Rev

Rev 1624 | Rev 1649 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.gebauz.burutaru.game.entities.enemies;

import com.gebauz.bauzoid.graphics.sprite.SpriteInstance;
import com.gebauz.bauzoid.math.Vector2;
import com.gebauz.bauzoid.math.Vector4;
import com.gebauz.bauzoid.math.collision.Shape;
import com.gebauz.bauzoid.parser.ParseUtil;
import com.gebauz.bauzoid.parser.ScanException;
import com.gebauz.bauzoid.parser.Tokenizer;
import com.gebauz.burutaru.GameConsts;
import com.gebauz.burutaru.game.entities.PowerUps;

/* A single instance of an enemy. */
public abstract class EnemyInstance
{


        // Constants========================================================================================
       
        public static final float UNSPAWN_DISTANCE = 500;
        public static final float DAMAGE_COOLDOWN_TIME = 0.2f;
       

        // Embedded Types===================================================================================

        // Fields===========================================================================================
       
        private Enemy mParent = null;
       
        public String name = "";
        //public float spawnAt = 0.0f;
       
        public boolean hasSpawned = false;
        public boolean hasAlreadySpawned = false;
       
        public Vector2 spawnTrigger = new Vector2(GameConsts.VIRTUAL_SCREEN_WIDTH + 100, GameConsts.VIRTUAL_SCREEN_HEIGHT/2);
       
        public Vector2 spawnPosition = null;
       
        public boolean spawnActive = true;
       
        public int health = 100;
       
        public boolean leavePowerUp = false;
        public PowerUps.Type powerUpType = PowerUps.Type.SPEED_UP;
       
        public Vector4 multiplyColor = new Vector4(1, 1, 1, 1);
        public Vector4 lerpColor = new Vector4(0, 0, 0, 0);
       
        public float alpha = 1.0f;
       
        protected SpriteInstance mSpriteInstance = null;
        protected float mLifeTime = 0.0f;              
        protected Shape mShapeInstance = null;
       
        protected Vector2 mVelocity = new Vector2();
        protected Vector2 mMovement = new Vector2();
       
        protected float mDamageCoolDown = 0.0f;

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


        public EnemyInstance(Enemy parent, String instanceName, int defaultHealth)
        {
                mParent = parent;
                name = instanceName;
                health = defaultHealth;
        }
       
        /** Called post-init. */
        public abstract void init();
       
        /** Called when called into appearance. */
        public abstract void spawn();
       
        public abstract void exit();
       
        public void update(float deltaTime)
        {
                updateScroll(deltaTime);
               
                mLifeTime += deltaTime;
               
                mDamageCoolDown -= deltaTime;
                if (mDamageCoolDown < 0.0f)
                        mDamageCoolDown = 0.0f;
        }
               
        /** Just update the scroller and nothing else, to wait until spawning. */
        public void updateScroll(float deltaTime)
        {
                mSpriteInstance.transform.x -= mParent.getGameLogic().getLevel().getCurrentScrollVelocity().x;
                mSpriteInstance.transform.y -= mParent.getGameLogic().getLevel().getCurrentScrollVelocity().y;
        }
       
        public void render()
        {
                if (mSpriteInstance != null)
                {
                        if (mDamageCoolDown > 0.0f)
                        {
                                float t = mParent.getGameLogic().getGame().getRandomFloat(0.25f, 0.95f);
                               
                                //mSpriteInstance.fogColor.set(lerpColor.x * (1-t) + t, lerpColor.y * (1-t) + t, lerpColor.z * (1-t) + t, t);
                                mSpriteInstance.fogColor.set(1, 1, 1, t);
                        }
                        else
                                mSpriteInstance.fogColor.setFrom(lerpColor);
                       
                        mSpriteInstance.alpha = alpha;
                       
                        mSpriteInstance.color.setFrom(multiplyColor);
                        mSpriteInstance.render();
                }
        }
       
        public abstract void die();
       
        public void unspawn()
        {
                hasSpawned = false;
                hasAlreadySpawned = true;
                health = 0;
        }
       
        /** TODO: maybe add more parameters (source of damage, etc.).
         * Returns true if call actually damages (i.e. no cooldown).
         */

        public boolean damage(int damage)
        {
                if (mDamageCoolDown > 0.0f)
                        return false;
                                       
                applyDamage(damage);
                mDamageCoolDown = DAMAGE_COOLDOWN_TIME;
               
                return true;
        }

        public abstract void addImpulse(float x, float y);
       
        //public abstract Shape getShape();
        public Shape getShape()
        {
                mShapeInstance.transform.set(mSpriteInstance.transform);
                return mShapeInstance;
        }
       
        public void applyDamage(int damage)
        {
                health -= damage;
                if (health <= 0)
                {
                        if (leavePowerUp)
                        {
                                mParent.getGameLogic().getPowerUps().spawn(mSpriteInstance.transform.x, mSpriteInstance.transform.y, powerUpType);
                        }
                       
                        die();
                }
        }
       
        public boolean readParameter(Tokenizer t, String id) throws ScanException
        {
                if (id.equalsIgnoreCase("spawnTrigger"))
                {
                        spawnTrigger = ParseUtil.readVector2(t);
                }
                else if (id.equalsIgnoreCase("spawnPosition"))
                {
                        spawnPosition = ParseUtil.readVector2(t);
                }
                else if (id.equalsIgnoreCase("leavePowerUp"))
                {
                        int type = (int)t.readNumber();
                        PowerUps.Type[] values = PowerUps.Type.values();
                        if ((type >= 0) && (type < values.length))
                        {
                                leavePowerUp = true;
                                powerUpType = values[type];
                        }
                }
                else if (id.equalsIgnoreCase("multiplyColor"))
                {
                        multiplyColor = ParseUtil.readVector4(t);
                }
                else if (id.equalsIgnoreCase("lerpColor"))
                {
                        lerpColor = ParseUtil.readVector4(t);
                }
                else if (id.equalsIgnoreCase("alpha"))
                {
                        alpha = t.readNumber();
                }
                else
                {
                        return false;
                }
               
                t.readToken(";");
                return true;
        }

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

        public final Enemy getParent() { return mParent; }
       
        /*public final boolean hasSpawned()
        {
                return (mParent.getGameLogic().getLevelTimer() >= spawnAt);                            
        }*/

       
        /** Subclasses can define this to narrower definitions. */
        public boolean isAlive()
        {
                return (hasSpawned && (health > 0));
        }
}