mnemo_cards/funny_letters/lib/runner/ui/health_bar_v2.dart

50 lines
1.3 KiB
Dart
Raw Normal View History

2025-11-10 23:55:41 +00:00
import 'package:flame/components.dart';
import 'package:flutter/material.dart';
import '../player.dart';
class HealthBarV2 extends PositionComponent {
int health;
final Color backgroundColor;
final Color foregroundColor;
HealthBarV2({
required this.health,
this.foregroundColor = const Color(0xFFE7513D),
this.backgroundColor = const Color(0xFFE7513D),
Vector2? size,
Vector2? position,
}) : super(
size: size,
position: position,
);
void updateHealth(Player player) {
if (player.health != health) {
health = player.health;
}
}
@override
void render(Canvas canvas) {
final paintBackground = Paint()
..color = backgroundColor
..style = PaintingStyle.stroke
..strokeWidth = 2;
final paintForeground = Paint()..color = foregroundColor;
final backgroundRect = Rect.fromLTWH(0, 0, size.x, size.y);
canvas.drawRRect(
RRect.fromRectAndRadius(backgroundRect, Radius.circular(size.y / 2)),
paintBackground,
);
// Draw foreground bar
final foregroundWidth = size.x * (health / 10.0).clamp(0.0, 1.0);
final foregroundRect = Rect.fromLTWH(0, 0, foregroundWidth, size.y);
canvas.drawRRect(
RRect.fromRectAndRadius(foregroundRect, Radius.circular(size.y / 2)),
paintForeground,
);
}
}