2021-09-22 19:26:06 +00:00
|
|
|
import 'package:firo_runner/main.dart';
|
|
|
|
import 'package:flame/components.dart';
|
|
|
|
|
2021-10-09 18:32:15 +00:00
|
|
|
// Class the holds the game state and several functions related to score and
|
|
|
|
// speed.
|
2021-09-22 19:26:06 +00:00
|
|
|
class GameState extends Component {
|
|
|
|
int start = 0;
|
|
|
|
bool isPaused = false;
|
|
|
|
int numCoins = 0;
|
2021-10-09 18:32:15 +00:00
|
|
|
int time = 0;
|
2021-09-22 19:26:06 +00:00
|
|
|
late MyGame gameRef;
|
|
|
|
int previousLevel = 1;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void update(double dt) {
|
|
|
|
super.update(dt);
|
|
|
|
if (!isPaused) {
|
2021-10-09 18:32:15 +00:00
|
|
|
time = DateTime.now().microsecondsSinceEpoch - start;
|
2021-09-22 19:26:06 +00:00
|
|
|
if (previousLevel != getLevel()) {
|
|
|
|
previousLevel = getLevel();
|
|
|
|
gameRef.fireworks.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void addCoin() {
|
|
|
|
numCoins++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setUp(MyGame gameRef) {
|
|
|
|
this.gameRef = gameRef;
|
|
|
|
numCoins = 0;
|
2021-10-09 18:32:15 +00:00
|
|
|
time = 0;
|
2021-09-22 19:26:06 +00:00
|
|
|
previousLevel = 1;
|
|
|
|
start = DateTime.now().microsecondsSinceEpoch;
|
|
|
|
isPaused = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setPaused() {
|
|
|
|
isPaused = true;
|
|
|
|
}
|
|
|
|
|
2021-10-09 18:32:15 +00:00
|
|
|
// This is the level of the game.
|
2021-09-22 19:26:06 +00:00
|
|
|
int getLevel() {
|
2021-10-09 18:32:15 +00:00
|
|
|
if (time > LEVEL7) {
|
2021-09-22 19:26:06 +00:00
|
|
|
return 7;
|
2021-10-09 18:32:15 +00:00
|
|
|
} else if (time > LEVEL6) {
|
2021-09-22 19:26:06 +00:00
|
|
|
return 6;
|
2021-10-09 18:32:15 +00:00
|
|
|
} else if (time > LEVEL5) {
|
2021-09-22 19:26:06 +00:00
|
|
|
return 5;
|
2021-10-09 18:32:15 +00:00
|
|
|
} else if (time > LEVEL4) {
|
2021-09-22 19:26:06 +00:00
|
|
|
return 4;
|
2021-10-09 18:32:15 +00:00
|
|
|
} else if (time > LEVEL3) {
|
2021-09-22 19:26:06 +00:00
|
|
|
return 3;
|
2021-10-09 18:32:15 +00:00
|
|
|
} else if (time > LEVEL2) {
|
2021-09-22 19:26:06 +00:00
|
|
|
return 2;
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-09 18:32:15 +00:00
|
|
|
// This determines the stages of the games and its animations.
|
2021-09-22 19:26:06 +00:00
|
|
|
int getScoreLevel() {
|
|
|
|
int score = getScore();
|
|
|
|
if (score > LEVEL7) {
|
|
|
|
return 12;
|
|
|
|
} else if (score > LEVEL6 + LEVEL6 / 2) {
|
|
|
|
return 11;
|
|
|
|
} else if (score > LEVEL6) {
|
|
|
|
return 10;
|
|
|
|
} else if (score > LEVEL5 + LEVEL5 / 2) {
|
|
|
|
return 9;
|
|
|
|
} else if (score > LEVEL5) {
|
|
|
|
return 8;
|
|
|
|
} else if (score > LEVEL4 + LEVEL4 / 2) {
|
|
|
|
return 7;
|
|
|
|
} else if (score > LEVEL4) {
|
|
|
|
return 6;
|
|
|
|
} else if (score > LEVEL3 + LEVEL3 / 2) {
|
|
|
|
return 5;
|
|
|
|
} else if (score > LEVEL3) {
|
|
|
|
return 4;
|
|
|
|
} else if (score > LEVEL2 + LEVEL2 / 2) {
|
|
|
|
return 3;
|
|
|
|
} else if (score > LEVEL2) {
|
|
|
|
return 2;
|
|
|
|
} else if (score > LEVEL2 - LEVEL2 / 2) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-09 18:32:15 +00:00
|
|
|
// Gets the danger level of the game, this determines the appearance of
|
|
|
|
// obstacles in the beginning of the game.
|
2021-10-02 02:46:44 +00:00
|
|
|
int getDangerLevel() {
|
|
|
|
int score = getScore();
|
|
|
|
if (score > LEVEL2 / 2 + LEVEL2 / (2 * 4)) {
|
|
|
|
return 5;
|
|
|
|
} else if (score > LEVEL2 / 2) {
|
|
|
|
return 4;
|
|
|
|
} else if (score > LEVEL2 / 2 - LEVEL2 / (2 * 4)) {
|
|
|
|
return 3;
|
|
|
|
} else if (score > LEVEL2 / 2 - 2 * LEVEL2 / (2 * 4)) {
|
|
|
|
return 2;
|
|
|
|
} else if (score > LEVEL2 / 2 - 3 * LEVEL2 / (2 * 4)) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-09 18:32:15 +00:00
|
|
|
// This score is used to determine the danger level of the game,
|
|
|
|
// and progression.
|
2021-09-22 19:26:06 +00:00
|
|
|
int getScore() {
|
2021-10-09 18:32:15 +00:00
|
|
|
return time ~/ 10 + numCoins * 1000000;
|
2021-09-22 19:26:06 +00:00
|
|
|
}
|
|
|
|
|
2021-10-09 18:32:15 +00:00
|
|
|
// This is the real score that the player sees.
|
2021-09-22 19:26:06 +00:00
|
|
|
int getPlayerScore() {
|
|
|
|
return getScore() ~/ 10000;
|
|
|
|
}
|
|
|
|
|
2021-10-09 18:32:15 +00:00
|
|
|
// Gets how long the player has been playing the game.
|
|
|
|
int getPlayerTime() {
|
|
|
|
return time ~/ 1000000;
|
2021-09-22 19:26:06 +00:00
|
|
|
}
|
|
|
|
|
2021-10-09 18:32:15 +00:00
|
|
|
// Get the relative pixel velocity at the current moment.
|
2021-09-22 19:26:06 +00:00
|
|
|
double getVelocity() {
|
|
|
|
if (!isPaused) {
|
|
|
|
switch (getLevel()) {
|
|
|
|
case 7:
|
2021-10-03 19:11:40 +00:00
|
|
|
return gameRef.viewport.canvasSize.x * 0.30;
|
2021-09-22 19:26:06 +00:00
|
|
|
case 6:
|
2021-10-03 19:11:40 +00:00
|
|
|
return gameRef.viewport.canvasSize.x * 0.28;
|
2021-09-22 19:26:06 +00:00
|
|
|
case 5:
|
2021-10-03 19:11:40 +00:00
|
|
|
return gameRef.viewport.canvasSize.x * 0.26;
|
2021-09-22 19:26:06 +00:00
|
|
|
case 4:
|
2021-10-03 19:11:40 +00:00
|
|
|
return gameRef.viewport.canvasSize.x * 0.24;
|
2021-09-22 19:26:06 +00:00
|
|
|
case 3:
|
2021-10-03 19:11:40 +00:00
|
|
|
return gameRef.viewport.canvasSize.x * 0.22;
|
2021-09-22 19:26:06 +00:00
|
|
|
case 2:
|
2021-10-03 19:11:40 +00:00
|
|
|
return gameRef.viewport.canvasSize.x * 0.20;
|
2021-09-22 19:26:06 +00:00
|
|
|
default:
|
2021-10-03 19:11:40 +00:00
|
|
|
return gameRef.viewport.canvasSize.x * 0.18;
|
2021-09-22 19:26:06 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2021-10-02 02:46:44 +00:00
|
|
|
|
2021-10-09 18:32:15 +00:00
|
|
|
// Returns the level of the Robot, used to determine what animations it uses.
|
2021-10-02 02:46:44 +00:00
|
|
|
int getRobotLevel() {
|
|
|
|
if (numCoins > COINS_ROBOT_UPGRADE2) {
|
|
|
|
return 3;
|
|
|
|
} else if (numCoins > COINS_ROBOT_UPGRADE1) {
|
|
|
|
return 2;
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2021-09-22 19:26:06 +00:00
|
|
|
}
|