Coverage Summary for Class: EntityManager (com.mygdx.game.Managers)

Class Class, % Method, % Line, %
EntityManager 100% (1/1) 58.3% (7/12) 40% (14/35)


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  * Dont call manually 31  * 32  * @param c the comp to add 33  */ 34  public static void addComponent(Component c) { 35  tryInit(); 36  components.add(c); 37  } 38  39  /** 40  * Dont call manually 41  * 42  * @param e the entity to add 43  */ 44  public static void addEntity(Entity e) { 45  tryInit(); 46  entities.add(e); 47  entityNames.add(e.getName()); 48  } 49  50  /** 51  * gets the first entity found with the given name 52  * 53  * @param name name of the entity 54  * @return the found entity 55  */ 56  public static Entity getEntity(String name) { 57  return entities.get(entityNames.indexOf(name)); 58  } 59  60  /** 61  * changes the entity's name 62  * 63  * @param prev old name 64  * @param new_ new name 65  */ 66  public static void changeName(String prev, String new_) { 67  entityNames.set(entityNames.indexOf(prev), new_); 68  } 69  70  /** 71  * raises the appropriate events for each entity's component. then renders after all entities have being processed if render is requested 72  * 73  * @param comps calls the events left to right 74  */ 75  public static void raiseEvents(ComponentEvent... comps) { 76  tryInit(); 77  for (Entity e : entities) { 78  e.raiseEvents(comps); 79  } 80  for (ComponentEvent c : comps) { 81  if (c == ComponentEvent.Render) { 82  RenderingManager.render(); 83  84  break; 85  } 86  } 87  for (Entity e : entities) { 88  e.update(); 89  } 90  } 91  92  /** 93  * Cleans up all entities and components. Disposes of the primary sprite batch 94  */ 95  public static void cleanUp() { 96  tryInit(); 97  for (Entity e : entities) { 98  e.cleanUp(); 99  } 100  for (Component c : components) { 101  c.cleanUp(); 102  } 103  } 104  105  /** 106  * automatically calls initialised if not done so 107  */ 108  private static void tryInit() { 109  if (!initialized) { 110  Initialize(); 111  } 112  } 113  114  /** 115  * gets the time between the last from and the current 116  * 117  * @return 1/FPS 118  */ 119  public static float getDeltaTime() { 120  return Gdx.graphics.getDeltaTime(); 121  } 122  123  public static int getFPS() { 124  return Gdx.graphics.getFramesPerSecond(); 125  } 126 }