Added a reset state to every object so that the game can cleanly be restarted.

This commit is contained in:
Marco Salazar 2021-09-05 16:36:36 -06:00
parent 9802a5c7ba
commit 7736eb1a1b
8 changed files with 190 additions and 70 deletions

View File

@ -16,6 +16,15 @@ class BugHolder {
Future loadBugs() 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++) { for (int i = 0; i < 9; i++) {
bugs.add([]); bugs.add([]);
} }
@ -93,12 +102,17 @@ class BugHolder {
} }
} }
void remove(List<Bug> levelHolder, int j) {
levelHolder[j].remove();
levelHolder[j].sprite.remove();
levelHolder.removeAt(j);
}
void removePast(MyGame gameRef) { void removePast(MyGame gameRef) {
for (List<Bug> bugLevel in bugs) { for (List<Bug> bugLevel in bugs) {
for (int i = 0; i < bugLevel.length;) { for (int i = 0; i < bugLevel.length;) {
if (bugLevel[i].sprite.x + bugLevel[i].sprite.width < 0) { if (bugLevel[i].sprite.x + bugLevel[i].sprite.width < 0) {
bugLevel[i].sprite.remove(); remove(bugLevel, i);
bugLevel.removeAt(i);
continue; continue;
} }
i++; i++;

View File

@ -19,7 +19,11 @@ class CircuitBackground extends MovingObject {
background = await Flame.images.load("bg.png"); background = await Flame.images.load("bg.png");
background1 = Sprite(background); background1 = Sprite(background);
background2 = Sprite(background); background2 = Sprite(background);
setUp();
}
void setUp() {
background1Position = Vector2(0, 0);
background1Size = Vector2( background1Size = Vector2(
gameRef.size.y * (background!.width / background!.height), gameRef.size.y * (background!.width / background!.height),
gameRef.size.y); gameRef.size.y);

View File

@ -13,6 +13,15 @@ class CoinHolder {
Future loadCoins() async { Future loadCoins() async {
coin = await Flame.images.load("coin-frames.png"); 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++) { for (int i = 0; i < 9; i++) {
coins.add([]); coins.add([]);
} }
@ -63,12 +72,17 @@ class CoinHolder {
} }
} }
void remove(List<Coin> levelHolder, int j) {
levelHolder[j].remove();
levelHolder[j].sprite.remove();
levelHolder.removeAt(j);
}
void removePast(MyGame gameRef) { void removePast(MyGame gameRef) {
for (List<Coin> coinLevel in coins) { for (List<Coin> coinLevel in coins) {
for (int i = 0; i < coinLevel.length;) { for (int i = 0; i < coinLevel.length;) {
if (coinLevel[i].sprite.x + coinLevel[i].sprite.width < 0) { if (coinLevel[i].sprite.x + coinLevel[i].sprite.width < 0) {
coinLevel[i].sprite.remove(); remove(coinLevel, i);
coinLevel.removeAt(i);
continue; continue;
} }
i++; i++;

View File

@ -4,19 +4,23 @@ import 'package:flame/components.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
class GameState extends Component { class GameState extends Component {
int numCoins = 0; static const int CIRCUIT_PERIOD = 500000;
int distance = 0;
late Rect square; late Rect square;
late Color color = Colors.white; late Color color = Colors.white;
int start = 0;
late ColorTween tween; late ColorTween tween;
static const int CIRCUIT_PERIOD = 500000;
int start = 0;
bool isPaused = false;
int numCoins = 0;
int distance = 0;
@override @override
void update(double dt) { void update(double dt) {
super.update(dt); super.update(dt);
distance = DateTime.now().microsecondsSinceEpoch - start; if (!isPaused) {
color = tween.lerp(sin(distance.toDouble() / CIRCUIT_PERIOD))!; distance = DateTime.now().microsecondsSinceEpoch - start;
color = tween.lerp(sin(distance.toDouble() / CIRCUIT_PERIOD))!;
}
} }
void addCoin() { void addCoin() {
@ -31,25 +35,35 @@ class GameState extends Component {
Future load(Vector2 size) async { Future load(Vector2 size) async {
square = Rect.fromLTWH(0, 0, size.x, size.y); square = Rect.fromLTWH(0, 0, size.x, size.y);
reset();
} }
void setSize(Vector2 size) { void setSize(Vector2 size) {
square = Rect.fromLTWH(0, 0, size.x, size.y); square = Rect.fromLTWH(0, 0, size.x, size.y);
} }
void reset() { void setUp() {
numCoins = 0;
distance = 0;
start = DateTime.now().microsecondsSinceEpoch; start = DateTime.now().microsecondsSinceEpoch;
tween = ColorTween(begin: Colors.yellow, end: Colors.yellowAccent); tween = ColorTween(begin: Colors.yellow, end: Colors.yellowAccent);
isPaused = false;
}
void setPaused() {
isPaused = true;
} }
double getVelocity() { double getVelocity() {
if (distance > 50000000) { if (!isPaused) {
return 250.0; if (distance > 50000000) {
} else if (distance > 10000000) return 250.0;
return 175.0; } else if (distance > 10000000)
else { return 175.0;
return 100.0; else {
return 100.0;
}
} else {
return 0;
} }
} }
} }

View File

@ -15,6 +15,15 @@ class PlatformHolder {
platform1 = await Flame.images.load('p1-frames.png'); platform1 = await Flame.images.load('p1-frames.png');
platform2 = await Flame.images.load('p2-frames.png'); platform2 = await Flame.images.load('p2-frames.png');
platform3 = await Flame.images.load('p3-frames.png'); platform3 = await Flame.images.load('p3-frames.png');
}
void setUp() {
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++) { for (int i = 0; i < 9; i++) {
platforms.add([]); platforms.add([]);
} }
@ -56,14 +65,20 @@ class PlatformHolder {
} }
} }
void remove(List<Platform> levelHolder, int j) {
levelHolder[j].remove();
levelHolder[j].sprite.remove();
levelHolder.removeAt(j);
print("removed platform");
}
void removePast(MyGame gameRef) { void removePast(MyGame gameRef) {
for (List<Platform> platformLevel in platforms) { for (List<Platform> platformLevel in platforms) {
int removed = 0; int removed = 0;
while (platformLevel.isNotEmpty && while (platformLevel.isNotEmpty &&
platformLevel[0].sprite.position.x + platformLevel[0].sprite.width < platformLevel[0].sprite.position.x + platformLevel[0].sprite.width <
0) { 0) {
platformLevel[0].sprite.remove(); remove(platformLevel, 0);
platformLevel.removeAt(0);
removed++; removed++;
} }
if (platformLevel.isNotEmpty && if (platformLevel.isNotEmpty &&
@ -74,13 +89,8 @@ class PlatformHolder {
double secondToLastPosition = double secondToLastPosition =
platformLevel.elementAt(secondToLast).sprite.x; platformLevel.elementAt(secondToLast).sprite.x;
if (secondToLastPosition > gameRef.size.x) { if (secondToLastPosition > gameRef.size.x) {
platformLevel[secondToLast].remove(); remove(platformLevel, secondToLast + 1);
platformLevel[secondToLast].sprite.remove(); remove(platformLevel, secondToLast);
platformLevel.removeAt(secondToLast);
platformLevel[secondToLast + 1].remove();
platformLevel[secondToLast + 1].sprite.remove();
platformLevel.removeAt(secondToLast + 1);
} }
} }
} }

View File

@ -22,6 +22,27 @@ enum RunnerState {
class Runner extends Component with HasGameRef<MyGame> { class Runner extends Component with HasGameRef<MyGame> {
late SpriteAnimationGroupComponent sprite; late SpriteAnimationGroupComponent sprite;
String runnerState = "run"; String runnerState = "run";
int level = 4;
String previousState = "run";
var runnerPosition = Vector2(0, 0);
var runnerSize;
bool dead = false;
void setUp() {
dead = false;
runnerState = "run";
previousState = "run";
level = 4;
runnerSize = Vector2(
gameRef.size.y / 9,
gameRef.size.y / 9,
);
setSize(runnerSize, gameRef.blockSize);
runnerPosition = Vector2(gameRef.blockSize, gameRef.blockSize * 4);
setPosition(runnerPosition);
}
void setPosition(Vector2 position) { void setPosition(Vector2 position) {
sprite.position = position; sprite.position = position;
@ -41,15 +62,14 @@ class Runner extends Component with HasGameRef<MyGame> {
getSprite().render(c, position: sprite.position, size: sprite.size); getSprite().render(c, position: sprite.position, size: sprite.size);
} }
int level = 1;
void updateLevel() { void updateLevel() {
level = (sprite.position.y / gameRef.blockSize).round(); level = (sprite.position.y / gameRef.blockSize).round();
} }
String previousState = "run";
void event(String event) { void event(String event) {
if (gameRef.gameState.isPaused) {
return;
}
previousState = runnerState; previousState = runnerState;
print(event); print(event);
switch (event) { switch (event) {
@ -129,12 +149,20 @@ class Runner extends Component with HasGameRef<MyGame> {
sprite.current = RunnerState.duck; sprite.current = RunnerState.duck;
break; break;
case "die": case "die":
if (dead) {
return;
}
runnerState = event; runnerState = event;
sprite.current = RunnerState.die; sprite.current = RunnerState.die;
gameRef.die();
break; break;
case "electro": case "electro":
if (dead) {
return;
}
runnerState = event; runnerState = event;
sprite.current = RunnerState.electro; sprite.current = RunnerState.electro;
gameRef.die();
break; break;
default: default:
break; break;
@ -142,6 +170,9 @@ class Runner extends Component with HasGameRef<MyGame> {
} }
void control(String input) { void control(String input) {
if (gameRef.gameState.isPaused) {
return;
}
print(input); print(input);
switch (input) { switch (input) {
case "up": case "up":
@ -190,7 +221,6 @@ class Runner extends Component with HasGameRef<MyGame> {
intersecting(); intersecting();
sprite.update(dt); sprite.update(dt);
print(runnerState);
} }
bool onTopOfPlatform() { bool onTopOfPlatform() {
@ -212,6 +242,9 @@ class Runner extends Component with HasGameRef<MyGame> {
} }
void intersecting() { void intersecting() {
if (gameRef.gameState.isPaused) {
return;
}
Rect runnerRect = sprite.toRect(); Rect runnerRect = sprite.toRect();
bool onTopOfPlatform = false; bool onTopOfPlatform = false;
for (List<Platform> platformLevel in gameRef.platformHolder.platforms) { for (List<Platform> platformLevel in gameRef.platformHolder.platforms) {
@ -234,8 +267,7 @@ class Runner extends Component with HasGameRef<MyGame> {
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++;
coinLevel[i].remove(); gameRef.coinHolder.remove(coinLevel, i);
coinLevel.removeAt(i);
print(gameRef.gameState.numCoins); print(gameRef.gameState.numCoins);
continue; continue;
} }
@ -247,6 +279,7 @@ class Runner extends Component with HasGameRef<MyGame> {
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("electro"); event("electro");
return;
} }
} }
} }
@ -266,6 +299,7 @@ class Runner extends Component with HasGameRef<MyGame> {
continue; continue;
} else if (aboveIntersect != "none") { } else if (aboveIntersect != "none") {
event("die"); event("die");
return;
} }
} else if (intersectState == "left" && runnerState == "kick") { } else if (intersectState == "left" && runnerState == "kick") {
bugLevel[i].sprite.current = BugState.breaking; bugLevel[i].sprite.current = BugState.breaking;
@ -273,6 +307,7 @@ class Runner extends Component with HasGameRef<MyGame> {
// bugLevel.removeAt(i); // bugLevel.removeAt(i);
} else { } else {
event("die"); event("die");
return;
} }
} }
} }
@ -356,8 +391,9 @@ class Runner extends Component with HasGameRef<MyGame> {
'electrocuted-frames.png', 'electrocuted-frames.png',
SpriteAnimationData.sequenced( SpriteAnimationData.sequenced(
amount: 2, amount: 2,
stepTime: 0.1, stepTime: 0.25,
textureSize: Vector2(512, 512), textureSize: Vector2(512, 512),
loop: false,
), ),
); );

View File

@ -14,6 +14,15 @@ class WireHolder {
Future loadWires() 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++) { for (int i = 0; i < 9; i++) {
wires.add([]); wires.add([]);
} }
@ -52,6 +61,7 @@ class WireHolder {
} }
Wire wire = Wire(gameRef); Wire wire = Wire(gameRef);
wire.sprite.renderFlipX = true;
if (level % 3 == 0) { if (level % 3 == 0) {
wire.sprite.renderFlipY = true; wire.sprite.renderFlipY = true;
wire.setPosition( wire.setPosition(
@ -93,12 +103,17 @@ class WireHolder {
} }
} }
void remove(List<Wire> levelHolder, int j) {
levelHolder[j].remove();
levelHolder[j].sprite.remove();
levelHolder.removeAt(j);
}
void removePast(MyGame gameRef) { void removePast(MyGame gameRef) {
for (List<Wire> wireLevel in wires) { for (List<Wire> wireLevel in wires) {
for (int i = 0; i < wireLevel.length;) { for (int i = 0; i < wireLevel.length;) {
if (wireLevel[i].sprite.x + wireLevel[i].sprite.width < 0) { if (wireLevel[i].sprite.x + wireLevel[i].sprite.width < 0) {
wireLevel[i].sprite.remove(); remove(wireLevel, i);
wireLevel.removeAt(i);
continue; continue;
} }
i++; i++;

View File

@ -48,17 +48,8 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
late BugHolder bugHolder; late BugHolder bugHolder;
Random random = Random(); Random random = Random();
// late Sprite background1;
// late Sprite background2;
late Runner runner; late Runner runner;
late GameState gameState; late GameState gameState;
// var background;
var runnerPosition = Vector2(0, 0);
var runnerSize;
// var backgroundSize;
// var background1Position;
// var background2Position;
late double blockSize; late double blockSize;
bool loaded = false; bool loaded = false;
@ -68,9 +59,6 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
Future<void> onLoad() async { Future<void> onLoad() async {
// debugMode = true; // debugMode = true;
FlameAudio.bgm.initialize(); FlameAudio.bgm.initialize();
// background = await Flame.images.load('bg.png');
// background1 = Sprite(background);
// background2 = Sprite(background);
circuitBackground = CircuitBackground(this); circuitBackground = CircuitBackground(this);
await circuitBackground.load(); await circuitBackground.load();
@ -88,22 +76,16 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
runner = Runner(); runner = Runner();
await runner.load(loadSpriteAnimation); await runner.load(loadSpriteAnimation);
runner.setSize(runnerSize, blockSize);
runnerPosition = Vector2(blockSize, blockSize * 1);
runner.setPosition(runnerPosition);
add(runner);
// Generate the first 4 Platforms that will always be there at the start.
for (int i = 0; i < 4; i++) {
platformHolder.generatePlatform(this, 8, true);
}
fillScreen();
FlameAudio.bgm.play('Infinite_Spankage_M.mp3'); FlameAudio.bgm.play('Infinite_Spankage_M.mp3');
loaded = true; loaded = true;
setUp();
} }
void fillScreen() { void fillScreen() {
if (shouldReset) {
return;
}
for (int i = 2; i < 9; i = i + 3) { for (int i = 2; i < 9; i = i + 3) {
while (!platformHolder.generatePlatform(this, i, false)) {} while (!platformHolder.generatePlatform(this, i, false)) {}
} }
@ -156,15 +138,48 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
return false; return false;
} }
bool shouldReset = false;
void reset() {
if (!(runner.sprite.animation?.done() ?? false)) {
return;
}
runner.sprite.animation!.reset();
shouldReset = false;
this.components.clear();
setUp();
}
void die() {
gameState.setPaused();
shouldReset = true;
}
void setUp() {
add(runner);
runner.sprite.clearEffects();
runner.sprite.current = RunnerState.run;
circuitBackground.setUp();
platformHolder.setUp();
coinHolder.setUp();
wireHolder.setUp();
bugHolder.setUp();
gameState.setUp();
runner.setUp();
// Generate the first 4 Platforms that will always be there at the start.
for (int i = 0; i < 4; i++) {
platformHolder.generatePlatform(this, 8, true);
}
fillScreen();
}
@override @override
void render(Canvas canvas) { void render(Canvas canvas) {
gameState.render(canvas); gameState.render(canvas);
circuitBackground.render(canvas); circuitBackground.render(canvas);
// background1.render(
// canvas,
// position: Vector2(0, 0),
// size: Vector2(size.y * (background!.width / background!.height), size.y),
// );
super.render(canvas); super.render(canvas);
final fpsCount = fps(1); final fpsCount = fps(1);
textPaint.render( textPaint.render(
@ -188,20 +203,18 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
coinHolder.update(dt); coinHolder.update(dt);
wireHolder.update(dt); wireHolder.update(dt);
bugHolder.update(dt); bugHolder.update(dt);
if (shouldReset) {
print("should reset");
reset();
}
} }
@override @override
void onResize(Vector2 size) { void onResize(Vector2 size) {
super.onResize(size); super.onResize(size);
blockSize = size.y / 9; blockSize = size.y / 9;
runnerSize = Vector2(
size.y / 9,
size.y / 9,
);
if (loaded) { if (loaded) {
// backgroundSize =
// Vector2(size.y * (background!.width / background!.height), size.y);
gameState.setSize(size); gameState.setSize(size);
} }
} }