util.dart 2.63 KB
Newer Older
1
part of skysprites;
2

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

math.Random _random = new math.Random();

// Random methods

double randomDouble() {
  return _random.nextDouble();
}

double randomSignedDouble() {
  return _random.nextDouble() * 2.0 - 1.0;
}

int randomInt(int max) {
  return _random.nextInt(max);
}

Viktor Lidholt's avatar
Viktor Lidholt committed
20 21 22 23
bool randomBool() {
  return _random.nextDouble() < 0.5;
}

24 25
// atan2

26
class _Atan2Constants {
27

28
  _Atan2Constants() {
29 30
    for (int i = 0; i <= size; i++) {
      double f = i.toDouble() / size.toDouble();
31 32 33 34 35 36 37 38
      ppy[i] = math.atan(f) * stretch / math.PI;
      ppx[i] = stretch * 0.5 - ppy[i];
      pny[i] = -ppy[i];
      pnx[i] = ppy[i] - stretch * 0.5;
      npy[i] = stretch - ppy[i];
      npx[i] = ppy[i] + stretch * 0.5;
      nny[i] = ppy[i] - stretch;
      nnx[i] = -stretch * 0.5 - ppy[i];
39 40 41
    }
  }

42 43
  static const int size = 1024;
  static const double stretch = math.PI;
44

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
  static const int ezis = -size;

  final Float64List ppy = new Float64List(size + 1);
  final Float64List ppx = new Float64List(size + 1);
  final Float64List pny = new Float64List(size + 1);
  final Float64List pnx = new Float64List(size + 1);
  final Float64List npy = new Float64List(size + 1);
  final Float64List npx = new Float64List(size + 1);
  final Float64List nny = new Float64List(size + 1);
  final Float64List nnx = new Float64List(size + 1);
}

class GameMath {
  static final _Atan2Constants _atan2 = new _Atan2Constants();

  static double atan2(double y, double x) {
61 62 63
    if (x >= 0) {
      if (y >= 0) {
        if (x >= y)
64
          return _atan2.ppy[(_Atan2Constants.size * y / x + 0.5).toInt()];
65
        else
66
          return _atan2.ppx[(_Atan2Constants.size * x / y + 0.5).toInt()];
67 68
      } else {
        if (x >= -y)
69
          return _atan2.pny[(_Atan2Constants.ezis * y / x + 0.5).toInt()];
70
        else
71
          return _atan2.pnx[(_Atan2Constants.ezis * x / y + 0.5).toInt()];
72 73 74 75
      }
    } else {
      if (y >= 0) {
        if (-x >= y)
76
          return _atan2.npy[(_Atan2Constants.ezis * y / x + 0.5).toInt()];
77
        else
78
          return _atan2.npx[(_Atan2Constants.ezis * x / y + 0.5).toInt()];
79 80
      } else {
        if (x <= y)
81
          return _atan2.nny[(_Atan2Constants.size * y / x + 0.5).toInt()];
82
        else
83
          return _atan2.nnx[(_Atan2Constants.size * x / y + 0.5).toInt()];
84 85 86
      }
    }
  }
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

  static double pointQuickDist(Point a, Point b) {
    double dx = a.x - b.x;
    double dy = a.y - b.y;
    if (dx < 0.0) dx = -dx;
    if (dy < 0.0) dy = -dy;
    if (dx > dy) {
      return dx + dy/2.0;
    }
    else {
      return dy + dx/2.0;
    }
  }

  static double filter (double a, double b, double filterFactor) {
      return (a * (1-filterFactor)) + b * filterFactor;
  }
104
}