OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 import 'dart:async'; | |
6 import 'generator.dart'; | |
7 import 'mechanics.dart'; | |
8 | |
9 class Simulation { | |
10 Stream<double> get onTick => _stream; | |
11 final System system; | |
12 | |
13 FrameGenerator _generator; | |
14 Stream<double> _stream; | |
15 double _previousTime = 0.0; | |
16 | |
17 Simulation(this.system, {Function terminationCondition, Function onDone}) { | |
18 _generator = new FrameGenerator(onDone: onDone); | |
19 _stream = _generator.onTick.map(_update); | |
20 | |
21 if (terminationCondition != null) { | |
22 bool done = false; | |
23 _stream = _stream.takeWhile((_) { | |
24 if (done) | |
25 return false; | |
26 done = terminationCondition(); | |
27 return true; | |
28 }); | |
29 } | |
30 } | |
31 | |
32 void cancel() { | |
33 _generator.cancel(); | |
34 } | |
35 | |
36 double _update(double timeStamp) { | |
37 double previousTime = _previousTime; | |
38 _previousTime = timeStamp; | |
39 if (previousTime == 0.0) | |
40 return timeStamp; | |
41 double deltaT = timeStamp - previousTime; | |
42 system.update(deltaT); | |
43 return timeStamp; | |
44 } | |
45 } | |
OLD | NEW |