Files
SDKmuta/lib/utils/target_scoring.dart
T
2026-11-27 14:37:44 +01:00

69 lines
2.6 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'dart:math';
import 'dart:ui';
/// The one scoring rule for a shot, in the 1200 × 1200 space every target
/// design shares (target-designs/v3: centre 600/600, ring 1 at r 510, the
/// black at r 180). Saving, the preview and the target view all use it, so a
/// shot scores the same everywhere and on every design.
///
/// 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 double size = 1200;
static const Offset center = Offset(600, 600);
static const double bulletRadius = 25;
static const double bulletDiameter = 53; // drawn size of a hit
/// Outer edge of ring 10, 9, ... 1, measured from [center].
///
/// A shot touching a ring line counts the higher value, so each edge is the
/// outer edge of its printed line: the line's centre radius plus half its
/// width. Lines in the v3 designs: 5 px white at r 70 and 125 (rings 9, 8),
/// 3 px on the paper at r 235 … 510 (rings 6 … 1). Ring 7 is the edge of
/// the black and 10 the centre dot, with no line of their own.
static const double _paperLine = 3;
static const double _blackLine = 5;
static const List<double> rings = [
15, // 10: centre dot
70 + _blackLine / 2, // 9
125 + _blackLine / 2, // 8
180, // 7: edge of the black
235 + _paperLine / 2, // 6
290 + _paperLine / 2, // 5
345 + _paperLine / 2, // 4
400 + _paperLine / 2, // 3
455 + _paperLine / 2, // 2
510 + _paperLine / 2, // 1
];
/// 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;
}
}