Merged fixes, and animations branch

This commit is contained in:
Marco Salazar 2021-09-30 18:13:45 -06:00
commit 23e925a1b9
9 changed files with 799 additions and 755 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 116 KiB

After

Width:  |  Height:  |  Size: 258 KiB

View File

@ -1,396 +1,397 @@
import 'package:firo_runner/moving_object.dart'; import 'package:firo_runner/moving_object.dart';
import 'package:firo_runner/main.dart'; import 'package:firo_runner/main.dart';
import 'package:flame/components.dart'; import 'package:flame/components.dart';
import 'package:flame/flame.dart'; import 'package:flame/flame.dart';
import 'package:flame/extensions.dart'; import 'package:flame/extensions.dart';
enum WindowState { enum WindowState {
first, first,
second, second,
third, third,
fourth, fourth,
fifth, fifth,
sixth, sixth,
seventh, seventh,
} }
enum OverlayState { enum OverlayState {
first, first,
second, second,
third, third,
fourth, fourth,
fifth, fifth,
sixth, sixth,
seventh, seventh,
} }
class CircuitBackground extends MovingObject { class CircuitBackground extends MovingObject {
late Image background; late Image background;
late Image overlay0; late Image overlay0;
late Image overlay1; late Image overlay1;
late Image overlay2; late Image overlay2;
late Image overlay3; late Image overlay3;
late Image overlay4; late Image overlay4;
late Image overlay5; late Image overlay5;
late Image overlay6; late Image overlay6;
late Image windows0; late Image windows0;
late Image windows1; late Image windows1;
late Image windows2; late Image windows2;
late Image windows3; late Image windows3;
late Image windows4; late Image windows4;
late Image windows5; late Image windows5;
late Image windows6; late Image windows6;
late Sprite background1; late Sprite background1;
late Sprite background2; late Sprite background2;
late SpriteAnimationGroupComponent windowA; late SpriteAnimationGroupComponent windowA;
late SpriteAnimationGroupComponent windowB; late SpriteAnimationGroupComponent windowB;
late SpriteAnimationGroupComponent overlayA; late SpriteAnimationGroupComponent overlayA;
late SpriteAnimationGroupComponent overlayB; late SpriteAnimationGroupComponent overlayB;
Vector2 background1Size = Vector2(0, 0); Vector2 background1Size = Vector2(0, 0);
Vector2 background2Size = Vector2(0, 0); Vector2 background2Size = Vector2(0, 0);
Vector2 background1Position = Vector2(0, 0); Vector2 background1Position = Vector2(0, 0);
Vector2 background2Position = Vector2(0, 0); Vector2 background2Position = Vector2(0, 0);
CircuitBackground(MyGame gameRef) : super(gameRef); CircuitBackground(MyGame gameRef) : super(gameRef);
Future load() async { Future load() async {
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);
overlay0 = await Flame.images.load("overlay100.png"); overlay0 = await Flame.images.load("overlay100.png");
overlay1 = await Flame.images.load("overlay90.png"); overlay1 = await Flame.images.load("overlay90.png");
overlay2 = await Flame.images.load("overlay80.png"); overlay2 = await Flame.images.load("overlay80.png");
overlay3 = await Flame.images.load("overlay70.png"); overlay3 = await Flame.images.load("overlay70.png");
overlay4 = await Flame.images.load("overlay60.png"); overlay4 = await Flame.images.load("overlay60.png");
overlay5 = await Flame.images.load("overlay50.png"); overlay5 = await Flame.images.load("overlay50.png");
overlay6 = await Flame.images.load("overlay40.png"); overlay6 = await Flame.images.load("overlay40.png");
SpriteAnimation firstOverlay = SpriteAnimation.fromFrameData( SpriteAnimation firstOverlay = SpriteAnimation.fromFrameData(
overlay0, overlay0,
SpriteAnimationData.sequenced( SpriteAnimationData.sequenced(
amount: 1, amount: 1,
stepTime: 1, stepTime: 1,
textureSize: Vector2(2048, 683), textureSize: Vector2(2048, 683),
loop: false)); loop: false));
SpriteAnimation secondOverlay = SpriteAnimation.fromFrameData( SpriteAnimation secondOverlay = SpriteAnimation.fromFrameData(
overlay1, overlay1,
SpriteAnimationData.sequenced( SpriteAnimationData.sequenced(
amount: 1, amount: 1,
stepTime: 1, stepTime: 1,
textureSize: Vector2(2048, 683), textureSize: Vector2(2048, 683),
loop: false)); loop: false));
SpriteAnimation thirdOverlay = SpriteAnimation.fromFrameData( SpriteAnimation thirdOverlay = SpriteAnimation.fromFrameData(
overlay2, overlay2,
SpriteAnimationData.sequenced( SpriteAnimationData.sequenced(
amount: 1, amount: 1,
stepTime: 1, stepTime: 1,
textureSize: Vector2(2048, 683), textureSize: Vector2(2048, 683),
loop: false)); loop: false));
SpriteAnimation fourthOverlay = SpriteAnimation.fromFrameData( SpriteAnimation fourthOverlay = SpriteAnimation.fromFrameData(
overlay3, overlay3,
SpriteAnimationData.sequenced( SpriteAnimationData.sequenced(
amount: 1, amount: 1,
stepTime: 1, stepTime: 1,
textureSize: Vector2(2048, 683), textureSize: Vector2(2048, 683),
loop: false)); loop: false));
SpriteAnimation fifthOverlay = SpriteAnimation.fromFrameData( SpriteAnimation fifthOverlay = SpriteAnimation.fromFrameData(
overlay4, overlay4,
SpriteAnimationData.sequenced( SpriteAnimationData.sequenced(
amount: 1, amount: 1,
stepTime: 1, stepTime: 1,
textureSize: Vector2(2048, 683), textureSize: Vector2(2048, 683),
loop: false)); loop: false));
SpriteAnimation sixthOverlay = SpriteAnimation.fromFrameData( SpriteAnimation sixthOverlay = SpriteAnimation.fromFrameData(
overlay5, overlay5,
SpriteAnimationData.sequenced( SpriteAnimationData.sequenced(
amount: 1, amount: 1,
stepTime: 1, stepTime: 1,
textureSize: Vector2(2048, 683), textureSize: Vector2(2048, 683),
loop: false)); loop: false));
SpriteAnimation seventhOverlay = SpriteAnimation.fromFrameData( SpriteAnimation seventhOverlay = SpriteAnimation.fromFrameData(
overlay6, overlay6,
SpriteAnimationData.sequenced( SpriteAnimationData.sequenced(
amount: 1, amount: 1,
stepTime: 1, stepTime: 1,
textureSize: Vector2(2048, 683), textureSize: Vector2(2048, 683),
loop: false)); loop: false));
overlayA = SpriteAnimationGroupComponent( overlayA = SpriteAnimationGroupComponent(
animations: { animations: {
OverlayState.first: firstOverlay, OverlayState.first: firstOverlay,
OverlayState.second: secondOverlay, OverlayState.second: secondOverlay,
OverlayState.third: thirdOverlay, OverlayState.third: thirdOverlay,
OverlayState.fourth: fourthOverlay, OverlayState.fourth: fourthOverlay,
OverlayState.fifth: fifthOverlay, OverlayState.fifth: fifthOverlay,
OverlayState.sixth: sixthOverlay, OverlayState.sixth: sixthOverlay,
OverlayState.seventh: seventhOverlay, OverlayState.seventh: seventhOverlay,
}, },
current: OverlayState.first, current: OverlayState.first,
); );
overlayB = SpriteAnimationGroupComponent( overlayB = SpriteAnimationGroupComponent(
animations: { animations: {
OverlayState.first: firstOverlay, OverlayState.first: firstOverlay,
OverlayState.second: secondOverlay, OverlayState.second: secondOverlay,
OverlayState.third: thirdOverlay, OverlayState.third: thirdOverlay,
OverlayState.fourth: fourthOverlay, OverlayState.fourth: fourthOverlay,
OverlayState.fifth: fifthOverlay, OverlayState.fifth: fifthOverlay,
OverlayState.sixth: sixthOverlay, OverlayState.sixth: sixthOverlay,
OverlayState.seventh: seventhOverlay, OverlayState.seventh: seventhOverlay,
}, },
current: OverlayState.first, current: OverlayState.first,
); );
overlayA.changePriorityWithoutResorting(WINDOW_PRIORITY - 1); overlayA.changePriorityWithoutResorting(WINDOW_PRIORITY - 1);
overlayA.changePriorityWithoutResorting(WINDOW_PRIORITY - 1); overlayA.changePriorityWithoutResorting(WINDOW_PRIORITY - 1);
windows0 = await Flame.images.load("windows-0.png"); windows0 = await Flame.images.load("windows-0.png");
windows1 = await Flame.images.load("windows-1.png"); windows1 = await Flame.images.load("windows-1.png");
windows2 = await Flame.images.load("windows-2.png"); windows2 = await Flame.images.load("windows-2.png");
windows3 = await Flame.images.load("windows-3.png"); windows3 = await Flame.images.load("windows-3.png");
windows4 = await Flame.images.load("windows-4.png"); windows4 = await Flame.images.load("windows-4.png");
windows5 = await Flame.images.load("windows-5.png"); windows5 = await Flame.images.load("windows-5.png");
windows6 = await Flame.images.load("windows-6.png"); windows6 = await Flame.images.load("windows-6.png");
SpriteAnimation firstWindow = SpriteAnimation.fromFrameData( SpriteAnimation firstWindow = SpriteAnimation.fromFrameData(
windows0, windows0,
SpriteAnimationData.sequenced( SpriteAnimationData.sequenced(
amount: 1, amount: 1,
stepTime: 1, stepTime: 1,
textureSize: Vector2(2048, 683), textureSize: Vector2(2048, 683),
loop: false)); loop: false));
SpriteAnimation secondWindow = SpriteAnimation.fromFrameData( SpriteAnimation secondWindow = SpriteAnimation.fromFrameData(
windows1, windows1,
SpriteAnimationData.sequenced( SpriteAnimationData.sequenced(
amount: 1, amount: 1,
stepTime: 1, stepTime: 1,
textureSize: Vector2(2048, 683), textureSize: Vector2(2048, 683),
loop: false)); loop: false));
SpriteAnimation thirdWindow = SpriteAnimation.fromFrameData( SpriteAnimation thirdWindow = SpriteAnimation.fromFrameData(
windows2, windows2,
SpriteAnimationData.sequenced( SpriteAnimationData.sequenced(
amount: 1, amount: 1,
stepTime: 1, stepTime: 1,
textureSize: Vector2(2048, 683), textureSize: Vector2(2048, 683),
loop: false)); loop: false));
SpriteAnimation fourthWindow = SpriteAnimation.fromFrameData( SpriteAnimation fourthWindow = SpriteAnimation.fromFrameData(
windows3, windows3,
SpriteAnimationData.sequenced( SpriteAnimationData.sequenced(
amount: 1, amount: 1,
stepTime: 1, stepTime: 1,
textureSize: Vector2(2048, 683), textureSize: Vector2(2048, 683),
loop: false)); loop: false));
SpriteAnimation fifthWindow = SpriteAnimation.fromFrameData( SpriteAnimation fifthWindow = SpriteAnimation.fromFrameData(
windows4, windows4,
SpriteAnimationData.sequenced( SpriteAnimationData.sequenced(
amount: 1, amount: 1,
stepTime: 1, stepTime: 1,
textureSize: Vector2(2048, 683), textureSize: Vector2(2048, 683),
loop: false)); loop: false));
SpriteAnimation sixthWindow = SpriteAnimation.fromFrameData( SpriteAnimation sixthWindow = SpriteAnimation.fromFrameData(
windows5, windows5,
SpriteAnimationData.sequenced( SpriteAnimationData.sequenced(
amount: 1, amount: 1,
stepTime: 1, stepTime: 1,
textureSize: Vector2(2048, 683), textureSize: Vector2(2048, 683),
loop: false)); loop: false));
SpriteAnimation seventhWindow = SpriteAnimation.fromFrameData( SpriteAnimation seventhWindow = SpriteAnimation.fromFrameData(
windows6, windows6,
SpriteAnimationData.sequenced( SpriteAnimationData.sequenced(
amount: 1, amount: 1,
stepTime: 1, stepTime: 1,
textureSize: Vector2(2048, 683), textureSize: Vector2(2048, 683),
loop: false)); loop: false));
windowA = SpriteAnimationGroupComponent( windowA = SpriteAnimationGroupComponent(
animations: { animations: {
WindowState.first: firstWindow, WindowState.first: firstWindow,
WindowState.second: secondWindow, WindowState.second: secondWindow,
WindowState.third: thirdWindow, WindowState.third: thirdWindow,
WindowState.fourth: fourthWindow, WindowState.fourth: fourthWindow,
WindowState.fifth: fifthWindow, WindowState.fifth: fifthWindow,
WindowState.sixth: sixthWindow, WindowState.sixth: sixthWindow,
WindowState.seventh: seventhWindow, WindowState.seventh: seventhWindow,
}, },
current: WindowState.first, current: WindowState.first,
); );
windowB = SpriteAnimationGroupComponent( windowB = SpriteAnimationGroupComponent(
animations: { animations: {
WindowState.first: firstWindow, WindowState.first: firstWindow,
WindowState.second: secondWindow, WindowState.second: secondWindow,
WindowState.third: thirdWindow, WindowState.third: thirdWindow,
WindowState.fourth: fourthWindow, WindowState.fourth: fourthWindow,
WindowState.fifth: fifthWindow, WindowState.fifth: fifthWindow,
WindowState.sixth: sixthWindow, WindowState.sixth: sixthWindow,
WindowState.seventh: seventhWindow, WindowState.seventh: seventhWindow,
}, },
current: WindowState.first, current: WindowState.first,
); );
windowA.changePriorityWithoutResorting(WINDOW_PRIORITY); windowA.changePriorityWithoutResorting(WINDOW_PRIORITY);
windowA.changePriorityWithoutResorting(WINDOW_PRIORITY); windowA.changePriorityWithoutResorting(WINDOW_PRIORITY);
setUp(); setUp();
} }
void setUp() { void setUp() {
windowA.current = WindowState.first; windowA.current = WindowState.first;
windowB.current = WindowState.first; windowB.current = WindowState.first;
overlayA.current = OverlayState.first; overlayA.current = OverlayState.first;
overlayB.current = OverlayState.first; overlayB.current = OverlayState.first;
background1Position = Vector2(0, 0); background1Position = Vector2(0, 0);
background1Size = Vector2( background1Size = Vector2(
gameRef.viewport.canvasSize.y * (background.width / background.height), gameRef.viewport.canvasSize.y * (background.width / background.height),
gameRef.viewport.canvasSize.y); gameRef.viewport.canvasSize.y);
windowA.position = background1Position; windowA.position = background1Position;
windowA.size = background1Size; windowA.size = background1Size;
overlayA.position = background1Position; overlayA.position = background1Position;
overlayA.size = background1Size; overlayA.size = background1Size;
background2Position = background2Position =
Vector2(background1Position.x + background1Size.x - 1, 0); Vector2(background1Position.x + background1Size.x - 1, 0);
background2Size = Vector2( background2Size = Vector2(
gameRef.viewport.canvasSize.y * (background.width / background.height), gameRef.viewport.canvasSize.y * (background.width / background.height),
gameRef.viewport.canvasSize.y); gameRef.viewport.canvasSize.y);
windowB.position = background2Position; windowB.position = background2Position;
windowB.size = background2Size; windowB.size = background2Size;
overlayB.position = background2Position; overlayB.position = background2Position;
overlayB.size = background2Size; overlayB.size = background2Size;
} }
@override @override
void update(double dt) { void update(double dt) {
switch (gameRef.gameState.getScoreLevel()) { switch (gameRef.gameState.getScoreLevel()) {
case 12: case 12:
windowA.current = WindowState.seventh; windowA.current = WindowState.seventh;
windowB.current = WindowState.seventh; windowB.current = WindowState.seventh;
break; break;
case 11: case 11:
overlayA.current = OverlayState.seventh; overlayA.current = OverlayState.seventh;
overlayB.current = OverlayState.seventh; overlayB.current = OverlayState.seventh;
break; break;
case 10: case 10:
windowA.current = WindowState.sixth; windowA.current = WindowState.sixth;
windowB.current = WindowState.sixth; windowB.current = WindowState.sixth;
break; break;
case 9: case 9:
overlayA.current = OverlayState.sixth; overlayA.current = OverlayState.sixth;
overlayB.current = OverlayState.sixth; overlayB.current = OverlayState.sixth;
break; break;
case 8: case 8:
windowA.current = WindowState.fifth; windowA.current = WindowState.fifth;
windowB.current = WindowState.fifth; windowB.current = WindowState.fifth;
break; break;
case 7: case 7:
overlayA.current = OverlayState.fifth; overlayA.current = OverlayState.fifth;
overlayB.current = OverlayState.fifth; overlayB.current = OverlayState.fifth;
break; break;
case 6: case 6:
windowA.current = WindowState.fourth; windowA.current = WindowState.fourth;
windowB.current = WindowState.fourth; windowB.current = WindowState.fourth;
break; break;
case 5: case 5:
overlayA.current = OverlayState.fourth; overlayA.current = OverlayState.fourth;
overlayB.current = OverlayState.fourth; overlayB.current = OverlayState.fourth;
break; break;
case 4: case 4:
windowA.current = WindowState.third; windowA.current = WindowState.third;
windowB.current = WindowState.third; windowB.current = WindowState.third;
break; break;
case 3: case 3:
overlayA.current = OverlayState.third; overlayA.current = OverlayState.third;
overlayB.current = OverlayState.third; overlayB.current = OverlayState.third;
break; break;
case 2: case 2:
windowA.current = WindowState.second; windowA.current = WindowState.second;
windowB.current = WindowState.second; windowB.current = WindowState.second;
break; break;
case 1: case 1:
overlayA.current = OverlayState.second; overlayA.current = OverlayState.second;
overlayB.current = OverlayState.second; overlayB.current = OverlayState.second;
break; break;
default: default:
windowA.current = WindowState.first; windowA.current = WindowState.first;
windowB.current = WindowState.first; windowB.current = WindowState.first;
overlayA.current = OverlayState.first; overlayA.current = OverlayState.first;
overlayB.current = OverlayState.first; overlayB.current = OverlayState.first;
break; break;
} }
windowA.update(dt); windowA.update(dt);
windowB.update(dt); windowB.update(dt);
overlayA.update(dt); overlayA.update(dt);
overlayB.update(dt); overlayB.update(dt);
if (background1Position.x + background1Size.x < 0) { if (background1Position.x + background1Size.x < 0) {
double newPosition = background2Position.x + background2Size.x; double newPosition = background2Position.x + background2Size.x;
background1Position = Vector2(newPosition - 1, 0); background1Position = Vector2(newPosition - 1, 0);
} else if (background2Position.x + background2Size.x < 0) { } else if (background2Position.x + background2Size.x < 0) {
double newPosition = background1Position.x + background1Size.x; double newPosition = background1Position.x + background1Size.x;
background2Position = Vector2(newPosition - 1, 0); background2Position = Vector2(newPosition - 1, 0);
} }
double velocity = gameRef.gameState.getVelocity() / 10.0; double velocity = gameRef.gameState.getVelocity() / 10.0;
background1Position = background1Position - Vector2(velocity * dt, 0); background1Position = background1Position - Vector2(velocity * dt, 0);
windowA.position = background1Position; windowA.position = background1Position;
overlayA.position = background1Position; overlayA.position = background1Position;
background2Position = background2Position - Vector2(velocity * dt, 0); background2Position = background2Position - Vector2(velocity * dt, 0);
windowB.position = background2Position; windowB.position = background2Position;
overlayB.position = background2Position; overlayB.position = background2Position;
} }
void render(Canvas canvas) { void render(Canvas canvas) {
background1.render(canvas, background1.render(canvas,
size: background1Size, position: background1Position); size: background1Size, position: background1Position);
canvas.save(); canvas.save();
overlayA.render(canvas); overlayA.render(canvas);
canvas.restore(); canvas.restore();
canvas.save(); canvas.save();
windowA.render(canvas); windowA.render(canvas);
canvas.restore(); canvas.restore();
background2.render(canvas, background2.render(canvas,
size: background2Size, position: background2Position); size: background2Size, position: background2Position);
canvas.save(); canvas.save();
overlayB.render(canvas); overlayB.render(canvas);
canvas.restore(); canvas.restore();
canvas.save(); canvas.save();
windowB.render(canvas); windowB.render(canvas);
canvas.restore(); canvas.restore();
} }
@override @override
void resize(Vector2 newSize, double xRatio, double yRatio) { void resize(Vector2 newSize, double xRatio, double yRatio) {
background1Size = background1Position.x *= xRatio;
Vector2(newSize.y * (background.width / background.height), newSize.y); background1Position.y *= yRatio;
windowA.position = background1Position; background1Size.x *= xRatio;
windowA.size = background1Size; background1Size.y *= yRatio;
overlayA.position = background1Position; background2Position.x *= xRatio;
overlayA.size = background1Size; background2Position.y *= yRatio;
background2Size.x *= xRatio;
background2Position = background2Size.y *= yRatio;
Vector2(background1Position.x + background1Size.x - 1, 0); windowA.position = background1Position;
background2Size = windowA.size = background1Size;
Vector2(newSize.y * (background.width / background.height), newSize.y); overlayA.position = background1Position;
windowB.position = background2Position; overlayA.size = background1Size;
windowB.size = background2Size; windowB.position = background2Position;
overlayB.position = background2Position; windowB.size = background2Size;
overlayB.size = background2Size; overlayB.position = background2Position;
} overlayB.size = background2Size;
} }
}

