2021-09-03 23:29:18 +00:00
|
|
|
import 'dart:math';
|
|
|
|
|
|
|
|
import 'package:firo_runner/main.dart';
|
|
|
|
import 'package:flame/flame.dart';
|
2021-09-08 21:44:12 +00:00
|
|
|
import 'package:firo_runner/platform.dart';
|
|
|
|
import 'package:flame/extensions.dart';
|
2021-09-03 23:29:18 +00:00
|
|
|
|
|
|
|
class PlatformHolder {
|
2021-09-08 21:44:12 +00:00
|
|
|
late Image l1;
|
|
|
|
late Image l2;
|
|
|
|
late Image m1;
|
|
|
|
late Image m2;
|
|
|
|
late Image r1;
|
|
|
|
late Image r2;
|
|
|
|
late Image o1;
|
|
|
|
late Image o2;
|
2021-09-03 23:29:18 +00:00
|
|
|
late List<List<Platform>> platforms = [];
|
|
|
|
Random random = Random();
|
|
|
|
|
|
|
|
Future loadPlatforms() async {
|
2021-09-08 19:27:06 +00:00
|
|
|
l1 = await Flame.images.load('platform-left-nowire-frames.png');
|
|
|
|
l2 = await Flame.images.load('platform-left-wire-frames.png');
|
|
|
|
m1 = await Flame.images.load('platform-mid-nowire-frames.png');
|
|
|
|
m2 = await Flame.images.load('platform-mid-wire-frames.png');
|
|
|
|
r1 = await Flame.images.load('platform-right-nowire-frames.png');
|
|
|
|
r2 = await Flame.images.load('platform-right-wire-frames.png');
|
|
|
|
o1 = await Flame.images.load('platform-single-nowire-frames.png');
|
|
|
|
o2 = await Flame.images.load('platform-single-wire-frames.png');
|
2021-09-05 22:36:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void setUp() {
|
|
|
|
for (int i = 0; i < platforms.length; i++) {
|
|
|
|
for (int j = 0; j < platforms[i].length; j++) {
|
|
|
|
remove(platforms[i], j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
platforms = [];
|
2021-09-03 23:29:18 +00:00
|
|
|
for (int i = 0; i < 9; i++) {
|
|
|
|
platforms.add([]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool generatePlatform(MyGame gameRef, int level, bool force) {
|
2021-09-04 22:56:08 +00:00
|
|
|
double xCoordinate = 0;
|
2021-09-03 23:29:18 +00:00
|
|
|
if (platforms[level].isNotEmpty) {
|
2021-09-04 22:56:08 +00:00
|
|
|
xCoordinate = platforms[level].last.getRightEnd();
|
2021-09-03 23:29:18 +00:00
|
|
|
}
|
|
|
|
|
2021-09-04 22:56:08 +00:00
|
|
|
if (xCoordinate > gameRef.size.x + 1000) {
|
2021-09-03 23:29:18 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
Platform platform = Platform(gameRef);
|
2021-09-04 22:56:08 +00:00
|
|
|
platform.setPosition(xCoordinate, gameRef.blockSize * level);
|
2021-09-08 19:27:06 +00:00
|
|
|
platform.row = level;
|
2021-09-04 22:56:08 +00:00
|
|
|
gameRef.add(platform.sprite);
|
2021-09-03 23:29:18 +00:00
|
|
|
platforms[level].add(platform);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void update(double dt) {
|
|
|
|
for (List<Platform> platformLevel in platforms) {
|
|
|
|
for (Platform p in platformLevel) {
|
|
|
|
p.update(dt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-05 22:36:36 +00:00
|
|
|
void remove(List<Platform> levelHolder, int j) {
|
|
|
|
levelHolder[j].remove();
|
|
|
|
levelHolder[j].sprite.remove();
|
|
|
|
levelHolder.removeAt(j);
|
|
|
|
}
|
|
|
|
|
2021-09-03 23:29:18 +00:00
|
|
|
void removePast(MyGame gameRef) {
|
|
|
|
for (List<Platform> platformLevel in platforms) {
|
|
|
|
int removed = 0;
|
|
|
|
while (platformLevel.isNotEmpty &&
|
|
|
|
platformLevel[0].sprite.position.x + platformLevel[0].sprite.width <
|
|
|
|
0) {
|
2021-09-05 22:36:36 +00:00
|
|
|
remove(platformLevel, 0);
|
2021-09-03 23:29:18 +00:00
|
|
|
removed++;
|
|
|
|
}
|
|
|
|
if (platformLevel.isNotEmpty &&
|
|
|
|
platformLevel.length > 3 &&
|
|
|
|
random.nextInt(100) > 65 &&
|
|
|
|
removed > 0) {
|
2021-09-04 03:03:05 +00:00
|
|
|
int secondToLast = platformLevel.length - 4;
|
2021-09-03 23:29:18 +00:00
|
|
|
double secondToLastPosition =
|
|
|
|
platformLevel.elementAt(secondToLast).sprite.x;
|
|
|
|
if (secondToLastPosition > gameRef.size.x) {
|
2021-09-05 22:36:36 +00:00
|
|
|
remove(platformLevel, secondToLast + 1);
|
|
|
|
remove(platformLevel, secondToLast);
|
2021-09-03 23:29:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-04 04:17:12 +00:00
|
|
|
|
|
|
|
double getFlushX() {
|
|
|
|
Platform platform =
|
|
|
|
platforms[2].firstWhere((element) => element.sprite.x > 0, orElse: () {
|
|
|
|
return platforms[5].firstWhere((element) => element.sprite.x > 0,
|
|
|
|
orElse: () {
|
|
|
|
return platforms[8].firstWhere((element) => element.sprite.x > 0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return platform.sprite.x;
|
|
|
|
}
|
2021-09-04 22:56:08 +00:00
|
|
|
|
|
|
|
Platform? getPlatformOffScreen(int level) {
|
|
|
|
for (int i = 0; i < platforms[level].length; i++) {
|
|
|
|
Platform p = platforms[level][i];
|
|
|
|
if (p.sprite.x > p.gameRef.size.x) {
|
|
|
|
int chosenIndex = random.nextInt(platforms[level].length - i) + i;
|
|
|
|
return platforms[level][chosenIndex];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2021-09-03 23:29:18 +00:00
|
|
|
}
|