diff --git a/lib/game/pinball_game.dart b/lib/game/pinball_game.dart index 1bac998a..55a6b23a 100644 --- a/lib/game/pinball_game.dart +++ b/lib/game/pinball_game.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'package:flame/components.dart'; +import 'package:flame/game.dart'; import 'package:flame/input.dart'; import 'package:flame_bloc/flame_bloc.dart'; import 'package:flame_forge2d/flame_forge2d.dart'; @@ -134,7 +135,7 @@ class _GameBallsController extends ComponentController } } -class DebugPinballGame extends PinballGame with TapDetector { +class DebugPinballGame extends PinballGame with FPSCounter, TapDetector { DebugPinballGame({ required PinballTheme theme, required PinballAudio audio, @@ -149,6 +150,7 @@ class DebugPinballGame extends PinballGame with TapDetector { Future onLoad() async { await super.onLoad(); await _loadBackground(); + await add(_DebugInformation()); } // TODO(alestiago): Move to PinballGame once we have the real background @@ -191,3 +193,35 @@ class _DebugGameBallsController extends _GameBallsController { return noBallsLeft && canBallRespawn; } } + +class _DebugInformation extends Component with HasGameRef { + _DebugInformation() : super(priority: RenderPriority.debugInfo); + + @override + PositionType get positionType => PositionType.widget; + + final _debugTextPaint = TextPaint( + style: const TextStyle( + color: Colors.green, + fontSize: 10, + ), + ); + + final _debugBackgroundPaint = Paint()..color = Colors.white; + + @override + void render(Canvas canvas) { + final debugText = [ + 'FPS: ${gameRef.fps().toStringAsFixed(1)}', + 'BALLS: ${gameRef.descendants().whereType().length}', + ].join(' | '); + + final height = _debugTextPaint.measureTextHeight(debugText); + final position = Vector2(0, gameRef.camera.canvasSize.y - height); + canvas.drawRect( + position & Vector2(gameRef.camera.canvasSize.x, height), + _debugBackgroundPaint, + ); + _debugTextPaint.render(canvas, debugText, position); + } +} diff --git a/packages/pinball_components/lib/src/components/render_priority.dart b/packages/pinball_components/lib/src/components/render_priority.dart index b179cce9..39798379 100644 --- a/packages/pinball_components/lib/src/components/render_priority.dart +++ b/packages/pinball_components/lib/src/components/render_priority.dart @@ -113,4 +113,7 @@ abstract class RenderPriority { // Score Text static const int scoreText = _above + spaceshipRampForegroundRailing; + + // Debug information + static const int debugInfo = _above + scoreText; }