feat(m3a): playable flame client with camera, hud and device perf gate

This commit is contained in:
2026-10-24 22:54:29 +02:00
parent e77595eaf0
commit a30784479b
15 changed files with 1238 additions and 144 deletions
+187 -99
View File
@@ -1,47 +1,7 @@
import 'dart:ffi';
import 'package:flame/game.dart';
import 'package:flutter/material.dart';
/// Expected hash for seed 42, recorded on x86_64 host.
/// Must match on arm64 (cross-target determinism, ADR 0002).
const hostHash = '6237310299140737418';
typedef _SelftestC = Uint64 Function(Uint64);
typedef _SelftestDart = int Function(int);
typedef _MarkerC = Uint32 Function();
typedef _MarkerDart = int Function();
typedef _VersionC = Uint32 Function();
typedef _VersionDart = int Function();
class Probe {
final String status;
final String marker;
final String version;
final String hash;
const Probe(this.status, this.marker, this.version, this.hash);
static Probe run() {
try {
final lib = DynamicLibrary.open('libffi.so');
final selftest =
lib.lookupFunction<_SelftestC, _SelftestDart>('iron_arc_selftest');
final marker =
lib.lookupFunction<_MarkerC, _MarkerDart>('iron_arc_abi_marker');
final version = lib.lookupFunction<_VersionC, _VersionDart>(
'iron_arc_sim_version_code');
return Probe(
'RUST OK',
marker().toRadixString(16).padLeft(8, '0'),
(version() / 100).toStringAsFixed(2),
BigInt.from(selftest(42)).toUnsigned(64).toString(),
);
} catch (e) {
return Probe('RUST FAIL', '-', '-', '$e');
}
}
}
import 'src/game/iron_arc_game.dart';
void main() => runApp(const IronArcApp());
@@ -50,80 +10,208 @@ class IronArcApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
final probe = Probe.run();
final matches = probe.hash == hostHash;
return MaterialApp(
title: 'Iron Arc',
debugShowCheckedModeBanner: false,
theme: ThemeData.dark(useMaterial3: true),
home: Scaffold(
backgroundColor: const Color(0xFF101418),
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(24),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text('IRON ARC',
style: TextStyle(
fontSize: 34,
fontWeight: FontWeight.w800,
letterSpacing: 4)),
const SizedBox(height: 4),
const Text('M0 pipeline probe',
style: TextStyle(color: Colors.white54)),
const SizedBox(height: 32),
_Row('FFI status', probe.status),
_Row('ABI marker', probe.marker),
_Row('sim version', probe.version),
_Row('device hash', probe.hash),
_Row('host hash', hostHash),
const SizedBox(height: 24),
Container(
padding: const EdgeInsets.symmetric(
horizontal: 16, vertical: 10),
decoration: BoxDecoration(
color: matches ? Colors.green.shade900 : Colors.red.shade900,
borderRadius: BorderRadius.circular(8),
),
child: Text(
matches
? 'DETERMINISM PASS (arm64 == x86_64)'
: 'DETERMINISM MISMATCH',
style: const TextStyle(fontWeight: FontWeight.bold),
),
),
],
home: const MatchScreen(),
);
}
}
/// One hotseat match; restart creates a new game with a new seed.
class MatchScreen extends StatefulWidget {
const MatchScreen({super.key});
@override
State<MatchScreen> createState() => _MatchScreenState();
}
class _MatchScreenState extends State<MatchScreen> {
int _seed = 42;
late IronArcGame _game = IronArcGame(seed: _seed);
void _restart() {
setState(() {
_seed += 1;
_game = IronArcGame(seed: _seed);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xFF05070A),
body: SafeArea(
child: Stack(
children: [
Positioned.fill(child: GameWidget(game: _game)),
Positioned.fill(
child: GestureDetector(
behavior: HitTestBehavior.translucent,
onPanStart: (d) => _game.beginAim(d.localPosition),
onPanUpdate: (d) => _game.updateAim(d.localPosition),
onPanEnd: (_) => _game.releaseAim(),
),
),
),
Positioned(
top: 8,
left: 12,
right: 12,
child: ValueListenableBuilder<HudState>(
valueListenable: _game.hud,
builder: (context, hud, _) => _Hud(hud: hud),
),
),
Positioned(
bottom: 16,
left: 16,
right: 16,
child: ValueListenableBuilder<HudState>(
valueListenable: _game.hud,
builder: (context, hud, _) => hud.isFinished
? _GameOver(hud: hud, onRestart: _restart)
: _Hint(hud: hud),
),
),
],
),
),
);
}
}
class _Row extends StatelessWidget {
const _Row(this.label, this.value);
final String label;
final String value;
class _Hud extends StatelessWidget {
const _Hud({required this.hud});
final HudState hud;
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 6),
child: Row(
return Row(
children: [
_Pill(
label: 'P1',
value: '${hud.hp0} HP',
color: const Color(0xFFFF8A3D),
active: hud.current == 0 && !hud.isFinished,
),
const SizedBox(width: 8),
Expanded(
child: Column(
children: [
Text('Zug ${hud.turn}',
style: const TextStyle(
fontSize: 13, fontWeight: FontWeight.w700)),
const SizedBox(height: 2),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(Icons.air, size: 14),
const SizedBox(width: 4),
Text('${hud.wind > 0 ? '+' : ''}${hud.wind}',
style: const TextStyle(fontSize: 13)),
],
),
],
),
),
const SizedBox(width: 8),
_Pill(
label: 'P2',
value: '${hud.hp1} HP',
color: const Color(0xFF4DD0E1),
active: hud.current == 1 && !hud.isFinished,
),
],
);
}
}
class _Pill extends StatelessWidget {
const _Pill({
required this.label,
required this.value,
required this.color,
required this.active,
});
final String label;
final String value;
final Color color;
final bool active;
@override
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
decoration: BoxDecoration(
color: color.withValues(alpha: active ? 0.28 : 0.10),
borderRadius: BorderRadius.circular(10),
border: Border.all(
color: color.withValues(alpha: active ? 0.9 : 0.3),
width: active ? 2 : 1,
),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
width: 120,
child: Text(label,
style: const TextStyle(color: Colors.white60, fontSize: 13))),
Expanded(
child: Text(value,
style: const TextStyle(fontFamily: 'monospace', fontSize: 13)),
),
Text(label,
style: TextStyle(
fontSize: 11,
color: color,
fontWeight: FontWeight.w800)),
Text(value, style: const TextStyle(fontSize: 15)),
],
),
);
}
}
class _Hint extends StatelessWidget {
const _Hint({required this.hud});
final HudState hud;
@override
Widget build(BuildContext context) {
final text = hud.phase == 1
? 'Projektil im Flug…'
: 'Ziehen zum Zielen · Loslassen zum Schießen';
return Center(
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 8),
decoration: BoxDecoration(
color: const Color(0xCC0B1220),
borderRadius: BorderRadius.circular(20),
),
child: Text(text, style: const TextStyle(fontSize: 13)),
),
);
}
}
class _GameOver extends StatelessWidget {
const _GameOver({required this.hud, required this.onRestart});
final HudState hud;
final VoidCallback onRestart;
@override
Widget build(BuildContext context) {
final text = switch (hud.winner) {
0 => 'Spieler 1 gewinnt',
1 => 'Spieler 2 gewinnt',
_ => 'Unentschieden',
};
return Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(text,
style: const TextStyle(fontSize: 26, fontWeight: FontWeight.w800)),
const SizedBox(height: 12),
FilledButton.icon(
onPressed: onRestart,
icon: const Icon(Icons.refresh),
label: const Text('Neuer Match'),
),
],
);
}
}
+405
View File
@@ -0,0 +1,405 @@
import 'dart:math' as math;
import 'dart:ui' as ui;
import 'package:flame/game.dart';
import 'package:flutter/material.dart';
import '../native/native_match.dart';
/// World dimensions in simulation pixels (must match crates/sim).
const double kWorldWidth = 2048;
const double kWorldHeight = 1152;
const int kSamplePx = 2;
/// Immutable HUD snapshot, only replaced when something actually changed.
@immutable
class HudState {
const HudState({
required this.turn,
required this.current,
required this.wind,
required this.hp0,
required this.hp1,
required this.phase,
required this.winner,
required this.aimAngle,
required this.aimPower,
});
final int turn;
final int current;
final int wind;
final int hp0;
final int hp1;
final int phase;
final int winner;
final double aimAngle;
final double aimPower;
bool get isFinished => phase == 2;
@override
bool operator ==(Object other) =>
other is HudState &&
other.turn == turn &&
other.current == current &&
other.wind == wind &&
other.hp0 == hp0 &&
other.hp1 == hp1 &&
other.phase == phase &&
other.winner == winner &&
other.aimAngle == aimAngle &&
other.aimPower == aimPower;
@override
int get hashCode => Object.hash(
turn, current, wind, hp0, hp1, phase, winner, aimAngle, aimPower);
}
/// The playable match. Flame drives the loop; Rust owns the simulation.
class IronArcGame extends FlameGame {
IronArcGame({this.seed = 42});
final int seed;
late NativeMatch _match;
final ValueNotifier<HudState> hud = ValueNotifier<HudState>(
const HudState(
turn: 1,
current: 0,
wind: 0,
hp0: 100,
hp1: 100,
phase: 0,
winner: 254,
aimAngle: 45,
aimPower: 0.6,
),
);
double _accumulator = 0;
double _impactFlash = 0;
Offset? _lastImpact;
/// Aim state in simulation units (degrees / 0..1).
double aimAngle = 45;
double aimPower = 0.6;
bool _dragging = false;
double _zoom = 1;
double _camX = 0;
int _terrainVersion = -1;
Path? _terrainFill;
Path? _terrainLine;
bool get isReady => isLoaded;
@override
Future<void> onLoad() async {
_match = NativeMatch(seed);
_pushHud();
}
@override
void update(double dt) {
super.update(dt);
if (_match.isFlying) {
_accumulator += dt;
const step = 1 / 60;
var steps = 0;
while (_accumulator >= step && steps < 8) {
final impacts = _match.tick(1);
if (impacts > 0) {
final p = _match.lastImpact();
_lastImpact = Offset(p.x.toDouble(), p.y.toDouble());
_impactFlash = 1;
}
_accumulator -= step;
steps++;
}
_pushHud();
} else if (_match.isAiming) {
_accumulator = 0;
}
if (_impactFlash > 0) {
_impactFlash = math.max(0, _impactFlash - dt * 1.8);
}
_updateCamera(dt);
}
void _pushHud() {
final next = HudState(
turn: _match.turn,
current: _match.current,
wind: _match.wind,
hp0: _match.tankHp(0),
hp1: _match.tankHp(1),
phase: _match.phase,
winner: _match.winner,
aimAngle: aimAngle,
aimPower: aimPower,
);
if (hud.value != next) hud.value = next;
}
// ---------------------------------------------------------------- input
/// Starts aiming at the given screen position.
void beginAim(Offset screen) {
if (!_match.isAiming) return;
_dragging = true;
_aimAt(screen);
}
/// Updates the aim while dragging.
void updateAim(Offset screen) {
if (!_dragging) return;
_aimAt(screen);
}
/// Fires if a drag produced a valid shot.
void releaseAim() {
if (!_dragging) return;
_dragging = false;
if (!_match.isAiming) return;
final angleMdeg = (aimAngle * 1000).round().clamp(1000, 89000);
final powerQ16 = (aimPower * 65536).round().clamp(3277, 65536);
_match.fire(angleMdeg: angleMdeg, powerQ16: powerQ16);
_pushHud();
}
void _aimAt(Offset screen) {
final world = screenToWorld(screen);
final player = _match.current;
final tank = Offset(_match.tankX(player).toDouble(),
_match.tankY(player).toDouble() - 14);
final dx = world.dx - tank.dx;
final dy = world.dy - tank.dy;
final distance = math.sqrt(dx * dx + dy * dy);
aimPower = (distance / 250).clamp(0.05, 1.0);
final radians = math.atan2(-dy, dx.abs());
aimAngle = (radians * 180 / math.pi).clamp(1.0, 89.0);
_pushHud();
}
// ------------------------------------------------------------- transform
/// Zoom fits the world height; the camera pans horizontally.
double get _viewWidthWorld => size.x / _zoom;
void _updateCamera(double dt) {
if (size.y <= 0 || size.x <= 0) return;
// Fit height, but never crop the world horizontally more than necessary.
final zoomForHeight = size.y / kWorldHeight;
final zoomForWidth = size.x / kWorldWidth;
_zoom = math.max(zoomForHeight, zoomForWidth);
final viewW = _viewWidthWorld;
final focus = _match.isFlying
? (_match.projectile()?.x.toDouble() ??
_match.tankX(_match.current).toDouble())
: _match.tankX(_match.current).toDouble();
final desiredLeft = (focus - viewW / 2).clamp(0.0, math.max(0, kWorldWidth - viewW));
// Smooth follow; instant on first frame.
final t = _camX == 0 ? 1.0 : (dt * 5).clamp(0.0, 1.0);
_camX += (desiredLeft - _camX) * t;
_camX = _camX.clamp(0.0, math.max(0, kWorldWidth - viewW));
}
/// Screen -> world pixels.
Offset screenToWorld(Offset p) => Offset(p.dx / _zoom + _camX, p.dy / _zoom);
/// World -> screen pixels.
Offset worldToScreen(Offset p) => Offset((p.dx - _camX) * _zoom, p.dy * _zoom);
// ------------------------------------------------------------- rendering
@override
void render(Canvas canvas) {
super.render(canvas);
if (!isLoaded) return;
canvas.save();
canvas.scale(_zoom);
canvas.translate(-_camX, 0);
_drawSky(canvas);
_drawTerrain(canvas);
_drawTank(canvas, 0);
_drawTank(canvas, 1);
_drawProjectile(canvas);
if (_match.isAiming) _drawAim(canvas);
_drawImpact(canvas);
canvas.restore();
}
void _drawSky(Canvas canvas) {
final rect = Rect.fromLTWH(0, 0, kWorldWidth, kWorldHeight);
final paint = Paint()
..shader = ui.Gradient.linear(
const Offset(0, 0),
const Offset(0, kWorldHeight),
const [Color(0xFF0B1220), Color(0xFF243B53), Color(0xFF7A5C3E)],
const [0.0, 0.55, 1.0],
);
canvas.drawRect(rect, paint);
// Distant hills for depth.
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)
..close();
canvas.drawPath(hills, Paint()..color = const Color(0x33FFFFFF));
}
void _drawTerrain(Canvas canvas) {
final version = _match.terrainVersion;
if (version != _terrainVersion || _terrainFill == null) {
_terrainVersion = version;
final heights = _match.terrain();
final fill = Path()..moveTo(0, heights[0].toDouble());
final line = Path()..moveTo(0, heights[0].toDouble());
for (var i = 1; i < heights.length; i++) {
final x = i * kSamplePx.toDouble();
final y = heights[i].toDouble();
fill.lineTo(x, y);
line.lineTo(x, y);
}
fill
..lineTo(kWorldWidth, kWorldHeight)
..lineTo(0, kWorldHeight)
..close();
_terrainFill = fill;
_terrainLine = line;
}
canvas.drawPath(
_terrainFill!,
Paint()
..shader = ui.Gradient.linear(
const Offset(0, 400),
const Offset(0, kWorldHeight),
const [Color(0xFF6B4A2F), Color(0xFF2E1F16)],
),
);
canvas.drawPath(
_terrainLine!,
Paint()
..color = const Color(0xFF8FD694)
..style = PaintingStyle.stroke
..strokeWidth = 6
..strokeJoin = StrokeJoin.round,
);
}
void _drawTank(Canvas canvas, int player) {
if (!isLoaded) return;
final x = _match.tankX(player).toDouble();
final y = _match.tankY(player).toDouble();
final alive = _match.tankHp(player) > 0;
final color = player == 0 ? const Color(0xFFFF8A3D) : const Color(0xFF4DD0E1);
final body = Paint()..color = alive ? color : color.withValues(alpha: 0.3);
final hull = RRect.fromRectAndRadius(
Rect.fromCenter(center: Offset(x, y - 9), width: 52, height: 18),
const Radius.circular(5),
);
canvas.drawRRect(hull, body);
canvas.drawRRect(
RRect.fromRectAndRadius(
Rect.fromCenter(center: Offset(x, y - 9), width: 26, height: 12),
const Radius.circular(4),
),
Paint()..color = const Color(0x22000000),
);
final dir = player == 0 ? 1.0 : -1.0;
final angle = player == _match.current ? aimAngle : 35.0;
final radians = angle * math.pi / 180;
final muzzle = Offset(
x + dir * math.cos(radians) * 34,
y - 16 - math.sin(radians) * 34,
);
canvas.drawLine(
Offset(x, y - 15),
muzzle,
Paint()
..color = Colors.white
..strokeWidth = 6
..strokeCap = StrokeCap.round,
);
// Health bar.
final hp = _match.tankHp(player).clamp(0, 100) / 100.0;
final barRect = RRect.fromRectAndRadius(
Rect.fromLTWH(x - 30, y - 46, 60, 8),
const Radius.circular(4),
);
canvas.drawRRect(barRect, Paint()..color = const Color(0xAA000000));
canvas.drawRRect(
RRect.fromRectAndRadius(
Rect.fromLTWH(x - 30, y - 46, 60 * hp, 8),
const Radius.circular(4),
),
Paint()..color = hp > 0.5
? const Color(0xFF66BB6A)
: hp > 0.25
? const Color(0xFFFFC107)
: const Color(0xFFE53935),
);
}
void _drawProjectile(Canvas canvas) {
final p = _match.projectile();
if (p == null) return;
final center = Offset(p.x.toDouble(), p.y.toDouble());
canvas.drawCircle(center, 16, Paint()..color = const Color(0x33FFD54F));
canvas.drawCircle(center, 7, Paint()..color = const Color(0xFFFFD54F));
}
void _drawAim(Canvas canvas) {
final player = _match.current;
final dir = player == 0 ? 1.0 : -1.0;
final x = _match.tankX(player).toDouble();
final y = _match.tankY(player).toDouble() - 16;
final radians = aimAngle * math.pi / 180;
final paint = Paint()
..color = const Color(0xCCFFFFFF)
..strokeWidth = 3
..strokeCap = StrokeCap.round;
for (var i = 1; i <= 6; i++) {
final r = 40.0 + i * 26;
final p = Offset(
x + dir * math.cos(radians) * r,
y - math.sin(radians) * r + 0.12 * i * i,
);
canvas.drawCircle(p, 4, paint);
}
canvas.drawRect(
Rect.fromLTWH(x - 40, y - 70, 80 * aimPower, 6),
Paint()..color = const Color(0xCCFFB74D),
);
}
void _drawImpact(Canvas canvas) {
final pos = _lastImpact;
if (pos == null || _impactFlash <= 0) return;
final alpha = (_impactFlash * 200).clamp(0, 200).toInt();
canvas.drawCircle(
pos,
30 + 60 * (1 - _impactFlash),
Paint()..color = Color.fromARGB(alpha, 0xFF, 0xB3, 0x00),
);
}
@override
void onRemove() {
_match.dispose();
super.onRemove();
}
}
+171
View File
@@ -0,0 +1,171 @@
import 'dart:ffi';
import 'dart:typed_data';
import 'package:ffi/ffi.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);
typedef _FreeMatchC = Void Function(Pointer<Void>);
typedef _FreeMatchDart = void Function(Pointer<Void>);
typedef _FireC = Int32 Function(Pointer<Void>, Uint32, Int32, Uint8);
typedef _FireDart = int Function(Pointer<Void>, int, int, int);
typedef _EndTurnC = Int32 Function(Pointer<Void>);
typedef _EndTurnDart = int Function(Pointer<Void>);
typedef _TickC = Uint32 Function(Pointer<Void>, Uint32);
typedef _TickDart = int Function(Pointer<Void>, int);
typedef _PhaseC = Uint8 Function(Pointer<Void>);
typedef _PhaseDart = int Function(Pointer<Void>);
typedef _CurrentC = Uint8 Function(Pointer<Void>);
typedef _CurrentDart = int Function(Pointer<Void>);
typedef _WindC = Int32 Function(Pointer<Void>);
typedef _WindDart = int Function(Pointer<Void>);
typedef _TurnC = Uint32 Function(Pointer<Void>);
typedef _TurnDart = int Function(Pointer<Void>);
typedef _WinnerC = Uint8 Function(Pointer<Void>);
typedef _WinnerDart = int Function(Pointer<Void>);
typedef _TankIntC = Int32 Function(Pointer<Void>, Uint8);
typedef _TankIntDart = int Function(Pointer<Void>, int);
typedef _TerrainPtrC = Pointer<Uint16> Function(Pointer<Void>);
typedef _TerrainPtrDart = Pointer<Uint16> Function(Pointer<Void>);
typedef _TerrainLenC = Uint32 Function();
typedef _TerrainLenDart = int Function();
typedef _TerrainVersionC = Uint32 Function(Pointer<Void>);
typedef _TerrainVersionDart = int Function(Pointer<Void>);
typedef _ProjectileC = Int32 Function(Pointer<Void>, Pointer<Int32>);
typedef _ProjectileDart = int Function(Pointer<Void>, Pointer<Int32>);
typedef _ImpactC = Uint32 Function(Pointer<Void>, Pointer<Int32>);
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 {
NativeMatch(int seed) : _handle = _newMatch(seed);
final Pointer<Void> _handle;
bool _disposed = false;
static final DynamicLibrary _lib = DynamicLibrary.open('libffi.so');
static final _NewMatchDart _newMatch =
_lib.lookupFunction<_NewMatchC, _NewMatchDart>('iron_arc_new_match');
static final _FreeMatchDart _freeMatch =
_lib.lookupFunction<_FreeMatchC, _FreeMatchDart>('iron_arc_free_match');
static final _FireDart _fire =
_lib.lookupFunction<_FireC, _FireDart>('iron_arc_fire');
static final _EndTurnDart _endTurn =
_lib.lookupFunction<_EndTurnC, _EndTurnDart>('iron_arc_end_turn');
static final _TickDart _tick =
_lib.lookupFunction<_TickC, _TickDart>('iron_arc_tick');
static final _PhaseDart _phase =
_lib.lookupFunction<_PhaseC, _PhaseDart>('iron_arc_phase');
static final _CurrentDart _current =
_lib.lookupFunction<_CurrentC, _CurrentDart>('iron_arc_current');
static final _WindDart _wind =
_lib.lookupFunction<_WindC, _WindDart>('iron_arc_wind');
static final _TurnDart _turn =
_lib.lookupFunction<_TurnC, _TurnDart>('iron_arc_turn');
static final _WinnerDart _winner =
_lib.lookupFunction<_WinnerC, _WinnerDart>('iron_arc_winner');
static final _TankIntDart _tankX =
_lib.lookupFunction<_TankIntC, _TankIntDart>('iron_arc_tank_x');
static final _TankIntDart _tankY =
_lib.lookupFunction<_TankIntC, _TankIntDart>('iron_arc_tank_y');
static final _TankIntDart _tankHp =
_lib.lookupFunction<_TankIntC, _TankIntDart>('iron_arc_tank_hp');
static final _TerrainPtrDart _terrainPtr =
_lib.lookupFunction<_TerrainPtrC, _TerrainPtrDart>('iron_arc_terrain_ptr');
static final _TerrainLenDart _terrainLen =
_lib.lookupFunction<_TerrainLenC, _TerrainLenDart>('iron_arc_terrain_len');
static final _TerrainVersionDart _terrainVersion = _lib
.lookupFunction<_TerrainVersionC, _TerrainVersionDart>(
'iron_arc_terrain_version');
static final _ProjectileDart _projectile =
_lib.lookupFunction<_ProjectileC, _ProjectileDart>('iron_arc_projectile');
static final _ImpactDart _lastImpact =
_lib.lookupFunction<_ImpactC, _ImpactDart>('iron_arc_last_impact');
/// Phase: 0 aiming, 1 flying, 2 finished.
int get phase => _phase(_handle);
bool get isAiming => phase == 0;
bool get isFlying => phase == 1;
bool get isFinished => phase == 2;
/// Active player index.
int get current => _current(_handle);
/// Wind in units -8..8.
int get wind => _wind(_handle);
/// Turn number starting at 1.
int get turn => _turn(_handle);
/// 0/1 winner, 255 draw, 254 still running.
int get winner => _winner(_handle);
int tankX(int player) => _tankX(_handle, player);
int tankY(int player) => _tankY(_handle, player);
int tankHp(int player) => _tankHp(_handle, player);
/// Fires a shot. Returns 0 on success.
int fire({required int angleMdeg, required int powerQ16, int weapon = 1}) =>
_fire(_handle, angleMdeg, powerQ16, weapon);
int endTurn() => _endTurn(_handle);
/// Advances the simulation, returns the number of impacts.
int tick(int ticks) => _tick(_handle, ticks);
/// Bumped whenever the terrain changed.
int get terrainVersion => _terrainVersion(_handle);
/// Live view into the Rust heightmap (valid until [dispose]).
Uint16List terrain() =>
_terrainPtr(_handle).asTypedList(_terrainLen());
/// Projectile position in world pixels, or null if none is in flight.
({int x, int y})? projectile() {
final out = calloc<Int32>(2);
try {
return _projectile(_handle, out) == 1
? (x: out[0], y: out[1])
: null;
} finally {
calloc.free(out);
}
}
/// World position of the most recent impact.
({int x, int y}) lastImpact() {
final out = calloc<Int32>(2);
try {
_lastImpact(_handle, out);
return (x: out[0], y: out[1]);
} finally {
calloc.free(out);
}
}
void dispose() {
if (_disposed) return;
_disposed = true;
_freeMatch(_handle);
}
}