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; //from ww w .j a va2s . co m import com.cladophora.ssniper.BaseActivity; import com.cladophora.ssniper.PlantPool; import com.cladophora.ssniper.scene.GameScene; import org.andengine.entity.Entity; import java.util.Iterator; import java.util.LinkedList; public class PlantLayer extends Entity { public static LinkedList<Plant> plants; public static PlantLayer instance; public int plantCount; public BaseActivity activity; final public static float[] plantBounds = {0.05f, 0.90f, 0.75f, 0.33f}; final public static float upperBound = plantBounds[0]; // top bound final public static float rightBound = plantBounds[1]; // right bound final public static float lowerBound = plantBounds[2]; // bottom bound final public static float leftBound = plantBounds[3]; // left bound final public static float plant_BOUNDS_WIDTHRATIO = rightBound - leftBound; final public static float plant_BOUNDS_HEIGHTRATIO = lowerBound - upperBound; public static PlantLayer getSharedInstance() { if (instance == null) { instance = new PlantLayer(0); } return instance; } public static boolean isEmpty() { return (plants.size() == 0); } public static int size() { return (plants.size()); } public static Iterator<Plant> getIterator() { return plants.iterator(); } public void purge() { for (Plant plant : plants) { PlantPool.getSharedInstance().recyclePoolItem(plant); } plants.clear(); } public PlantLayer(int x) { activity = BaseActivity.getSharedInstance(); plants = new LinkedList<Plant>(); instance = this; plantCount = x; } public void addPlants(int n) { for (int i = 0; i < n; i++) { final Plant plant = PlantPool.getSharedInstance().obtainPoolItem(); GameScene.getSharedInstance().attachChild(plant.sprite); plant.sprite.setZIndex((int) (plant.sprite.getY() + plant.sprite.getHeight())); plants.add(plant); } } public static void addPlantsRandomly() { clear(); int nPlants = 3 + BaseActivity.r.nextInt(3); getSharedInstance().addPlants(nPlants); } @Override public void onDetached() { purge(); clearUpdateHandlers(); super.onDetached(); } public static void clear() { getSharedInstance().purge(); } public static void show() { for (Plant p : plants) { p.sprite.setVisible(true); } } public static void hide() { for (Plant p : plants) { p.sprite.setVisible(false); } } }