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

Class Method, % Line, %
AINavigation 14.8% (4/27) 18.3% (11/60)
AINavigation$1
AINavigation$Attributes 100% (1/1) 100% (7/7)
Total 17.9% (5/28) 26.9% (18/67)


1 package com.mygdx.game.Components; 2  3 import com.badlogic.gdx.ai.steer.Steerable; 4 import com.badlogic.gdx.ai.steer.SteeringAcceleration; 5 import com.badlogic.gdx.ai.steer.SteeringBehavior; 6 import com.badlogic.gdx.ai.utils.Location; 7 import com.badlogic.gdx.math.Vector2; 8 import com.mygdx.game.Entitys.Ship; 9 import com.mygdx.game.Managers.GameManager; 10 import com.mygdx.utils.Utilities; 11  12 /** 13  * Used to control NPCs with steerable for movement and state machines for behaviour 14  */ 15 public class AINavigation extends Component implements Steerable<Vector2> { 16  /** 17  * NPC settings for steerable 18  */ 19  private static class Attributes { 20  public float boundingRadius = 128; 21  public float maxSpd = GameManager.getSettings().get("AI").getFloat("maxSpeed"); 22  public float maxAcc = 50000; 23  public float maxAngSpd = 0; 24  public float maxAngAcc = 0; 25  public boolean isTagged = false; 26  } 27  28  RigidBody rb; 29  Transform t; 30  Attributes attributes; 31  SteeringBehavior<Vector2> behavior; 32  SteeringAcceleration<Vector2> steeringOutput; 33  34  public AINavigation() { 35  super(); 36  attributes = new Attributes(); 37  setRequirements(ComponentType.RigidBody); 38  type = ComponentType.AINavigation; 39  steeringOutput = new SteeringAcceleration<>(new Vector2()); 40  } 41  42  public void setBehavior(SteeringBehavior<Vector2> behavior) { 43  this.behavior = behavior; 44  } 45  46  /** 47  * Gets the components if != null 48  */ 49  private void getComps() { 50  if (rb == null) { 51  rb = parent.getComponent(RigidBody.class); 52  t = parent.getComponent(Transform.class); 53  } 54  } 55  56  /** 57  * Called once per frame. Apply the steering behaviour and sets the ship direction, so it faces the right way 58  */ 59  @Override 60  public void update() { 61  super.update(); 62  getComps(); 63  if (behavior != null) { 64  behavior.calculateSteering(steeringOutput); 65  applySteering(); 66  } else { 67  stop(); 68  } 69  70  Vector2 vel = rb.getVelocity().cpy(); 71  if (vel.x == 0 && vel.y == 0) { 72  ((Ship) parent).setShipDirection("-up"); 73  return; 74  } 75  vel.nor(); 76  Utilities.round(vel); 77  78  if (Ship.shipDirections.containsKey(vel)) { 79  ((Ship) parent).setShipDirection(vel); 80  } 81  } 82  83  /** 84  * Calculates the forces required by the steering behaviour (no rotation) 85  */ 86  private void applySteering() { 87  boolean anyAcc = false; 88  if (!steeringOutput.linear.isZero()) { 89  Vector2 f = steeringOutput.linear; 90  rb.applyForce(f); 91  anyAcc = true; 92  } 93  94  if (anyAcc) { 95  Vector2 vel = rb.getVelocity(); 96  float speed = vel.len2(); 97  if (speed > attributes.maxSpd * attributes.maxSpd) { 98  rb.setVelocity(vel.scl(attributes.maxSpd / (float) Math.sqrt(speed))); 99  } 100  } 101  } 102  103  /** 104  * Stops all motion 105  */ 106  public void stop() { 107  getComps(); 108  rb.setVelocity(new Vector2(0, 0)); 109  } 110  111  // Overrides for steerable 112  113  @Override 114  public Vector2 getLinearVelocity() { 115  getComps(); 116  return rb.getVelocity(); 117  } 118  119  @Override 120  public float getAngularVelocity() { 121  getComps(); 122  return rb.getAngularVelocity(); 123  } 124  125  @Override 126  public float getBoundingRadius() { 127  return attributes.boundingRadius; 128  } 129  130  @Override 131  public boolean isTagged() { 132  return attributes.isTagged; 133  } 134  135  @Override 136  public void setTagged(boolean tagged) { 137  attributes.isTagged = tagged; 138  } 139  140  @Override 141  public float getZeroLinearSpeedThreshold() { 142  return 0.01f; 143  } 144  145  @Override 146  public void setZeroLinearSpeedThreshold(float value) { 147  148  } 149  150  @Override 151  public float getMaxLinearSpeed() { 152  return attributes.maxSpd; 153  } 154  155  @Override 156  public void setMaxLinearSpeed(float maxLinearSpeed) { 157  attributes.maxSpd = maxLinearSpeed; 158  } 159  160  @Override 161  public float getMaxLinearAcceleration() { 162  return attributes.maxAcc; 163  } 164  165  @Override 166  public void setMaxLinearAcceleration(float maxLinearAcceleration) { 167  attributes.maxAcc = maxLinearAcceleration; 168  } 169  170  @Override 171  public float getMaxAngularSpeed() { 172  return attributes.maxAngSpd; 173  } 174  175  @Override 176  public void setMaxAngularSpeed(float maxAngularSpeed) { 177  attributes.maxAngSpd = maxAngularSpeed; 178  } 179  180  @Override 181  public float getMaxAngularAcceleration() { 182  return attributes.maxAngAcc; 183  } 184  185  @Override 186  public void setMaxAngularAcceleration(float maxAngularAcceleration) { 187  attributes.maxAngAcc = maxAngularAcceleration; 188  } 189  190  @Override 191  public Vector2 getPosition() { 192  getComps(); 193  return t.getPosition(); 194  } 195  196  @Override 197  public float getOrientation() { 198  getComps(); 199  return t.getRotation(); 200  } 201  202  @Override 203  public void setOrientation(float orientation) { 204  205  } 206  207  @Override 208  public float vectorToAngle(Vector2 vector) { 209  return Utilities.vectorToAngle(vector); 210  } 211  212  @Override 213  public Vector2 angleToVector(Vector2 outVector, float angle) { 214  return Utilities.angleToVector(outVector, angle); 215  } 216  217  @Override 218  public Location<Vector2> newLocation() { 219  getComps(); 220  return t; 221  } 222 }