From 42873d274aba655fb69ecc48900a8957fdbd0732 Mon Sep 17 00:00:00 2001 From: marco Date: Thu, 16 Sep 2021 15:15:32 -0600 Subject: [PATCH] Cleaning up the code so it is more generalized. --- lib/bug.dart | 8 --- lib/bug_holder.dart | 75 +++------------------- lib/coin.dart | 8 --- lib/coin_holder.dart | 75 +++------------------- lib/holder.dart | 67 ++++++++++++++++++++ lib/main.dart | 37 ++++++----- lib/moving_object.dart | 8 +++ lib/platform.dart | 10 +-- lib/platform_holder.dart | 132 ++++++++++++--------------------------- lib/runner.dart | 18 +++--- lib/wire.dart | 8 --- lib/wire_holder.dart | 73 +++------------------- 12 files changed, 177 insertions(+), 342 deletions(-) create mode 100644 lib/holder.dart diff --git a/lib/bug.dart b/lib/bug.dart index 320c410..92f0a8d 100644 --- a/lib/bug.dart +++ b/lib/bug.dart @@ -42,12 +42,4 @@ class Bug extends MovingObject { gameRef.blockSize, ); } - - double getRightEnd() { - return sprite.position.x + sprite.width; - } - - void remove() { - sprite.remove(); - } } diff --git a/lib/bug_holder.dart b/lib/bug_holder.dart index 5172f30..3cb290e 100644 --- a/lib/bug_holder.dart +++ b/lib/bug_holder.dart @@ -1,5 +1,4 @@ -import 'dart:math'; - +import 'package:firo_runner/holder.dart'; import 'package:firo_runner/platform.dart'; import 'package:flame/extensions.dart'; import 'package:flame/flame.dart'; @@ -7,30 +6,16 @@ import 'package:flame/flame.dart'; import 'package:firo_runner/bug.dart'; import 'package:firo_runner/main.dart'; -class BugHolder { +class BugHolder extends Holder { late Image bug; late Image breaking; - Random random = Random(); - late List> bugs = []; - - Future loadBugs() async { + @override + Future load() async { bug = await Flame.images.load("bug-frames.png"); breaking = await Flame.images.load("bug-break-frames.png"); } - void setUp() { - for (int i = 0; i < bugs.length; i++) { - for (int j = 0; j < bugs[i].length; j++) { - remove(bugs[i], j); - } - } - bugs = []; - for (int i = 0; i < 9; i++) { - bugs.add([]); - } - } - getBug(String state) { switch (state) { case "normal": @@ -41,14 +26,14 @@ class BugHolder { } bool generateBug(MyGame gameRef, int level, bool force) { - if (bugs[level].isNotEmpty) { + if (objects[level].isNotEmpty) { return false; } if (random.nextInt(100) > 25) { return true; } else { - int nearestPlatform = gameRef.platformHolder.getNearestPlatform(level); + int nearestPlatform = getNearestPlatform(level); Platform? platform = gameRef.platformHolder.getPlatformOffScreen(nearestPlatform); @@ -72,59 +57,15 @@ class BugHolder { return false; } - bugs[level].add(bug); + objects[level].add(bug); gameRef.add(bug.sprite); if (platform != null) { platform.removeChildren.add(() { - bugs[level].remove(bug); + objects[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 remove(List levelHolder, int j) { - levelHolder[j].remove(); - levelHolder[j].sprite.remove(); - levelHolder.removeAt(j); - } - - 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) { - remove(bugLevel, i); - continue; - } - i++; - } - } - } - - void resize(Vector2 newSize, double xRatio, double yRatio) { - for (List platformLevel in bugs) { - for (Bug p in platformLevel) { - p.resize(newSize, xRatio, yRatio); - p.sprite.y = (p.sprite.position.y / p.gameRef.blockSize).round() * - p.gameRef.blockSize; - } - } - } } diff --git a/lib/coin.dart b/lib/coin.dart index 94efa7b..115a01e 100644 --- a/lib/coin.dart +++ b/lib/coin.dart @@ -32,12 +32,4 @@ class Coin extends MovingObject { gameRef.blockSize * (platform.width / platform.height / 14), ); } - - double getRightEnd() { - return sprite.position.x + sprite.width; - } - - void remove() { - sprite.remove(); - } } diff --git a/lib/coin_holder.dart b/lib/coin_holder.dart index d12fac2..107a768 100644 --- a/lib/coin_holder.dart +++ b/lib/coin_holder.dart @@ -1,5 +1,4 @@ -import 'dart:math'; - +import 'package:firo_runner/holder.dart'; import 'package:flame/flame.dart'; import 'package:firo_runner/coin.dart'; @@ -7,45 +6,31 @@ import 'package:firo_runner/main.dart'; import 'package:flame/extensions.dart'; import 'package:firo_runner/platform.dart'; -class CoinHolder { +class CoinHolder extends Holder { late Image coin; - Random random = Random(); - late List> coins = []; - - Future loadCoins() async { + @override + Future load() async { coin = await Flame.images.load("coin-frames.png"); } - void setUp() { - for (int i = 0; i < coins.length; i++) { - for (int j = 0; j < coins[i].length; j++) { - remove(coins[i], j); - } - } - coins = []; - for (int i = 0; i < 9; i++) { - coins.add([]); - } - } - - getCoin() { + Image getCoin() { return coin; } bool generateCoin(MyGame gameRef, int level, bool force) { - if (totalCoins() > 5) { + if (total() > 5) { return false; } - if (coins[level].isNotEmpty) { + if (objects[level].isNotEmpty) { return false; } if (random.nextInt(100) > 25) { return true; } else { - int nearestPlatform = gameRef.platformHolder.getNearestPlatform(level); + int nearestPlatform = getNearestPlatform(level); Platform? platform = gameRef.platformHolder.getPlatformOffScreen(nearestPlatform); @@ -66,51 +51,9 @@ class CoinHolder { return false; } - coins[level].add(coin); + objects[level].add(coin); gameRef.add(coin.sprite); } return false; } - - int totalCoins() { - int total = 0; - for (List levelCoins in coins) { - total += levelCoins.length; - } - return total; - } - - void update(double dt) { - for (List coinLevel in coins) { - for (Coin p in coinLevel) { - p.update(dt); - } - } - } - - void remove(List levelHolder, int j) { - levelHolder[j].remove(); - levelHolder[j].sprite.remove(); - levelHolder.removeAt(j); - } - - 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) { - remove(coinLevel, i); - continue; - } - i++; - } - } - } - - void resize(Vector2 newSize, double xRatio, double yRatio) { - for (List coinLevel in coins) { - for (Coin p in coinLevel) { - p.resize(newSize, xRatio, yRatio); - } - } - } } diff --git a/lib/holder.dart b/lib/holder.dart new file mode 100644 index 0000000..7546a1f --- /dev/null +++ b/lib/holder.dart @@ -0,0 +1,67 @@ +import 'dart:math'; + +import 'package:firo_runner/moving_object.dart'; + +import 'package:firo_runner/main.dart'; +import 'package:flame/extensions.dart'; + +class Holder { + Random random = Random(); + + late List> objects = []; + + Future load() async {} + + void setUp() { + for (int i = 0; i < objects.length; i++) { + for (int j = 0; j < objects[i].length; j++) { + remove(objects[i], j); + } + } + objects = []; + for (int i = 0; i < 9; i++) { + objects.add([]); + } + } + + int total() { + int total = 0; + for (List levelObjects in objects) { + total += levelObjects.length; + } + return total; + } + + void update(double dt) { + for (List objectLevel in objects) { + for (MovingObject p in objectLevel) { + p.update(dt); + } + } + } + + void remove(List levelHolder, int j) { + levelHolder[j].remove(); + levelHolder.removeAt(j); + } + + void removePast(MyGame gameRef) { + for (List objectLevel in objects) { + for (int i = 0; i < objectLevel.length;) { + if (objectLevel[i].sprite.x + objectLevel[i].sprite.width < 0) { + remove(objectLevel, i); + continue; + } + i++; + } + } + } + + void resize(Vector2 newSize, double xRatio, double yRatio) { + for (List platformLevel in objects) { + for (MovingObject p in platformLevel) { + p.resize(newSize, xRatio, yRatio); + } + } + } +} diff --git a/lib/main.dart b/lib/main.dart index e25eb1e..50caa53 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -5,6 +5,7 @@ import 'package:firo_runner/circuit_background.dart'; import 'package:firo_runner/coin_holder.dart'; import 'package:firo_runner/firework.dart'; import 'package:firo_runner/game_state.dart'; +import 'package:firo_runner/moving_object.dart'; import 'package:firo_runner/platform.dart'; import 'package:firo_runner/platform_holder.dart'; import 'package:firo_runner/wire.dart'; @@ -18,8 +19,6 @@ import 'package:flame/keyboard.dart'; import 'package:flame_audio/flame_audio.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; -import 'package:firo_runner/bug.dart'; -import 'package:firo_runner/coin.dart'; import 'package:firo_runner/runner.dart'; import 'package:flutter/foundation.dart' show kIsWeb; @@ -48,6 +47,16 @@ void main() async { runApp(GameWidget(game: myGame)); } +int getNearestPlatform(int level) { + return level <= 0 + ? 0 + : level <= 3 + ? 2 + : level <= 6 + ? 5 + : 8; +} + class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents { TextPaint fireworksPaint = TextPaint( config: const TextPaintConfig( @@ -85,13 +94,13 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents { circuitBackground = CircuitBackground(this); await circuitBackground.load(); platformHolder = PlatformHolder(); - await platformHolder.loadPlatforms(); + await platformHolder.load(); coinHolder = CoinHolder(); - await coinHolder.loadCoins(); + await coinHolder.load(); wireHolder = WireHolder(); - await wireHolder.loadWires(); + await wireHolder.load(); bugHolder = BugHolder(); - await bugHolder.loadBugs(); + await bugHolder.load(); fireworks = Firework(this); await fireworks.load(); @@ -143,24 +152,24 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents { 3 * rect.top - 2 * rect.bottom - 1, 3 * rect.right - 2 * rect.left + 1, 3 * rect.bottom - 2 * rect.top + 1); - for (List wireLevel in wireHolder.wires) { - for (Wire wire in wireLevel) { + for (List wireLevel in wireHolder.objects) { + for (MovingObject wire in wireLevel) { if (wire.intersect(obstacleBounds) != "none") { return true; } } } - for (List coinLevel in coinHolder.coins) { - for (Coin coin in coinLevel) { + for (List coinLevel in coinHolder.objects) { + for (MovingObject coin in coinLevel) { if (coin.intersect(obstacleBounds) != "none") { return true; } } } - for (List bugLevel in bugHolder.bugs) { - for (Bug bug in bugLevel) { + for (List bugLevel in bugHolder.objects) { + for (MovingObject bug in bugLevel) { if (bug.intersect(obstacleBounds) != "none") { return true; } @@ -204,8 +213,8 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents { runner.setUp(); fillScreen(); - platformHolder.platforms[2][0].sprite.current = PlatformState.left; - platformHolder.platforms[5][0].sprite.current = PlatformState.left; + platformHolder.objects[2][0].sprite.current = PlatformState.left; + platformHolder.objects[5][0].sprite.current = PlatformState.left; } @override diff --git a/lib/moving_object.dart b/lib/moving_object.dart index 479de1a..6a5b7fd 100644 --- a/lib/moving_object.dart +++ b/lib/moving_object.dart @@ -26,6 +26,14 @@ class MovingObject { sprite.position = sprite.position - Vector2(velocity * dt, 0); } + double getRightEnd() { + return sprite.position.x + sprite.width; + } + + void remove() { + sprite.remove(); + } + String intersect(Rect other) { final collision = sprite.toRect().intersect(other); if (!collision.isEmpty) { diff --git a/lib/platform.dart b/lib/platform.dart index af000ef..85d06f7 100644 --- a/lib/platform.dart +++ b/lib/platform.dart @@ -78,11 +78,13 @@ class Platform extends MovingObject { ); } - double getRightEnd() { - return sprite.position.x + sprite.width; + @override + void remove() { + removeChildrenObjects(); + super.remove(); } - void remove() { + void removeChildrenObjects() { if (removeChildren.isNotEmpty) { for (Function removeChild in removeChildren) { removeChild(); @@ -92,7 +94,7 @@ class Platform extends MovingObject { @override void update(double dt) { - List platformLevel = gameRef.platformHolder.platforms[row]; + List platformLevel = gameRef.platformHolder.objects[row]; int index = platformLevel.indexOf(this); Vector2 right = Vector2(-200, -200); if (index + 1 < platformLevel.length) { diff --git a/lib/platform_holder.dart b/lib/platform_holder.dart index 4d3703d..65ee336 100644 --- a/lib/platform_holder.dart +++ b/lib/platform_holder.dart @@ -1,13 +1,11 @@ -import 'dart:math'; - -import 'package:firo_runner/bug.dart'; +import 'package:firo_runner/holder.dart'; import 'package:firo_runner/main.dart'; -import 'package:firo_runner/wire.dart'; +import 'package:firo_runner/moving_object.dart'; import 'package:flame/flame.dart'; import 'package:firo_runner/platform.dart'; import 'package:flame/extensions.dart'; -class PlatformHolder { +class PlatformHolder extends Holder { late Image l1; late Image l2; late Image m1; @@ -16,12 +14,11 @@ class PlatformHolder { late Image r2; late Image o1; late Image o2; - late List> platforms = []; int timeSinceLastTopHole = 0; int timeSinceLastBottomHole = 0; - Random random = Random(); - Future loadPlatforms() async { + @override + Future load() async { l1 = await Flame.images.load('platform-left-nowire-frames.png'); l2 = await Flame.images.load('platform-left-wire-frames.png'); m1 = await Flame.images.load('platform-mid-nowire-frames.png'); @@ -32,47 +29,40 @@ class PlatformHolder { o2 = await Flame.images.load('platform-single-wire-frames.png'); } + @override 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); - } - } - platforms = []; - for (int i = 0; i < 9; i++) { - platforms.add([]); - } + super.setUp(); } void removeUnfairObstacles( MyGame gameRef, Platform currentPlatform, int from, int to) { for (int i = from; i <= to; i++) { if (i == 0) { - for (Bug bug in gameRef.bugHolder.bugs[0]) { + List bugLevel = gameRef.bugHolder.objects[0]; + for (MovingObject bug in gameRef.bugHolder.objects[0]) { if (bug.sprite.x >= currentPlatform.sprite.x && bug.sprite.x < currentPlatform.sprite.x + 4 * currentPlatform.sprite.width) { - gameRef.bugHolder.bugs[0].remove(bug); - bug.remove(); + gameRef.bugHolder.remove(bugLevel, bugLevel.indexOf(bug)); } } - for (Wire wire in gameRef.wireHolder.wires[0]) { + List wireLevel = gameRef.wireHolder.objects[0]; + for (MovingObject wire in gameRef.wireHolder.objects[0]) { if (wire.sprite.x >= currentPlatform.sprite.x && wire.sprite.x < currentPlatform.sprite.x + 4 * currentPlatform.sprite.width) { - gameRef.wireHolder.wires[0].remove(wire); - wire.remove(); + gameRef.wireHolder.remove(wireLevel, wireLevel.indexOf(wire)); } } } else { int nearestPlatform = getNearestPlatform(i); - for (Platform platform in platforms[nearestPlatform]) { + for (MovingObject platform in objects[nearestPlatform]) { if (platform.sprite.x >= currentPlatform.sprite.x && platform.sprite.x < currentPlatform.sprite.x + 4 * currentPlatform.sprite.width) { - platform.remove(); + (platform as Platform).removeChildrenObjects(); platform.prohibitObstacles = true; } } @@ -80,16 +70,6 @@ class PlatformHolder { } } - int getNearestPlatform(int level) { - return level <= 0 - ? 0 - : level <= 3 - ? 2 - : level <= 6 - ? 5 - : 8; - } - void generatePlatforms(MyGame gameRef) { while (!generatePlatform(gameRef, 2)) { timeSinceLastTopHole++; @@ -105,29 +85,31 @@ class PlatformHolder { if (topChance > 50) { removeUnfairObstacles( - gameRef, platforms[2][platforms[2].length - 4], 0, 4); + gameRef, objects[2][objects[2].length - 4] as Platform, 0, 4); // Create an opening in the top. - remove(platforms[2], platforms[2].length - 2); - remove(platforms[2], platforms[2].length - 2); + remove(objects[2], objects[2].length - 2); + remove(objects[2], objects[2].length - 2); timeSinceLastTopHole = 0; } if (bottomChance > 30) { - Platform start = platforms[5].elementAt(platforms[5].length - 10); + Platform start = objects[5].elementAt(objects[5].length - 10) as Platform; 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; + int lastToRemove = objects[5].length - 3; + int firstToRemove = objects[5].length - 10; - removeUnfairObstacles(gameRef, platforms[5][lastToRemove - 1], 3, 7); - remove(platforms[5], lastToRemove); - remove(platforms[5], lastToRemove); + removeUnfairObstacles( + gameRef, objects[5][lastToRemove - 1] as Platform, 3, 7); + remove(objects[5], lastToRemove); + remove(objects[5], lastToRemove); - removeUnfairObstacles(gameRef, platforms[5][firstToRemove - 1], 3, 7); - remove(platforms[5], firstToRemove); - remove(platforms[5], firstToRemove); + removeUnfairObstacles( + gameRef, objects[5][firstToRemove - 1] as Platform, 3, 7); + remove(objects[5], firstToRemove); + remove(objects[5], firstToRemove); timeSinceLastBottomHole = 0; } @@ -135,8 +117,8 @@ class PlatformHolder { bool generatePlatform(MyGame gameRef, int level, {double xPosition = 0}) { double xCoordinate = xPosition; - if (platforms[level].isNotEmpty && xPosition == 0) { - xCoordinate = platforms[level].last.getRightEnd(); + if (objects[level].isNotEmpty && xPosition == 0) { + xCoordinate = objects[level].last.getRightEnd(); } if (xCoordinate > gameRef.size.x + 2000) { @@ -146,64 +128,30 @@ class PlatformHolder { platform.setPosition(xCoordinate, gameRef.blockSize * level); platform.row = level; gameRef.add(platform.sprite); - platforms[level].add(platform); + objects[level].add(platform); return false; } } - void update(double dt) { - for (List platformLevel in platforms) { - for (Platform p in platformLevel) { - p.update(dt); - } - } - } - - void remove(List levelHolder, int j) { - levelHolder[j].remove(); - levelHolder[j].sprite.remove(); - levelHolder.removeAt(j); - } - - void removePast(MyGame gameRef) { - for (List platformLevel in platforms) { - while (platformLevel.isNotEmpty && - platformLevel[0].sprite.position.x + platformLevel[0].sprite.width < - 0) { - remove(platformLevel, 0); - } - } - } - double getFlushX() { - Platform platform = - platforms[2].firstWhere((element) => element.sprite.x > 0, orElse: () { - return platforms[5].firstWhere((element) => element.sprite.x > 0, + MovingObject platform = + objects[2].firstWhere((element) => element.sprite.x > 0, orElse: () { + return objects[5].firstWhere((element) => element.sprite.x > 0, orElse: () { - return platforms[8].firstWhere((element) => element.sprite.x > 0); + return objects[8].firstWhere((element) => element.sprite.x > 0); }); }); return platform.sprite.x; } Platform? getPlatformOffScreen(int level) { - for (int i = 0; i < platforms[level].length; i++) { - Platform p = platforms[level][i]; + for (int i = 0; i < objects[level].length; i++) { + Platform p = objects[level][i] as Platform; if (p.sprite.x > p.gameRef.size.x) { - int chosenIndex = random.nextInt(platforms[level].length - i) + i; - return platforms[level][chosenIndex]; + int chosenIndex = random.nextInt(objects[level].length - i) + i; + return objects[level][chosenIndex] as Platform; } } return null; } - - void resize(Vector2 newSize, double xRatio, double yRatio) { - for (List platformLevel in platforms) { - for (Platform p in platformLevel) { - p.resize(newSize, xRatio, yRatio); - p.sprite.y = (p.sprite.position.y / p.gameRef.blockSize).round() * - p.gameRef.blockSize; - } - } - } } diff --git a/lib/runner.dart b/lib/runner.dart index 2093791..a25cbf3 100644 --- a/lib/runner.dart +++ b/lib/runner.dart @@ -1,11 +1,9 @@ import 'package:firo_runner/bug.dart'; -import 'package:firo_runner/coin.dart'; -import 'package:firo_runner/wire.dart'; +import 'package:firo_runner/moving_object.dart'; import 'package:firo_runner/main.dart'; import 'package:flame/effects.dart'; import 'package:flame/extensions.dart'; import 'package:flutter/material.dart'; -import 'package:firo_runner/platform.dart'; import 'package:flame/components.dart'; @@ -278,8 +276,8 @@ class Runner extends Component with HasGameRef { bool onTopOfPlatform() { Rect runnerRect = sprite.toRect(); bool onTopOfPlatform = false; - for (List platformLevel in gameRef.platformHolder.platforms) { - for (Platform p in platformLevel) { + for (List platformLevel in gameRef.platformHolder.objects) { + for (MovingObject p in platformLevel) { String side = p.intersect(runnerRect); if (side == "none") { Rect belowRunner = Rect.fromLTRB(runnerRect.left, runnerRect.top, @@ -296,8 +294,8 @@ class Runner extends Component with HasGameRef { bool belowPlatform() { Rect runnerRect = sprite.toRect(); bool belowPlatform = false; - for (List platformLevel in gameRef.platformHolder.platforms) { - for (Platform p in platformLevel) { + for (List platformLevel in gameRef.platformHolder.objects) { + for (MovingObject p in platformLevel) { String side = p.intersect(runnerRect); if (side == "none") { Rect belowRunner = Rect.fromLTRB(runnerRect.left, runnerRect.top - 1, @@ -318,7 +316,7 @@ class Runner extends Component with HasGameRef { Rect runnerRect = sprite.toRect(); bool onTopOfPlatform = this.onTopOfPlatform(); - for (List coinLevel in gameRef.coinHolder.coins) { + for (List coinLevel in gameRef.coinHolder.objects) { for (int i = 0; i < coinLevel.length;) { if (coinLevel[i].intersect(runnerRect) != "none") { gameRef.gameState.numCoins++; @@ -332,7 +330,7 @@ class Runner extends Component with HasGameRef { } } - for (List wireLevel in gameRef.wireHolder.wires) { + for (List wireLevel in gameRef.wireHolder.objects) { for (int i = 0; i < wireLevel.length; i++) { if (wireLevel[i].intersect(runnerRect) != "none") { event("electrocute"); @@ -341,7 +339,7 @@ class Runner extends Component with HasGameRef { } } - for (List bugLevel in gameRef.bugHolder.bugs) { + for (List bugLevel in gameRef.bugHolder.objects) { for (int i = 0; i < bugLevel.length; i++) { String intersectState = bugLevel[i].intersect(runnerRect); if (bugLevel[i].sprite.current == BugState.breaking) { diff --git a/lib/wire.dart b/lib/wire.dart index 61b62c7..63e8501 100644 --- a/lib/wire.dart +++ b/lib/wire.dart @@ -32,14 +32,6 @@ class Wire extends MovingObject { ); } - double getRightEnd() { - return sprite.position.x + sprite.width; - } - - void remove() { - sprite.remove(); - } - @override String intersect(Rect other) { Rect currentRect = sprite.toRect(); diff --git a/lib/wire_holder.dart b/lib/wire_holder.dart index e43a54e..b123d19 100644 --- a/lib/wire_holder.dart +++ b/lib/wire_holder.dart @@ -1,5 +1,4 @@ -import 'dart:math'; - +import 'package:firo_runner/holder.dart'; import 'package:firo_runner/platform.dart'; import 'package:flame/flame.dart'; @@ -7,41 +6,27 @@ import 'package:firo_runner/wire.dart'; import 'package:firo_runner/main.dart'; import 'package:flame/extensions.dart'; -class WireHolder { +class WireHolder extends Holder { late Image wire; - Random random = Random(); - late List> wires = []; - - Future loadWires() async { + @override + Future load() async { wire = await Flame.images.load("wire-frames.png"); } - void setUp() { - for (int i = 0; i < wires.length; i++) { - for (int j = 0; j < wires[i].length; j++) { - remove(wires[i], j); - } - } - wires = []; - for (int i = 0; i < 9; i++) { - wires.add([]); - } - } - getWire() { return wire; } bool generateWire(MyGame gameRef, int level, bool force) { - if (wires[level].isNotEmpty) { + if (objects[level].isNotEmpty) { return false; } if (random.nextInt(100) > 100) { return true; } else { - int nearestPlatform = gameRef.platformHolder.getNearestPlatform(level); + int nearestPlatform = getNearestPlatform(level); Platform? platform = gameRef.platformHolder.getPlatformOffScreen(nearestPlatform); @@ -73,57 +58,15 @@ class WireHolder { return false; } - wires[level].add(wire); + objects[level].add(wire); gameRef.add(wire.sprite); if (platform != null) { platform.removeChildren.add(() { - wires[level].remove(wire); + objects[level].remove(wire); wire.remove(); }); } return false; } } - - int totalWires() { - int total = 0; - for (List levelWires in wires) { - total += levelWires.length; - } - return total; - } - - void update(double dt) { - for (List wireLevel in wires) { - for (Wire p in wireLevel) { - p.update(dt); - } - } - } - - void remove(List levelHolder, int j) { - levelHolder[j].remove(); - levelHolder[j].sprite.remove(); - levelHolder.removeAt(j); - } - - void removePast(MyGame gameRef) { - for (List wireLevel in wires) { - for (int i = 0; i < wireLevel.length;) { - if (wireLevel[i].sprite.x + wireLevel[i].sprite.width < 0) { - remove(wireLevel, i); - continue; - } - i++; - } - } - } - - void resize(Vector2 newSize, double xRatio, double yRatio) { - for (List platformLevel in wires) { - for (Wire p in platformLevel) { - p.resize(newSize, xRatio, yRatio); - } - } - } }