diff --git a/lib/Coin.dart b/lib/Coin.dart new file mode 100644 index 0000000..332d69e --- /dev/null +++ b/lib/Coin.dart @@ -0,0 +1,45 @@ +import 'dart:math'; + +import 'package:firo_runner/MovingObject.dart'; +import 'package:firo_runner/main.dart'; +import 'package:flame/components.dart'; +import 'package:flutter/material.dart'; + +enum CoinState { normal } + +class Coin extends MovingObject { + Coin(MyGame gameRef) : super(gameRef) { + var coin = gameRef.coinHolder.getCoin(); + SpriteAnimation normal = SpriteAnimation.fromFrameData( + coin, + SpriteAnimationData.sequenced( + amount: 10, + stepTime: 0.1, + textureSize: Vector2(512, 512), + ), + ); + + sprite = SpriteAnimationGroupComponent( + animations: { + CoinState.normal: normal, + }, + current: CoinState.normal, + ); + + var platform = gameRef.platformHolder.getPlatform(0); + + setSize( + gameRef.blockSize * (platform!.width / platform!.height / 14), + gameRef.blockSize * (platform!.width / platform!.height / 14), + ); + } + + double getRightEnd() { + return sprite.position.x + sprite.width; + } + + @override + void render(Canvas c) { + getSprite().render(c, position: sprite.position, size: sprite.size); + } +} diff --git a/lib/CoinHolder.dart b/lib/CoinHolder.dart new file mode 100644 index 0000000..43c3ffb --- /dev/null +++ b/lib/CoinHolder.dart @@ -0,0 +1,83 @@ +import 'dart:math'; + +import 'package:flame/flame.dart'; +import 'package:flutter/material.dart'; + +import 'Coin.dart'; +import 'main.dart'; + +class Coinholder { + var coin; + Random random = Random(); + + late List> coins = []; + + Future loadCoins() async { + coin = await Flame.images.load("coin-frames.png"); + for (int i = 0; i < 9; i++) { + coins.add([]); + } + } + + getCoin() { + return coin; + } + + bool generateCoin(MyGame gameRef, int level, bool force) { + if (totalCoins() > 5) { + return false; + } + double xCordinate = gameRef.platformHolder.getFlushX(); + // if (coins[level].isNotEmpty) { + // xCordinate = coins[level].last.getRightEnd(); + // } + xCordinate = xCordinate + + gameRef.blockSize * random.nextInt(5) + + gameRef.blockSize * 20; + + if (xCordinate < gameRef.size.x && random.nextInt(1000000) > 99999) { + return true; + } else { + Coin coin = Coin(gameRef); + coin.setPosition(xCordinate, gameRef.blockSize * level); + coins[level].add(coin); + return false; + } + } + + int totalCoins() { + int total = 0; + for (List levelCoins in coins) { + total += levelCoins.length; + } + return total; + } + + void render(Canvas canvas) { + for (List coinLevel in coins) { + for (Coin p in coinLevel) { + p.render(canvas); + } + } + } + + void update(double dt) { + for (List coinLevel in coins) { + for (Coin p in coinLevel) { + p.update(dt); + } + } + } + + void removePast(MyGame gameRef) { + for (List coinLevel in coins) { + for (int i = 0; i < coinLevel.length;) { + if (coinLevel[i].sprite.x + coinLevel[i].sprite.width < 0) { + coinLevel.removeAt(i); + continue; + } + i++; + } + } + } +} diff --git a/lib/PlatformLoader.dart b/lib/PlatformHolder.dart similarity index 87% rename from lib/PlatformLoader.dart rename to lib/PlatformHolder.dart index 76aeb69..db55fbe 100644 --- a/lib/PlatformLoader.dart +++ b/lib/PlatformHolder.dart @@ -87,4 +87,15 @@ class PlatformHolder { } } } + + 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; + } } diff --git a/lib/Runner.dart b/lib/Runner.dart index ba9d85c..bdbc3c7 100644 --- a/lib/Runner.dart +++ b/lib/Runner.dart @@ -1,3 +1,4 @@ +import 'package:firo_runner/Coin.dart'; import 'package:firo_runner/main.dart'; import 'package:flame/effects.dart'; import 'package:flutter/material.dart'; @@ -232,6 +233,18 @@ class Runner extends Component with HasGameRef { } } + for (List coinLevel in gameRef.coinHolder.coins) { + for (int i = 0; i < coinLevel.length;) { + if (coinLevel[i].intersect(runnerRect) != "none") { + gameRef.gameState.numCoins++; + coinLevel.removeAt(i); + print(gameRef.gameState.numCoins); + continue; + } + i++; + } + } + if (!onTopOfPlatform && (runnerState == "run" || runnerState == "kick" || diff --git a/lib/main.dart b/lib/main.dart index 0182602..0b7fc95 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,7 +1,10 @@ +import 'dart:math'; + +import 'package:firo_runner/CoinHolder.dart'; import 'package:firo_runner/GameState.dart'; import 'package:firo_runner/MovingObject.dart'; import 'package:firo_runner/Platform.dart'; -import 'package:firo_runner/PlatformLoader.dart'; +import 'package:firo_runner/PlatformHolder.dart'; import 'package:flame/components.dart'; import 'package:flame/extensions.dart'; import 'package:flame/flame.dart'; @@ -34,6 +37,8 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents { ); late PlatformHolder platformHolder; + late Coinholder coinHolder; + Random random = Random(); late Sprite background1; late Sprite background2; @@ -72,6 +77,8 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents { platformHolder = PlatformHolder(); await platformHolder.loadPlatforms(); + coinHolder = Coinholder(); + await coinHolder.loadCoins(); gameState = GameState(); await gameState.load(size); @@ -97,6 +104,10 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents { for (int i = 2; i < 9; i = i + 3) { while (!platformHolder.generatePlatform(this, i, false)); } + int choseCoinLevel = random.nextInt(9); + if (choseCoinLevel % 3 != 2) { + coinHolder.generateCoin(this, choseCoinLevel, false); + } } @override @@ -109,6 +120,7 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents { ); super.render(canvas); platformHolder.render(canvas); + coinHolder.render(canvas); final fpsCount = fps(1); textPaint.render( canvas, @@ -120,10 +132,12 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents { @override void update(double dt) { platformHolder.removePast(this); + coinHolder.removePast(this); fillScreen(); super.update(dt); gameState.update(dt); platformHolder.update(dt); + coinHolder.update(dt); } @override