Coverage Summary for Class: NPCShip (com.mygdx.game.Entitys)
| Class | Class, % | Method, % | Line, % |
|---|---|---|---|
| NPCShip | 100% (1/1) | 83.3% (10/12) | 58.8% (57/97) |
1 package com.mygdx.game.Entitys; 2 3 import com.badlogic.gdx.Gdx; 4 import com.badlogic.gdx.ai.fsm.DefaultStateMachine; 5 import com.badlogic.gdx.ai.fsm.StateMachine; 6 import com.badlogic.gdx.ai.steer.behaviors.Arrive; 7 import com.badlogic.gdx.math.Vector2; 8 import com.badlogic.gdx.utils.JsonValue; 9 import com.mygdx.game.AI.EnemyState; 10 import com.mygdx.game.Components.*; 11 import com.mygdx.game.Managers.GameManager; 12 import com.mygdx.game.Managers.ResourceManager; 13 import com.mygdx.game.Physics.CollisionCallBack; 14 import com.mygdx.game.Physics.CollisionInfo; 15 import com.mygdx.game.UI.PowerupScreen; 16 import com.mygdx.utils.QueueFIFO; 17 import com.mygdx.utils.Utilities; 18 19 import java.util.Objects; 20 import java.util.Vector; 21 22 /** 23 * NPC ship entity class. 24 */ 25 public class NPCShip extends Ship implements CollisionCallBack { 26 public StateMachine<NPCShip, EnemyState> stateMachine; 27 private static JsonValue AISettings; 28 private final QueueFIFO<Vector2> path; 29 private int timer; 30 31 /** 32 * Creates an initial state machine 33 */ 34 public NPCShip() { 35 super(); 36 path = new QueueFIFO<>(); 37 38 if (AISettings == null) { 39 AISettings = GameManager.getSettings().get("AI"); 40 } 41 42 stateMachine = new DefaultStateMachine<>(this, EnemyState.WANDER); 43 44 setName("NPC"); 45 AINavigation nav = new AINavigation(); 46 47 addComponent(nav); 48 49 50 RigidBody rb = getComponent(RigidBody.class); 51 // rb.setCallback(this); 52 53 JsonValue starting = GameManager.getSettings().get("starting"); 54 55 // agro trigger 56 rb.addTrigger(Utilities.tilesToDistance(starting.getFloat("argoRange_tiles")), "agro"); 57 timer = 0; 58 } 59 60 /** 61 * gets the top of targets from pirate component 62 * 63 * @return the top target 64 */ 65 private Ship getTarget() { 66 return getComponent(Pirate.class).getTarget(); 67 } 68 69 /** 70 * updates the state machine 71 */ 72 @Override 73 public void update() { 74 if(!isAlive() ){ 75 return; 76 } 77 if(getFreeze()){ 78 stopMovement(); 79 return; 80 } 81 super.update(); 82 stateMachine.update(); 83 AINavigation nav = getComponent(AINavigation.class); 84 if(stateMachine.isInState(EnemyState.ATTACK) ){ 85 if (timer ==100){ 86 Vector2 target = new Vector2(-1 * (this.getPosition().x - GameManager.ships.get(0).getPosition().x), -1 * (this.getPosition().y - GameManager.ships.get(0).getPosition().y)); 87 GameManager.shoot(this, target); 88 89 //shoot(); 90 91 timer = 0; 92 } 93 else { 94 timer ++; 95 } 96 97 98 } 99 // System.out.println(getComponent(Pirate.class).targetCount()); 100 } 101 102 /** 103 * is meant to path find to the target but didn't work 104 */ 105 public void goToTarget() { 106 /*path = GameManager.getPath( 107 Utilities.distanceToTiles(getPosition()), 108 Utilities.distanceToTiles(getTarget().getPosition()));*/ 109 } 110 111 /** 112 * creates a new steering behaviour that will make the NPC beeline for the target doesn't factor in obstetrical 113 */ 114 public void followTarget() { 115 if (getTarget() == null || getFreeze()) { 116 stopMovement(); 117 return; 118 } 119 AINavigation nav = getComponent(AINavigation.class); 120 121 Arrive<Vector2> arrives = new Arrive<>(nav, 122 getTarget().getComponent(Transform.class)) 123 .setTimeToTarget(AISettings.getFloat("accelerationTime")) 124 .setArrivalTolerance(AISettings.getFloat("arrivalTolerance")) 125 .setDecelerationRadius(AISettings.getFloat("slowRadius")); 126 127 nav.setBehavior(arrives); 128 } 129 130 /** 131 * stops all movement and sets the behaviour to null 132 */ 133 public void stopMovement() { 134 AINavigation nav = getComponent(AINavigation.class); 135 nav.setBehavior(null); 136 nav.stop(); 137 } 138 139 /** 140 * Meant to cause the npc to wander 141 */ 142 public void wander() { 143 144 } 145 146 @Override 147 public void BeginContact(CollisionInfo info) { 148 149 } 150 151 @Override 152 public void EndContact(CollisionInfo info) { 153 154 } 155 156 /** 157 * if the agro fixture hit a ship set it as the target 158 * 159 * @param info the collision info 160 */ 161 @Override 162 public void EnterTrigger(CollisionInfo info) { 163 super.EnterTrigger(info); 164 if (!(info.a instanceof Ship)) { 165 return; 166 } 167 Ship other = (Ship) info.a; 168 if (Objects.equals(other.getComponent(Pirate.class).getFaction().getName(), getComponent(Pirate.class).getFaction().getName())) { 169 // is the same faction 170 return; 171 } 172 // add the new collision as a new target 173 Pirate pirate = getComponent(Pirate.class); 174 pirate.addTarget(other); 175 176 } 177 178 /** 179 * if a taget has left remove it from the potential targets Queue 180 * 181 * @param info collision info 182 */ 183 @Override 184 public void ExitTrigger(CollisionInfo info) { 185 186 if (!(info.a instanceof Ship)) { 187 return; 188 } 189 Pirate pirate = getComponent(Pirate.class); 190 Ship o = (Ship) info.a; 191 // remove the object from the targets list 192 for (Ship targ : pirate.getTargets()) { 193 if (targ == o) { 194 pirate.getTargets().remove(targ); 195 break; 196 } 197 } 198 } 199 200 /**NEW 201 * Called when the ship dies 202 */ 203 @Override 204 public void ShipDeath(){ 205 stopMovement(); 206 getComponent(Renderable.class).hide(); 207 208 209 RigidBody rb = getComponent(RigidBody.class); 210 rb.removeFromPhysicsWorld(); 211 212 } 213 }