diff --git a/assets/images/debris-frames.png b/assets/images/debris-frames.png new file mode 100644 index 0000000..fc66887 Binary files /dev/null and b/assets/images/debris-frames.png differ diff --git a/lib/debris.dart b/lib/debris.dart new file mode 100644 index 0000000..7c2d6bc --- /dev/null +++ b/lib/debris.dart @@ -0,0 +1,37 @@ +import 'package:firo_runner/moving_object.dart'; +import 'package:firo_runner/main.dart'; +import 'package:flame/components.dart'; + +enum DebrisState { normal } + +class Debris extends MovingObject { + Debris(MyGame gameRef) : super(gameRef) { + var debris = gameRef.debrisHolder.getDebris(); + SpriteAnimation normal = SpriteAnimation.fromFrameData( + debris, + SpriteAnimationData.sequenced( + amount: 21, + stepTime: 0.1, + textureSize: Vector2(360, 1000), + ), + ); + + sprite = SpriteAnimationGroupComponent( + animations: { + DebrisState.normal: normal, + }, + current: DebrisState.normal, + ); + + sprite.changePriorityWithoutResorting(DEBRIS_PRIORITY); + + setSize( + gameRef.blockSize * + (gameRef.debrisHolder.debris.width / + gameRef.debrisHolder.debris.height / + 21) * + 1.5, + gameRef.blockSize * 1.5, + ); + } +} diff --git a/lib/debris_holder.dart b/lib/debris_holder.dart new file mode 100644 index 0000000..bc1233d --- /dev/null +++ b/lib/debris_holder.dart @@ -0,0 +1,65 @@ +import 'package:firo_runner/holder.dart'; +import 'package:firo_runner/platform.dart'; +import 'package:flame/extensions.dart'; +import 'package:flame/flame.dart'; + +import 'package:firo_runner/debris.dart'; +import 'package:firo_runner/main.dart'; + +class DebrisHolder extends Holder { + late Image debris; + + @override + Future load() async { + debris = await Flame.images.load("debris-frames.png"); + } + + getDebris() { + return debris; + } + + bool generateDebris(MyGame gameRef, int level, bool force) { + if (objects[level].isNotEmpty) { + return false; + } + + if (random.nextInt(100) > 25) { + return true; + } else { + int nearestPlatform = getNearestPlatform(level); + + Platform? platform = + gameRef.platformHolder.getPlatformOffScreen(nearestPlatform); + if (platform != null && platform.prohibitObstacles) { + return false; + } + double xCoordinate = -100; + + if (level == 0) { + xCoordinate = gameRef.size.x; + } else if (platform != null) { + xCoordinate = platform.sprite.x; + } else { + return false; + } + + Debris debris = Debris(gameRef); + debris.setPosition( + xCoordinate, gameRef.blockSize * level - gameRef.blockSize / 3); + + if (gameRef.isTooNearOtherObstacles(debris.sprite.toRect())) { + return false; + } + + objects[level].add(debris); + gameRef.add(debris.sprite); + if (platform != null) { + platform.removeChildren.add(() { + objects[level].remove(debris); + debris.remove(); + }); + } + return false; + } + } +} diff --git a/lib/main.dart b/lib/main.dart index 50caa53..83bab5c 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -3,6 +3,7 @@ import 'dart:math'; import 'package:firo_runner/bug_holder.dart'; import 'package:firo_runner/circuit_background.dart'; import 'package:firo_runner/coin_holder.dart'; +import 'package:firo_runner/debris_holder.dart'; import 'package:firo_runner/firework.dart'; import 'package:firo_runner/game_state.dart'; import 'package:firo_runner/moving_object.dart'; @@ -35,6 +36,7 @@ const RUNNER_PRIORITY = 100; const BUG_PRIORITY = 75; const COIN_PRIORITY = 70; const PLATFORM_PRIORITY = 50; +const DEBRIS_PRIORITY = 30; const WIRE_PRIORITY = 25; const FIREWORK_PRIORITY = 15; const WINDOW_PRIORITY = 10; @@ -69,6 +71,7 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents { late WireHolder wireHolder; late BugHolder bugHolder; late Firework fireworks; + late DebrisHolder debrisHolder; Random random = Random(); bool playingMusic = false; @@ -101,6 +104,8 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents { await wireHolder.load(); bugHolder = BugHolder(); await bugHolder.load(); + debrisHolder = DebrisHolder(); + await debrisHolder.load(); fireworks = Firework(this); await fireworks.load(); @@ -136,10 +141,15 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents { } int bugChosenRegion = random.nextInt(9); - if (bugChosenRegion % 3 != 2 && bugChosenRegion != 6) { + if (bugChosenRegion % 3 != 2 && bugChosenRegion % 3 != 0) { bugHolder.generateBug(this, bugChosenRegion, false); } + int debrisChosenRegion = random.nextInt(9); + if (debrisChosenRegion % 3 == 0 && debrisChosenRegion != 6) { + debrisHolder.generateDebris(this, debrisChosenRegion, false); + } + int choseCoinLevel = random.nextInt(9); if (choseCoinLevel % 3 != 2 && choseCoinLevel != 6) { coinHolder.generateCoin(this, choseCoinLevel, false); @@ -176,6 +186,14 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents { } } + for (List debrisLevel in debrisHolder.objects) { + for (MovingObject debris in debrisLevel) { + if (debris.intersect(obstacleBounds) != "none") { + return true; + } + } + } + return false; } @@ -207,6 +225,7 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents { coinHolder.setUp(); wireHolder.setUp(); bugHolder.setUp(); + debrisHolder.setUp(); gameState.setUp(this); @@ -237,6 +256,7 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents { coinHolder.removePast(this); wireHolder.removePast(this); bugHolder.removePast(this); + debrisHolder.removePast(this); fillScreen(); super.update(dt); circuitBackground.update(dt); @@ -245,6 +265,7 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents { coinHolder.update(dt); wireHolder.update(dt); bugHolder.update(dt); + debrisHolder.update(dt); if (shouldReset) { reset(); } @@ -264,6 +285,7 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents { coinHolder.resize(canvasSize, xRatio, yRatio); wireHolder.resize(canvasSize, xRatio, yRatio); bugHolder.resize(canvasSize, xRatio, yRatio); + debrisHolder.resize(canvasSize, xRatio, yRatio); fireworks.resize(canvasSize, xRatio, yRatio); } } diff --git a/lib/runner.dart b/lib/runner.dart index a9c1b25..67e6872 100644 --- a/lib/runner.dart +++ b/lib/runner.dart @@ -402,6 +402,25 @@ class Runner extends Component with HasGameRef { } } + for (List debrisLevel in gameRef.debrisHolder.objects) { + for (int i = 0; i < debrisLevel.length; i++) { + Rect slim = Rect.fromLTRB( + runnerRect.left + sprite.width / 3, + runnerRect.top, + runnerRect.right - sprite.width / 3, + runnerRect.bottom); + String intersectState = debrisLevel[i].intersect(slim); + if (intersectState == "none") { + continue; + } else if ((runnerState == "duck" || runnerState == "float") && + intersectState != "above") { + continue; + } else { + event("die"); + } + } + } + if (!onTopOfPlatform && (runnerState == "run" || runnerState == "kick" ||