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

Class Class, % Method, % Line, %
Monster 100% (1/1) 58.8% (10/17) 60% (42/70)


1 package com.mygdx.game.Entitys; 2  3 import com.badlogic.gdx.graphics.g2d.Sprite; 4 import com.badlogic.gdx.math.Vector2; 5 import com.badlogic.gdx.utils.ObjectMap; 6 import com.mygdx.game.Components.Pirate; 7 import com.mygdx.game.Components.Renderable; 8 import com.mygdx.game.Components.RigidBody; 9 import com.mygdx.game.Components.Transform; 10 import com.mygdx.game.Managers.GameManager; 11 import com.mygdx.game.Managers.RenderLayer; 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.Physics.PhysicsBodyType; 16  17 import java.util.Objects; 18  19 /** NEW 20  * Base class. 21  */ 22 public class Monster extends Entity implements CollisionCallBack { 23  private static int monsterCount = 0; 24  public static ObjectMap<Vector2, String> directions; 25  private int health = 200; 26  27  private final Vector2 currentDir; 28  RigidBody rb; 29  int count = 0; 30  /** 31  * Creates a monster entity, containing Transform, Renderable, RigidBody, and Pirate components. 32  */ 33  public Monster() { 34  super(4); 35  currentDir = new Vector2(); 36  setName("Storm (" + monsterCount++ + ")"); // each monster has a unique name 37  if (directions == null) { 38  directions = new ObjectMap<>(); 39  directions.put(new Vector2(0, -1), "-up"); 40  directions.put(new Vector2(0, 1), "-down"); 41  directions.put(new Vector2(-1, 0), "-right"); 42  directions.put(new Vector2(1, 0), "-left"); 43  directions.put(new Vector2(-1, -1), "-up-right"); 44  directions.put(new Vector2(1, -1), "-up-left"); 45  directions.put(new Vector2(-1, 1), "-down-right"); 46  directions.put(new Vector2(1, 1), "-down-left"); 47  } 48  49  Transform t = new Transform(); 50  t.setPosition(1500, 1000); 51  Renderable r = new Renderable("monster-up.png", RenderLayer.Transparent) ; 52  rb = new RigidBody(PhysicsBodyType.Dynamic, r, t); 53  rb.setCallback(this); 54  55  addComponents(t, r, rb); 56  } 57  58  /** 59  * gets the string representation of the direction the monster is facing 60  * 61  * @param dir the vector dir the monster is facing 62  * @return the string representation of the direction 63  */ 64  private String getDirection(Vector2 dir) { 65  if (!currentDir.equals(dir) && directions.containsKey(dir)) { 66  currentDir.set(dir); 67  return directions.get(dir); 68  } 69  return ""; 70  } 71  72  /** 73  * will rotate the monster to face the direction (just changes the sprite doesn't actually rotate) 74  * 75  * @param dir the dir to face (used to get the correct sprite ) 76  */ 77  public void setDirection(Vector2 dir) { 78  setDirection(getDirection(dir)); 79  } 80  81  /** 82  * will rotate the monster to face the direction (just changes the sprite doesn't actually rotate) 83  * 84  * @param direction the dir to face (used to get the correct sprite ) 85  */ 86  public void setDirection(String direction) { 87  if (Objects.equals(direction, "")) { 88  return; 89  } 90  Renderable r = getComponent(Renderable.class); 91  Sprite s = ResourceManager.getSprite("monster" + direction + ".png"); 92  93  try { 94  r.setTexture(s); 95  } catch (Exception ignored) { 96  97  } 98  } 99  100  101  public Vector2 getPosition() { 102  return getComponent(Transform.class).getPosition().cpy(); 103  } 104  105  private void moveMonster(Vector2 dir) { 106  rb.setVelocity(dir); 107  } 108  109  110  @Override 111  public void update() { 112  super.update(); 113  114  float x = this.getPosition().x - GameManager.ships.get(0).getPosition().x; 115  float y = this.getPosition().y - GameManager.ships.get(0).getPosition().y; 116  Vector2 dir = new Vector2(x / -3, y / -3); 117  if (y==0 && x==0){ 118  this.setDirection(new Vector2( x, y)); 119  } else if (Math.abs(x) < Math.abs(y)) { 120  this.setDirection(new Vector2( Math.round(x/Math.abs(y)), Math.round(y/Math.abs(y)))); 121  } else { 122  this.setDirection(new Vector2( Math.round(x/Math.abs(x)), Math.round(y/Math.abs(x)))); 123  } 124  if (count == 200) { 125  moveMonster(dir); 126  count = 0; 127  } 128  count++; 129  } 130  131  public int getHealth() { 132  return health; 133  }; 134  135  public void setHealth(int number) { 136  health = number; 137  }; 138  139  private void takeDamage(int amount) { 140  health = health - amount; 141  }; 142  143  public boolean isAlive() { 144  return this.getHealth() > 0; 145  } 146  147  public void MonsterDeath(){ 148  getComponent(Renderable.class).hide(); 149  RigidBody rb = getComponent(RigidBody.class); 150  rb.removeFromPhysicsWorld(); 151  } 152  153  @Override 154  public void BeginContact(CollisionInfo info) { 155  EnterTrigger(info); 156  } 157  158  @Override 159  public void EndContact(CollisionInfo info) { 160  } 161  162  /** 163  * if called on a 164  Monster against anything else call it on the other thing 165  */ 166  @Override 167  public void EnterTrigger(CollisionInfo info) { 168  if (this instanceof Monster && !(info.a instanceof Monster) && !(info.a == null)) { 169  ((CollisionCallBack) info.a).EnterTrigger(info); 170  } 171  172  if (info.a instanceof CannonBall) { 173  CannonBall ball = (CannonBall) info.a; 174  takeDamage(5); 175  ball.kill(); 176  if (!isAlive()) { 177  Ship shooter = ball.getShooter(); 178  shooter.plunder(200); 179  shooter.getComponent(Pirate.class).addPoints(100); 180  MonsterDeath(); 181  } 182  } 183  } 184  185  /** 186  * if called on a Monsteer against anything else call it on the other thing 187  */ 188  @Override 189  public void ExitTrigger(CollisionInfo info) { 190  if (this instanceof Monster && !(info.a instanceof Monster) && !(info.a == null)) { 191  ((CollisionCallBack) info.a).ExitTrigger(info); 192  } 193  } 194 }