Coverage Summary for Class: Component (com.mygdx.game.Components)
| Class | Class, % | Method, % | Line, % |
|---|---|---|---|
| Component | 100% (1/1) | 72.7% (8/11) | 81.8% (18/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 /** 19 * sets up the Component 20 */ 21 protected Component() { 22 reqsMet = false; 23 type = ComponentType.Unknown; 24 parent = null; 25 requirements = new ArrayList<>(); 26 EntityManager.addComponent(this); 27 } 28 29 /** 30 * @param e the Entity to set the parent of this component to 31 */ 32 public void setParent(Entity e) { 33 parent = e; 34 } 35 36 /** 37 * @return the parent of this component 38 */ 39 public Entity getParent() { 40 return parent; 41 } 42 43 /** 44 * Sets the required components 45 * @param reqs take a guess 46 */ 47 public final void setRequirements(ComponentType... reqs) { 48 requirements.addAll(Arrays.asList(reqs)); 49 } 50 51 /** 52 * Checks if the passed requirements exist will crash if they aren't 53 */ 54 private void checkRequirements() { 55 if (reqsMet) { 56 return; 57 } 58 for (ComponentType t : requirements) { 59 Component c = parent.getComponent(t); 60 if (c == null) { 61 throw new RuntimeException("Component: " + t.name() + " Is not found for " + type.name()); 62 } 63 } 64 reqsMet = true; 65 } 66 67 /** 68 * @return the type of the component 69 */ 70 public final ComponentType getType() { 71 return type; 72 } 73 74 /** 75 * Called once before start prior to the update loop. 76 */ 77 public void awake() { 78 checkRequirements(); 79 } 80 81 /** 82 * Called once after awake but prior to the update loop. 83 */ 84 public void start() { 85 checkRequirements(); 86 } 87 88 /** 89 * Called once after the update loop has finished. 90 */ 91 public void cleanUp() { 92 checkRequirements(); 93 } 94 95 /** 96 * Called once per frame 97 */ 98 public void update() { 99 checkRequirements(); 100 } 101 102 /** 103 * Called once per frame used exclusively for rendering 104 */ 105 public void render() { 106 checkRequirements(); 107 } 108 }