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 {
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([]);
}
@ -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) {
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);
remove(bugLevel, i);
continue;
}
i++;

View File

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

View File

@ -13,6 +13,15 @@ class CoinHolder {
Future loadCoins() 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([]);
}
@ -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) {
for (List<Coin> coinLevel in coins) {
for (int i = 0; i < coinLevel.length;) {
if (coinLevel[i].sprite.x + coinLevel[i].sprite.width < 0) {
coinLevel[i].sprite.remove();
coinLevel.removeAt(i);
remove(coinLevel, i);
continue;
}
i++;

View File

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

View File

@ -15,6 +15,15 @@ class PlatformHolder {
platform1 = await Flame.images.load('p1-frames.png');
platform2 = await Flame.images.load('p2-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++) {
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) {
for (List<Platform> platformLevel in platforms) {
int removed = 0;
while (platformLevel.isNotEmpty &&
platformLevel[0].sprite.position.x + platformLevel[0].sprite.width <
0) {
platformLevel[0].sprite.remove();
platformLevel.removeAt(0);
remove(platformLevel, 0);
removed++;
}
if (platformLevel.isNotEmpty &&
@ -74,13 +89,8 @@ class PlatformHolder {
double secondToLastPosition =
platformLevel.elementAt(secondToLast).sprite.x;
if (secondToLastPosition > gameRef.size.x) {
platformLevel[secondToLast].remove();
platformLevel[secondToLast].sprite.remove();
platformLevel.removeAt(secondToLast);
platformLevel[secondToLast + 1].remove();
platformLevel[secondToLast + 1].sprite.remove();
platformLevel.removeAt(secondToLast + 1);
remove(platformLevel, secondToLast + 1);
remove(platformLevel, secondToLast);
}
}
}

View File

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

View File

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

View File

@ -48,17 +48,8 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
late BugHolder bugHolder;
Random random = Random();
// late Sprite background1;
// late Sprite background2;
late Runner runner;
late GameState gameState;
// var background;
var runnerPosition = Vector2(0, 0);
var runnerSize;
// var backgroundSize;
// var background1Position;
// var background2Position;
late double blockSize;
bool loaded = false;
@ -68,9 +59,6 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
Future<void> onLoad() async {
// debugMode = true;
FlameAudio.bgm.initialize();
// background = await Flame.images.load('bg.png');
// background1 = Sprite(background);
// background2 = Sprite(background);
circuitBackground = CircuitBackground(this);
await circuitBackground.load();
@ -88,22 +76,16 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
runner = Runner();
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');
loaded = true;
setUp();
}
void fillScreen() {
if (shouldReset) {
return;
}
for (int i = 2; i < 9; i = i + 3) {
while (!platformHolder.generatePlatform(this, i, false)) {}
}
@ -156,15 +138,48 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
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
void render(Canvas canvas) {
gameState.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);
final fpsCount = fps(1);
textPaint.render(
@ -188,20 +203,18 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
coinHolder.update(dt);
wireHolder.update(dt);
bugHolder.update(dt);
if (shouldReset) {
print("should reset");
reset();
}
}
@override
void onResize(Vector2 size) {
super.onResize(size);
blockSize = size.y / 9;
runnerSize = Vector2(
size.y / 9,
size.y / 9,
);
if (loaded) {
// backgroundSize =
// Vector2(size.y * (background!.width / background!.height), size.y);
gameState.setSize(size);
}
}