View File

@ -1,131 +1,131 @@
import 'dart:math'; import 'dart:math';
import 'package:firo_runner/main.dart'; import 'package:firo_runner/main.dart';
import 'package:flame/components.dart'; import 'package:flame/components.dart';
import 'package:flame/extensions.dart'; import 'package:flame/extensions.dart';
import 'package:flame/flame.dart'; import 'package:flame/flame.dart';
enum FireworkState { normal } enum FireworkState { normal }
class Firework extends Component { class Firework extends Component {
MyGame gameRef; MyGame gameRef;
late SpriteAnimationGroupComponent sprite1; late SpriteAnimationGroupComponent sprite1;
late SpriteAnimationGroupComponent sprite2; late SpriteAnimationGroupComponent sprite2;
Firework(this.gameRef); Firework(this.gameRef);
double timeSinceFirework = 0; double timeSinceFirework = 0;
late Random random; late Random random;
String message = ""; String message = "";
List<String> messages = [ List<String> messages = [
"Speed Up!", "Speed Up!",
"Speed Up!", "Speed Up!",
"Speed Up!", "Speed Up!",
"Speed Up!", "Speed Up!",
"Speed Up!", "Speed Up!",
"Speed Up!", "Speed Up!",
"Speed Up!", "Speed Up!",
"Speed Up!", "Speed Up!",
"Speed Up!", "Speed Up!",
"Speed Up!", "Speed Up!",
"Speed Up!", "Speed Up!",
"Speed Up!", "Speed Up!",
"Speed Up!", "Speed Up!",
]; ];
Future load() async { Future load() async {
random = Random(); random = Random();
Image firework = await Flame.images.load("fireworks-frames.png"); Image firework = await Flame.images.load("fireworks-frames.png");
SpriteAnimation normal = SpriteAnimation.fromFrameData( SpriteAnimation normal = SpriteAnimation.fromFrameData(
firework, firework,
SpriteAnimationData.sequenced( SpriteAnimationData.sequenced(
amount: 10, amount: 10,
stepTime: 0.25, stepTime: 0.25,
textureSize: Vector2(512, 512), textureSize: Vector2(512, 512),
loop: false, loop: false,
), ),
); );
sprite1 = SpriteAnimationGroupComponent( sprite1 = SpriteAnimationGroupComponent(
animations: { animations: {
FireworkState.normal: normal, FireworkState.normal: normal,
}, },
current: FireworkState.normal, current: FireworkState.normal,
); );
sprite1.changePriorityWithoutResorting(FIREWORK_PRIORITY); sprite1.changePriorityWithoutResorting(FIREWORK_PRIORITY);
sprite1.update(100); sprite1.update(100);
sprite1.size = sprite1.size =
Vector2(gameRef.viewport.canvasSize.y, gameRef.viewport.canvasSize.y); Vector2(gameRef.viewport.canvasSize.y, gameRef.viewport.canvasSize.y);
sprite1.position = Vector2(0, 0); sprite1.position = Vector2(0, 0);
sprite2 = SpriteAnimationGroupComponent( sprite2 = SpriteAnimationGroupComponent(
animations: { animations: {
FireworkState.normal: normal, FireworkState.normal: normal,
}, },
current: FireworkState.normal, current: FireworkState.normal,
); );
sprite2.changePriorityWithoutResorting(FIREWORK_PRIORITY); sprite2.changePriorityWithoutResorting(FIREWORK_PRIORITY);
sprite2.size = sprite2.size =
Vector2(gameRef.viewport.canvasSize.y, gameRef.viewport.canvasSize.y); Vector2(gameRef.viewport.canvasSize.y, gameRef.viewport.canvasSize.y);
sprite2.position = sprite2.position =
Vector2(gameRef.viewport.canvasSize.x - sprite2.size.x, 0); Vector2(gameRef.viewport.canvasSize.x - sprite2.size.x, 0);
sprite2.update(100); sprite2.update(100);
} }
void setUp() { void setUp() {
message = ""; message = "";
timeSinceFirework = 0; timeSinceFirework = 0;
gameRef.add(sprite1); gameRef.add(sprite1);
gameRef.add(sprite2); gameRef.add(sprite2);
} }
@override @override
void update(double dt) { void update(double dt) {
if (!(sprite1.animation?.done() ?? false)) { if (!(sprite1.animation?.done() ?? false)) {
timeSinceFirework = 0; timeSinceFirework = 0;
} else { } else {
timeSinceFirework += dt; timeSinceFirework += dt;
} }
sprite1.update(dt); sprite1.update(dt);
sprite2.update(dt); sprite2.update(dt);
} }
void renderText(Canvas canvas) { void renderText(Canvas canvas) {
sprite1.render(canvas); sprite1.render(canvas);
sprite1.render(canvas); sprite1.render(canvas);
if ((sprite1.animation?.done() ?? false) && if ((sprite1.animation?.done() ?? false) &&
timeSinceFirework < 1 && timeSinceFirework < 1 &&
message != "") { message != "") {
gameRef.fireworksPaint.render( gameRef.fireworksPaint.render(
canvas, canvas,
message, message,
Vector2( Vector2(
gameRef.size.x / 2 - gameRef.size.x / 2 -
gameRef.fireworksPaint.measureTextWidth(message) / 2, gameRef.fireworksPaint.measureTextWidth(message) / 2,
gameRef.size.y / 9 - gameRef.size.y / 9 -
gameRef.fireworksPaint.measureTextHeight(message) / 2), gameRef.fireworksPaint.measureTextHeight(message) / 2),
); );
} }
} }
void reset() { void reset() {
message = messages.elementAt(random.nextInt(messages.length)); message = messages.elementAt(random.nextInt(messages.length));
sprite1.animation!.reset(); sprite1.animation!.reset();
sprite2.animation!.reset(); sprite2.animation!.reset();
} }
void resize(Vector2 newSize, double xRatio, double yRatio) { void resize(Vector2 newSize, double xRatio, double yRatio) {
sprite1.x *= xRatio; sprite1.x *= xRatio;
sprite1.y *= yRatio; sprite1.y *= yRatio;
sprite1.width *= xRatio; sprite1.width *= xRatio;
sprite1.height *= yRatio; sprite1.height *= yRatio;
sprite2.x *= xRatio; sprite2.x *= xRatio;
sprite2.y *= yRatio; sprite2.y *= yRatio;
sprite2.width *= xRatio; sprite2.width *= xRatio;
sprite2.height *= yRatio; sprite2.height *= yRatio;
} }
} }

