firo_runner/lib/wire_holder.dart

73 lines
1.8 KiB
Dart

import 'package:firo_runner/holder.dart';
import 'package:firo_runner/platform.dart';
import 'package:flame/flame.dart';
import 'package:firo_runner/wire.dart';
import 'package:firo_runner/main.dart';
import 'package:flame/extensions.dart';
class WireHolder extends Holder {
late Image wire;
@override
Future load() async {
wire = await Flame.images.load("wire-frames.png");
}
getWire() {
return wire;
}
bool generateWire(MyGame gameRef, int level, bool force) {
if (objects[level].isNotEmpty) {
return false;
}
if (random.nextInt(100) > 100) {
return true;
} else {
int nearestPlatform = getNearestPlatform(level);
Platform? platform =
gameRef.platformHolder.getPlatformOffScreen(nearestPlatform);
if (platform != null && platform.prohibitObstacles) {
return false;
}
double xCoordinate = -100;
if (level == 0) {
xCoordinate = gameRef.size.x;
} else if (platform != null) {
xCoordinate = platform.sprite.x;
} else {
return false;
}
Wire wire = Wire(gameRef);
wire.sprite.renderFlipX = true;
if (level % 3 == 0) {
wire.sprite.renderFlipY = true;
wire.setPosition(
xCoordinate, gameRef.blockSize * level - 2 * gameRef.blockSize / 7);
} else {
wire.setPosition(
xCoordinate, gameRef.blockSize * level + gameRef.blockSize / 10);
}
if (gameRef.isTooNearOtherObstacles(wire.sprite.toRect())) {
return false;
}
objects[level].add(wire);
gameRef.add(wire.sprite);
if (platform != null) {
platform.removeChildren.add(() {
objects[level].remove(wire);
wire.remove();
});
}
return false;
}
}
}