Coverage Summary for Class: EntityManager (com.mygdx.game.Managers)
| Class | Class, % | Method, % | Line, % | 
|---|---|---|---|
| EntityManager | 100% (1/1) | 80% (8/10) | 63.6% (21/33) | 
1 package com.mygdx.game.Managers; 2 3 import com.badlogic.gdx.Gdx; 4 import com.mygdx.game.Components.Component; 5 import com.mygdx.game.Components.ComponentEvent; 6 import com.mygdx.game.Entitys.Entity; 7 8 import java.util.ArrayList; 9 10 /** 11 * Responsible for Managing the entity and component events. Entity's can be accessed by a String name 12 */ 13 public final class EntityManager { 14 private static ArrayList<String> entityNames; 15 private static ArrayList<Entity> entities; 16 private static ArrayList<Component> components; 17 private static boolean initialized = false; 18 19 /** 20 * Should only be called once although if it isn't called at all it will be called automatically 21 */ 22 public static void Initialize() { 23 entityNames = new ArrayList<>(); 24 entities = new ArrayList<>(); 25 components = new ArrayList<>(); 26 initialized = true; 27 } 28 29 /** 30 * Don't call manually 31 * @param c the comp to add 32 */ 33 public static void addComponent(Component c) { 34 tryInit(); 35 components.add(c); 36 } 37 38 /** 39 * Don't call manually 40 * @param e the entity to add 41 */ 42 public static void addEntity(Entity e) { 43 tryInit(); 44 entities.add(e); 45 entityNames.add(e.getName()); 46 } 47 48 49 /** 50 * changes the entity's name 51 * @param prev old name 52 * @param new_ new name 53 */ 54 public static void changeName(String prev, String new_) { 55 entityNames.set(entityNames.indexOf(prev), new_); 56 } 57 58 /** 59 * raises the appropriate events for each entity's component. then renders after all entities have being processed if render is requested 60 * @param comps calls the events left to right 61 */ 62 public static void raiseEvents(ComponentEvent... comps) { 63 tryInit(); 64 for (Entity e : entities) { 65 e.raiseEvents(comps); 66 } 67 for (ComponentEvent c : comps) { 68 if (c == ComponentEvent.Render) { 69 RenderingManager.render(); 70 71 break; 72 } 73 } 74 for (Entity e : entities) { 75 e.update(); 76 } 77 } 78 79 /** 80 * Cleans up all entities and components. Disposes of the primary sprite batch 81 */ 82 public static void cleanUp() { 83 tryInit(); 84 for (Entity e : entities) { 85 e.cleanUp(); 86 } 87 for (Component c : components) { 88 c.cleanUp(); 89 } 90 } 91 92 /** 93 * automatically calls initialised if not done so 94 */ 95 private static void tryInit() { 96 if (!initialized) { 97 Initialize(); 98 } 99 } 100 101 /** 102 * gets the time between the last from and the current 103 * @return 1/FPS 104 */ 105 public static float getDeltaTime() { 106 return Gdx.graphics.getDeltaTime(); 107 } 108 109 110 // public static Entity getEntity(String name) { 111 // return entities.get(entityNames.indexOf(name)); 112 // } 113 114 //public static int getFPS() { 115 // return Gdx.graphics.getFramesPerSecond(); 116 // } 117 118 }