5f9e2a3b86
BOOK update.
49 lines
1.7 KiB
Dart
49 lines
1.7 KiB
Dart
import 'dart:math';
|
|
import 'dart:ui';
|
|
|
|
/// The one scoring rule for a shot on the default target (assets/target.png,
|
|
/// 1200x1200). Saving, the preview and the target view all use it, so a shot
|
|
/// scores the same everywhere.
|
|
///
|
|
/// A shot counts for the best ring its edge touches: the distance from the
|
|
/// centre to the bullet's centre, minus the bullet's radius, is compared with
|
|
/// the outer edge of each ring.
|
|
class TargetScoring {
|
|
TargetScoring._();
|
|
|
|
static const Offset center = Offset(601, 598);
|
|
static const double bulletRadius = 25;
|
|
|
|
/// Outer edge of ring 10, 9, ... 1, measured from [center].
|
|
static const List<double> rings = [
|
|
14, 70, 125, 180, 235, 290, 345, 400, 455, 510,
|
|
];
|
|
|
|
/// Score of a shot whose centre is at [bullet] (default target coordinates).
|
|
static int scoreAt(Offset bullet) {
|
|
final edge = (bullet - center).distance - bulletRadius;
|
|
for (int i = 0; i < rings.length; i++) {
|
|
if (edge <= rings[i]) return 10 - i;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/// Where to draw a shot that was scored by hand, in direction [angle]
|
|
/// (radians): the middle of the band of positions [scoreAt] gives [score]
|
|
/// for, so the dot always scores what was entered.
|
|
static Offset positionFor(int score, double angle) {
|
|
final s = score.clamp(0, 10);
|
|
// Distance of the bullet's edge from the centre, middle of the band
|
|
final double edge;
|
|
if (s == 10) {
|
|
edge = (-bulletRadius + rings[0]) / 2; // bullet over the centre
|
|
} else if (s == 0) {
|
|
edge = rings.last + 30; // just outside the target
|
|
} else {
|
|
edge = (rings[9 - s] + rings[10 - s]) / 2;
|
|
}
|
|
final distance = edge + bulletRadius;
|
|
return center + Offset(cos(angle), sin(angle)) * distance;
|
|
}
|
|
}
|