Cleaning up the code so it is more generalized.

This commit is contained in:
Marco Salazar 2021-09-16 15:15:32 -06:00
parent 5e48cb0444
commit 42873d274a
12 changed files with 177 additions and 342 deletions

View File

@ -42,12 +42,4 @@ class Bug extends MovingObject {
gameRef.blockSize, gameRef.blockSize,
); );
} }
double getRightEnd() {
return sprite.position.x + sprite.width;
}
void remove() {
sprite.remove();
}
} }

View File

@ -1,5 +1,4 @@
import 'dart:math'; import 'package:firo_runner/holder.dart';
import 'package:firo_runner/platform.dart'; import 'package:firo_runner/platform.dart';
import 'package:flame/extensions.dart'; import 'package:flame/extensions.dart';
import 'package:flame/flame.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/bug.dart';
import 'package:firo_runner/main.dart'; import 'package:firo_runner/main.dart';
class BugHolder { class BugHolder extends Holder {
late Image bug; late Image bug;
late Image breaking; late Image breaking;
Random random = Random();
late List<List<Bug>> bugs = []; @override
Future load() async {
Future loadBugs() async {
bug = await Flame.images.load("bug-frames.png"); bug = await Flame.images.load("bug-frames.png");
breaking = await Flame.images.load("bug-break-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) { getBug(String state) {
switch (state) { switch (state) {
case "normal": case "normal":
@ -41,14 +26,14 @@ class BugHolder {
} }
bool generateBug(MyGame gameRef, int level, bool force) { bool generateBug(MyGame gameRef, int level, bool force) {
if (bugs[level].isNotEmpty) { if (objects[level].isNotEmpty) {
return false; return false;
} }
if (random.nextInt(100) > 25) { if (random.nextInt(100) > 25) {
return true; return true;
} else { } else {
int nearestPlatform = gameRef.platformHolder.getNearestPlatform(level); int nearestPlatform = getNearestPlatform(level);
Platform? platform = Platform? platform =
gameRef.platformHolder.getPlatformOffScreen(nearestPlatform); gameRef.platformHolder.getPlatformOffScreen(nearestPlatform);
@ -72,59 +57,15 @@ class BugHolder {
return false; return false;
} }
bugs[level].add(bug); objects[level].add(bug);
gameRef.add(bug.sprite); gameRef.add(bug.sprite);
if (platform != null) { if (platform != null) {
platform.removeChildren.add(() { platform.removeChildren.add(() {
bugs[level].remove(bug); objects[level].remove(bug);
bug.remove(); bug.remove();
}); });
} }
return false; return false;
} }
} }
int totalBugs() {
int total = 0;
for (List<Bug> levelBugs in bugs) {
total += levelBugs.length;
}
return total;
}
void update(double dt) {
for (List<Bug> bugLevel in bugs) {
for (Bug p in bugLevel) {
p.update(dt);
}
}
}
void remove(List<Bug> levelHolder, int j) {
levelHolder[j].remove();
levelHolder[j].sprite.remove();
levelHolder.removeAt(j);
}
void removePast(MyGame gameRef) {
for (List<Bug> 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<Bug> 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;
}
}
}
} }

View File

@ -32,12 +32,4 @@ class Coin extends MovingObject {
gameRef.blockSize * (platform.width / platform.height / 14), gameRef.blockSize * (platform.width / platform.height / 14),
); );
} }
double getRightEnd() {
return sprite.position.x + sprite.width;
}
void remove() {
sprite.remove();
}
} }

View File

