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

Class Method, % Line, %
Entity 92.3% (12/13) 73.7% (28/38)
Entity$1 0% (0/1) 0% (0/1)
Total 85.7% (12/14) 71.8% (28/39)


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  /** 21  * Constructs the base elements of the object 22  */ 23  public Entity() { 24  components = new ArrayList<>(); 25  entityName = "Entity (" + ++entityCount + ")"; // makes ure by default every entity has a unique name (although it's not automatically important if they don't) 26  EntityManager.addEntity(this); 27  } 28  29  /** 30  * Allocates the correct amount of memory for components 31  * @param numComponents number of components to allocate memory for 32  */ 33  public Entity(int numComponents) { 34  this(); 35  components.ensureCapacity(numComponents); 36  } 37  38  /** 39  * Sets the name of the entity 40  * @param name string to set the name to 41  */ 42  public final void setName(String name) { 43  EntityManager.changeName(entityName, name); 44  entityName = name; 45  } 46  47  /** 48  * @return the name of the Entity 49  */ 50  public final String getName() { 51  return entityName; 52  } 53  54  /** 55  * @param component the component to be added 56  */ 57  public void addComponent(Component component) { 58  components.add(component); 59  component.setParent(this); 60  } 61  62  /** 63  * @param components the components to be added 64  */ 65  public void addComponents(Component... components) { 66  for (Component c : components) { 67  addComponent(c); 68  } 69  } 70  71  /** 72  * gets component of type 73  * @param type the type of the desired component 74  * @return the component not cast 75  */ 76  public Component getComponent(ComponentType type) { 77  for (Component c : components) { 78  if (c.getType() == type) { 79  return c; 80  } 81  } 82  return null; 83  } 84  85  /** 86  * Gets the first component that is of the same type as T 87  * @param type [T].class 88  * @param <T> the type of the desired component 89  * @return the component cast to the appropriate type 90  */ 91  @SuppressWarnings("unchecked") 92  public <T> T getComponent(Class<T> type) { 93  for (Component c : components) { 94  if (type.isInstance(c)) { 95  return (T) c; 96  } 97  } 98  return null; 99  } 100 /* 101  *//** 102  * Gets the list of components that is of the same type as T 103  * Removed for Assesment 2 as never used 104  * @param type [T].class 105  * @param <T> the type of the desired component 106  * @return the components cast to the appropriate type 107  *//* 108  public <T> ArrayList<T> getComponents(Class<T> type) { 109  ArrayList<T> res = new ArrayList<>(); 110  for (Component c : components) { 111  if (type.isInstance(c)) { 112  res.add((T) c); 113  } 114  } 115  return res; 116  }*/ 117  118  119  /** 120  * Raises the appropriate events on each component with exception to rendering 121  */ 122  public final void raiseEvents(ComponentEvent... events) { 123  for (ComponentEvent event : events) { 124  for (Component c : components) { 125  switch (event) { 126  case Awake: 127  c.awake(); 128  break; 129  case Start: 130  c.start(); 131  break; 132  case Update: 133  c.update(); 134  break; 135  } 136  } 137  } 138  } 139  140  /** 141  * Similar to the Component's cleanUp event 142  */ 143  public void cleanUp() { 144  145  } 146  147  /** 148  * Similar to the Component's update event 149  */ 150  public void update() { 151  152  } 153  154  /** 155  * disposes of any components attached to this object 156  */ 157  public void dispose() { 158  for (Component component : components) { 159  component = null; 160  } 161  } 162  163  // public <T> ArrayList<T> getComponents(Class<T> type) { 164  // ArrayList<T> res = new ArrayList<>(); 165  // for (Component c : components) { 166  // if (type.isInstance(c)) { 167  // res.add((T) c); 168  // } 169  // } 170  // return res; 171  // } 172 }