diff --git a/app/lib/src/game/iron_arc_game.dart b/app/lib/src/game/iron_arc_game.dart index 10ecc0c..1695a54 100644 --- a/app/lib/src/game/iron_arc_game.dart +++ b/app/lib/src/game/iron_arc_game.dart @@ -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 hud = ValueNotifier( const HudState( turn: 1, @@ -97,7 +101,7 @@ class IronArcGame extends FlameGame { @override Future 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) { diff --git a/app/lib/src/native/match_source.dart b/app/lib/src/native/match_source.dart new file mode 100644 index 0000000..34811a2 --- /dev/null +++ b/app/lib/src/native/match_source.dart @@ -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(); +} diff --git a/app/lib/src/native/native_match.dart b/app/lib/src/native/native_match.dart index 08fb878..2dc275d 100644 --- a/app/lib/src/native/native_match.dart +++ b/app/lib/src/native/native_match.dart @@ -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 Function(Uint64); typedef _NewMatchDart = Pointer Function(int); @@ -55,7 +57,7 @@ typedef _ImpactDart = int Function(Pointer, Pointer); /// 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 _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(2); try { @@ -153,6 +165,7 @@ class NativeMatch { } /// World position of the most recent impact. + @override ({int x, int y}) lastImpact() { final out = calloc(2); try { @@ -163,6 +176,7 @@ class NativeMatch { } } + @override void dispose() { if (_disposed) return; _disposed = true; diff --git a/app/test/fake_match.dart b/app/test/fake_match.dart new file mode 100644 index 0000000..4af2fb9 --- /dev/null +++ b/app/test/fake_match.dart @@ -0,0 +1,125 @@ +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? 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 _defaultHeights() { + final heights = []; + 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() {} +} diff --git a/app/test/golden_test.dart b/app/test/golden_test.dart new file mode 100644 index 0000000..e40f885 --- /dev/null +++ b/app/test/golden_test.dart @@ -0,0 +1,49 @@ +import 'package:flame/game.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:iron_arc/src/game/iron_arc_game.dart'; + +import 'fake_match.dart'; + +/// Golden tests render the game headlessly (no device, no native library). +/// Update with: flutter test --update-goldens +Future pumpGame(WidgetTester tester, IronArcGame game) async { + await tester.binding.setSurfaceSize(const Size(420, 840)); + addTearDown(() => tester.binding.setSurfaceSize(null)); + await tester.pumpWidget(MaterialApp( + debugShowCheckedModeBanner: false, + theme: ThemeData.dark(useMaterial3: true), + home: GameWidget(game: game), + )); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 40)); +} + +void main() { + testWidgets('aiming scene renders', (tester) async { + final game = IronArcGame(matchFactory: FakeMatch.aiming); + await pumpGame(tester, game); + await expectLater( + find.byType(MaterialApp), + matchesGoldenFile('goldens/aiming.png'), + ); + }); + + testWidgets('projectile in flight renders', (tester) async { + final game = IronArcGame(matchFactory: FakeMatch.flying); + await pumpGame(tester, game); + await expectLater( + find.byType(MaterialApp), + matchesGoldenFile('goldens/flying.png'), + ); + }); + + testWidgets('crater after impact renders', (tester) async { + final game = IronArcGame(matchFactory: FakeMatch.afterImpact); + await pumpGame(tester, game); + await expectLater( + find.byType(MaterialApp), + matchesGoldenFile('goldens/after_impact.png'), + ); + }); +} diff --git a/app/test/goldens/after_impact.png b/app/test/goldens/after_impact.png new file mode 100644 index 0000000..2731dae Binary files /dev/null and b/app/test/goldens/after_impact.png differ diff --git a/app/test/goldens/aiming.png b/app/test/goldens/aiming.png new file mode 100644 index 0000000..26598cb Binary files /dev/null and b/app/test/goldens/aiming.png differ diff --git a/app/test/goldens/flying.png b/app/test/goldens/flying.png new file mode 100644 index 0000000..9529619 Binary files /dev/null and b/app/test/goldens/flying.png differ