diff --git a/lib/coin_holder.dart b/lib/coin_holder.dart index 8fd3902..220bf76 100644 --- a/lib/coin_holder.dart +++ b/lib/coin_holder.dart @@ -5,6 +5,7 @@ import 'package:flame/flame.dart'; import 'package:firo_runner/coin.dart'; import 'package:firo_runner/main.dart'; import 'package:flame/extensions.dart'; +import 'package:firo_runner/platform.dart'; class CoinHolder { late Image coin; @@ -36,14 +37,34 @@ class CoinHolder { if (totalCoins() > 5) { 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; } 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.setPosition(xCoordinate, gameRef.blockSize * level); @@ -53,8 +74,28 @@ class CoinHolder { coins[level].add(coin); 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() { diff --git a/lib/main.dart b/lib/main.dart index 4332daa..65d0ac1 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -115,21 +115,23 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents { if (shouldReset) { return; } - for (int i = 2; i < 9; i = i + 3) { - while (!platformHolder.generatePlatform(this, i, false)) {} - } - int wireChosenRegion = random.nextInt(8) + 1; - if (wireChosenRegion % 3 != 2) { + + platformHolder.generatePlatforms(this); + + int wireChosenRegion = random.nextInt(9); + if (wireChosenRegion % 3 != 2 && + wireChosenRegion != 6 && + wireChosenRegion != 7) { wireHolder.generateWire(this, wireChosenRegion, false); } - int bugChosenRegion = random.nextInt(8) + 1; - if (bugChosenRegion % 3 != 2) { + int bugChosenRegion = random.nextInt(9); + if (bugChosenRegion % 3 != 2 && bugChosenRegion != 6) { bugHolder.generateBug(this, bugChosenRegion, false); } - int choseCoinLevel = random.nextInt(8) + 1; - if (choseCoinLevel % 3 != 2) { + int choseCoinLevel = random.nextInt(9); + if (choseCoinLevel % 3 != 2 && choseCoinLevel != 6) { coinHolder.generateCoin(this, choseCoinLevel, false); } } @@ -200,10 +202,6 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents { 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(); } diff --git a/lib/platform_holder.dart b/lib/platform_holder.dart index 5b4ba36..ff37b5f 100644 --- a/lib/platform_holder.dart +++ b/lib/platform_holder.dart @@ -15,6 +15,8 @@ class PlatformHolder { late Image o1; late Image o2; late List> platforms = []; + int timeSinceLastTopHole = 0; + int timeSinceLastBottomHole = 0; Random random = Random(); Future loadPlatforms() async { @@ -29,6 +31,8 @@ class PlatformHolder { } void setUp() { + timeSinceLastTopHole = 0; + timeSinceLastBottomHole = 0; for (int i = 0; i < platforms.length; i++) { for (int j = 0; j < platforms[i].length; j++) { remove(platforms[i], j); @@ -40,13 +44,47 @@ class PlatformHolder { } } - bool generatePlatform(MyGame gameRef, int level, bool force) { - double xCoordinate = 0; - if (platforms[level].isNotEmpty) { + void generatePlatforms(MyGame gameRef) { + while (!generatePlatform(gameRef, 2)) { + 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(); } - if (xCoordinate > gameRef.size.x + 1000) { + if (xCoordinate > gameRef.size.x + 2000) { return true; } else { Platform platform = Platform(gameRef); @@ -74,24 +112,10 @@ class PlatformHolder { void removePast(MyGame gameRef) { for (List platformLevel in platforms) { - int removed = 0; while (platformLevel.isNotEmpty && platformLevel[0].sprite.position.x + platformLevel[0].sprite.width < 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); - } } } }