2021-09-16 21:15:32 +00:00
|
|
|
import 'package:firo_runner/holder.dart';
|
2021-09-04 04:17:12 +00:00
|
|
|
import 'package:flame/flame.dart';
|
|
|
|
|
2021-09-08 21:44:12 +00:00
|
|
|
import 'package:firo_runner/coin.dart';
|
|
|
|
import 'package:firo_runner/main.dart';
|
|
|
|
import 'package:flame/extensions.dart';
|
2021-09-11 02:39:32 +00:00
|
|
|
import 'package:firo_runner/platform.dart';
|
2021-09-04 04:17:12 +00:00
|
|
|
|
2021-09-16 21:15:32 +00:00
|
|
|
class CoinHolder extends Holder {
|
2021-09-08 21:44:12 +00:00
|
|
|
late Image coin;
|
2021-09-04 04:17:12 +00:00
|
|
|
|
2021-09-16 21:15:32 +00:00
|
|
|
@override
|
|
|
|
Future load() async {
|
2021-09-04 04:17:12 +00:00
|
|
|
coin = await Flame.images.load("coin-frames.png");
|
2021-09-05 22:36:36 +00:00
|
|
|
}
|
|
|
|
|
2021-09-16 21:15:32 +00:00
|
|
|
Image getCoin() {
|
2021-09-04 04:17:12 +00:00
|
|
|
return coin;
|
|
|
|
}
|
|
|
|
|
2021-09-17 03:26:57 +00:00
|
|
|
bool generateCoin(MyGame gameRef, int level,
|
|
|
|
{bool force = false, double xPosition = 0}) {
|
|
|
|
if (total() > 5 && !force) {
|
2021-09-04 04:17:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-17 03:26:57 +00:00
|
|
|
if (objects[level].isNotEmpty && !force) {
|
2021-09-11 02:39:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-17 03:26:57 +00:00
|
|
|
if (random.nextInt(100) > 25 && !force) {
|
2021-09-04 04:17:12 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
2021-09-16 21:15:32 +00:00
|
|
|
int nearestPlatform = getNearestPlatform(level);
|
2021-09-11 02:39:32 +00:00
|
|
|
|
|
|
|
Platform? platform =
|
|
|
|
gameRef.platformHolder.getPlatformOffScreen(nearestPlatform);
|
|
|
|
double xCoordinate = -100;
|
|
|
|
|
2021-09-17 03:26:57 +00:00
|
|
|
if (force) {
|
|
|
|
xCoordinate = xPosition;
|
|
|
|
} else if (level == 0) {
|
2021-09-11 02:39:32 +00:00
|
|
|
xCoordinate = gameRef.size.x;
|
|
|
|
} else if (platform != null) {
|
|
|
|
xCoordinate = platform.sprite.x;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-04 04:17:12 +00:00
|
|
|
Coin coin = Coin(gameRef);
|
2021-09-05 00:18:37 +00:00
|
|
|
coin.setPosition(xCoordinate, gameRef.blockSize * level);
|
2021-09-05 00:36:14 +00:00
|
|
|
|
2021-09-17 03:26:57 +00:00
|
|
|
if (gameRef.isTooNearOtherObstacles(coin.sprite.toRect()) && !force) {
|
2021-09-05 00:36:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-16 21:15:32 +00:00
|
|
|
objects[level].add(coin);
|
2021-09-04 22:56:08 +00:00
|
|
|
gameRef.add(coin.sprite);
|
2021-09-04 04:17:12 +00:00
|
|
|
}
|
2021-09-11 02:39:32 +00:00
|
|
|
return false;
|
2021-09-04 04:17:12 +00:00
|
|
|
}
|
|
|
|
}
|