Back to project page ssniper-andengine.
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.
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); } } }