Coverage Summary for Class: Entity (com.mygdx.game.Entitys)

Class Method, % Line, %
Entity 76.9% (10/13) 58.5% (24/41)
Entity$1 0% (0/1) 0% (0/1)
Total 71.4% (10/14) 57.1% (24/42)


1 package com.mygdx.game.Entitys; 2  3 import com.mygdx.game.Components.Component; 4 import com.mygdx.game.Components.ComponentEvent; 5 import com.mygdx.game.Components.ComponentType; 6 import com.mygdx.game.Managers.EntityManager; 7  8 import java.util.ArrayList; 9  10 /** 11  * The base class for all entities in the game. 12  * I am calling an entity pretty much anything that the user sees or interacts with except the UI. 13  * However, there is over head with this class so in some cases it's better to just use raw sprites 14  */ 15 public class Entity { 16  private static int entityCount = 0; 17  private String entityName; 18  private final ArrayList<Component> components; 19  20  public Entity() { 21  components = new ArrayList<>(); 22  entityName = "Entity (" + ++entityCount + ")"; // makes ure by default every entity has a unique name (although it's not automatically important if they don't) 23  EntityManager.addEntity(this); 24  } 25  26  /** 27  * Allocates the correct amount of memory for components 28  * 29  * @param numComponents number of components to allocate memory for 30  */ 31  public Entity(int numComponents) { 32  this(); 33  components.ensureCapacity(numComponents); 34  } 35  36  public final void setName(String name) { 37  EntityManager.changeName(entityName, name); 38  entityName = name; 39  } 40  41  public final String getName() { 42  return entityName; 43  } 44  45  public void addComponent(Component component) { 46  components.add(component); 47  component.setParent(this); 48  } 49  50  public void addComponents(Component... components) { 51  for (Component c : components) { 52  addComponent(c); 53  } 54  } 55  56  /** 57  * gets component of type 58  * 59  * @param type the type of the desired component 60  * @return the component not cast 61  */ 62  public Component getComponent(ComponentType type) { 63  for (Component c : components) { 64  if (c.getType() == type) { 65  return c; 66  } 67  } 68  return null; 69  } 70  71  /** 72  * Gets the first component that is of the same type as T 73  * 74  * @param type [T].class 75  * @param <T> the type of the desired component 76  * @return the component cast to the appropriate type 77  */ 78  @SuppressWarnings("unchecked") 79  public <T> T getComponent(Class<T> type) { 80  for (Component c : components) { 81  if (type.isInstance(c)) { 82  return (T) c; 83  } 84  } 85  return null; 86  } 87  88  /** 89  * Gets the list of components that is of the same type as T 90  * 91  * @param type [T].class 92  * @param <T> the type of the desired component 93  * @return the components cast to the appropriate type 94  */ 95  @SuppressWarnings("unchecked") 96  public <T> ArrayList<T> getComponents(Class<T> type) { 97  ArrayList<T> res = new ArrayList<>(); 98  for (Component c : components) { 99  if (type.isInstance(c)) { 100  res.add((T) c); 101  } 102  } 103  return res; 104  } 105  106  /** 107  * Raises the appropriate events on each component with exception to rendering 108  */ 109  public final void raiseEvents(ComponentEvent... events) { 110  for (ComponentEvent event : events) { 111  for (Component c : components) { 112  switch (event) { 113  case Awake: 114  c.awake(); 115  break; 116  case Start: 117  c.start(); 118  break; 119  case Update: 120  c.update(); 121  break; 122  } 123  } 124  } 125  } 126  127  /** 128  * Similar to the Component's cleanUp event 129  */ 130  public void cleanUp() { 131  132  } 133  134  /** 135  * Similar to the Component's update event 136  */ 137  public void update() { 138  139  } 140 }