test(m3b): headless golden tests via m3b match source abstraction

This commit is contained in:
2026-10-24 23:02:13 +02:00
parent bf267a97f9
commit e8908ef4bb
8 changed files with 265 additions and 16 deletions
+24 -11
View File
@@ -4,6 +4,7 @@ import 'dart:ui' as ui;
import 'package:flame/game.dart';
import 'package:flutter/material.dart';
import '../native/match_source.dart';
import '../native/native_match.dart';
/// World dimensions in simulation pixels (must match crates/sim).
@@ -58,11 +59,14 @@ class HudState {
/// The playable match. Flame drives the loop; Rust owns the simulation.
class IronArcGame extends FlameGame {
IronArcGame({this.seed = 42});
IronArcGame({this.seed = 42, this.matchFactory});
final int seed;
late NativeMatch _match;
/// Optional override, used by golden tests to run without the native library.
final MatchSource Function()? matchFactory;
late final MatchSource _match;
final ValueNotifier<HudState> hud = ValueNotifier<HudState>(
const HudState(
turn: 1,
@@ -97,7 +101,7 @@ class IronArcGame extends FlameGame {
@override
Future<void> onLoad() async {
_match = NativeMatch(seed);
_match = matchFactory?.call() ?? NativeMatch(seed);
_pushHud();
}
@@ -245,16 +249,25 @@ class IronArcGame extends FlameGame {
);
canvas.drawRect(rect, paint);
// Distant hills for depth.
// Distant hills for depth. They stay well above any playable ground so they
// can never bleed through the destructible terrain.
final hills = Path()
..moveTo(0, 760)
..quadraticBezierTo(360, 620, 720, 750)
..quadraticBezierTo(1120, 880, 1500, 720)
..quadraticBezierTo(1800, 610, kWorldWidth, 740)
..lineTo(kWorldWidth, kWorldHeight)
..lineTo(0, kWorldHeight)
..moveTo(0, 470)
..quadraticBezierTo(300, 360, 620, 450)
..quadraticBezierTo(1000, 540, 1340, 400)
..quadraticBezierTo(1700, 300, kWorldWidth, 440)
..lineTo(kWorldWidth, 560)
..lineTo(0, 560)
..close();
canvas.drawPath(hills, Paint()..color = const Color(0x33FFFFFF));
canvas.drawPath(
hills,
Paint()
..shader = ui.Gradient.linear(
const Offset(0, 330),
const Offset(0, 560),
const [Color(0x2EFFFFFF), Color(0x00FFFFFF)],
),
);
}
void _drawTerrain(Canvas canvas) {
+48
View File
@@ -0,0 +1,48 @@
import 'dart:typed_data';
/// Everything the renderer needs from a match.
///
/// Implemented by [NativeMatch] (real Rust simulation) and by a fake in tests,
/// so the renderer can be verified headlessly (golden tests) without the
/// Android shared library.
abstract class MatchSource {
/// Allows subclasses to inherit the default getters below.
const MatchSource();
/// 0 = aiming, 1 = flying, 2 = finished.
int get phase;
/// Player whose turn it is.
int get current;
/// Wind in units -8..8.
int get wind;
/// Turn number starting at 1.
int get turn;
/// 0/1 winner, 255 draw, 254 running.
int get winner;
bool get isAiming => phase == 0;
bool get isFlying => phase == 1;
bool get isFinished => phase == 2;
int tankX(int player);
int tankY(int player);
int tankHp(int player);
/// Bumped whenever the terrain changed.
int get terrainVersion;
/// Heightmap samples in world pixels.
Uint16List terrain();
({int x, int y})? projectile();
({int x, int y}) lastImpact();
int fire({required int angleMdeg, required int powerQ16, int weapon});
int endTurn();
int tick(int ticks);
void dispose();
}
+19 -5
View File
@@ -3,6 +3,8 @@ import 'dart:typed_data';
import 'package:ffi/ffi.dart';
import 'match_source.dart';
/// Raw C signatures of the Rust match API (see crates/ffi/src/match_api.rs).
typedef _NewMatchC = Pointer<Void> Function(Uint64);
typedef _NewMatchDart = Pointer<Void> Function(int);
@@ -55,7 +57,7 @@ typedef _ImpactDart = int Function(Pointer<Void>, Pointer<Int32>);
/// Thin typed wrapper around the Rust match handle.
///
/// The handle owns the simulation; Dart only pulls snapshots for rendering.
class NativeMatch {
class NativeMatch extends MatchSource {
NativeMatch(int seed) : _handle = _newMatch(seed);
final Pointer<Void> _handle;
@@ -102,45 +104,55 @@ class NativeMatch {
_lib.lookupFunction<_ImpactC, _ImpactDart>('iron_arc_last_impact');
/// Phase: 0 aiming, 1 flying, 2 finished.
@override
int get phase => _phase(_handle);
bool get isAiming => phase == 0;
bool get isFlying => phase == 1;
bool get isFinished => phase == 2;
/// Active player index.
@override
int get current => _current(_handle);
/// Wind in units -8..8.
@override
int get wind => _wind(_handle);
/// Turn number starting at 1.
@override
int get turn => _turn(_handle);
/// 0/1 winner, 255 draw, 254 still running.
@override
int get winner => _winner(_handle);
@override
int tankX(int player) => _tankX(_handle, player);
@override
int tankY(int player) => _tankY(_handle, player);
@override
int tankHp(int player) => _tankHp(_handle, player);
/// Fires a shot. Returns 0 on success.
@override
int fire({required int angleMdeg, required int powerQ16, int weapon = 1}) =>
_fire(_handle, angleMdeg, powerQ16, weapon);
@override
int endTurn() => _endTurn(_handle);
/// Advances the simulation, returns the number of impacts.
@override
int tick(int ticks) => _tick(_handle, ticks);
/// Bumped whenever the terrain changed.
@override
int get terrainVersion => _terrainVersion(_handle);
/// Live view into the Rust heightmap (valid until [dispose]).
@override
Uint16List terrain() =>
_terrainPtr(_handle).asTypedList(_terrainLen());
/// Projectile position in world pixels, or null if none is in flight.
@override
({int x, int y})? projectile() {
final out = calloc<Int32>(2);
try {
@@ -153,6 +165,7 @@ class NativeMatch {
}
/// World position of the most recent impact.
@override
({int x, int y}) lastImpact() {
final out = calloc<Int32>(2);
try {
@@ -163,6 +176,7 @@ class NativeMatch {
}
}
@override
void dispose() {
if (_disposed) return;
_disposed = true;