Doors, lifts and buttons: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: live (line 47), press-live (line 120), world (line 152), wire (line 155), press (line 177).
1/// Doors, lifts and buttons: one machine that travels between two places, and
2/// a switch that relays an activation to it by name.
3///
4/// Quoted by `level_mechanisms.md` and shown whole in the Source tab.
5library;
7import 'package:flutter3d/flutter3d.dart';
8import 'package:flutter3d_physics/flutter3d_physics.dart';
9import 'package:flutter3d_showcase/src/demo/demo.dart';
10import 'package:flutter3d_showcase/src/demo/run_physics.dart';
11import 'package:flutter3d_showcase/src/demo/scene_kit.dart';
12import 'package:flutter3d_sim/flutter3d_sim.dart';
14/// The `level-mechanisms` page: a door and the button that opens it by name.
15/// Its check is that the door starts closed and opens within three seconds
16/// of a press.
17final class LevelMechanismsDemo extends ShowcaseDemo {
18 late final String _report;
20 /// Whether the page presses the button itself, every eight seconds after the
21 /// first two.
22 bool automatic = true;
23 bool _pressAsked = false;
25 late final MechanismWorld _mechanisms;
26 late final Door _door;
27 late final Collider _doorCollider;
28 late final MeshNode _doorNode;
29 late final MeshNode _buttonNode;
30 late final BarGauge _progress;
31 double _clock = 0.0;
32 double _lit = 0.0;
35 void configureView(DemoContext context) {
36 context.orbit
37 ..distance = 9.0
38 ..pitch = 0.3
39 ..yaw = 0.4;
40 context.orbit.target.setValues(2.5, 1.2, 0.0);
41 }
44 Scene build(DemoContext context) {
45 _report = _run();
47 // The same door and button, running: the door's collider is what the
48 // mechanism moves, and the picture follows it.
49 final CollisionWorld world = onRunPhysics(CollisionWorld());
50 _mechanisms = MechanismWorld(world);
51 _doorCollider = world.addBox(Vector3(5, 1, 0), Vector3(1, 2, 0.2));
52 _door = _mechanisms.add(
53 Door(
54 name: 'north-door',
55 collider: _doorCollider,
56 travel: Vector3(0, 2, 0),
57 ),
58 );
59 _mechanisms.add(
60 Button(
61 name: 'door-button',
62 target: 'north-door',
63 collider: world.addBox(Vector3(0, 1, 0), Vector3(0.3, 0.3, 0.3)),
64 ),
65 );
67 _doorNode = blockNode(
68 context,
69 'door',
70 Vector3(1.0, 2.0, 0.2),
71 Vector4(0.65, 0.42, 0.3, 1.0),
72 at: Vector3(5.0, 1.0, 0.0),
73 );
74 _buttonNode = blockNode(
75 context,
76 'button',
77 Vector3(0.3, 0.3, 0.3),
78 Vector4(0.8, 0.2, 0.2, 1.0),
79 at: Vector3(0.0, 1.0, 0.0),
80 );
81 _progress = BarGauge(
82 context,
83 'progress',
84 Vector4(0.9, 0.75, 0.3, 1.0),
85 Vector3(7.0, 0.0, 0.0),
86 height: 2.0,
87 vertical: true,
88 );
89 return sceneOf(<SceneNode>[
90 floorNode(context, width: 12.0, depth: 6.0),
91 // The frame the door slides up out of, and the wall beside it.
92 blockNode(
93 context,
94 'wall left',
95 Vector3(4.5, 3.0, 0.4),
96 Vector4(0.5, 0.5, 0.52, 1.0),
97 at: Vector3(2.75, 1.5, 0.0),
98 ),
99 blockNode(
100 context,
101 'wall right',
102 Vector3(1.5, 3.0, 0.4),
103 Vector4(0.5, 0.5, 0.52, 1.0),
104 at: Vector3(5.75 + 0.25, 1.5, 0.0),
105 ),
106 _doorNode,
107 _buttonNode,
108 ..._progress.nodes,
109 ]);
110 }
113 void update(DemoContext context, double dt) {
114 _clock += dt;
115 if (automatic && _clock > 2.0 && (_clock - 2.0) % 8.0 < dt) {
116 _pressAsked = true;
117 }
118 if (_pressAsked) {
119 _pressAsked = false;
120 _mechanisms.activate('door-button', const Activation());
121 _lit = 1.0;
122 }
123 _mechanisms.step(dt);
124 _lit = (_lit - dt * 1.5).clamp(0.0, 1.0);
125 _doorNode.setPositionFrom(_doorCollider.position);
126 _progress.set(_door.progress);
127 _buttonNode.material.baseColor = LinearColor.fromSrgb(
128 0.8 - 0.6 * _lit,
129 0.2 + 0.65 * _lit,
130 0.2,
131 1.0,
132 );
133 }
136 List<DemoControl> controls(DemoContext context) => <DemoControl>[
137 ToggleControl(
138 'Press the button',
139 value: () => false,
140 onChanged: (bool v) {
141 if (v) _pressAsked = true;
142 },
143 ),
144 ToggleControl(
145 'Press it for me',
146 value: () => automatic,
147 onChanged: (bool v) => automatic = v,
148 ),
149 ];
151 static String _run() {
152 final world = onRunPhysics(CollisionWorld());
153 final mechanisms = MechanismWorld(world);
155 final doorCollider = world.addBox(Vector3(5, 1, 0), Vector3(1, 2, 0.2));
156 final door = mechanisms.add(
157 Door(
158 name: 'north-door',
159 collider: doorCollider,
160 travel: Vector3(0, 2, 0),
161 ),
162 );
163 final buttonCollider = world.addBox(
164 Vector3(0, 1, 0),
165 Vector3(0.3, 0.3, 0.3),
166 );
167 mechanisms.add(
168 Button(
169 name: 'door-button',
170 target: 'north-door',
171 collider: buttonCollider,
172 ),
173 );
175 final beforeProgress = door.progress;
177 mechanisms.activate('door-button', const Activation());
178 for (var i = 0; i < 180; i++) {
179 mechanisms.step(1 / 60);
180 }
182 return 'the door started at progress ${beforeProgress.toStringAsFixed(2)}\n'
183 'after pressing the button and stepping three seconds, it is at '
184 '${door.progress.toStringAsFixed(2)}, state ${door.state.name}';
185 }
188 void verify(Scene scene, FrameResult frame) {
189 if (frame.drawCalls < 1) {
190 throw StateError('the door marker was not drawn');
191 }
192 if (!_report.contains('started at progress 0.00')) {
193 throw StateError('a door should start closed');
194 }
195 if (!_report.contains('is at 1.00, state open')) {
196 throw StateError(
197 'pressing the button should open the door within '
198 'three seconds',
199 );
200 }
201 }
202}