2021-09-07 20:45:42 +00:00
|
|
|
import 'package:firo_runner/main.dart';
|
2021-09-03 23:29:18 +00:00
|
|
|
import 'package:flame/components.dart';
|
|
|
|
|
|
|
|
class GameState extends Component {
|
2021-09-05 22:36:36 +00:00
|
|
|
int start = 0;
|
|
|
|
bool isPaused = false;
|
|
|
|
int numCoins = 0;
|
|
|
|
int distance = 0;
|
2021-09-08 23:07:54 +00:00
|
|
|
late MyGame gameRef;
|
2021-09-18 21:52:28 +00:00
|
|
|
int previousLevel = 1;
|
2021-09-03 23:29:18 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void update(double dt) {
|
|
|
|
super.update(dt);
|
2021-09-05 22:36:36 +00:00
|
|
|
if (!isPaused) {
|
|
|
|
distance = DateTime.now().microsecondsSinceEpoch - start;
|
2021-09-18 21:52:28 +00:00
|
|
|
if (previousLevel != getLevel()) {
|
|
|
|
previousLevel = getLevel();
|
|
|
|
gameRef.fireworks.reset();
|
|
|
|
}
|
2021-09-05 22:36:36 +00:00
|
|
|
}
|
2021-09-03 23:29:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void addCoin() {
|
|
|
|
numCoins++;
|
|
|
|
}
|
|
|
|
|
2021-09-08 23:07:54 +00:00
|
|
|
void setUp(MyGame gameRef) {
|
|
|
|
this.gameRef = gameRef;
|
2021-09-05 22:36:36 +00:00
|
|
|
numCoins = 0;
|
|
|
|
distance = 0;
|
2021-09-18 21:52:28 +00:00
|
|
|
previousLevel = 1;
|
2021-09-03 23:29:18 +00:00
|
|
|
start = DateTime.now().microsecondsSinceEpoch;
|
2021-09-05 22:36:36 +00:00
|
|
|
isPaused = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setPaused() {
|
|
|
|
isPaused = true;
|
2021-09-03 23:29:18 +00:00
|
|
|
}
|
|
|
|
|
2021-09-07 20:45:42 +00:00
|
|
|
int getLevel() {
|
|
|
|
if (distance > LEVEL7) {
|
|
|
|
return 7;
|
|
|
|
} else if (distance > LEVEL6) {
|
|
|
|
return 6;
|
|
|
|
} else if (distance > LEVEL5) {
|
|
|
|
return 5;
|
|
|
|
} else if (distance > LEVEL4) {
|
|
|
|
return 4;
|
|
|
|
} else if (distance > LEVEL3) {
|
|
|
|
return 3;
|
|
|
|
} else if (distance > LEVEL2) {
|
|
|
|
return 2;
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-18 21:52:28 +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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int getScore() {
|
|
|
|
return distance ~/ 10 + numCoins * 1000000;
|
|
|
|
}
|
|
|
|
|
2021-09-03 23:29:18 +00:00
|
|
|
double getVelocity() {
|
2021-09-05 22:36:36 +00:00
|
|
|
if (!isPaused) {
|
2021-09-07 20:45:42 +00:00
|
|
|
switch (getLevel()) {
|
|
|
|
case 7:
|
2021-09-08 23:07:54 +00:00
|
|
|
return gameRef.viewport.canvasSize.x * 0.25;
|
2021-09-07 20:45:42 +00:00
|
|
|
case 6:
|
2021-09-08 23:07:54 +00:00
|
|
|
return gameRef.viewport.canvasSize.x * 0.20;
|
2021-09-07 20:45:42 +00:00
|
|
|
case 5:
|
2021-09-08 23:07:54 +00:00
|
|
|
return gameRef.viewport.canvasSize.x * 0.18;
|
2021-09-07 20:45:42 +00:00
|
|
|
case 4:
|
2021-09-08 23:07:54 +00:00
|
|
|
return gameRef.viewport.canvasSize.x * 0.16;
|
2021-09-07 20:45:42 +00:00
|
|
|
case 3:
|
2021-09-08 23:07:54 +00:00
|
|
|
return gameRef.viewport.canvasSize.x * 0.14;
|
2021-09-07 20:45:42 +00:00
|
|
|
case 2:
|
2021-09-08 23:07:54 +00:00
|
|
|
return gameRef.viewport.canvasSize.x * 0.12;
|
2021-09-07 20:45:42 +00:00
|
|
|
default:
|
2021-09-08 23:07:54 +00:00
|
|
|
return gameRef.viewport.canvasSize.x * 0.1;
|
2021-09-05 22:36:36 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return 0;
|
2021-09-03 23:29:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|