An automap: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: grid (line 39), reveal (line 63), live (line 84), check (line 131).
1/// The level as the player has seen it, from above: floor where they walked,
2/// walls where the floor stopped, nothing where they have not been.
3///
4/// Quoted by `automap.md` and shown whole in the Source tab.
5library;
7import 'package:flutter/widgets.dart';
8import 'package:flutter3d/flutter3d.dart';
9import 'package:flutter3d_showcase/src/demo/demo.dart';
10import 'package:flutter3d_sim/flutter3d_sim.dart';
12/// The `automap` page: a walker revealing a level's map as it goes. Its check
13/// is that a cell it passed near is floor and one nobody went near is not
14/// revealed.
15final class AutomapDemo extends ShowcaseDemo {
16 late final NavGrid _grid;
17 late Automap _map;
19 /// How fast the walker goes, in metres a second, 1 to 10.
20 double speed = 3.0;
21 bool _forgetAsked = false;
22 final Vector3 _walker = Vector3(2.0, 0.0, 2.0);
23 int _leg = 0;
25 /// The walk: room, doorway, corridor, second room, and round again.
26 static final List<Vector3> _route = <Vector3>[
27 Vector3(2.0, 0.0, 2.0),
28 Vector3(8.0, 0.0, 8.0),
29 Vector3(5.0, 0.0, 5.0),
30 Vector3(13.0, 0.0, 5.0),
31 Vector3(20.0, 0.0, 3.0),
32 Vector3(23.0, 0.0, 8.0),
33 Vector3(20.0, 0.0, 5.0),
34 Vector3(13.0, 0.0, 5.0),
35 ];
38 Scene build(DemoContext context) {
39 // Two rooms and a corridor between them.
40 _grid = NavGrid.bake(<Brush>[
41 Brush(center: WorldPosition(5, 0, 5), size: Vector3(10, 1, 10)),
42 Brush(center: WorldPosition(13, 0, 5), size: Vector3(6, 1, 2)),
43 Brush(center: WorldPosition(21, 0, 5), size: Vector3(10, 1, 10)),
44 ], cellSize: 0.5);
45 _map = Automap(_grid, revealRadius: 3.0);
47 final material = RenderMaterial(
48 name: 'walker',
49 baseColor: LinearColor.fromSrgb(0.6, 0.9, 0.7, 1.0),
50 );
51 final node = MeshNode(
52 DeviceMesh.upload(context.device, SphereShape(segments: 16).build()),
53 material,
54 );
55 return Scene()
56 ..add(node)
57 ..add(
58 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
59 ..setLocalForward(Vector3(-0.4, -1.0, -0.3)),
60 );
61 }
63 void _walk() {
64 _map.reveal(Vector3(1.0, 0.0, 1.0));
65 _map.reveal(Vector3(3.0, 0.0, 1.0));
66 }
69 void update(DemoContext context, double dt) {
70 if (_forgetAsked) {
71 _forgetAsked = false;
72 _map = Automap(_grid, revealRadius: 3.0);
73 }
74 // Walk the route at `speed`, a leg at a time.
75 final Vector3 to = _route[(_leg + 1) % _route.length];
76 final Vector3 way = to - _walker;
77 final double step = speed * dt;
78 if (way.length <= step) {
79 _walker.setFrom(to);
80 _leg = (_leg + 1) % _route.length;
81 } else {
82 _walker.add(way.normalized() * step);
83 }
84 // Every frame the player stands somewhere, and the map learns of it.
85 _map.reveal(_walker);
86 }
89 List<DemoControl> controls(DemoContext context) => <DemoControl>[
90 SliderControl(
91 'Walking speed',
92 min: 1.0,
93 max: 10.0,
94 value: () => speed,
95 onChanged: (double v) => speed = v,
96 format: (double v) => '${v.toStringAsFixed(1)} m/s',
97 ),
98 ToggleControl(
99 'Forget the map',
100 value: () => false,
101 onChanged: (bool v) {
102 if (v) _forgetAsked = true;
103 },
104 ),
105 ];
108 Widget? customBody(BuildContext buildContext, DemoContext context) =>
109 ColoredBox(
110 color: const Color(0xFF14161A),
111 child: Padding(
112 padding: const EdgeInsets.all(24),
113 child: Center(
114 child: AspectRatio(
115 aspectRatio: _grid.columns / _grid.rows,
116 child: CustomPaint(
117 painter: _MapPainter(_grid, _map, _walker),
118 child: const SizedBox.expand(),
119 ),
120 ),
121 ),
122 ),
123 );
126 void verify(Scene scene, FrameResult frame) {
127 if (frame.drawCalls < 1) {
128 throw StateError('the walker marker was not drawn');
129 }
130 _walk();
131 final nearWhereWalked = _map.isFloor(_grid.cellAtPoint(2.0, 1.0));
132 final farAcrossTheFloor = _map.isRevealed(_grid.cellAtPoint(9.0, 9.0));
133 if (!nearWhereWalked) {
134 throw StateError(
135 'a cell the walk passed near should be revealed as floor',
136 );
137 }
138 if (farAcrossTheFloor) {
139 throw StateError(
140 'the automap should not reveal floor nobody walked near',
141 );
142 }
143 }
144}
146/// Draws revealed floor light, revealed walls dark, and everything else
147/// blank.
148final class _MapPainter extends CustomPainter {
149 const _MapPainter(this.grid, this.map, this.walker);
151 final NavGrid grid;
152 final Automap map;
153 final Vector3 walker;
156 void paint(Canvas canvas, Size size) {
157 final cellW = size.width / grid.columns;
158 final cellH = size.height / grid.rows;
159 final paint = Paint();
160 for (var index = 0; index < grid.cellCount; index++) {
161 if (!map.isRevealed(index)) continue;
162 final cx = grid.cellX(index);
163 final cz = grid.cellZ(index);
164 paint.color = map.isFloor(index)
165 ? const Color(0xFFB6E3B0)
166 : const Color(0xFF3A2A2A);
167 canvas.drawRect(
168 Rect.fromLTWH(cx * cellW, cz * cellH, cellW, cellH),
169 paint,
170 );
171 }
172 // Where the player is, in the same cells.
173 canvas.drawCircle(
174 Offset(
175 walker.x / grid.cellSize * cellW,
176 walker.z / grid.cellSize * cellH,
177 ),
178 cellW * 1.4,
179 Paint()..color = const Color(0xFFE8A33D),
180 );
181 }
184 bool shouldRepaint(covariant _MapPainter oldDelegate) => true;
185}