@ -1,5 +1,4 @@
import 'dart:math'; import 'package:firo_runner/holder.dart';
import 'package:flame/flame.dart'; import 'package:flame/flame.dart';
import 'package:firo_runner/coin.dart'; import 'package:firo_runner/coin.dart';
@ -7,45 +6,31 @@ import 'package:firo_runner/main.dart';
import 'package:flame/extensions.dart'; import 'package:flame/extensions.dart';
import 'package:firo_runner/platform.dart'; import 'package:firo_runner/platform.dart';
class CoinHolder { class CoinHolder extends Holder {
late Image coin; late Image coin;
Random random = Random();
late List<List<Coin>> coins = []; @override
Future load() async {
Future loadCoins() async {
coin = await Flame.images.load("coin-frames.png"); coin = await Flame.images.load("coin-frames.png");
} }
void setUp() { Image getCoin() {
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() {
return coin; return coin;
} }
bool generateCoin(MyGame gameRef, int level, bool force) { bool generateCoin(MyGame gameRef, int level, bool force) {
if (totalCoins() > 5) { if (total() > 5) {
return false; return false;
} }
if (coins[level].isNotEmpty) { if (objects[level].isNotEmpty) {
return false; return false;
} }
if (random.nextInt(100) > 25) { if (random.nextInt(100) > 25) {
return true; return true;
} else { } else {
int nearestPlatform = gameRef.platformHolder.getNearestPlatform(level); int nearestPlatform = getNearestPlatform(level);
Platform? platform = Platform? platform =
gameRef.platformHolder.getPlatformOffScreen(nearestPlatform); gameRef.platformHolder.getPlatformOffScreen(nearestPlatform);
@ -66,51 +51,9 @@ class CoinHolder {
return false; return false;
} }
coins[level].add(coin); objects[level].add(coin);
gameRef.add(coin.sprite); gameRef.add(coin.sprite);
} }
return false; return false;
} }
int totalCoins() {
int total = 0;
for (List<Coin> levelCoins in coins) {
total += levelCoins.length;
}
return total;
}
void update(double dt) {
for (List<Coin> coinLevel in coins) {
for (Coin p in coinLevel) {
p.update(dt);
}
}
}
void remove(List<Coin> levelHolder, int j) {
levelHolder[j].remove();
levelHolder[j].sprite.remove();
levelHolder.removeAt(j);
}
void removePast(MyGame gameRef) {
for (List<Coin> 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<Coin> coinLevel in coins) {
for (Coin p in coinLevel) {
p.resize(newSize, xRatio, yRatio);
}
}
}
} }

67
lib/holder.dart Normal file
View File

@ -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<List<MovingObject>> 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<MovingObject> levelObjects in objects) {
total += levelObjects.length;
}
return total;
}
void update(double dt) {
for (List<MovingObject> objectLevel in objects) {
for (MovingObject p in objectLevel) {
p.update(dt);
}
}
}
void remove(List<MovingObject> levelHolder, int j) {
levelHolder[j].remove();
levelHolder.removeAt(j);
}
void removePast(MyGame gameRef) {
for (List<MovingObject> 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<MovingObject> platformLevel in objects) {
for (MovingObject p in platformLevel) {
p.resize(newSize, xRatio, yRatio);
}
}
}
}

View File

