2021-09-16 21:15:32 +00:00
|
|
|
import 'dart:math';
|
|
|
|
|
2021-10-09 03:59:38 +00:00
|
|
|
import 'package:firo_runner/moving_objects/moving_object.dart';
|
2021-09-16 21:15:32 +00:00
|
|
|
|
|
|
|
import 'package:firo_runner/main.dart';
|
2021-10-05 23:19:58 +00:00
|
|
|
import 'package:flame/components.dart';
|
2021-09-16 21:15:32 +00:00
|
|
|
import 'package:flame/extensions.dart';
|
2021-10-05 23:19:58 +00:00
|
|
|
import 'package:flame/flame.dart';
|
2021-09-16 21:15:32 +00:00
|
|
|
|
|
|
|
class Holder {
|
|
|
|
Random random = Random();
|
|
|
|
|
|
|
|
late List<List<MovingObject>> objects = [];
|
|
|
|
|
2021-10-09 18:32:15 +00:00
|
|
|
// Load method to be overridden by classes that extend this.
|
2021-09-16 21:15:32 +00:00
|
|
|
Future load() async {}
|
|
|
|
|
2021-10-09 18:32:15 +00:00
|
|
|
// Basic method to reset the state of the holder object.
|
2021-09-16 21:15:32 +00:00
|
|
|
void setUp() {
|
|
|
|
for (int i = 0; i < objects.length; i++) {
|
|
|
|
for (int j = 0; j < objects[i].length; j++) {
|
|
|
|
remove(objects[i], j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
objects = [];
|
|
|
|
for (int i = 0; i < 9; i++) {
|
|
|
|
objects.add([]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-09 18:32:15 +00:00
|
|
|
// Get the total amount of objects currently in the logical game.
|
2021-09-16 21:15:32 +00:00
|
|
|
int total() {
|
|
|
|
int total = 0;
|
|
|
|
for (List<MovingObject> levelObjects in objects) {
|
|
|
|
total += levelObjects.length;
|
|
|
|
}
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2021-10-09 18:32:15 +00:00
|
|
|
// Update every object that this holder holds.
|
2021-09-16 21:15:32 +00:00
|
|
|
void update(double dt) {
|
|
|
|
for (List<MovingObject> objectLevel in objects) {
|
|
|
|
for (MovingObject p in objectLevel) {
|
|
|
|
p.update(dt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-09 18:32:15 +00:00
|
|
|
// Remove and object from this holder.
|
2021-09-16 21:15:32 +00:00
|
|
|
void remove(List<MovingObject> levelHolder, int j) {
|
|
|
|
levelHolder[j].remove();
|
|
|
|
levelHolder.removeAt(j);
|
|
|
|
}
|
|
|
|
|
2021-10-09 18:32:15 +00:00
|
|
|
// Remove any object is past rendering distance.
|
2021-09-16 21:15:32 +00:00
|
|
|
void removePast(MyGame gameRef) {
|
|
|
|
for (List<MovingObject> objectLevel in objects) {
|
|
|
|
for (int i = 0; i < objectLevel.length;) {
|
|
|
|
if (objectLevel[i].sprite.x + objectLevel[i].sprite.width < 0) {
|
|
|
|
remove(objectLevel, i);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-09 18:32:15 +00:00
|
|
|
// Resize this object for screen rotations or changing window size.
|
2021-09-16 21:15:32 +00:00
|
|
|
void resize(Vector2 newSize, double xRatio, double yRatio) {
|
|
|
|
for (List<MovingObject> platformLevel in objects) {
|
|
|
|
for (MovingObject p in platformLevel) {
|
|
|
|
p.resize(newSize, xRatio, yRatio);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-05 23:19:58 +00:00
|
|
|
|
2021-10-09 18:32:15 +00:00
|
|
|
// Load sprites dynamically.
|
2021-10-05 23:19:58 +00:00
|
|
|
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;
|
|
|
|
}
|
2021-09-16 21:15:32 +00:00
|
|
|
}
|