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

Class Class, % Method, % Line, %
PlayerController 100% (1/1) 40% (2/5) 15% (6/40)


1 package com.mygdx.game.Components; 2  3 import com.badlogic.gdx.Gdx; 4 import com.badlogic.gdx.Input; 5 import com.badlogic.gdx.math.Vector2; 6 import com.badlogic.gdx.math.Vector3; 7 import com.badlogic.gdx.scenes.scene2d.ui.TextButton; 8 import com.mygdx.game.Entitys.Player; 9 import com.mygdx.game.Entitys.Ship; 10 import com.mygdx.game.Managers.RenderingManager; 11 import com.mygdx.game.UI.GameScreen; 12  13 import java.util.ArrayList; 14  15 import static com.mygdx.utils.Constants.HALF_DIMENSIONS; 16  17 /** 18  * Responsible for the keyboard/mouse control of the player 19  */ 20 public class PlayerController extends Component { 21  private Player player; 22  private float speed; 23  private ArrayList<TextButton> buttons; //Added for Assessment 2, stores list of Shop UI buttons 24  25  /** 26  * Creates the base PlayerController Component, setting up its initial values 27  */ 28  public PlayerController() { 29  super(); 30  type = ComponentType.PlayerController; 31  setRequirements(ComponentType.RigidBody); 32  } 33  34  /** 35  * @param player the parent 36  * @param speed speed 37  */ 38  public PlayerController(Player player, float speed) { 39  this(); 40  this.player = player; 41  this.speed = speed; 42  } 43  44  /** 45  * Amended for Assessment 2, added check for Shop UI elements being pressed before shooting 46  * Reads keyboard and mouse inputs, moving and shooting as required. 47  */ 48  @Override 49  public void update() { 50  super.update(); 51  final float s = speed; 52  53  Vector2 dir = getDirFromWASDInput(); 54  ((Ship) parent).setShipDirection(dir); 55  dir.scl(s); 56  57  RigidBody rb = parent.getComponent(RigidBody.class); 58  rb.setVelocity(dir); 59  60  RenderingManager.getCamera().position.set(new Vector3(player.getPosition(), 0.0f)); 61  RenderingManager.getCamera().update(); 62  63  if (Gdx.input.isButtonJustPressed(Input.Buttons.LEFT)) { 64  if(buttons != null){ 65  for(TextButton button : buttons){ 66  if(button.isPressed()){ 67  return; 68  } 69  } 70  } 71  72  int x = Gdx.input.getX(); 73  int y = Gdx.input.getY(); 74  75  // in range 0 to VIEWPORT 0, 0 bottom left 76  Vector2 delta = new Vector2(x, y); 77  delta.sub(HALF_DIMENSIONS); // center 0, 0 78  delta.nor(); 79  delta.y *= -1; 80  // unit dir to fire 81  ((Ship) parent).shoot(delta); 82  } 83  84  if (Gdx.input.isKeyJustPressed(Input.Keys.SPACE)) { 85  // unit dir to fire 86  ((Ship) parent).shoot(); 87  } 88  } 89  90  /** 91  * Converts WASD or arrows to direction of travel 92  * @return -1 <= (x, y) <= 1 93  */ 94  private Vector2 getDirFromWASDInput() { 95  Vector2 dir = new Vector2(0, 0); 96  97  if (Gdx.input.isKeyPressed(Input.Keys.W) || Gdx.input.isKeyPressed(Input.Keys.DPAD_UP)) { 98  dir.y += 1; 99  } 100  101  if (Gdx.input.isKeyPressed(Input.Keys.S) || Gdx.input.isKeyPressed(Input.Keys.DPAD_DOWN)) { 102  dir.y -= 1; 103  } 104  105  if (Gdx.input.isKeyPressed(Input.Keys.A) || Gdx.input.isKeyPressed(Input.Keys.DPAD_LEFT)) { 106  dir.x -= 1; 107  } 108  109  if (Gdx.input.isKeyPressed(Input.Keys.D) || Gdx.input.isKeyPressed(Input.Keys.DPAD_RIGHT)) { 110  dir.x += 1; 111  } 112  return dir; 113  } 114  115  /** 116  * @param buttons the list of ShopUI buttons 117  */ 118  public void setButtons(ArrayList<TextButton> buttons) { 119  this.buttons = buttons; 120  } 121 }