View File

@ -1,124 +1,124 @@
import 'package:firo_runner/main.dart'; import 'package:firo_runner/main.dart';
import 'package:flame/components.dart'; import 'package:flame/components.dart';
class GameState extends Component { class GameState extends Component {
int start = 0; int start = 0;
bool isPaused = false; bool isPaused = false;
int numCoins = 0; int numCoins = 0;
int distance = 0; int distance = 0;
late MyGame gameRef; late MyGame gameRef;
int previousLevel = 1; int previousLevel = 1;
@override @override
void update(double dt) { void update(double dt) {
super.update(dt); super.update(dt);
if (!isPaused) { if (!isPaused) {
distance = DateTime.now().microsecondsSinceEpoch - start; distance = DateTime.now().microsecondsSinceEpoch - start;
if (previousLevel != getLevel()) { if (previousLevel != getLevel()) {
previousLevel = getLevel(); previousLevel = getLevel();
gameRef.fireworks.reset(); gameRef.fireworks.reset();
} }
} }
} }
void addCoin() { void addCoin() {
numCoins++; numCoins++;
} }
void setUp(MyGame gameRef) { void setUp(MyGame gameRef) {
this.gameRef = gameRef; this.gameRef = gameRef;
numCoins = 0; numCoins = 0;
distance = 0; distance = 0;
previousLevel = 1; previousLevel = 1;
start = DateTime.now().microsecondsSinceEpoch; start = DateTime.now().microsecondsSinceEpoch;
isPaused = false; isPaused = false;
} }
void setPaused() { void setPaused() {
isPaused = true; isPaused = true;
} }
int getLevel() { int getLevel() {
if (distance > LEVEL7) { if (distance > LEVEL7) {
return 7; return 7;
} else if (distance > LEVEL6) { } else if (distance > LEVEL6) {
return 6; return 6;
} else if (distance > LEVEL5) { } else if (distance > LEVEL5) {
return 5; return 5;
} else if (distance > LEVEL4) { } else if (distance > LEVEL4) {
return 4; return 4;
} else if (distance > LEVEL3) { } else if (distance > LEVEL3) {
return 3; return 3;
} else if (distance > LEVEL2) { } else if (distance > LEVEL2) {
return 2; return 2;
} else { } else {
return 1; return 1;
} }
} }
int getScoreLevel() { int getScoreLevel() {
int score = getScore(); int score = getScore();
if (score > LEVEL7) { if (score > LEVEL7) {
return 12; return 12;
} else if (score > LEVEL6 + LEVEL6 / 2) { } else if (score > LEVEL6 + LEVEL6 / 2) {
return 11; return 11;
} else if (score > LEVEL6) { } else if (score > LEVEL6) {
return 10; return 10;
} else if (score > LEVEL5 + LEVEL5 / 2) { } else if (score > LEVEL5 + LEVEL5 / 2) {
return 9; return 9;
} else if (score > LEVEL5) { } else if (score > LEVEL5) {
return 8; return 8;
} else if (score > LEVEL4 + LEVEL4 / 2) { } else if (score > LEVEL4 + LEVEL4 / 2) {
return 7; return 7;
} else if (score > LEVEL4) { } else if (score > LEVEL4) {
return 6; return 6;
} else if (score > LEVEL3 + LEVEL3 / 2) { } else if (score > LEVEL3 + LEVEL3 / 2) {
return 5; return 5;
} else if (score > LEVEL3) { } else if (score > LEVEL3) {
return 4; return 4;
} else if (score > LEVEL2 + LEVEL2 / 2) { } else if (score > LEVEL2 + LEVEL2 / 2) {
return 3; return 3;
} else if (score > LEVEL2) { } else if (score > LEVEL2) {
return 2; return 2;
} else if (score > LEVEL2 - LEVEL2 / 2) { } else if (score > LEVEL2 - LEVEL2 / 2) {
return 1; return 1;
} else { } else {
return 0; return 0;
} }
} }
int getScore() { int getScore() {
return distance ~/ 10 + numCoins * 1000000; return distance ~/ 10 + numCoins * 1000000;
} }
int getPlayerScore() { int getPlayerScore() {
return getScore() ~/ 10000; return getScore() ~/ 10000;
} }
int getPlayerDistance() { int getPlayerDistance() {
return distance ~/ 1000000; return distance ~/ 1000000;
} }
double getVelocity() { double getVelocity() {
if (!isPaused) { if (!isPaused) {
switch (getLevel()) { switch (getLevel()) {
case 7: case 7:
return gameRef.viewport.canvasSize.x * 0.25; return gameRef.viewport.canvasSize.x * 0.25;
case 6: case 6:
return gameRef.viewport.canvasSize.x * 0.20; return gameRef.viewport.canvasSize.x * 0.20;
case 5: case 5:
return gameRef.viewport.canvasSize.x * 0.18; return gameRef.viewport.canvasSize.x * 0.18;
case 4: case 4:
return gameRef.viewport.canvasSize.x * 0.16; return gameRef.viewport.canvasSize.x * 0.16;
case 3: case 3:
return gameRef.viewport.canvasSize.x * 0.14; return gameRef.viewport.canvasSize.x * 0.14;
case 2: case 2:
return gameRef.viewport.canvasSize.x * 0.12; return gameRef.viewport.canvasSize.x * 0.12;
default: default:
return gameRef.viewport.canvasSize.x * 0.1; return gameRef.viewport.canvasSize.x * 0.1;
} }
} else { } else {
return 0; return 0;
} }
} }
} }

