firo_runner/lib/firework.dart

138 lines
3.6 KiB
Dart
Raw Normal View History

import 'dart:math';
import 'package:firo_runner/main.dart';
import 'package:flame/components.dart';
import 'package:flame/extensions.dart';
import 'package:flame/flame.dart';
import 'package:flame_audio/flame_audio.dart';
enum FireworkState { normal }
2021-10-09 12:32:15 -06:00
// Class that shoots off fireworks whenever the game speeds up.
class Firework extends Component {
MyGame gameRef;
late SpriteAnimationGroupComponent sprite1;
late SpriteAnimationGroupComponent sprite2;
Firework(this.gameRef);
double timeSinceFirework = 0;
late Random random;
String message = "";
List<String> messages = [
"Speed Up!",
"Speed Up!",
"Speed Up!",
"Speed Up!",
"Speed Up!",
"Speed Up!",
"Speed Up!",
"Speed Up!",
"Speed Up!",
"Speed Up!",
"Speed Up!",
"Speed Up!",
"Speed Up!",
];
Future load() async {
random = Random();
List<Sprite> firework = await loadListSprites("firework", "firework", 10);
SpriteAnimation normal =
SpriteAnimation.spriteList(firework, stepTime: 0.25, loop: false);
sprite1 = SpriteAnimationGroupComponent(
animations: {
FireworkState.normal: normal,
},
current: FireworkState.normal,
);
2024-05-24 18:14:17 +03:00
sprite1.priority = FIREWORK_PRIORITY;
sprite1.update(100);
2024-05-24 18:14:17 +03:00
sprite1.size = Vector2(gameRef.canvasSize.y, gameRef.canvasSize.y);
sprite1.position = Vector2(0, 0);
sprite2 = SpriteAnimationGroupComponent(
animations: {
FireworkState.normal: normal,
},
current: FireworkState.normal,
);
2024-05-24 18:14:17 +03:00
sprite2.priority = FIREWORK_PRIORITY;
2024-05-24 18:14:17 +03:00
sprite2.size = Vector2(gameRef.canvasSize.y, gameRef.canvasSize.y);
sprite2.position = Vector2(gameRef.canvasSize.x - sprite2.size.x, 0);
sprite2.update(100);
}
void setUp() {
message = "";
timeSinceFirework = 0;
gameRef.add(sprite1);
gameRef.add(sprite2);
}
@override
void update(double dt) {
2024-05-24 18:14:17 +03:00
if (!(sprite1.animationTicker?.done() ?? false)) {
timeSinceFirework = 0;
} else {
timeSinceFirework += dt;
}
sprite1.update(dt);
sprite2.update(dt);
}
void renderText(Canvas canvas) {
sprite1.render(canvas);
sprite1.render(canvas);
2024-05-24 18:14:17 +03:00
final textPainter = gameRef.fireworksPaint.toTextPainter(message);
textPainter.layout();
final textWidth = textPainter.width;
final textHeight = textPainter.height;
if ((sprite1.animationTicker?.done() ?? false) &&
timeSinceFirework < 1 &&
message != "") {
gameRef.fireworksPaint.render(
canvas,
message,
2024-05-24 18:14:17 +03:00
Vector2(gameRef.size.x / 2 - textWidth / 2,
gameRef.size.y / 9 - textHeight / 2),
);
}
}
void reset() {
message = messages.elementAt(random.nextInt(messages.length));
2024-05-24 18:14:17 +03:00
sprite1.animationTicker!.reset();
sprite2.animationTicker!.reset();
FlameAudio.play("sfx/fireworks.mp3", volume: 0.75);
}
void resize(Vector2 newSize, double xRatio, double yRatio) {
sprite1.x *= xRatio;
sprite1.y *= yRatio;
sprite1.width *= xRatio;
sprite1.height *= yRatio;
sprite2.x *= xRatio;
sprite2.y *= yRatio;
sprite2.width *= xRatio;
sprite2.height *= yRatio;
}
Future<List<Sprite>> loadListSprites(
String folderName, String extraName, int howManyFrames) async {
List<Sprite> sprites = [];
for (int i = 0; i < howManyFrames; i++) {
sprites.add(Sprite(
await Flame.images.load('$folderName/${extraName}_$i.png'),
));
}
return sprites;
}
}