Files
iron-arc/app/test/fake_match.dart
T

126 lines
2.9 KiB
Dart

import 'dart:math' as math;
import 'dart:typed_data';
import 'package:iron_arc/src/native/match_source.dart';
/// Deterministic stand-in for [NativeMatch] used by golden tests.
///
/// Golden tests must run on a plain Dart VM without the Android shared library,
/// so the renderer is fed with a hand-built world instead.
class FakeMatch extends MatchSource {
FakeMatch({
this.phase = 0,
this.current = 0,
this.wind = 3,
this.turn = 1,
this.winner = 254,
this.projectilePos,
this.impactPos,
List<int>? heights,
}) : _heights = Uint16List.fromList(heights ?? _defaultHeights()),
_terrainVersion = heights == null ? 0 : 1;
@override
int phase;
@override
int current;
@override
int wind;
@override
int turn;
@override
int winner;
({int x, int y})? projectilePos;
({int x, int y})? impactPos;
final Uint16List _heights;
int _terrainVersion;
/// Aims scene: player 0 to move, no projectile.
factory FakeMatch.aiming() => FakeMatch();
/// Mid-flight scene: projectile above the middle of the map.
factory FakeMatch.flying() => FakeMatch(
phase: 1,
projectilePos: (x: 1180, y: 300),
);
/// After impact: crater carved, player 1 to move.
factory FakeMatch.afterImpact() {
final heights = _defaultHeights();
// Carve a crater around x = 1500 (sample index 750).
for (var i = 735; i <= 765; i++) {
final dx = (i - 750).abs();
heights[i] = math.min(1152, heights[i] + (30 - dx) * 4);
}
heights[820] = math.min(1152, heights[820] + 25);
return FakeMatch(
phase: 0,
current: 1,
turn: 2,
wind: -2,
heights: heights,
impactPos: (x: 1500, y: 700),
);
}
static List<int> _defaultHeights() {
final heights = <int>[];
for (var i = 0; i < 1024; i++) {
final y = 700 +
(60 * math.sin(i / 47.0)).round() +
(26 * math.sin(i / 13.0 + 1.3)).round();
heights.add(y.clamp(120, 1100));
}
// Level platform under both spawns so tanks stand straight.
for (var i = 100; i <= 120; i++) {
heights[i] = heights[110];
}
for (var i = 900; i <= 920; i++) {
heights[i] = heights[910];
}
return heights;
}
@override
int tankX(int player) => player == 0 ? 220 : 1828;
@override
int tankY(int player) => _heights[tankX(player) ~/ 2];
@override
int tankHp(int player) => player == 0 ? 100 : 62;
@override
int get terrainVersion => _terrainVersion;
@override
Uint16List terrain() => _heights;
@override
({int x, int y})? projectile() => projectilePos;
@override
({int x, int y}) lastImpact() => impactPos ?? (x: 0, y: 0);
@override
int fire({required int angleMdeg, required int powerQ16, int weapon = 1}) {
phase = 1;
return 0;
}
@override
int endTurn() {
current = 1 - current;
turn += 1;
return 0;
}
@override
int tick(int ticks) => 0;
@override
void dispose() {}
}