@ -5,6 +5,7 @@ import 'package:firo_runner/circuit_background.dart';
import 'package:firo_runner/coin_holder.dart'; import 'package:firo_runner/coin_holder.dart';
import 'package:firo_runner/firework.dart'; import 'package:firo_runner/firework.dart';
import 'package:firo_runner/game_state.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.dart';
import 'package:firo_runner/platform_holder.dart'; import 'package:firo_runner/platform_holder.dart';
import 'package:firo_runner/wire.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:flame_audio/flame_audio.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.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:firo_runner/runner.dart';
import 'package:flutter/foundation.dart' show kIsWeb; import 'package:flutter/foundation.dart' show kIsWeb;
@ -48,6 +47,16 @@ void main() async {
runApp(GameWidget(game: myGame)); 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 { class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
TextPaint fireworksPaint = TextPaint( TextPaint fireworksPaint = TextPaint(
config: const TextPaintConfig( config: const TextPaintConfig(
@ -85,13 +94,13 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
circuitBackground = CircuitBackground(this); circuitBackground = CircuitBackground(this);
await circuitBackground.load(); await circuitBackground.load();
platformHolder = PlatformHolder(); platformHolder = PlatformHolder();
await platformHolder.loadPlatforms(); await platformHolder.load();
coinHolder = CoinHolder(); coinHolder = CoinHolder();
await coinHolder.loadCoins(); await coinHolder.load();
wireHolder = WireHolder(); wireHolder = WireHolder();
await wireHolder.loadWires(); await wireHolder.load();
bugHolder = BugHolder(); bugHolder = BugHolder();
await bugHolder.loadBugs(); await bugHolder.load();
fireworks = Firework(this); fireworks = Firework(this);
await fireworks.load(); await fireworks.load();
@ -143,24 +152,24 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
3 * rect.top - 2 * rect.bottom - 1, 3 * rect.top - 2 * rect.bottom - 1,
3 * rect.right - 2 * rect.left + 1, 3 * rect.right - 2 * rect.left + 1,
3 * rect.bottom - 2 * rect.top + 1); 3 * rect.bottom - 2 * rect.top + 1);
for (List<Wire> wireLevel in wireHolder.wires) { for (List<MovingObject> wireLevel in wireHolder.objects) {
for (Wire wire in wireLevel) { for (MovingObject wire in wireLevel) {
if (wire.intersect(obstacleBounds) != "none") { if (wire.intersect(obstacleBounds) != "none") {
return true; return true;
} }
} }
} }
for (List<Coin> coinLevel in coinHolder.coins) { for (List<MovingObject> coinLevel in coinHolder.objects) {
for (Coin coin in coinLevel) { for (MovingObject coin in coinLevel) {
if (coin.intersect(obstacleBounds) != "none") { if (coin.intersect(obstacleBounds) != "none") {
return true; return true;
} }
} }
} }
for (List<Bug> bugLevel in bugHolder.bugs) { for (List<MovingObject> bugLevel in bugHolder.objects) {
for (Bug bug in bugLevel) { for (MovingObject bug in bugLevel) {
if (bug.intersect(obstacleBounds) != "none") { if (bug.intersect(obstacleBounds) != "none") {
return true; return true;
} }
@ -204,8 +213,8 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
runner.setUp(); runner.setUp();
fillScreen(); fillScreen();
platformHolder.platforms[2][0].sprite.current = PlatformState.left; platformHolder.objects[2][0].sprite.current = PlatformState.left;
platformHolder.platforms[5][0].sprite.current = PlatformState.left; platformHolder.objects[5][0].sprite.current = PlatformState.left;
} }
@override @override

View File

@ -26,6 +26,14 @@ class MovingObject {
sprite.position = sprite.position - Vector2(velocity * dt, 0); sprite.position = sprite.position - Vector2(velocity * dt, 0);
} }
double getRightEnd() {
return sprite.position.x + sprite.width;
}
void remove() {
sprite.remove();
}
String intersect(Rect other) { String intersect(Rect other) {
final collision = sprite.toRect().intersect(other); final collision = sprite.toRect().intersect(other);
if (!collision.isEmpty) { if (!collision.isEmpty) {

View File

@ -78,11 +78,13 @@ class Platform extends MovingObject {
); );
} }
double getRightEnd() { @override
return sprite.position.x + sprite.width; void remove() {
removeChildrenObjects();
super.remove();
} }
void remove() { void removeChildrenObjects() {
if (removeChildren.isNotEmpty) { if (removeChildren.isNotEmpty) {
for (Function removeChild in removeChildren) { for (Function removeChild in removeChildren) {
removeChild(); removeChild();
@ -92,7 +94,7 @@ class Platform extends MovingObject {
@override @override
void update(double dt) { void update(double dt) {
List<Platform> platformLevel = gameRef.platformHolder.platforms[row]; List<MovingObject> platformLevel = gameRef.platformHolder.objects[row];
int index = platformLevel.indexOf(this); int index = platformLevel.indexOf(this);
Vector2 right = Vector2(-200, -200); Vector2 right = Vector2(-200, -200);
if (index + 1 < platformLevel.length) { if (index + 1 < platformLevel.length) {

View File

@ -1,13 +1,11 @@
import 'dart:math'; import 'package:firo_runner/holder.dart';
import 'package:firo_runner/bug.dart';
import 'package:firo_runner/main.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:flame/flame.dart';
import 'package:firo_runner/platform.dart'; import 'package:firo_runner/platform.dart';
import 'package:flame/extensions.dart'; import 'package:flame/extensions.dart';
class PlatformHolder { class PlatformHolder extends Holder {
late Image l1; late Image l1;
late Image l2; late Image l2;
late Image m1; late Image m1;
@ -16,12 +14,11 @@ class PlatformHolder {
late Image r2; late Image r2;
late Image o1; late Image o1;
late Image o2; late Image o2;
late List<List<Platform>> platforms = [];
int timeSinceLastTopHole = 0; int timeSinceLastTopHole = 0;
int timeSinceLastBottomHole = 0; int timeSinceLastBottomHole = 0;
Random random = Random();
Future loadPlatforms() async { @override
Future load() async {
l1 = await Flame.images.load('platform-left-nowire-frames.png'); l1 = await Flame.images.load('platform-left-nowire-frames.png');
l2 = await Flame.images.load('platform-left-wire-frames.png'); l2 = await Flame.images.load('platform-left-wire-frames.png');
m1 = await Flame.images.load('platform-mid-nowire-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'); o2 = await Flame.images.load('platform-single-wire-frames.png');
} }
@override
void setUp() { void setUp() {
timeSinceLastTopHole = 0; timeSinceLastTopHole = 0;
timeSinceLastBottomHole = 0; timeSinceLastBottomHole = 0;
for (int i = 0; i < platforms.length; i++) { super.setUp();
for (int j = 0; j < platforms[i].length; j++) {
remove(platforms[i], j);
}
}
platforms = [];
for (int i = 0; i < 9; i++) {
platforms.add([]);
}
} }
void removeUnfairObstacles( void removeUnfairObstacles(
MyGame gameRef, Platform currentPlatform, int from, int to) { MyGame gameRef, Platform currentPlatform, int from, int to) {
for (int i = from; i <= to; i++) { for (int i = from; i <= to; i++) {
if (i == 0) { if (i == 0) {
for (Bug bug in gameRef.bugHolder.bugs[0]) { List<MovingObject> bugLevel = gameRef.bugHolder.objects[0];
for (MovingObject bug in gameRef.bugHolder.objects[0]) {
if (bug.sprite.x >= currentPlatform.sprite.x && if (bug.sprite.x >= currentPlatform.sprite.x &&
bug.sprite.x < bug.sprite.x <
currentPlatform.sprite.x + 4 * currentPlatform.sprite.width) { currentPlatform.sprite.x + 4 * currentPlatform.sprite.width) {
gameRef.bugHolder.bugs[0].remove(bug); gameRef.bugHolder.remove(bugLevel, bugLevel.indexOf(bug));
bug.remove();
} }
} }
for (Wire wire in gameRef.wireHolder.wires[0]) { List<MovingObject> wireLevel = gameRef.wireHolder.objects[0];
for (MovingObject wire in gameRef.wireHolder.objects[0]) {
if (wire.sprite.x >= currentPlatform.sprite.x && if (wire.sprite.x >= currentPlatform.sprite.x &&
wire.sprite.x < wire.sprite.x <
currentPlatform.sprite.x + 4 * currentPlatform.sprite.width) { currentPlatform.sprite.x + 4 * currentPlatform.sprite.width) {
gameRef.wireHolder.wires[0].remove(wire); gameRef.wireHolder.remove(wireLevel, wireLevel.indexOf(wire));
wire.remove();
} }
} }
} else { } else {
int nearestPlatform = getNearestPlatform(i); int nearestPlatform = getNearestPlatform(i);
for (Platform platform in platforms[nearestPlatform]) { for (MovingObject platform in objects[nearestPlatform]) {
if (platform.sprite.x >= currentPlatform.sprite.x && if (platform.sprite.x >= currentPlatform.sprite.x &&
platform.sprite.x < platform.sprite.x <
currentPlatform.sprite.x + 4 * currentPlatform.sprite.width) { currentPlatform.sprite.x + 4 * currentPlatform.sprite.width) {
platform.remove(); (platform as Platform).removeChildrenObjects();
platform.prohibitObstacles = true; 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) { void generatePlatforms(MyGame gameRef) {
while (!generatePlatform(gameRef, 2)) { while (!generatePlatform(gameRef, 2)) {
timeSinceLastTopHole++; timeSinceLastTopHole++;
@ -105,29 +85,31 @@ class PlatformHolder {
if (topChance > 50) { if (topChance > 50) {
removeUnfairObstacles( 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. // Create an opening in the top.
remove(platforms[2], platforms[2].length - 2); remove(objects[2], objects[2].length - 2);
remove(platforms[2], platforms[2].length - 2); remove(objects[2], objects[2].length - 2);
timeSinceLastTopHole = 0; timeSinceLastTopHole = 0;
} }
if (bottomChance > 30) { 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); generatePlatform(gameRef, 8, xPosition: start.sprite.position.x);
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
generatePlatform(gameRef, 8); generatePlatform(gameRef, 8);
} }
int lastToRemove = platforms[5].length - 3; int lastToRemove = objects[5].length - 3;
int firstToRemove = platforms[5].length - 10; int firstToRemove = objects[5].length - 10;
removeUnfairObstacles(gameRef, platforms[5][lastToRemove - 1], 3, 7); removeUnfairObstacles(
remove(platforms[5], lastToRemove); gameRef, objects[5][lastToRemove - 1] as Platform, 3, 7);
remove(platforms[5], lastToRemove); remove(objects[5], lastToRemove);
remove(objects[5], lastToRemove);
removeUnfairObstacles(gameRef, platforms[5][firstToRemove - 1], 3, 7); removeUnfairObstacles(
remove(platforms[5], firstToRemove); gameRef, objects[5][firstToRemove - 1] as Platform, 3, 7);
remove(platforms[5], firstToRemove); remove(objects[5], firstToRemove);
remove(objects[5], firstToRemove);
timeSinceLastBottomHole = 0; timeSinceLastBottomHole = 0;
} }
@ -135,8 +117,8 @@ class PlatformHolder {
bool generatePlatform(MyGame gameRef, int level, {double xPosition = 0}) { bool generatePlatform(MyGame gameRef, int level, {double xPosition = 0}) {
double xCoordinate = xPosition; double xCoordinate = xPosition;
if (platforms[level].isNotEmpty && xPosition == 0) { if (objects[level].isNotEmpty && xPosition == 0) {
xCoordinate = platforms[level].last.getRightEnd(); xCoordinate = objects[level].last.getRightEnd();
} }
if (xCoordinate > gameRef.size.x + 2000) { if (xCoordinate > gameRef.size.x + 2000) {
@ -146,64 +128,30 @@ class PlatformHolder {
platform.setPosition(xCoordinate, gameRef.blockSize * level); platform.setPosition(xCoordinate, gameRef.blockSize * level);
platform.row = level; platform.row = level;
gameRef.add(platform.sprite); gameRef.add(platform.sprite);
platforms[level].add(platform); objects[level].add(platform);
return false; return false;
} }
} }
void update(double dt) {
for (List<Platform> platformLevel in platforms) {
for (Platform p in platformLevel) {
p.update(dt);
}
}
}
void remove(List<Platform> levelHolder, int j) {
levelHolder[j].remove();
levelHolder[j].sprite.remove();
levelHolder.removeAt(j);
}
void removePast(MyGame gameRef) {
for (List<Platform> platformLevel in platforms) {
while (platformLevel.isNotEmpty &&
platformLevel[0].sprite.position.x + platformLevel[0].sprite.width <
0) {
remove(platformLevel, 0);
}
}
}
double getFlushX() { double getFlushX() {
Platform platform = MovingObject platform =
platforms[2].firstWhere((element) => element.sprite.x > 0, orElse: () { objects[2].firstWhere((element) => element.sprite.x > 0, orElse: () {
return platforms[5].firstWhere((element) => element.sprite.x > 0, return objects[5].firstWhere((element) => element.sprite.x > 0,
orElse: () { orElse: () {
return platforms[8].firstWhere((element) => element.sprite.x > 0); return objects[8].firstWhere((element) => element.sprite.x > 0);
}); });
}); });
return platform.sprite.x; return platform.sprite.x;
} }
Platform? getPlatformOffScreen(int level) { Platform? getPlatformOffScreen(int level) {
for (int i = 0; i < platforms[level].length; i++) { for (int i = 0; i < objects[level].length; i++) {
Platform p = platforms[level][i]; Platform p = objects[level][i] as Platform;
if (p.sprite.x > p.gameRef.size.x) { if (p.sprite.x > p.gameRef.size.x) {
int chosenIndex = random.nextInt(platforms[level].length - i) + i; int chosenIndex = random.nextInt(objects[level].length - i) + i;
return platforms[level][chosenIndex]; return objects[level][chosenIndex] as Platform;
} }
} }
return null; return null;
} }
void resize(Vector2 newSize, double xRatio, double yRatio) {
for (List<Platform> 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;
}
}
}
} }

View File

@ -1,11 +1,9 @@
import 'package:firo_runner/bug.dart'; import 'package:firo_runner/bug.dart';
import 'package:firo_runner/coin.dart'; import 'package:firo_runner/moving_object.dart';
import 'package:firo_runner/wire.dart';
import 'package:firo_runner/main.dart'; import 'package:firo_runner/main.dart';
import 'package:flame/effects.dart'; import 'package:flame/effects.dart';
import 'package:flame/extensions.dart'; import 'package:flame/extensions.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:firo_runner/platform.dart';
import 'package:flame/components.dart'; import 'package:flame/components.dart';
@ -278,8 +276,8 @@ class Runner extends Component with HasGameRef<MyGame> {
bool onTopOfPlatform() { bool onTopOfPlatform() {
Rect runnerRect = sprite.toRect(); Rect runnerRect = sprite.toRect();
bool onTopOfPlatform = false; bool onTopOfPlatform = false;
for (List<Platform> platformLevel in gameRef.platformHolder.platforms) { for (List<MovingObject> platformLevel in gameRef.platformHolder.objects) {
for (Platform p in platformLevel) { for (MovingObject p in platformLevel) {
String side = p.intersect(runnerRect); String side = p.intersect(runnerRect);
if (side == "none") { if (side == "none") {
Rect belowRunner = Rect.fromLTRB(runnerRect.left, runnerRect.top, Rect belowRunner = Rect.fromLTRB(runnerRect.left, runnerRect.top,
@ -296,8 +294,8 @@ class Runner extends Component with HasGameRef<MyGame> {
bool belowPlatform() { bool belowPlatform() {
Rect runnerRect = sprite.toRect(); Rect runnerRect = sprite.toRect();
bool belowPlatform = false; bool belowPlatform = false;
for (List<Platform> platformLevel in gameRef.platformHolder.platforms) { for (List<MovingObject> platformLevel in gameRef.platformHolder.objects) {
for (Platform p in platformLevel) { for (MovingObject p in platformLevel) {
String side = p.intersect(runnerRect); String side = p.intersect(runnerRect);
if (side == "none") { if (side == "none") {
Rect belowRunner = Rect.fromLTRB(runnerRect.left, runnerRect.top - 1, Rect belowRunner = Rect.fromLTRB(runnerRect.left, runnerRect.top - 1,
@ -318,7 +316,7 @@ class Runner extends Component with HasGameRef<MyGame> {
Rect runnerRect = sprite.toRect(); Rect runnerRect = sprite.toRect();
bool onTopOfPlatform = this.onTopOfPlatform(); bool onTopOfPlatform = this.onTopOfPlatform();
for (List<Coin> coinLevel in gameRef.coinHolder.coins) { for (List<MovingObject> coinLevel in gameRef.coinHolder.objects) {
for (int i = 0; i < coinLevel.length;) { for (int i = 0; i < coinLevel.length;) {
if (coinLevel[i].intersect(runnerRect) != "none") { if (coinLevel[i].intersect(runnerRect) != "none") {
gameRef.gameState.numCoins++; gameRef.gameState.numCoins++;
@ -332,7 +330,7 @@ class Runner extends Component with HasGameRef<MyGame> {
} }
} }
for (List<Wire> wireLevel in gameRef.wireHolder.wires) { for (List<MovingObject> wireLevel in gameRef.wireHolder.objects) {
for (int i = 0; i < wireLevel.length; i++) { for (int i = 0; i < wireLevel.length; i++) {
if (wireLevel[i].intersect(runnerRect) != "none") { if (wireLevel[i].intersect(runnerRect) != "none") {
event("electrocute"); event("electrocute");
@ -341,7 +339,7 @@ class Runner extends Component with HasGameRef<MyGame> {
} }
} }
for (List<Bug> bugLevel in gameRef.bugHolder.bugs) { for (List<MovingObject> bugLevel in gameRef.bugHolder.objects) {
for (int i = 0; i < bugLevel.length; i++) { for (int i = 0; i < bugLevel.length; i++) {
String intersectState = bugLevel[i].intersect(runnerRect); String intersectState = bugLevel[i].intersect(runnerRect);
if (bugLevel[i].sprite.current == BugState.breaking) { if (bugLevel[i].sprite.current == BugState.breaking) {

View File

@ -32,14 +32,6 @@ class Wire extends MovingObject {
); );
} }
double getRightEnd() {
return sprite.position.x + sprite.width;
}
void remove() {
sprite.remove();
}
@override @override
String intersect(Rect other) { String intersect(Rect other) {
Rect currentRect = sprite.toRect(); Rect currentRect = sprite.toRect();

View File

@ -1,5 +1,4 @@
import 'dart:math'; import 'package:firo_runner/holder.dart';
import 'package:firo_runner/platform.dart'; import 'package:firo_runner/platform.dart';
import 'package:flame/flame.dart'; import 'package:flame/flame.dart';
@ -7,41 +6,27 @@ import 'package:firo_runner/wire.dart';
import 'package:firo_runner/main.dart'; import 'package:firo_runner/main.dart';
import 'package:flame/extensions.dart'; import 'package:flame/extensions.dart';
class WireHolder { class WireHolder extends Holder {
late Image wire; late Image wire;
Random random = Random();
late List<List<Wire>> wires = []; @override
Future load() async {
Future loadWires() async {
wire = await Flame.images.load("wire-frames.png"); 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() { getWire() {
return wire; return wire;
} }
bool generateWire(MyGame gameRef, int level, bool force) { bool generateWire(MyGame gameRef, int level, bool force) {
if (wires[level].isNotEmpty) { if (objects[level].isNotEmpty) {
return false; return false;
} }
if (random.nextInt(100) > 100) { if (random.nextInt(100) > 100) {
return true; return true;
} else { } else {
int nearestPlatform = gameRef.platformHolder.getNearestPlatform(level); int nearestPlatform = getNearestPlatform(level);
Platform? platform = Platform? platform =
gameRef.platformHolder.getPlatformOffScreen(nearestPlatform); gameRef.platformHolder.getPlatformOffScreen(nearestPlatform);
@ -73,57 +58,15 @@ class WireHolder {
return false; return false;
} }
wires[level].add(wire); objects[level].add(wire);
gameRef.add(wire.sprite); gameRef.add(wire.sprite);
if (platform != null) { if (platform != null) {
platform.removeChildren.add(() { platform.removeChildren.add(() {
wires[level].remove(wire); objects[level].remove(wire);
wire.remove(); wire.remove();
}); });
} }
return false; return false;
} }
} }
int totalWires() {
int total = 0;
for (List<Wire> levelWires in wires) {
total += levelWires.length;
}
return total;
}
void update(double dt) {
for (List<Wire> wireLevel in wires) {
for (Wire p in wireLevel) {
p.update(dt);
}
}
}
void remove(List<Wire> levelHolder, int j) {
levelHolder[j].remove();
levelHolder[j].sprite.remove();
levelHolder.removeAt(j);
}
void removePast(MyGame gameRef) {
for (List<Wire> 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<Wire> platformLevel in wires) {
for (Wire p in platformLevel) {
p.resize(newSize, xRatio, yRatio);
}
}
}
} }