View File

@ -1,88 +1,94 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'main.dart'; import 'main.dart';
class LoseMenuOverlay extends StatelessWidget { class LoseMenuOverlay extends StatelessWidget {
const LoseMenuOverlay({ const LoseMenuOverlay({
Key? key, Key? key,
required this.game, required this.game,
}) : super(key: key); }) : super(key: key);
final MyGame game; final MyGame game;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Center( return Center(
child: Container( child: Container(
height: game.viewport.canvasSize.y, height: game.viewport.canvasSize.y,
width: game.viewport.canvasSize.x, width: game.viewport.canvasSize.x,
decoration: const BoxDecoration( decoration: const BoxDecoration(
image: DecorationImage( image: DecorationImage(
image: AssetImage('assets/images/overlay100.png'), image: AssetImage('assets/images/overlay100.png'),
fit: BoxFit.fill, fit: BoxFit.fill,
), ),
), ),
child: Column( child: Column(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: [ children: [
Text( Text(
'Score: ' + game.gameState.getPlayerScore().toString(), 'Score: ' + game.gameState.getPlayerScore().toString(),
style: overlayText, style: overlayText,
), ),
const SizedBox(height: 32.0), const SizedBox(height: 32.0),
Row( Row(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: [ children: [
MaterialButton( MaterialButton(
padding: const EdgeInsets.all(8.0), padding: const EdgeInsets.all(8.0),
textColor: Colors.white, textColor: Colors.white,
splashColor: Colors.greenAccent, splashColor: Colors.greenAccent,
elevation: 8.0, elevation: 8.0,
child: Container( child: Container(
decoration: const BoxDecoration( decoration: const BoxDecoration(
image: DecorationImage( image: DecorationImage(
image: AssetImage('assets/images/button.png'), image: AssetImage('assets/images/button.png'),
fit: BoxFit.fill), fit: BoxFit.fill),
), ),
child: const Padding( child: const Padding(
padding: EdgeInsets.all(8.0), padding: EdgeInsets.all(8.0),
child: Text("Main Menu"), child: Text(
), " Main Menu ",
), style: overlayText,
// ), ),
onPressed: () { ),
// Go to the Main Menu ),
}, // ),
), onPressed: () {
const SizedBox( // Go to the Main Menu
width: 32.0, },
), ),
MaterialButton( const SizedBox(
padding: const EdgeInsets.all(8.0), width: 32.0,
textColor: Colors.white, ),
splashColor: Colors.greenAccent, MaterialButton(
elevation: 8.0, padding: const EdgeInsets.all(8.0),
child: Container( textColor: Colors.white,
decoration: const BoxDecoration( splashColor: Colors.greenAccent,
image: DecorationImage( elevation: 8.0,
image: AssetImage('assets/images/button.png'), child: Container(
fit: BoxFit.fill), decoration: const BoxDecoration(
), image: DecorationImage(
child: const Padding( image: AssetImage('assets/images/button.png'),
padding: EdgeInsets.all(8.0), fit: BoxFit.fill),
child: Text("Replay"), ),
), child: const Padding(
), padding: EdgeInsets.all(8.0),
// ), child: Text(
onPressed: () { " Replay ",
game.reset(); style: overlayText,
}, ),
), ),
], ),
), // ),
], onPressed: () {
), game.reset();
), },
); ),
} ],
} ),
],
),
),
);
}
}

