diff --git a/lib/Bug.dart b/lib/Bug.dart new file mode 100644 index 0000000..951d9f3 --- /dev/null +++ b/lib/Bug.dart @@ -0,0 +1,53 @@ +import 'package:firo_runner/MovingObject.dart'; +import 'package:firo_runner/main.dart'; +import 'package:flame/components.dart'; + +enum BugState { normal, breaking } + +class Bug extends MovingObject { + Bug(MyGame gameRef) : super(gameRef) { + var bug = gameRef.bugHolder.getBug("normal"); + var breakingImage = gameRef.bugHolder.getBug("breaking"); + SpriteAnimation normal = SpriteAnimation.fromFrameData( + bug, + SpriteAnimationData.sequenced( + amount: 8, + stepTime: 0.1, + textureSize: Vector2(512, 512), + ), + ); + + SpriteAnimation breaking = SpriteAnimation.fromFrameData( + breakingImage, + SpriteAnimationData.sequenced( + amount: 12, + stepTime: 0.01, + textureSize: Vector2(512, 512), + loop: false, + ), + ); + + sprite = SpriteAnimationGroupComponent( + animations: { + BugState.normal: normal, + BugState.breaking: breaking, + }, + current: BugState.normal, + ); + + sprite.changePriorityWithoutResorting(BUG_PRIORITY); + + setSize( + gameRef.blockSize, + gameRef.blockSize, + ); + } + + double getRightEnd() { + return sprite.position.x + sprite.width; + } + + void remove() { + sprite.remove(); + } +} diff --git a/lib/BugHolder.dart b/lib/BugHolder.dart new file mode 100644 index 0000000..18b2a9a --- /dev/null +++ b/lib/BugHolder.dart @@ -0,0 +1,103 @@ +import 'dart:math'; + +import 'package:firo_runner/Platform.dart'; +import 'package:flame/flame.dart'; + +import 'Bug.dart'; +import 'main.dart'; + +class BugHolder { + var bug; + var breaking; + Random random = Random(); + + late List> bugs = []; + + Future loadBugs() async { + bug = await Flame.images.load("bug-frames.png"); + breaking = await Flame.images.load("bug-break-frames.png"); + for (int i = 0; i < 9; i++) { + bugs.add([]); + } + } + + getBug(String state) { + switch (state) { + case "normal": + return bug; + default: + return breaking; + } + } + + bool generateBug(MyGame gameRef, int level, bool force) { + if (bugs[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; + } + + Bug bug = Bug(gameRef); + bug.setPosition(xCoordinate, gameRef.blockSize * level); + bugs[level].add(bug); + gameRef.add(bug.sprite); + if (platform != null) { + platform.removeChildren.add(() { + bugs[level].remove(bug); + bug.remove(); + }); + } + return false; + } + } + + int totalBugs() { + int total = 0; + for (List levelBugs in bugs) { + total += levelBugs.length; + } + return total; + } + + void update(double dt) { + for (List bugLevel in bugs) { + for (Bug p in bugLevel) { + p.update(dt); + } + } + } + + void removePast(MyGame gameRef) { + for (List bugLevel in bugs) { + for (int i = 0; i < bugLevel.length;) { + if (bugLevel[i].sprite.x + bugLevel[i].sprite.width < 0) { + bugLevel[i].sprite.remove(); + bugLevel.removeAt(i); + continue; + } + i++; + } + } + } +} diff --git a/lib/CoinHolder.dart b/lib/CoinHolder.dart index bec361d..0a5e468 100644 --- a/lib/CoinHolder.dart +++ b/lib/CoinHolder.dart @@ -26,16 +26,16 @@ class CoinHolder { if (totalCoins() > 5) { return false; } - double xCordinate = gameRef.platformHolder.getFlushX(); - xCordinate = xCordinate + + double xCoordinate = gameRef.platformHolder.getFlushX(); + xCoordinate = xCoordinate + gameRef.blockSize * random.nextInt(5) + gameRef.blockSize * 20; - if (xCordinate < gameRef.size.x || random.nextInt(100) > 10) { + if (xCoordinate < gameRef.size.x || random.nextInt(100) > 25) { return true; } else { Coin coin = Coin(gameRef); - coin.setPosition(xCordinate, gameRef.blockSize * level); + coin.setPosition(xCoordinate, gameRef.blockSize * level); coins[level].add(coin); gameRef.add(coin.sprite); return false; diff --git a/lib/Platform.dart b/lib/Platform.dart index f4f2217..9e1e8be 100644 --- a/lib/Platform.dart +++ b/lib/Platform.dart @@ -7,7 +7,7 @@ import 'package:flame/components.dart'; enum PlatformState { normal } class Platform extends MovingObject { - var removeChildren = null; + List removeChildren = []; Platform(MyGame gameRef) : super(gameRef) { var random = Random(); @@ -42,8 +42,10 @@ class Platform extends MovingObject { } void remove() { - if (removeChildren != null) { - removeChildren(); + if (removeChildren.isNotEmpty) { + for (Function removeChild in removeChildren) { + removeChild(); + } } } } diff --git a/lib/Runner.dart b/lib/Runner.dart index 64cd5c9..33799ec 100644 --- a/lib/Runner.dart +++ b/lib/Runner.dart @@ -1,3 +1,4 @@ +import 'package:firo_runner/Bug.dart'; import 'package:firo_runner/Coin.dart'; import 'package:firo_runner/Wire.dart'; import 'package:firo_runner/main.dart'; @@ -189,6 +190,7 @@ class Runner extends Component with HasGameRef { intersecting(); sprite.update(dt); + print(runnerState); } bool onTopOfPlatform() { @@ -249,6 +251,32 @@ class Runner extends Component with HasGameRef { } } + for (List bugLevel in gameRef.bugHolder.bugs) { + for (int i = 0; i < bugLevel.length; i++) { + String intersectState = bugLevel[i].intersect(runnerRect); + if (bugLevel[i].sprite.current == BugState.breaking) { + continue; + } + if (intersectState == "none") { + Rect above = Rect.fromLTRB(runnerRect.left, runnerRect.top - 1, + runnerRect.right, runnerRect.bottom); + String aboveIntersect = bugLevel[i].intersect(above); + if (aboveIntersect != "none" && + (runnerState == "duck" || runnerState == "float")) { + continue; + } else if (aboveIntersect != "none") { + event("die"); + } + } else if (intersectState == "left" && runnerState == "kick") { + bugLevel[i].sprite.current = BugState.breaking; + // bugLevel[i].remove(); + // bugLevel.removeAt(i); + } else { + event("die"); + } + } + } + if (!onTopOfPlatform && (runnerState == "run" || runnerState == "kick" || @@ -290,7 +318,7 @@ class Runner extends Component with HasGameRef { 'kick-frames.png', SpriteAnimationData.sequenced( amount: 13, - stepTime: 0.03, + stepTime: 0.05, textureSize: Vector2(512, 512), loop: false, ), diff --git a/lib/WireHolder.dart b/lib/WireHolder.dart index 9c5e0b7..eead570 100644 --- a/lib/WireHolder.dart +++ b/lib/WireHolder.dart @@ -63,10 +63,10 @@ class WireHolder { wires[level].add(wire); gameRef.add(wire.sprite); if (platform != null) { - platform.removeChildren = () { + platform.removeChildren.add(() { wires[level].remove(wire); wire.remove(); - }; + }); } return false; } diff --git a/lib/main.dart b/lib/main.dart index 404d166..b3dbca5 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,5 +1,6 @@ import 'dart:math'; +import 'package:firo_runner/BugHolder.dart'; import 'package:firo_runner/CoinHolder.dart'; import 'package:firo_runner/GameState.dart'; import 'package:firo_runner/PlatformHolder.dart'; @@ -40,6 +41,7 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents { late PlatformHolder platformHolder; late CoinHolder coinHolder; late WireHolder wireHolder; + late BugHolder bugHolder; Random random = Random(); late Sprite background1; @@ -72,6 +74,8 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents { await coinHolder.loadCoins(); wireHolder = WireHolder(); await wireHolder.loadWires(); + bugHolder = BugHolder(); + await bugHolder.loadBugs(); gameState = GameState(); await gameState.load(size); @@ -102,6 +106,11 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents { wireHolder.generateWire(this, wireChosenRegion, false); } + int bugChosenRegion = random.nextInt(8) + 1; + if (bugChosenRegion % 3 != 2) { + bugHolder.generateBug(this, bugChosenRegion, false); + } + int choseCoinLevel = random.nextInt(8) + 1; if (choseCoinLevel % 3 != 2) { coinHolder.generateCoin(this, choseCoinLevel, false); @@ -130,12 +139,14 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents { platformHolder.removePast(this); coinHolder.removePast(this); wireHolder.removePast(this); + bugHolder.removePast(this); fillScreen(); super.update(dt); gameState.update(dt); platformHolder.update(dt); coinHolder.update(dt); wireHolder.update(dt); + bugHolder.update(dt); } @override