Coverage Summary for Class: Component (com.mygdx.game.Components)

Class Class, % Method, % Line, %
Component 100% (1/1) 54.5% (6/11) 72.7% (16/22)


1 package com.mygdx.game.Components; 2  3 import com.mygdx.game.Entitys.Entity; 4 import com.mygdx.game.Managers.EntityManager; 5  6 import java.util.ArrayList; 7 import java.util.Arrays; 8  9 /** 10  * Base class for the Components 11  */ 12 public abstract class Component { 13  protected ComponentType type; 14  protected Entity parent; 15  protected ArrayList<ComponentType> requirements; 16  protected boolean reqsMet; 17  18  protected Component() { 19  reqsMet = false; 20  type = ComponentType.Unknown; 21  parent = null; 22  requirements = new ArrayList<>(); 23  EntityManager.addComponent(this); 24  } 25  26  public void setParent(Entity e) { 27  parent = e; 28  } 29  30  public Entity getParent() { 31  return parent; 32  } 33  34  /** 35  * Sets the required components 36  * 37  * @param reqs take a guess 38  */ 39  public final void setRequirements(ComponentType... reqs) { 40  requirements.addAll(Arrays.asList(reqs)); 41  } 42  43  /** 44  * Checks if the passed requirements exist will crash if they aren't 45  */ 46  private void checkRequirements() { 47  if (reqsMet) { 48  return; 49  } 50  for (ComponentType t : requirements) { 51  Component c = parent.getComponent(t); 52  if (c == null) { 53  throw new RuntimeException("Component: " + t.name() + " Is not found for " + type.name()); 54  } 55  } 56  reqsMet = true; 57  } 58  59  public final ComponentType getType() { 60  return type; 61  } 62  63  /** 64  * Called once before start prior to the update loop. 65  */ 66  public void awake() { 67  checkRequirements(); 68  } 69  70  /** 71  * Called once after awake but prior to the update loop. 72  */ 73  public void start() { 74  checkRequirements(); 75  } 76  77  /** 78  * Called once after the update loop has finished. 79  */ 80  public void cleanUp() { 81  checkRequirements(); 82  } 83  84  /** 85  * Called once per frame 86  */ 87  public void update() { 88  checkRequirements(); 89  } 90  91  /** 92  * Called once per frame used exclusively for rendering 93  */ 94  public void render() { 95  checkRequirements(); 96  } 97 }