View File

@ -64,7 +64,7 @@ class Platform extends MovingObject {
PlatformState.right: right, PlatformState.right: right,
PlatformState.single: single, PlatformState.single: single,
}, },
current: PlatformState.single, current: PlatformState.mid,
); );
sprite.changePriorityWithoutResorting(PLATFORM_PRIORITY); sprite.changePriorityWithoutResorting(PLATFORM_PRIORITY);
@ -112,14 +112,19 @@ class Platform extends MovingObject {
bool hasLeft = (left.x - sprite.position.x).abs() < 1.9 * sprite.size.x; bool hasLeft = (left.x - sprite.position.x).abs() < 1.9 * sprite.size.x;
bool hasRight = (sprite.position.x - right.x).abs() < 1.9 * sprite.size.x; bool hasRight = (sprite.position.x - right.x).abs() < 1.9 * sprite.size.x;
if (hasLeft && hasRight) { // If the platform cannot be seen by the player.
sprite.current = PlatformState.mid; if (!((sprite.x >= 0 && sprite.x <= gameRef.size.x) ||
} else if (hasLeft && !hasRight) { (sprite.x + sprite.width >= 0 &&
sprite.current = PlatformState.right; sprite.x + sprite.width <= gameRef.size.x))) {
} else if (!hasLeft && hasRight) { if (hasLeft && hasRight) {
sprite.current = PlatformState.left; sprite.current = PlatformState.mid;
} else { } else if (hasLeft && !hasRight) {
sprite.current = PlatformState.single; sprite.current = PlatformState.right;
} else if (!hasLeft && hasRight) {
sprite.current = PlatformState.left;
} else {
sprite.current = PlatformState.single;
}
} }
} }
} }

