Changed the different difficulties of each level in the game.

This commit is contained in:
Marco Salazar 2021-09-10 20:39:32 -06:00
parent eb7491a852
commit c6868f23fd
3 changed files with 100 additions and 37 deletions

View File

@ -5,6 +5,7 @@ import 'package:flame/flame.dart';
import 'package:firo_runner/coin.dart'; import 'package:firo_runner/coin.dart';
import 'package:firo_runner/main.dart'; import 'package:firo_runner/main.dart';
import 'package:flame/extensions.dart'; import 'package:flame/extensions.dart';
import 'package:firo_runner/platform.dart';
class CoinHolder { class CoinHolder {
late Image coin; late Image coin;
@ -36,14 +37,34 @@ class CoinHolder {
if (totalCoins() > 5) { if (totalCoins() > 5) {
return false; return false;
} }
double xCoordinate = gameRef.platformHolder.getFlushX();
xCoordinate = xCoordinate +
gameRef.blockSize * random.nextInt(5) +
gameRef.blockSize * 20;
if (xCoordinate < gameRef.size.x || random.nextInt(100) > 25) { if (coins[level].isNotEmpty) {
return false;
}
if (random.nextInt(100) > 25) {
return true; return true;
} else { } else {
int nearestPlatform = level <= 0
? 0
: level <= 3
? 2
: level <= 6
? 5
: 8;
Platform? platform =
gameRef.platformHolder.getPlatformOffScreen(nearestPlatform);
double xCoordinate = -100;
if (level == 0) {
xCoordinate = gameRef.size.x;
} else if (platform != null) {
xCoordinate = platform.sprite.x;
} else {
return false;
}
Coin coin = Coin(gameRef); Coin coin = Coin(gameRef);
coin.setPosition(xCoordinate, gameRef.blockSize * level); coin.setPosition(xCoordinate, gameRef.blockSize * level);
@ -53,8 +74,28 @@ class CoinHolder {
coins[level].add(coin); coins[level].add(coin);
gameRef.add(coin.sprite); gameRef.add(coin.sprite);
return false;
} }
return false;
// double xCoordinate = gameRef.platformHolder.getFlushX();
// xCoordinate = xCoordinate +
// gameRef.blockSize * random.nextInt(5) +
// gameRef.blockSize * 20;
//
// if (xCoordinate < gameRef.size.x || random.nextInt(100) > 25) {
// return true;
// } else {
// Coin coin = Coin(gameRef);
// coin.setPosition(xCoordinate, gameRef.blockSize * level);
//
// if (gameRef.isTooNearOtherObstacles(coin.sprite.toRect())) {
// return false;
// }
//
// coins[level].add(coin);
// gameRef.add(coin.sprite);
// return false;
// }
} }
int totalCoins() { int totalCoins() {

View File

@ -115,21 +115,23 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
if (shouldReset) { if (shouldReset) {
return; return;
} }
for (int i = 2; i < 9; i = i + 3) {
while (!platformHolder.generatePlatform(this, i, false)) {} platformHolder.generatePlatforms(this);
}
int wireChosenRegion = random.nextInt(8) + 1; int wireChosenRegion = random.nextInt(9);
if (wireChosenRegion % 3 != 2) { if (wireChosenRegion % 3 != 2 &&
wireChosenRegion != 6 &&
wireChosenRegion != 7) {
wireHolder.generateWire(this, wireChosenRegion, false); wireHolder.generateWire(this, wireChosenRegion, false);
} }
int bugChosenRegion = random.nextInt(8) + 1; int bugChosenRegion = random.nextInt(9);
if (bugChosenRegion % 3 != 2) { if (bugChosenRegion % 3 != 2 && bugChosenRegion != 6) {
bugHolder.generateBug(this, bugChosenRegion, false); bugHolder.generateBug(this, bugChosenRegion, false);
} }
int choseCoinLevel = random.nextInt(8) + 1; int choseCoinLevel = random.nextInt(9);
if (choseCoinLevel % 3 != 2) { if (choseCoinLevel % 3 != 2 && choseCoinLevel != 6) {
coinHolder.generateCoin(this, choseCoinLevel, false); coinHolder.generateCoin(this, choseCoinLevel, false);
} }
} }
@ -200,10 +202,6 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
runner.setUp(); runner.setUp();
// Generate the first 4 Platforms that will always be there at the start.
for (int i = 0; i < 4; i++) {
platformHolder.generatePlatform(this, 8, true);
}
fillScreen(); fillScreen();
} }

