mirror of
https://github.com/johrpan/christmas_cats.git
synced 2025-10-28 19:27:25 +01:00
Version 0.1.0
This commit is contained in:
commit
5dda59a6cd
73 changed files with 5741 additions and 0 deletions
65
lib/components/progress.dart
Normal file
65
lib/components/progress.dart
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
import 'dart:ui';
|
||||
|
||||
import 'package:flame/anchor.dart';
|
||||
import 'package:flame/components/component.dart';
|
||||
import 'package:flame/components/mixins/resizable.dart';
|
||||
import 'package:flame/position.dart';
|
||||
import 'package:flame/text_config.dart';
|
||||
import 'package:sprintf/sprintf.dart';
|
||||
|
||||
import '../localizations.dart';
|
||||
|
||||
class Progress extends Component with Resizable {
|
||||
static const height = 2.0;
|
||||
static const color = Color(0xff000000);
|
||||
|
||||
static const subtileTextConfig = TextConfig(
|
||||
fontSize: 28.0,
|
||||
fontFamily: 'Tangerine',
|
||||
textAlign: TextAlign.center,
|
||||
);
|
||||
|
||||
final ChristmasCatsLocalizations localizations;
|
||||
final void Function() onComplete;
|
||||
|
||||
int rounds = 0;
|
||||
double progress = 0.0;
|
||||
double seconds = 0.0;
|
||||
bool playing = false;
|
||||
bool completed = false;
|
||||
|
||||
double get endSeconds => 20.0 + rounds * 10.0;
|
||||
int get score => (2 * (rounds * endSeconds + seconds)).floor();
|
||||
|
||||
Progress(this.localizations, this.onComplete);
|
||||
|
||||
@override
|
||||
void render(Canvas canvas) {
|
||||
final paint = Paint()..color = color;
|
||||
final width = (seconds / endSeconds) * size.width;
|
||||
canvas.drawRect(Rect.fromLTWH(0, 0, width, height), paint);
|
||||
subtileTextConfig.render(
|
||||
canvas,
|
||||
sprintf(localizations.score, [score]),
|
||||
Position(size.width / 2, 4),
|
||||
anchor: Anchor.topCenter,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void update(double dt) {
|
||||
if (playing && !completed) {
|
||||
seconds += dt;
|
||||
if (seconds > endSeconds) {
|
||||
completed = true;
|
||||
if (onComplete != null) {
|
||||
rounds++;
|
||||
seconds = 0;
|
||||
onComplete();
|
||||
}
|
||||
}
|
||||
} else if (seconds < endSeconds) {
|
||||
completed = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue