Created
July 27, 2020 11:59
-
-
Save mausworks/f37f6773da6aaa7ddb839bc23ba1fdac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
class Game { | |
String id = null; | |
Duration tickDuration = const Duration(microseconds: (1000000 ~/ 60)); | |
DateTime updated = DateTime(0); | |
Timer _timer = null; | |
Game(String this.id); | |
void update(Timer timer) { | |
final now = DateTime.now(); | |
final delta = now.difference(updated); | |
print(delta.inMicroseconds); | |
updated = now; | |
} | |
void start() { | |
print("Game '$id' started"); | |
_timer = Timer.periodic(tickDuration, update); | |
} | |
void stop() { | |
print("Game '$id' ended"); | |
_timer?.cancel(); | |
} | |
} | |
void main() async { | |
var game = Game("test")..start(); | |
await Future.delayed(Duration(seconds: 10)); | |
game.stop(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment