Files
SDKmuta/lib/utils/target_scoring.dart
T
2026-11-28 12:22:02 +01:00

84 lines
3.4 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/v5: 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
// Where a hit's centre may be drawn: the 1200 square, less the dot's
// radius and a little air. Outside ring 1 a miss simply lies on the screen
// background, where it landed
static const double _hitInset = bulletDiameter / 2 + 2; // 28.5
/// [p] if its whole dot is inside the target's square, otherwise the
/// nearest place that is (only for shots that landed far off; still 0).
static Offset shown(Offset p) =>
Offset(p.dx.clamp(_hitInset, size - _hitInset), p.dy.clamp(_hitInset, size - _hitInset));
/// 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. A 0 goes towards the
/// corner nearest to [angle], clear of ring 1's decoration.
static Offset positionFor(int score, double angle) {
final s = score.clamp(0, 10);
if (s == 0) {
// Nearest diagonal, keeping a little of the angle so 0s don't stack
final diagonal = ((angle - pi / 4) / (pi / 2)).round() * (pi / 2) + pi / 4;
final a = diagonal + (angle - diagonal) * 0.3;
return shown(center + Offset(cos(a), sin(a)) * 700);
}
// 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 {
edge = (rings[9 - s] + rings[10 - s]) / 2;
}
final distance = edge + bulletRadius;
return center + Offset(cos(angle), sin(angle)) * distance;
}
}