57 lines
1.5 KiB
Dart
57 lines
1.5 KiB
Dart
import 'dart:math' as math;
|
|
|
|
import 'package:flame/components.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
/// حلقهی شمارشِ معکوسِ نوبت: کمانی که در طولِ مهلتِ نوبت خالی میشود.
|
|
/// رنگ از طلایی به قرمز میرود و در ثانیههای پایانی هشدار میدهد.
|
|
class TurnTimer extends PositionComponent {
|
|
final double radius;
|
|
final double durationSeconds;
|
|
double _elapsed = 0;
|
|
|
|
TurnTimer({
|
|
required this.radius,
|
|
required this.durationSeconds,
|
|
required Vector2 position,
|
|
}) : super(
|
|
position: position,
|
|
size: Vector2.all(radius * 2),
|
|
anchor: Anchor.center,
|
|
);
|
|
|
|
static final _bg = Paint()
|
|
..style = PaintingStyle.stroke
|
|
..strokeWidth = 3
|
|
..color = Colors.black54;
|
|
|
|
@override
|
|
void update(double dt) {
|
|
_elapsed += dt;
|
|
}
|
|
|
|
@override
|
|
void render(Canvas canvas) {
|
|
final center = Offset(radius, radius);
|
|
canvas.drawCircle(center, radius, _bg);
|
|
|
|
final frac =
|
|
durationSeconds <= 0 ? 0.0 : (1 - _elapsed / durationSeconds).clamp(0.0, 1.0);
|
|
if (frac <= 0) return;
|
|
|
|
final color = frac > 0.3 ? const Color(0xFFE9B949) : const Color(0xFFE53935);
|
|
final arc = Paint()
|
|
..style = PaintingStyle.stroke
|
|
..strokeWidth = 4
|
|
..strokeCap = StrokeCap.round
|
|
..color = color;
|
|
canvas.drawArc(
|
|
Rect.fromCircle(center: center, radius: radius),
|
|
-math.pi / 2, // شروع از بالا
|
|
2 * math.pi * frac,
|
|
false,
|
|
arc,
|
|
);
|
|
}
|
|
}
|