2021-09-03 23:29:18 +00:00
|
|
|
import 'dart:math';
|
|
|
|
|
2021-09-08 21:44:12 +00:00
|
|
|
import 'package:firo_runner/moving_object.dart';
|
2021-09-03 23:29:18 +00:00
|
|
|
import 'package:firo_runner/main.dart';
|
|
|
|
import 'package:flame/components.dart';
|
|
|
|
|
2021-09-08 19:27:06 +00:00
|
|
|
enum PlatformState {
|
|
|
|
left,
|
|
|
|
mid,
|
|
|
|
right,
|
|
|
|
single,
|
|
|
|
}
|
2021-09-03 23:29:18 +00:00
|
|
|
|
|
|
|
class Platform extends MovingObject {
|
2021-09-08 19:27:06 +00:00
|
|
|
int row = 0;
|
2021-09-13 19:21:52 +00:00
|
|
|
bool prohibitObstacles = false;
|
2021-09-05 00:18:37 +00:00
|
|
|
List<Function> removeChildren = [];
|
2021-09-04 22:56:08 +00:00
|
|
|
|
2021-09-03 23:29:18 +00:00
|
|
|
Platform(MyGame gameRef) : super(gameRef) {
|
|
|
|
var random = Random();
|
2021-09-08 19:27:06 +00:00
|
|
|
|
|
|
|
int version = random.nextInt(2);
|
|
|
|
|
2021-10-05 23:19:58 +00:00
|
|
|
SpriteAnimation left = SpriteAnimation.spriteList(
|
|
|
|
version == 0 ? gameRef.platformHolder.l1 : gameRef.platformHolder.l2,
|
|
|
|
stepTime: 0.12);
|
2021-09-08 19:27:06 +00:00
|
|
|
|
2021-10-05 23:19:58 +00:00
|
|
|
SpriteAnimation mid = SpriteAnimation.spriteList(
|
|
|
|
version == 0 ? gameRef.platformHolder.m1 : gameRef.platformHolder.m2,
|
|
|
|
stepTime: 0.12);
|
2021-09-08 19:27:06 +00:00
|
|
|
|
2021-10-05 23:19:58 +00:00
|
|
|
SpriteAnimation right = SpriteAnimation.spriteList(
|
|
|
|
version == 0 ? gameRef.platformHolder.r1 : gameRef.platformHolder.r2,
|
|
|
|
stepTime: 0.12);
|
2021-09-08 19:27:06 +00:00
|
|
|
|
2021-10-05 23:19:58 +00:00
|
|
|
SpriteAnimation single = SpriteAnimation.spriteList(
|
|
|
|
version == 0 ? gameRef.platformHolder.o1 : gameRef.platformHolder.o2,
|
|
|
|
stepTime: 0.12);
|
2021-09-03 23:29:18 +00:00
|
|
|
|
|
|
|
sprite = SpriteAnimationGroupComponent(
|
|
|
|
animations: {
|
2021-09-08 19:27:06 +00:00
|
|
|
PlatformState.left: left,
|
|
|
|
PlatformState.mid: mid,
|
|
|
|
PlatformState.right: right,
|
|
|
|
PlatformState.single: single,
|
2021-09-03 23:29:18 +00:00
|
|
|
},
|
2021-09-22 19:26:06 +00:00
|
|
|
current: PlatformState.mid,
|
2021-09-03 23:29:18 +00:00
|
|
|
);
|
|
|
|
|
2021-09-04 22:56:08 +00:00
|
|
|
sprite.changePriorityWithoutResorting(PLATFORM_PRIORITY);
|
|
|
|
|
2021-09-03 23:29:18 +00:00
|
|
|
setSize(
|
2021-09-08 19:27:06 +00:00
|
|
|
gameRef.blockSize *
|
2021-10-05 23:19:58 +00:00
|
|
|
(gameRef.platformHolder.l1[0].image.width /
|
|
|
|
gameRef.platformHolder.l1[0].image.height),
|
2021-09-03 23:29:18 +00:00
|
|
|
gameRef.blockSize,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-09-16 21:15:32 +00:00
|
|
|
@override
|
|
|
|
void remove() {
|
|
|
|
removeChildrenObjects();
|
|
|
|
super.remove();
|
2021-09-03 23:29:18 +00:00
|
|
|
}
|
|
|
|
|
2021-09-16 21:15:32 +00:00
|
|
|
void removeChildrenObjects() {
|
2021-09-05 00:18:37 +00:00
|
|
|
if (removeChildren.isNotEmpty) {
|
|
|
|
for (Function removeChild in removeChildren) {
|
|
|
|
removeChild();
|
|
|
|
}
|
2021-09-04 22:56:08 +00:00
|
|
|
}
|
2021-09-03 23:29:18 +00:00
|
|
|
}
|
2021-09-08 19:27:06 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void update(double dt) {
|
2021-09-16 21:15:32 +00:00
|
|
|
List<MovingObject> platformLevel = gameRef.platformHolder.objects[row];
|
2021-09-08 19:27:06 +00:00
|
|
|
int index = platformLevel.indexOf(this);
|
|
|
|
Vector2 right = Vector2(-200, -200);
|
|
|
|
if (index + 1 < platformLevel.length) {
|
|
|
|
right = platformLevel.elementAt(index + 1).sprite.position;
|
|
|
|
}
|
|
|
|
super.update(dt);
|
2021-09-14 17:23:02 +00:00
|
|
|
if (index == -1 || (index < 1 && sprite.x <= sprite.width)) {
|
2021-09-08 19:27:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
Vector2 left = Vector2(-200, -200);
|
|
|
|
if (index - 1 >= 0) {
|
|
|
|
left = platformLevel.elementAt(index - 1).sprite.position;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasLeft = (left.x - sprite.position.x).abs() < 1.9 * sprite.size.x;
|
|
|
|
bool hasRight = (sprite.position.x - right.x).abs() < 1.9 * sprite.size.x;
|
|
|
|
|
2021-09-22 19:26:06 +00:00
|
|
|
// If the platform cannot be seen by the player.
|
|
|
|
if (!((sprite.x >= 0 && sprite.x <= gameRef.size.x) ||
|
|
|
|
(sprite.x + sprite.width >= 0 &&
|
|
|
|
sprite.x + sprite.width <= gameRef.size.x))) {
|
|
|
|
if (hasLeft && hasRight) {
|
|
|
|
sprite.current = PlatformState.mid;
|
|
|
|
} else if (hasLeft && !hasRight) {
|
|
|
|
sprite.current = PlatformState.right;
|
|
|
|
} else if (!hasLeft && hasRight) {
|
|
|
|
sprite.current = PlatformState.left;
|
|
|
|
} else {
|
|
|
|
sprite.current = PlatformState.single;
|
|
|
|
}
|
2021-09-08 19:27:06 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-03 23:29:18 +00:00
|
|
|
}
|