Added the bugs that you can kick and duck. Next commit will make sure Coins, Bugs, and Wires never overlap.

This commit is contained in:
Marco Salazar 2021-09-04 18:18:37 -06:00
parent 09db52bcc9
commit 513d2d08c0
7 changed files with 207 additions and 10 deletions

53
lib/Bug.dart Normal file
View File

@ -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();
}
}

103
lib/BugHolder.dart Normal file
View File

@ -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<List<Bug>> 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<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 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) {
bugLevel[i].sprite.remove();
bugLevel.removeAt(i);
continue;
}
i++;
}
}
}
}

View File

@ -26,16 +26,16 @@ class CoinHolder {
if (totalCoins() > 5) { if (totalCoins() > 5) {
return false; return false;
} }
double xCordinate = gameRef.platformHolder.getFlushX(); double xCoordinate = gameRef.platformHolder.getFlushX();
xCordinate = xCordinate + xCoordinate = xCoordinate +
gameRef.blockSize * random.nextInt(5) + gameRef.blockSize * random.nextInt(5) +
gameRef.blockSize * 20; gameRef.blockSize * 20;
if (xCordinate < gameRef.size.x || random.nextInt(100) > 10) { if (xCoordinate < gameRef.size.x || random.nextInt(100) > 25) {
return true; return true;
} else { } else {
Coin coin = Coin(gameRef); Coin coin = Coin(gameRef);
coin.setPosition(xCordinate, gameRef.blockSize * level); coin.setPosition(xCoordinate, gameRef.blockSize * level);
coins[level].add(coin); coins[level].add(coin);
gameRef.add(coin.sprite); gameRef.add(coin.sprite);
return false; return false;

View File

@ -7,7 +7,7 @@ import 'package:flame/components.dart';
enum PlatformState { normal } enum PlatformState { normal }
class Platform extends MovingObject { class Platform extends MovingObject {
var removeChildren = null; List<Function> removeChildren = [];
Platform(MyGame gameRef) : super(gameRef) { Platform(MyGame gameRef) : super(gameRef) {
var random = Random(); var random = Random();
@ -42,8 +42,10 @@ class Platform extends MovingObject {
} }
void remove() { void remove() {
if (removeChildren != null) { if (removeChildren.isNotEmpty) {
removeChildren(); for (Function removeChild in removeChildren) {
removeChild();
}
} }
} }
} }

View File

@ -1,3 +1,4 @@
import 'package:firo_runner/Bug.dart';
import 'package:firo_runner/Coin.dart'; import 'package:firo_runner/Coin.dart';
import 'package:firo_runner/Wire.dart'; import 'package:firo_runner/Wire.dart';
import 'package:firo_runner/main.dart'; import 'package:firo_runner/main.dart';
@ -189,6 +190,7 @@ class Runner extends Component with HasGameRef<MyGame> {
intersecting(); intersecting();
sprite.update(dt); sprite.update(dt);
print(runnerState);
} }
bool onTopOfPlatform() { bool onTopOfPlatform() {
@ -249,6 +251,32 @@ class Runner extends Component with HasGameRef<MyGame> {
} }
} }
for (List<Bug> 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 && if (!onTopOfPlatform &&
(runnerState == "run" || (runnerState == "run" ||
runnerState == "kick" || runnerState == "kick" ||
@ -290,7 +318,7 @@ class Runner extends Component with HasGameRef<MyGame> {
'kick-frames.png', 'kick-frames.png',
SpriteAnimationData.sequenced( SpriteAnimationData.sequenced(
amount: 13, amount: 13,
stepTime: 0.03, stepTime: 0.05,
textureSize: Vector2(512, 512), textureSize: Vector2(512, 512),
loop: false, loop: false,
), ),

View File

@ -63,10 +63,10 @@ class WireHolder {
wires[level].add(wire); wires[level].add(wire);
gameRef.add(wire.sprite); gameRef.add(wire.sprite);
if (platform != null) { if (platform != null) {
platform.removeChildren = () { platform.removeChildren.add(() {
wires[level].remove(wire); wires[level].remove(wire);
wire.remove(); wire.remove();
}; });
} }
return false; return false;
} }

View File

@ -1,5 +1,6 @@
import 'dart:math'; import 'dart:math';
import 'package:firo_runner/BugHolder.dart';
import 'package:firo_runner/CoinHolder.dart'; import 'package:firo_runner/CoinHolder.dart';
import 'package:firo_runner/GameState.dart'; import 'package:firo_runner/GameState.dart';
import 'package:firo_runner/PlatformHolder.dart'; import 'package:firo_runner/PlatformHolder.dart';
@ -40,6 +41,7 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
late PlatformHolder platformHolder; late PlatformHolder platformHolder;
late CoinHolder coinHolder; late CoinHolder coinHolder;
late WireHolder wireHolder; late WireHolder wireHolder;
late BugHolder bugHolder;
Random random = Random(); Random random = Random();
late Sprite background1; late Sprite background1;
@ -72,6 +74,8 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
await coinHolder.loadCoins(); await coinHolder.loadCoins();
wireHolder = WireHolder(); wireHolder = WireHolder();
await wireHolder.loadWires(); await wireHolder.loadWires();
bugHolder = BugHolder();
await bugHolder.loadBugs();
gameState = GameState(); gameState = GameState();
await gameState.load(size); await gameState.load(size);
@ -102,6 +106,11 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
wireHolder.generateWire(this, wireChosenRegion, false); 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; int choseCoinLevel = random.nextInt(8) + 1;
if (choseCoinLevel % 3 != 2) { if (choseCoinLevel % 3 != 2) {
coinHolder.generateCoin(this, choseCoinLevel, false); coinHolder.generateCoin(this, choseCoinLevel, false);
@ -130,12 +139,14 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
platformHolder.removePast(this); platformHolder.removePast(this);
coinHolder.removePast(this); coinHolder.removePast(this);
wireHolder.removePast(this); wireHolder.removePast(this);
bugHolder.removePast(this);
fillScreen(); fillScreen();
super.update(dt); super.update(dt);
gameState.update(dt); gameState.update(dt);
platformHolder.update(dt); platformHolder.update(dt);
coinHolder.update(dt); coinHolder.update(dt);
wireHolder.update(dt); wireHolder.update(dt);
bugHolder.update(dt);
} }
@override @override