mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-02 03:15:43 +08:00
This adds a platformer tutorial called Ember Quest. I hope I have done a service, because I am tired, lol. I am sure there will be comments. I just want to say, I did my best. I approached this as someone new to Flame, just like I was about 10 months ago. Are there concepts that can be improved, sure. We can always optimize code, but I didn't want any concepts to be super abstract. I had never coded a game before when I began my journey with Flame this year, so things might be a bit simple for experienced game developers, but for myself, I had never even thought about a game loop or animations, etc.
67 lines
1.5 KiB
Dart
67 lines
1.5 KiB
Dart
import 'package:flame/components.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../ember_quest.dart';
|
|
import 'heart.dart';
|
|
|
|
class Hud extends PositionComponent with HasGameRef<EmberQuestGame> {
|
|
Hud({
|
|
super.position,
|
|
super.size,
|
|
super.scale,
|
|
super.angle,
|
|
super.anchor,
|
|
super.children,
|
|
super.priority = 5,
|
|
}) {
|
|
positionType = PositionType.viewport;
|
|
}
|
|
|
|
late TextComponent _scoreTextComponent;
|
|
|
|
@override
|
|
Future<void>? onLoad() async {
|
|
_scoreTextComponent = TextComponent(
|
|
text: '${game.starsCollected}',
|
|
textRenderer: TextPaint(
|
|
style: const TextStyle(
|
|
fontSize: 32,
|
|
color: Color.fromRGBO(10, 10, 10, 1),
|
|
),
|
|
),
|
|
anchor: Anchor.center,
|
|
position: Vector2(game.size.x - 60, 20),
|
|
);
|
|
add(_scoreTextComponent);
|
|
|
|
final starSprite = await game.loadSprite('star.png');
|
|
add(
|
|
SpriteComponent(
|
|
sprite: starSprite,
|
|
position: Vector2(game.size.x - 100, 20),
|
|
size: Vector2.all(32),
|
|
anchor: Anchor.center,
|
|
),
|
|
);
|
|
|
|
for (var i = 1; i <= game.health; i++) {
|
|
final positionX = 40 * i;
|
|
await add(
|
|
HeartHealthComponent(
|
|
heartNumber: i,
|
|
position: Vector2(positionX.toDouble(), 20),
|
|
size: Vector2.all(32),
|
|
),
|
|
);
|
|
}
|
|
|
|
return super.onLoad();
|
|
}
|
|
|
|
@override
|
|
void update(double dt) {
|
|
_scoreTextComponent.text = '${game.starsCollected}';
|
|
super.update(dt);
|
|
}
|
|
}
|