From 4b2288623ebb8bb173000e2436a6971ed16eb59c Mon Sep 17 00:00:00 2001 From: erickzanardo Date: Tue, 25 Jun 2019 12:50:44 -0300 Subject: [PATCH] Implementing Timer and its examples --- doc/examples/timer/.gitignore | 70 ++++++++++++++++++++++++++++++++ doc/examples/timer/.metadata | 10 +++++ doc/examples/timer/README.md | 3 ++ doc/examples/timer/lib/main.dart | 45 ++++++++++++++++++++ doc/examples/timer/pubspec.yaml | 17 ++++++++ lib/time.dart | 27 +++++++++--- 6 files changed, 167 insertions(+), 5 deletions(-) create mode 100644 doc/examples/timer/.gitignore create mode 100644 doc/examples/timer/.metadata create mode 100644 doc/examples/timer/README.md create mode 100644 doc/examples/timer/lib/main.dart create mode 100644 doc/examples/timer/pubspec.yaml diff --git a/doc/examples/timer/.gitignore b/doc/examples/timer/.gitignore new file mode 100644 index 000000000..07488ba61 --- /dev/null +++ b/doc/examples/timer/.gitignore @@ -0,0 +1,70 @@ +# Miscellaneous +*.class +*.log +*.pyc +*.swp +.DS_Store +.atom/ +.buildlog/ +.history +.svn/ + +# IntelliJ related +*.iml +*.ipr +*.iws +.idea/ + +# Visual Studio Code related +.vscode/ + +# Flutter/Dart/Pub related +**/doc/api/ +.dart_tool/ +.flutter-plugins +.packages +.pub-cache/ +.pub/ +/build/ + +# Android related +**/android/**/gradle-wrapper.jar +**/android/.gradle +**/android/captures/ +**/android/gradlew +**/android/gradlew.bat +**/android/local.properties +**/android/**/GeneratedPluginRegistrant.java + +# iOS/XCode related +**/ios/**/*.mode1v3 +**/ios/**/*.mode2v3 +**/ios/**/*.moved-aside +**/ios/**/*.pbxuser +**/ios/**/*.perspectivev3 +**/ios/**/*sync/ +**/ios/**/.sconsign.dblite +**/ios/**/.tags* +**/ios/**/.vagrant/ +**/ios/**/DerivedData/ +**/ios/**/Icon? +**/ios/**/Pods/ +**/ios/**/.symlinks/ +**/ios/**/profile +**/ios/**/xcuserdata +**/ios/.generated/ +**/ios/Flutter/App.framework +**/ios/Flutter/Flutter.framework +**/ios/Flutter/Generated.xcconfig +**/ios/Flutter/app.flx +**/ios/Flutter/app.zip +**/ios/Flutter/flutter_assets/ +**/ios/ServiceDefinitions.json +**/ios/Runner/GeneratedPluginRegistrant.* + +# Exceptions to above rules. +!**/ios/**/default.mode1v3 +!**/ios/**/default.mode2v3 +!**/ios/**/default.pbxuser +!**/ios/**/default.perspectivev3 +!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages diff --git a/doc/examples/timer/.metadata b/doc/examples/timer/.metadata new file mode 100644 index 000000000..8fbc696e8 --- /dev/null +++ b/doc/examples/timer/.metadata @@ -0,0 +1,10 @@ +# This file tracks properties of this Flutter project. +# Used by Flutter tool to assess capabilities and perform upgrades etc. +# +# This file should be version controlled and should not be manually edited. + +version: + revision: 0ba67226ee62d6c9d663a6f8410fb4b2f1076046 + channel: dev + +project_type: app diff --git a/doc/examples/timer/README.md b/doc/examples/timer/README.md new file mode 100644 index 000000000..6322a7330 --- /dev/null +++ b/doc/examples/timer/README.md @@ -0,0 +1,3 @@ +# timer + +Flame game showcasing Timer class diff --git a/doc/examples/timer/lib/main.dart b/doc/examples/timer/lib/main.dart new file mode 100644 index 000000000..77776ce2a --- /dev/null +++ b/doc/examples/timer/lib/main.dart @@ -0,0 +1,45 @@ +import 'package:flutter/gestures.dart'; +import 'package:flutter/material.dart'; +import 'package:flame/flame.dart'; +import 'package:flame/game.dart'; +import 'package:flame/time.dart'; +import 'package:flame/text_config.dart'; +import 'package:flame/position.dart'; + +void main() { + final game = MyGame(); + runApp(game.widget); + + Flame.util.addGestureRecognizer(TapGestureRecognizer() + ..onTapDown = (TapDownDetails evt) { + game.countdown.start(); + }); +} + +class MyGame extends Game { + final TextConfig textConfig = TextConfig(color: const Color(0xFFFFFFFF)); + Timer countdown; + Timer interval; + + int elapsedSecs = 0; + + MyGame() { + countdown = Timer(2); + interval = Timer(1, repeat: true, callback: () { + elapsedSecs += 1; + }); + interval.start(); + } + + @override + void update(double dt) { + countdown.update(dt); + interval.update(dt); + } + + @override + void render(Canvas canvas) { + textConfig.render(canvas, "Countdown: ${countdown.current.toString()}", Position(10, 100)); + textConfig.render(canvas, "Elapsed time: $elapsedSecs", Position(10, 150)); + } +} diff --git a/doc/examples/timer/pubspec.yaml b/doc/examples/timer/pubspec.yaml new file mode 100644 index 000000000..18a7824e7 --- /dev/null +++ b/doc/examples/timer/pubspec.yaml @@ -0,0 +1,17 @@ +name: timer +description: Example app using Timer class + +version: 1.0.0+1 + +environment: + sdk: ">=2.0.0-dev.68.0 <3.0.0" + +dependencies: + flutter: + sdk: flutter + flame: + path: ../../../ + +dev_dependencies: + flutter_test: + sdk: flutter diff --git a/lib/time.dart b/lib/time.dart index 852598e3c..b07fbfcbf 100644 --- a/lib/time.dart +++ b/lib/time.dart @@ -1,19 +1,36 @@ +/// Simple utility class that helps handling time counting and implementing interval like events. +/// class Timer { final double _limit; void Function() _callback; - double _current; + bool _repeat; + double _current = 0; bool _running = false; - Timer(this._limit, this._callback); + Timer(this._limit, { repeat = false, callback }) { + _repeat = repeat; + _callback = callback; + } + + double get current { + return _current; + } void update(double dt) { if (_running) { _current += dt; if (isFinished()) { - _running = false; - _callback(); + if (_repeat) { + _current -= _limit; + } else { + _running = false; + } + + if (_callback != null) { + _callback(); + } } } } @@ -26,7 +43,7 @@ class Timer { _current = 0; _running = true; } - + void stop() { _current = 0; _running = false;