Android Open Source - ssniper-andengine Enemy Layer






From Project

Back to project page ssniper-andengine.

License

The source code is released under:

Apache License

If you think the Android project ssniper-andengine listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.cladophora.ssniper.entity;
// www .  ja v  a2  s.c o  m
import com.cladophora.ssniper.BaseActivity;
import com.cladophora.ssniper.EnemyPool;
import com.cladophora.ssniper.LevelLoader;
import com.cladophora.ssniper.scene.GameScene;
import org.andengine.entity.Entity;
import org.json.JSONObject;

import java.util.Iterator;
import java.util.LinkedList;

public class EnemyLayer extends Entity {

    public static LinkedList<Enemy> enemies;
  public static EnemyLayer instance;
  public int enemyCount;
    public BaseActivity activity;

    final public static float[] enemyBounds = {0.05f, 0.90f, 0.75f, 0.33f};
    final public static float upperBound = enemyBounds[0]; // top bound
    final public static float rightBound = enemyBounds[1]; // right bound
    final public static float lowerBound = enemyBounds[2]; // bottom bound
    final public static float leftBound = enemyBounds[3]; // left bound
    final public static float ENEMY_BOUNDS_WIDTHRATIO = rightBound - leftBound;
    final public static float ENEMY_BOUNDS_HEIGHTRATIO = lowerBound - upperBound;

  public static EnemyLayer getSharedInstance() {
        if (instance == null) {
            instance = new EnemyLayer(0);
        }
    return instance;
  }

  public static boolean isEmpty() { return (enemies.size() == 0); }

    public static int size() { return (enemies.size()); }

  public static Iterator<Enemy> getIterator() {
    return enemies.iterator();
  }

    public static void purge() {
    for (Enemy e : enemies) {
            EnemyPool.sharedEnemyPool().recyclePoolItem(e);
    }
    enemies.clear();
  }

  public EnemyLayer(int x) {
        activity = BaseActivity.getSharedInstance();
        enemies = new LinkedList<Enemy>();
    instance = this;
    enemyCount = x;
  }

    public void spawnEnemies(final float minWait, final float maxWait, final int initialEnemyCount) {
        final EnemyPool enemyPool = EnemyPool.sharedEnemyPool();
        enemies.clear();

        for (int i = 0; i < initialEnemyCount; i++) {
            final Enemy e = enemyPool.obtainPoolItem();
            GameScene.getSharedInstance().attachChild(e.sprite);
            e.moveRandomly();
            e.beginFiringRandomly();
            e.beginRandomMovement(minWait,maxWait);
            enemies.add(e);
        }
        setVisible(true);
        GameScene.getSharedInstance().sortChildren();
    }

    public static void addEnemies(int n) {
        for (int i = 0; i < n; i++) {
            final Enemy e = EnemyPool.sharedEnemyPool().obtainPoolItem();
            GameScene.getSharedInstance().attachChild(e.sprite);
            e.advance();
            e.beginFiringRandomly();
            e.beginAttackMovement();
            enemies.add(e);
        }
    }

    public static void loadLevel(final JSONObject currentLevel) {
        final LevelLoader levelLoader = new LevelLoader();
        final float minWait = levelLoader.getMinWait(currentLevel);
        final float maxWait = levelLoader.getMaxWait(currentLevel);
        final int initialEnemyCount = levelLoader.getEnemyCount(currentLevel);
        instance.spawnEnemies(minWait, maxWait, initialEnemyCount);
    }

    @Override
  public void onDetached() {
    purge();
    clearUpdateHandlers();
    super.onDetached();
  }

    public static void show() {
        for (Enemy e : enemies) { e.sprite.setVisible(true); }
    }

    public static void hide() {
        for (Enemy e : enemies) { e.sprite.setVisible(false); }
    }
}




Java Source Code List

com.cladophora.ssniper.BaseActivity.java
com.cladophora.ssniper.DeviceUtil.java
com.cladophora.ssniper.EnemyPool.java
com.cladophora.ssniper.GameEffects.java
com.cladophora.ssniper.GameEvents.java
com.cladophora.ssniper.GameLoopUpdateHandler.java
com.cladophora.ssniper.GameUtil.java
com.cladophora.ssniper.HUDManager.java
com.cladophora.ssniper.HoverListener.java
com.cladophora.ssniper.ItemPool.java
com.cladophora.ssniper.LevelLoader.java
com.cladophora.ssniper.PlantPool.java
com.cladophora.ssniper.SPenHoverView.java
com.cladophora.ssniper.ScopeEngine.java
com.cladophora.ssniper.SoundManager.java
com.cladophora.ssniper.SpriteManager.java
com.cladophora.ssniper.andengine.IRandomTimerCallBack.java
com.cladophora.ssniper.andengine.RandomTimerHandler.java
com.cladophora.ssniper.entity.EnemyLayer.java
com.cladophora.ssniper.entity.Enemy.java
com.cladophora.ssniper.entity.ItemLayer.java
com.cladophora.ssniper.entity.Item.java
com.cladophora.ssniper.entity.PlantLayer.java
com.cladophora.ssniper.entity.Plant.java
com.cladophora.ssniper.entity.Reticle.java
com.cladophora.ssniper.entity.Rifle.java
com.cladophora.ssniper.entity.SPen.java
com.cladophora.ssniper.scene.GameScene.java
com.cladophora.ssniper.scene.InstructionScene.java
com.cladophora.ssniper.scene.IntroScene.java
com.cladophora.ssniper.scene.MainMenuScene.java
com.cladophora.ssniper.scene.PauseScene.java
com.cladophora.ssniper.scene.ResultScene.java
com.cladophora.ssniper.scene.ShopScene.java
com.cladophora.ssniper.scene.SplashScene.java
com.cladophora.ssniper.scene.SurvivorIntroScene.java
com.cladophora.ssniper.scene.SurvivorScene.java
com.cladophora.ssniper.scene.TutorialScene.java
com.cladophora.ssniper.scene.WeaponInfoScene.java