2021-09-16 21:15:32 +00:00
|
|
|
import 'package:firo_runner/holder.dart';
|
2021-09-08 21:44:12 +00:00
|
|
|
import 'package:firo_runner/platform.dart';
|
2021-10-05 23:19:58 +00:00
|
|
|
import 'package:flame/components.dart';
|
2021-09-05 00:18:37 +00:00
|
|
|
|
2021-09-08 21:44:12 +00:00
|
|
|
import 'package:firo_runner/bug.dart';
|
|
|
|
import 'package:firo_runner/main.dart';
|
2021-09-05 00:18:37 +00:00
|
|
|
|
2021-09-16 21:15:32 +00:00
|
|
|
class BugHolder extends Holder {
|
2021-10-05 23:19:58 +00:00
|
|
|
late List<Sprite> bug;
|
|
|
|
late List<Sprite> breaking;
|
2021-09-05 00:18:37 +00:00
|
|
|
|
2021-09-16 21:15:32 +00:00
|
|
|
@override
|
|
|
|
Future load() async {
|
2021-10-05 23:19:58 +00:00
|
|
|
bug = await loadListSprites("bug", "bug", 8);
|
|
|
|
breaking = await loadListSprites("bug", "bug_break", 13);
|
2021-09-05 22:36:36 +00:00
|
|
|
}
|
|
|
|
|
2021-10-05 23:19:58 +00:00
|
|
|
List<Sprite> getBug(String state) {
|
2021-09-05 00:18:37 +00:00
|
|
|
switch (state) {
|
|
|
|
case "normal":
|
|
|
|
return bug;
|
|
|
|
default:
|
|
|
|
return breaking;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-17 03:26:57 +00:00
|
|
|
bool generateBug(MyGame gameRef, int level,
|
|
|
|
{bool force = false, double xPosition = 0}) {
|
2021-10-05 23:19:58 +00:00
|
|
|
if (objects[level].isNotEmpty) {
|
2021-09-05 00:18:37 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (random.nextInt(100) > 25) {
|
|
|
|
return true;
|
|
|
|
} else {
|
2021-09-16 21:15:32 +00:00
|
|
|
int nearestPlatform = getNearestPlatform(level);
|
2021-09-05 00:18:37 +00:00
|
|
|
|
|
|
|
Platform? platform =
|
|
|
|
gameRef.platformHolder.getPlatformOffScreen(nearestPlatform);
|
2021-09-13 19:21:52 +00:00
|
|
|
if (platform != null && platform.prohibitObstacles) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-09-05 00:18:37 +00:00
|
|
|
double xCoordinate = -100;
|
|
|
|
|
|
|
|
if (level == 0) {
|
|
|
|
xCoordinate = gameRef.size.x;
|
|
|
|
} else if (platform != null) {
|
|
|
|
xCoordinate = platform.sprite.x;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Bug bug = Bug(gameRef);
|
|
|
|
bug.setPosition(xCoordinate, gameRef.blockSize * level);
|
2021-09-05 00:36:14 +00:00
|
|
|
|
|
|
|
if (gameRef.isTooNearOtherObstacles(bug.sprite.toRect())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-16 21:15:32 +00:00
|
|
|
objects[level].add(bug);
|
2021-09-05 00:18:37 +00:00
|
|
|
gameRef.add(bug.sprite);
|
|
|
|
if (platform != null) {
|
|
|
|
platform.removeChildren.add(() {
|
2021-09-16 21:15:32 +00:00
|
|
|
objects[level].remove(bug);
|
2021-09-05 00:18:37 +00:00
|
|
|
bug.remove();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|