directionThePlayerIsIn method

Direction? directionThePlayerIsIn()

Implementation

Direction? directionThePlayerIsIn() {
  Player? player = gameRef.player;
  if (player == null) return null;
  var diffX = center.x - player.center.x;
  var diffPositiveX = diffX < 0 ? diffX *= -1 : diffX;
  var diffY = center.y - player.center.y;
  var diffPositiveY = diffY < 0 ? diffY *= -1 : diffY;

  if (diffPositiveX > diffPositiveY) {
    if (player.center.x > center.x) {
      return Direction.right;
    } else if (player.center.x < center.x) {
      return Direction.left;
    }
  } else {
    if (player.center.y > center.y) {
      return Direction.down;
    } else if (player.center.y < center.y) {
      return Direction.up;
    }
  }

  return null;
}