Running without a screen: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: run (line 13), loop (line 146), report (line 153).
1/// The interface a tool that plays a game blind is written against: no
2/// window, no renderer, only a step, a save and a sentence about how things
3/// stand.
4///
5/// Quoted by `headless_run.md` and shown whole in the Source tab.
6library;
8import 'package:flutter3d/flutter3d.dart';
9import 'package:flutter3d_showcase/src/demo/demo.dart';
10import 'package:flutter3d_showcase/src/demo/scene_kit.dart';
11import 'package:flutter3d_sim/flutter3d_sim.dart';
13/// A toy game: a walker that moves towards x=10 and stops.
14final class _WalkerRun extends HeadlessRun {
15 final Vector3 _position = Vector3.zero();
18 void step(double dt) {
19 if (outcome != RunOutcome.playing) return;
20 _position.x += 2.0 * dt;
21 if (_position.x >= 10.0) {
22 _position.x = 10.0;
23 outcome = RunOutcome.won;
24 }
25 }
28 Snapshot save() => Snapshot(<String, Object?>{'x': _position.x});
31 RunOutcome outcome = RunOutcome.playing;
34 WorldPosition get position => _position.toWorldPosition();
37 WorldPosition get eye => WorldPosition(_position.x, 1.7, _position.z);
40 void aim(Vector3 out) => out.setValues(1.0, 0.0, 0.0);
43 String get summary =>
44 'walking at x=${_position.x.toStringAsFixed(2)}, ${outcome.name}';
47 Map<String, Object?> get reading => <String, Object?>{
48 'x': _position.x,
49 'outcome': outcome.name,
50 };
51}
53/// The `headless-run` page: a walker driven with no screen. Its check is that
54/// it reaches the outcome its own rules describe.
55final class HeadlessRunDemo extends ShowcaseDemo {
56 late final String _report;
58 bool _againAsked = false;
59 _WalkerRun _live = _WalkerRun();
60 late final MeshNode _walker;
61 late final BarGauge _progress;
62 late final MeshNode _flag;
65 void configureView(DemoContext context) {
66 context.orbit
67 ..distance = 14.0
68 ..pitch = 0.4
69 ..yaw = 0.2;
70 context.orbit.target.setValues(5.0, 0.5, 0.0);
71 }
74 Scene build(DemoContext context) {
75 _report = _drive();
76 _walker = ballNode(context, 'walker', 0.45, Vector4(0.8, 0.65, 0.3, 1.0));
77 _flag = blockNode(
78 context,
79 'goal',
80 Vector3(0.2, 2.0, 0.2),
81 Vector4(0.4, 0.85, 0.45, 1.0),
82 at: Vector3(10.0, 1.0, 0.0),
83 );
84 _progress = BarGauge(
85 context,
86 'progress',
87 Vector4(0.7, 0.45, 0.85, 1.0),
88 Vector3(0.0, 0.0, 2.0),
89 height: 10.0,
90 );
91 return sceneOf(<SceneNode>[
92 blockNode(
93 context,
94 'track',
95 Vector3(13.0, 0.1, 3.0),
96 Vector4(0.34, 0.38, 0.36, 1.0),
97 at: Vector3(5.0, -0.05, 0.0),
98 ),
99 _walker,
100 _flag,
101 ..._progress.nodes,
102 ]);
103 }
106 void update(DemoContext context, double dt) {
107 if (_againAsked) {
108 _againAsked = false;
109 _live = _WalkerRun();
110 }
111 // The same object the blind loop drives, stepped once a frame; when it
112 // has won it waits a moment and starts again.
113 _live.step(dt);
114 if (_live.outcome != RunOutcome.playing && _live.position.x >= 10.0) {
115 _pause += dt;
116 if (_pause > 1.5) {
117 _pause = 0.0;
118 _live = _WalkerRun();
119 }
120 }
121 _walker.setPosition(_live.position.x, 0.45, 0.0);
122 _progress.set(_live.position.x / 10.0);
123 _flag.material.baseColor = LinearColor.fromSrgb(
124 _live.outcome == RunOutcome.won ? 0.4 : 0.85,
125 _live.outcome == RunOutcome.won ? 0.9 : 0.4,
126 0.4,
127 1.0,
128 );
129 }
131 double _pause = 0.0;
134 List<DemoControl> controls(DemoContext context) => <DemoControl>[
135 ToggleControl(
136 'Run it again',
137 value: () => false,
138 onChanged: (bool v) {
139 if (v) _againAsked = true;
140 },
141 ),
142 ];
144 /// What a tool that plays this blind does: step until the run is over.
145 static String _drive() {
146 final run = _WalkerRun();
147 var steps = 0;
148 while (run.outcome == RunOutcome.playing && steps < 1000) {
149 run.step(1 / 60);
150 steps++;
151 }
153 return '${run.summary} after $steps steps\n'
154 'reading: ${run.reading}\n'
155 'save: ${run.save().toJson()}';
156 }
159 void verify(Scene scene, FrameResult frame) {
160 if (frame.drawCalls < 1) {
161 throw StateError('the walker was not drawn');
162 }
163 if (!_report.contains('won')) {
164 throw StateError(
165 'a walker driven with no screen should still reach '
166 'the outcome its own rules describe',
167 );
168 }
169 }
170}