Subversion Repositories AndroidProjects

Rev

Rev 1543 | Rev 1563 | 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;

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

        // Fields===========================================================================================
       
        private Enemy mParent = null;
       
        public String name = "";
        //public float spawnAt = 0.0f;
       
        public boolean hasSpawned = false;
       
        public Vector2 spawnTrigger = new Vector2(GameConsts.VIRTUAL_SCREEN_WIDTH + 100, GameConsts.VIRTUAL_SCREEN_HEIGHT/2);
       
        public Vector2 spawnPosition = null;
       
        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;
       
        // 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 abstract void update(float deltaTime);
       
        /** Just update the scroller and nothing else, to wait until spawning. */
        public abstract void updateScroll(float deltaTime);
       
        public abstract void render();
       
        public abstract void die();
       
        public void unspawn()
        {
                hasSpawned = false;
                health = 0;
        }
       
        /** TODO: maybe add more parameters (source of damage, etc.).
         * Returns true if call actually damages (i.e. no cooldown).
         */

        public abstract boolean damage(int damage);

        public abstract void addImpulse(float x, float y);
       
        public abstract Shape getShape();
       
        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));
        }
}