View File

@ -15,6 +15,8 @@ class PlatformHolder {
late Image o1; late Image o1;
late Image o2; late Image o2;
late List<List<Platform>> platforms = []; late List<List<Platform>> platforms = [];
int timeSinceLastTopHole = 0;
int timeSinceLastBottomHole = 0;
Random random = Random(); Random random = Random();
Future loadPlatforms() async { Future loadPlatforms() async {
@ -29,6 +31,8 @@ class PlatformHolder {
} }
void setUp() { void setUp() {
timeSinceLastTopHole = 0;
timeSinceLastBottomHole = 0;
for (int i = 0; i < platforms.length; i++) { for (int i = 0; i < platforms.length; i++) {
for (int j = 0; j < platforms[i].length; j++) { for (int j = 0; j < platforms[i].length; j++) {
remove(platforms[i], j); remove(platforms[i], j);
@ -40,13 +44,47 @@ class PlatformHolder {
} }
} }
bool generatePlatform(MyGame gameRef, int level, bool force) { void generatePlatforms(MyGame gameRef) {
double xCoordinate = 0; while (!generatePlatform(gameRef, 2)) {
if (platforms[level].isNotEmpty) { timeSinceLastTopHole++;
}
while (!generatePlatform(gameRef, 5)) {
timeSinceLastBottomHole++;
}
int topChance =
random.nextInt(timeSinceLastTopHole > 0 ? timeSinceLastTopHole : 1);
int bottomChance = random
.nextInt(timeSinceLastBottomHole > 0 ? timeSinceLastBottomHole : 1);
if (topChance > 50) {
remove(platforms[2], platforms[2].length - 2);
remove(platforms[2], platforms[2].length - 2);
timeSinceLastTopHole = 0;
}
if (bottomChance > 30) {
Platform start = platforms[5].elementAt(platforms[5].length - 10);
generatePlatform(gameRef, 8, xPosition: start.sprite.position.x);
for (int i = 0; i < 8; i++) {
generatePlatform(gameRef, 8);
}
int lastToRemove = platforms[5].length - 3;
int firstToRemove = platforms[5].length - 10;
remove(platforms[5], lastToRemove);
remove(platforms[5], lastToRemove);
remove(platforms[5], firstToRemove);
remove(platforms[5], firstToRemove);
timeSinceLastBottomHole = 0;
}
}
bool generatePlatform(MyGame gameRef, int level, {double xPosition = 0}) {
double xCoordinate = xPosition;
if (platforms[level].isNotEmpty && xPosition == 0) {
xCoordinate = platforms[level].last.getRightEnd(); xCoordinate = platforms[level].last.getRightEnd();
} }
if (xCoordinate > gameRef.size.x + 1000) { if (xCoordinate > gameRef.size.x + 2000) {
return true; return true;
} else { } else {
Platform platform = Platform(gameRef); Platform platform = Platform(gameRef);
@ -74,24 +112,10 @@ class PlatformHolder {
void removePast(MyGame gameRef) { void removePast(MyGame gameRef) {
for (List<Platform> platformLevel in platforms) { for (List<Platform> platformLevel in platforms) {
int removed = 0;
while (platformLevel.isNotEmpty && while (platformLevel.isNotEmpty &&
platformLevel[0].sprite.position.x + platformLevel[0].sprite.width < platformLevel[0].sprite.position.x + platformLevel[0].sprite.width <
0) { 0) {
remove(platformLevel, 0); remove(platformLevel, 0);
removed++;
}
if (platformLevel.isNotEmpty &&
platformLevel.length > 3 &&
random.nextInt(100) > 65 &&
removed > 0) {
int secondToLast = platformLevel.length - 4;
double secondToLastPosition =
platformLevel.elementAt(secondToLast).sprite.x;
if (secondToLastPosition > gameRef.size.x) {
remove(platformLevel, secondToLast + 1);
remove(platformLevel, secondToLast);
}
} }
} }
} }