View File

@ -14,6 +14,8 @@ class PlatformHolder extends Holder {
late Image r2; late Image r2;
late Image o1; late Image o1;
late Image o2; late Image o2;
bool noTopObstaclesForNext = false;
bool noMiddleObstaclesForNext = false;
int timeSinceLastTopHole = 0; int timeSinceLastTopHole = 0;
int timeSinceLastBottomHole = 0; int timeSinceLastBottomHole = 0;
@ -91,6 +93,7 @@ class PlatformHolder extends Holder {
remove(objects[2], objects[2].length - 2); remove(objects[2], objects[2].length - 2);
timeSinceLastTopHole = 0; timeSinceLastTopHole = 0;
noTopObstaclesForNext = true;
} }
if (bottomChance > 30) { if (bottomChance > 30) {
Platform start = objects[5].elementAt(objects[5].length - 10) as Platform; Platform start = objects[5].elementAt(objects[5].length - 10) as Platform;
@ -112,6 +115,7 @@ class PlatformHolder extends Holder {
remove(objects[5], firstToRemove); remove(objects[5], firstToRemove);
timeSinceLastBottomHole = 0; timeSinceLastBottomHole = 0;
noMiddleObstaclesForNext = true;
} }
} }
@ -129,6 +133,13 @@ class PlatformHolder extends Holder {
platform.row = level; platform.row = level;
gameRef.add(platform.sprite); gameRef.add(platform.sprite);
objects[level].add(platform); objects[level].add(platform);
if (level == 2 && noTopObstaclesForNext) {
platform.prohibitObstacles = true;
noTopObstaclesForNext = false;
} else if (level == 5 && noMiddleObstaclesForNext) {
platform.prohibitObstacles = true;
noMiddleObstaclesForNext = false;
}
return false; return false;
} }
} }

View File

@ -4,8 +4,8 @@ import 'package:firo_runner/moving_object.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/flame.dart'; import 'package:flame/flame.dart';
// import 'package:flutter/material.dart';
import 'dart:math'; import 'dart:math';
import 'package:flame/components.dart'; import 'package:flame/components.dart';
import 'package:flame/image_composition.dart'; import 'package:flame/image_composition.dart';
import 'package:flutter/animation.dart'; import 'package:flutter/animation.dart';
@ -78,9 +78,9 @@ class Runner extends Component with HasGameRef<MyGame> {
if (gameRef.gameState.isPaused) { if (gameRef.gameState.isPaused) {
return; return;
} }
previousState = runnerState;
switch (event) { switch (event) {
case "jump": case "jump":
previousState = runnerState;
runnerState = event; runnerState = event;
sprite.current = RunnerState.jump; sprite.current = RunnerState.jump;
sprite.addEffect(MoveEffect( sprite.addEffect(MoveEffect(
@ -100,6 +100,7 @@ class Runner extends Component with HasGameRef<MyGame> {
if (belowPlatform()) { if (belowPlatform()) {
break; break;
} }
previousState = runnerState;
sprite.clearEffects(); sprite.clearEffects();
if (level - 1 < 0) { if (level - 1 < 0) {
break; break;
@ -123,20 +124,24 @@ class Runner extends Component with HasGameRef<MyGame> {
)); ));
break; break;
case "fall": case "fall":
previousState = runnerState;
sprite.clearEffects(); sprite.clearEffects();
runnerState = event; runnerState = event;
sprite.current = RunnerState.fall; sprite.current = RunnerState.fall;
sprite.addEffect(getFallingEffect()); sprite.addEffect(getFallingEffect());
break; break;
case "kick": case "kick":
previousState = runnerState;
runnerState = event; runnerState = event;
sprite.current = RunnerState.kick; sprite.current = RunnerState.kick;
break; break;
case "run": case "run":
previousState = runnerState;
runnerState = event; runnerState = event;
sprite.current = RunnerState.run; sprite.current = RunnerState.run;
break; break;
case "float": case "float":
previousState = runnerState;
runnerState = event; runnerState = event;
sprite.current = RunnerState.float; sprite.current = RunnerState.float;
sprite.addEffect(MoveEffect( sprite.addEffect(MoveEffect(
@ -154,6 +159,7 @@ class Runner extends Component with HasGameRef<MyGame> {
)); ));
break; break;
case "duck": case "duck":
previousState = runnerState;
runnerState = event; runnerState = event;
sprite.current = RunnerState.duck; sprite.current = RunnerState.duck;
sprite.addEffect(MoveEffect( sprite.addEffect(MoveEffect(
@ -169,6 +175,7 @@ class Runner extends Component with HasGameRef<MyGame> {
if (dead) { if (dead) {
return; return;
} }
previousState = runnerState;
sprite.clearEffects(); sprite.clearEffects();
level = 11; level = 11;
sprite.addEffect(MoveEffect( sprite.addEffect(MoveEffect(
@ -185,6 +192,7 @@ class Runner extends Component with HasGameRef<MyGame> {
if (dead) { if (dead) {
return; return;
} }
previousState = runnerState;
sprite.clearEffects(); sprite.clearEffects();
level = 11; level = 11;
sprite.addEffect(MoveEffect( sprite.addEffect(MoveEffect(
@ -201,6 +209,7 @@ class Runner extends Component with HasGameRef<MyGame> {
if (dead) { if (dead) {
return; return;
} }
previousState = runnerState;
sprite.clearEffects(); sprite.clearEffects();
level = 11; level = 11;
sprite.addEffect(MoveEffect( sprite.addEffect(MoveEffect(
@ -330,6 +339,14 @@ class Runner extends Component with HasGameRef<MyGame> {
sprite.current = RunnerState.run; sprite.current = RunnerState.run;
} }
if (runnerState == "float" || runnerState == "double_jump") {
if (onTopOfPlatform()) {
updateLevel();
sprite.clearEffects();
event("run");
}
}
intersecting(); intersecting();
sprite.update(dt); sprite.update(dt);
} }
@ -353,7 +370,11 @@ class Runner extends Component with HasGameRef<MyGame> {
} }
bool belowPlatform() { bool belowPlatform() {
Rect runnerRect = sprite.toRect(); Rect runnerRect = Rect.fromLTRB(
sprite.toRect().left,
sprite.toRect().top,
sprite.toRect().right - sprite.toRect().width / 2,
sprite.toRect().bottom);
bool belowPlatform = false; bool belowPlatform = false;
for (List<MovingObject> platformLevel in gameRef.platformHolder.objects) { for (List<MovingObject> platformLevel in gameRef.platformHolder.objects) {
for (MovingObject p in platformLevel) { for (MovingObject p in platformLevel) {
@ -443,14 +464,14 @@ class Runner extends Component with HasGameRef<MyGame> {
} }
} }
for (List<MovingObject> debrisLevel in gameRef.wallHolder.objects) { for (List<MovingObject> wallLevel in gameRef.wallHolder.objects) {
for (int i = 0; i < debrisLevel.length; i++) { for (int i = 0; i < wallLevel.length; i++) {
Rect slim = Rect.fromLTRB( Rect slim = Rect.fromLTRB(
runnerRect.left + sprite.width / 3, runnerRect.left + sprite.width / 3,
runnerRect.top + sprite.height / (runnerState == "duck" ? 3 : 6), runnerRect.top + sprite.height / (runnerState == "duck" ? 3 : 6),
runnerRect.right - sprite.width / 3, runnerRect.right - sprite.width / 3,
runnerRect.bottom - sprite.height / 3); runnerRect.bottom - sprite.height / 3);
String intersectState = debrisLevel[i].intersect(slim); String intersectState = wallLevel[i].intersect(slim);
if (intersectState == "none") { if (intersectState == "none") {
continue; continue;
} else { } else {

View File

@ -31,7 +31,7 @@ class Wall extends MovingObject {
gameRef.blockSize * gameRef.blockSize *
(gameRef.wallHolder.wall.width / gameRef.wallHolder.wall.height / 5) * (gameRef.wallHolder.wall.width / gameRef.wallHolder.wall.height / 5) *
2.0, 2.0,
gameRef.blockSize * 0.5, gameRef.blockSize * 0.35,
); );
} }