2021-09-08 21:44:12 +00:00
|
|
|
import 'package:firo_runner/bug.dart';
|
|
|
|
import 'package:firo_runner/coin.dart';
|
|
|
|
import 'package:firo_runner/wire.dart';
|
2021-09-02 22:30:18 +00:00
|
|
|
import 'package:firo_runner/main.dart';
|
|
|
|
import 'package:flame/effects.dart';
|
2021-09-08 21:44:12 +00:00
|
|
|
import 'package:flame/extensions.dart';
|
2021-09-02 22:30:18 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2021-09-08 21:44:12 +00:00
|
|
|
import 'package:firo_runner/platform.dart';
|
2021-09-02 22:30:18 +00:00
|
|
|
|
|
|
|
import 'package:flame/components.dart';
|
|
|
|
|
|
|
|
enum RunnerState {
|
|
|
|
run,
|
|
|
|
jump,
|
|
|
|
duck,
|
|
|
|
kick,
|
|
|
|
float,
|
|
|
|
fall,
|
|
|
|
die,
|
2021-09-08 21:44:12 +00:00
|
|
|
electrocute,
|
2021-09-06 22:28:56 +00:00
|
|
|
glitch,
|
2021-09-02 22:30:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class Runner extends Component with HasGameRef<MyGame> {
|
|
|
|
late SpriteAnimationGroupComponent sprite;
|
2021-09-04 03:03:05 +00:00
|
|
|
String runnerState = "run";
|
2021-09-05 22:36:36 +00:00
|
|
|
int level = 4;
|
|
|
|
String previousState = "run";
|
|
|
|
var runnerPosition = Vector2(0, 0);
|
2021-09-08 21:44:12 +00:00
|
|
|
late Vector2 runnerSize;
|
2021-09-05 22:36:36 +00:00
|
|
|
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);
|
2021-09-08 19:27:06 +00:00
|
|
|
runnerPosition = Vector2(gameRef.blockSize * 2, gameRef.blockSize * 4);
|
2021-09-05 22:36:36 +00:00
|
|
|
setPosition(runnerPosition);
|
|
|
|
}
|
2021-09-02 22:30:18 +00:00
|
|
|
|
|
|
|
void setPosition(Vector2 position) {
|
|
|
|
sprite.position = position;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setSize(Vector2 size, double ySize) {
|
|
|
|
sprite.size = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
Sprite getSprite() {
|
|
|
|
return sprite.animation!.getSprite();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void render(Canvas c) {
|
|
|
|
super.render(c);
|
|
|
|
getSprite().render(c, position: sprite.position, size: sprite.size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateLevel() {
|
|
|
|
level = (sprite.position.y / gameRef.blockSize).round();
|
|
|
|
}
|
|
|
|
|
|
|
|
void event(String event) {
|
2021-09-05 22:36:36 +00:00
|
|
|
if (gameRef.gameState.isPaused) {
|
|
|
|
return;
|
|
|
|
}
|
2021-09-04 03:03:05 +00:00
|
|
|
previousState = runnerState;
|
2021-09-02 22:30:18 +00:00
|
|
|
switch (event) {
|
|
|
|
case "jump":
|
|
|
|
runnerState = event;
|
|
|
|
sprite.current = RunnerState.jump;
|
|
|
|
sprite.addEffect(MoveEffect(
|
|
|
|
path: [
|
|
|
|
// sprite.position,
|
|
|
|
Vector2(sprite.x, (level - 1) * gameRef.blockSize),
|
|
|
|
],
|
2021-09-06 22:28:56 +00:00
|
|
|
speed: 150,
|
2021-09-02 22:30:18 +00:00
|
|
|
curve: Curves.bounceIn,
|
|
|
|
onComplete: () {
|
|
|
|
updateLevel();
|
2021-09-04 03:03:05 +00:00
|
|
|
this.event("float");
|
2021-09-02 22:30:18 +00:00
|
|
|
},
|
|
|
|
));
|
|
|
|
break;
|
2021-09-08 21:44:12 +00:00
|
|
|
case "double_jump":
|
2021-09-04 03:03:05 +00:00
|
|
|
if (level - 1 < 0) {
|
|
|
|
break;
|
|
|
|
}
|
2021-09-02 22:30:18 +00:00
|
|
|
runnerState = event;
|
2021-09-04 22:56:08 +00:00
|
|
|
sprite.current = RunnerState.float;
|
2021-09-02 22:30:18 +00:00
|
|
|
sprite.addEffect(MoveEffect(
|
|
|
|
path: [
|
2021-09-04 03:03:05 +00:00
|
|
|
Vector2(sprite.x, (level - 2) * gameRef.blockSize),
|
2021-09-02 22:30:18 +00:00
|
|
|
],
|
2021-09-06 22:28:56 +00:00
|
|
|
speed: 150,
|
2021-09-03 23:29:18 +00:00
|
|
|
curve: Curves.ease,
|
2021-09-02 22:30:18 +00:00
|
|
|
onComplete: () {
|
|
|
|
updateLevel();
|
2021-09-04 22:56:08 +00:00
|
|
|
this.event("float");
|
2021-09-02 22:30:18 +00:00
|
|
|
},
|
|
|
|
));
|
|
|
|
break;
|
|
|
|
case "fall":
|
|
|
|
runnerState = event;
|
|
|
|
sprite.current = RunnerState.fall;
|
|
|
|
sprite.addEffect(MoveEffect(
|
|
|
|
path: [
|
|
|
|
Vector2(sprite.x, (level + 1) * gameRef.blockSize),
|
|
|
|
],
|
2021-09-04 03:03:05 +00:00
|
|
|
speed: 100,
|
2021-09-02 22:30:18 +00:00
|
|
|
curve: Curves.ease,
|
2021-09-04 03:03:05 +00:00
|
|
|
onComplete: () {
|
2021-09-04 22:56:08 +00:00
|
|
|
updateLevel();
|
|
|
|
this.event("float");
|
2021-09-04 03:03:05 +00:00
|
|
|
},
|
2021-09-02 22:30:18 +00:00
|
|
|
));
|
|
|
|
break;
|
|
|
|
case "kick":
|
|
|
|
runnerState = event;
|
|
|
|
sprite.current = RunnerState.kick;
|
|
|
|
break;
|
|
|
|
case "run":
|
|
|
|
runnerState = event;
|
|
|
|
sprite.current = RunnerState.run;
|
|
|
|
break;
|
|
|
|
case "float":
|
|
|
|
runnerState = event;
|
|
|
|
sprite.current = RunnerState.float;
|
|
|
|
sprite.addEffect(MoveEffect(
|
2021-09-04 03:03:05 +00:00
|
|
|
path: [sprite.position],
|
2021-09-04 22:56:08 +00:00
|
|
|
speed: 500,
|
2021-09-02 22:30:18 +00:00
|
|
|
curve: Curves.ease,
|
|
|
|
onComplete: () {
|
|
|
|
updateLevel();
|
|
|
|
runnerState = event;
|
|
|
|
sprite.current = RunnerState.float;
|
|
|
|
},
|
|
|
|
));
|
|
|
|
break;
|
|
|
|
case "duck":
|
|
|
|
runnerState = event;
|
|
|
|
sprite.current = RunnerState.duck;
|
|
|
|
break;
|
|
|
|
case "die":
|
2021-09-05 22:36:36 +00:00
|
|
|
if (dead) {
|
|
|
|
return;
|
|
|
|
}
|
2021-09-02 22:30:18 +00:00
|
|
|
runnerState = event;
|
|
|
|
sprite.current = RunnerState.die;
|
2021-09-05 22:36:36 +00:00
|
|
|
gameRef.die();
|
2021-09-02 22:30:18 +00:00
|
|
|
break;
|
2021-09-08 21:44:12 +00:00
|
|
|
case "electrocute":
|
2021-09-05 22:36:36 +00:00
|
|
|
if (dead) {
|
|
|
|
return;
|
|
|
|
}
|
2021-09-02 22:30:18 +00:00
|
|
|
runnerState = event;
|
2021-09-08 21:44:12 +00:00
|
|
|
sprite.current = RunnerState.electrocute;
|
2021-09-05 22:36:36 +00:00
|
|
|
gameRef.die();
|
2021-09-02 22:30:18 +00:00
|
|
|
break;
|
2021-09-06 22:28:56 +00:00
|
|
|
case "glitch":
|
|
|
|
if (dead) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
runnerState = event;
|
|
|
|
sprite.current = RunnerState.glitch;
|
|
|
|
gameRef.die();
|
|
|
|
break;
|
2021-09-02 22:30:18 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void control(String input) {
|
2021-09-05 22:36:36 +00:00
|
|
|
if (gameRef.gameState.isPaused) {
|
|
|
|
return;
|
|
|
|
}
|
2021-09-02 22:30:18 +00:00
|
|
|
switch (input) {
|
|
|
|
case "up":
|
|
|
|
if (runnerState == "run") {
|
|
|
|
event("jump");
|
2021-09-04 03:03:05 +00:00
|
|
|
} else if (runnerState == "float" && previousState == "jump") {
|
2021-09-08 21:44:12 +00:00
|
|
|
event("double_jump");
|
2021-09-02 22:30:18 +00:00
|
|
|
} else if (runnerState == "duck") {
|
|
|
|
event("run");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "down":
|
|
|
|
if (runnerState == "run") {
|
|
|
|
event("duck");
|
2021-09-04 03:03:05 +00:00
|
|
|
} else if (runnerState == "float" && onTopOfPlatform()) {
|
|
|
|
event("run");
|
2021-09-02 22:30:18 +00:00
|
|
|
} else if (runnerState == "float") {
|
|
|
|
event("fall");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "right":
|
|
|
|
if (runnerState == "run") {
|
|
|
|
event("kick");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "center":
|
2021-09-04 03:03:05 +00:00
|
|
|
if (runnerState == "fall") {
|
|
|
|
updateLevel();
|
2021-09-02 22:30:18 +00:00
|
|
|
event("float");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void update(double dt) {
|
|
|
|
super.update(dt);
|
|
|
|
// If the animation is finished
|
|
|
|
if (sprite.animation?.done() ?? false) {
|
|
|
|
sprite.animation!.reset();
|
|
|
|
if (runnerState == "kick") {
|
|
|
|
event("run");
|
|
|
|
}
|
|
|
|
sprite.current = RunnerState.run;
|
|
|
|
}
|
|
|
|
|
2021-09-04 03:03:05 +00:00
|
|
|
intersecting();
|
2021-09-02 22:30:18 +00:00
|
|
|
sprite.update(dt);
|
|
|
|
}
|
|
|
|
|
2021-09-04 03:03:05 +00:00
|
|
|
bool onTopOfPlatform() {
|
|
|
|
Rect runnerRect = sprite.toRect();
|
|
|
|
bool onTopOfPlatform = false;
|
|
|
|
for (List<Platform> platformLevel in gameRef.platformHolder.platforms) {
|
|
|
|
for (Platform p in platformLevel) {
|
|
|
|
String side = p.intersect(runnerRect);
|
|
|
|
if (side == "none") {
|
|
|
|
Rect belowRunner = Rect.fromLTRB(runnerRect.left, runnerRect.top,
|
|
|
|
runnerRect.right, runnerRect.bottom + 1);
|
|
|
|
if (p.intersect(belowRunner) != "none") {
|
|
|
|
onTopOfPlatform = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return onTopOfPlatform;
|
|
|
|
}
|
|
|
|
|
|
|
|
void intersecting() {
|
2021-09-05 22:36:36 +00:00
|
|
|
if (gameRef.gameState.isPaused) {
|
|
|
|
return;
|
|
|
|
}
|
2021-09-04 03:03:05 +00:00
|
|
|
Rect runnerRect = sprite.toRect();
|
|
|
|
bool onTopOfPlatform = false;
|
|
|
|
for (List<Platform> platformLevel in gameRef.platformHolder.platforms) {
|
|
|
|
for (Platform p in platformLevel) {
|
|
|
|
String side = p.intersect(runnerRect);
|
|
|
|
if (side == "none") {
|
|
|
|
Rect belowRunner = Rect.fromLTRB(runnerRect.left, runnerRect.top,
|
|
|
|
runnerRect.right, runnerRect.bottom + 1);
|
|
|
|
if (p.intersect(belowRunner) != "none") {
|
|
|
|
onTopOfPlatform = true;
|
|
|
|
}
|
|
|
|
} else if (side == "bottom") {
|
|
|
|
event("die");
|
2021-09-04 22:56:08 +00:00
|
|
|
return;
|
2021-09-04 03:03:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-04 04:17:12 +00:00
|
|
|
for (List<Coin> coinLevel in gameRef.coinHolder.coins) {
|
|
|
|
for (int i = 0; i < coinLevel.length;) {
|
|
|
|
if (coinLevel[i].intersect(runnerRect) != "none") {
|
|
|
|
gameRef.gameState.numCoins++;
|
2021-09-09 22:56:26 +00:00
|
|
|
if (gameRef.gameState.numCoins % 5 == 0) {
|
|
|
|
gameRef.fireworks.reset();
|
|
|
|
}
|
2021-09-05 22:36:36 +00:00
|
|
|
gameRef.coinHolder.remove(coinLevel, i);
|
2021-09-04 04:17:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-04 22:56:08 +00:00
|
|
|
for (List<Wire> wireLevel in gameRef.wireHolder.wires) {
|
|
|
|
for (int i = 0; i < wireLevel.length; i++) {
|
|
|
|
if (wireLevel[i].intersect(runnerRect) != "none") {
|
2021-09-08 21:44:12 +00:00
|
|
|
event("electrocute");
|
2021-09-05 22:36:36 +00:00
|
|
|
return;
|
2021-09-04 22:56:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-05 00:18:37 +00:00
|
|
|
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") {
|
2021-09-06 22:28:56 +00:00
|
|
|
event("glitch");
|
2021-09-05 22:36:36 +00:00
|
|
|
return;
|
2021-09-05 00:18:37 +00:00
|
|
|
}
|
|
|
|
} else if (intersectState == "left" && runnerState == "kick") {
|
|
|
|
bugLevel[i].sprite.current = BugState.breaking;
|
|
|
|
// bugLevel[i].remove();
|
|
|
|
// bugLevel.removeAt(i);
|
|
|
|
} else {
|
2021-09-06 22:28:56 +00:00
|
|
|
event("glitch");
|
2021-09-05 22:36:36 +00:00
|
|
|
return;
|
2021-09-05 00:18:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-04 03:03:05 +00:00
|
|
|
if (!onTopOfPlatform &&
|
|
|
|
(runnerState == "run" ||
|
|
|
|
runnerState == "kick" ||
|
|
|
|
runnerState == "duck")) {
|
|
|
|
event("fall");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-02 22:30:18 +00:00
|
|
|
Future load(loadSpriteAnimation) async {
|
|
|
|
SpriteAnimation running = await loadSpriteAnimation(
|
|
|
|
'run-frames.png',
|
|
|
|
SpriteAnimationData.sequenced(
|
|
|
|
amount: 7,
|
|
|
|
stepTime: 0.1,
|
|
|
|
textureSize: Vector2(512, 512),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
SpriteAnimation jumping = await loadSpriteAnimation(
|
|
|
|
'jump-frames.png',
|
|
|
|
SpriteAnimationData.sequenced(
|
|
|
|
amount: 5,
|
|
|
|
stepTime: 0.1,
|
|
|
|
textureSize: Vector2(512, 512),
|
|
|
|
loop: false,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
SpriteAnimation ducking = await loadSpriteAnimation(
|
|
|
|
'crawl-frames.png',
|
|
|
|
SpriteAnimationData.sequenced(
|
|
|
|
amount: 3,
|
|
|
|
stepTime: 0.1,
|
|
|
|
textureSize: Vector2(512, 512),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
SpriteAnimation kicking = await loadSpriteAnimation(
|
|
|
|
'kick-frames.png',
|
|
|
|
SpriteAnimationData.sequenced(
|
|
|
|
amount: 13,
|
2021-09-05 00:18:37 +00:00
|
|
|
stepTime: 0.05,
|
2021-09-02 22:30:18 +00:00
|
|
|
textureSize: Vector2(512, 512),
|
|
|
|
loop: false,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
SpriteAnimation floating = await loadSpriteAnimation(
|
2021-09-04 22:56:08 +00:00
|
|
|
'hover-frames.png',
|
2021-09-02 22:30:18 +00:00
|
|
|
SpriteAnimationData.sequenced(
|
2021-09-04 22:56:08 +00:00
|
|
|
amount: 3,
|
2021-09-02 22:30:18 +00:00
|
|
|
stepTime: 0.1,
|
|
|
|
textureSize: Vector2(512, 512),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
SpriteAnimation falling = await loadSpriteAnimation(
|
2021-09-09 22:56:26 +00:00
|
|
|
'fall-frames.png',
|
2021-09-02 22:30:18 +00:00
|
|
|
SpriteAnimationData.sequenced(
|
2021-09-09 22:56:26 +00:00
|
|
|
amount: 7,
|
2021-09-02 22:30:18 +00:00
|
|
|
stepTime: 0.1,
|
|
|
|
textureSize: Vector2(512, 512),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2021-09-04 22:56:08 +00:00
|
|
|
SpriteAnimation dying = await loadSpriteAnimation(
|
2021-09-02 22:30:18 +00:00
|
|
|
'death-normal-frames.png',
|
|
|
|
SpriteAnimationData.sequenced(
|
|
|
|
amount: 20,
|
2021-09-03 23:29:18 +00:00
|
|
|
stepTime: 0.05,
|
2021-09-02 22:30:18 +00:00
|
|
|
textureSize: Vector2(512, 512),
|
|
|
|
loop: false,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2021-09-04 22:56:08 +00:00
|
|
|
SpriteAnimation dyingElectrocuted = await loadSpriteAnimation(
|
|
|
|
'electrocuted-frames.png',
|
2021-09-02 22:30:18 +00:00
|
|
|
SpriteAnimationData.sequenced(
|
|
|
|
amount: 2,
|
2021-09-05 22:36:36 +00:00
|
|
|
stepTime: 0.25,
|
2021-09-02 22:30:18 +00:00
|
|
|
textureSize: Vector2(512, 512),
|
2021-09-05 22:36:36 +00:00
|
|
|
loop: false,
|
2021-09-02 22:30:18 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2021-09-06 22:28:56 +00:00
|
|
|
SpriteAnimation dyingGlitch = await loadSpriteAnimation(
|
|
|
|
'death-glitched-frames.png',
|
|
|
|
SpriteAnimationData.sequenced(
|
|
|
|
amount: 8,
|
|
|
|
stepTime: 0.1,
|
|
|
|
textureSize: Vector2(512, 512),
|
|
|
|
loop: false,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2021-09-02 22:30:18 +00:00
|
|
|
sprite = SpriteAnimationGroupComponent(
|
|
|
|
animations: {
|
|
|
|
RunnerState.run: running,
|
|
|
|
RunnerState.jump: jumping,
|
|
|
|
RunnerState.duck: ducking,
|
|
|
|
RunnerState.kick: kicking,
|
|
|
|
RunnerState.float: floating,
|
|
|
|
RunnerState.fall: falling,
|
2021-09-04 22:56:08 +00:00
|
|
|
RunnerState.die: dying,
|
2021-09-08 21:44:12 +00:00
|
|
|
RunnerState.electrocute: dyingElectrocuted,
|
2021-09-06 22:28:56 +00:00
|
|
|
RunnerState.glitch: dyingGlitch,
|
2021-09-02 22:30:18 +00:00
|
|
|
},
|
|
|
|
current: RunnerState.run,
|
|
|
|
);
|
2021-09-04 22:56:08 +00:00
|
|
|
|
|
|
|
changePriorityWithoutResorting(RUNNER_PRIORITY);
|
2021-09-02 22:30:18 +00:00
|
|
|
}
|
|
|
|
}
|