|
|
|
@ -16,23 +16,47 @@ class Plunger extends BodyComponent with InitialPosition, Layered {
|
|
|
|
|
// are fixed.
|
|
|
|
|
}) : super(priority: RenderPriority.plunger) {
|
|
|
|
|
layer = Layer.launcher;
|
|
|
|
|
renderBody = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Distance the plunger can lower.
|
|
|
|
|
final double compressionDistance;
|
|
|
|
|
|
|
|
|
|
late final _PlungerSpriteAnimationGroupComponent _spriteComponent;
|
|
|
|
|
|
|
|
|
|
List<FixtureDef> _createFixtureDefs() {
|
|
|
|
|
final fixturesDef = <FixtureDef>[];
|
|
|
|
|
|
|
|
|
|
final leftShapeVertices = [
|
|
|
|
|
Vector2(0, 0),
|
|
|
|
|
Vector2(-1.8, 0),
|
|
|
|
|
Vector2(-1.8, -2.2),
|
|
|
|
|
Vector2(0, -0.3),
|
|
|
|
|
]..map((vector) => vector.rotate(BoardDimensions.perspectiveAngle))
|
|
|
|
|
.toList();
|
|
|
|
|
final leftTriangleShape = PolygonShape()..set(leftShapeVertices);
|
|
|
|
|
|
|
|
|
|
final leftTriangleFixtureDef = FixtureDef(leftTriangleShape)..density = 80;
|
|
|
|
|
fixturesDef.add(leftTriangleFixtureDef);
|
|
|
|
|
|
|
|
|
|
final rightShapeVertices = [
|
|
|
|
|
Vector2(0, 0),
|
|
|
|
|
Vector2(1.8, 0),
|
|
|
|
|
Vector2(1.8, -2.2),
|
|
|
|
|
Vector2(0, -0.3),
|
|
|
|
|
]..map((vector) => vector.rotate(BoardDimensions.perspectiveAngle))
|
|
|
|
|
.toList();
|
|
|
|
|
final rightTriangleShape = PolygonShape()..set(rightShapeVertices);
|
|
|
|
|
|
|
|
|
|
final rightTriangleFixtureDef = FixtureDef(rightTriangleShape)
|
|
|
|
|
..density = 80;
|
|
|
|
|
fixturesDef.add(rightTriangleFixtureDef);
|
|
|
|
|
|
|
|
|
|
return fixturesDef;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Body createBody() {
|
|
|
|
|
final shape = PolygonShape()
|
|
|
|
|
..setAsBox(
|
|
|
|
|
1.35,
|
|
|
|
|
0.5,
|
|
|
|
|
Vector2.zero(),
|
|
|
|
|
BoardDimensions.perspectiveAngle,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
final fixtureDef = FixtureDef(shape)..density = 80;
|
|
|
|
|
|
|
|
|
|
final bodyDef = BodyDef(
|
|
|
|
|
position: initialPosition,
|
|
|
|
|
userData: this,
|
|
|
|
@ -40,12 +64,15 @@ class Plunger extends BodyComponent with InitialPosition, Layered {
|
|
|
|
|
gravityScale: Vector2.zero(),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return world.createBody(bodyDef)..createFixture(fixtureDef);
|
|
|
|
|
final body = world.createBody(bodyDef);
|
|
|
|
|
_createFixtureDefs().forEach(body.createFixture);
|
|
|
|
|
return body;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Set a constant downward velocity on the [Plunger].
|
|
|
|
|
void pull() {
|
|
|
|
|
body.linearVelocity = Vector2(0, 7);
|
|
|
|
|
_spriteComponent.pull();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Set an upward velocity on the [Plunger].
|
|
|
|
@ -55,6 +82,7 @@ class Plunger extends BodyComponent with InitialPosition, Layered {
|
|
|
|
|
void release() {
|
|
|
|
|
final velocity = (initialPosition.y - body.position.y) * 5;
|
|
|
|
|
body.linearVelocity = Vector2(0, velocity);
|
|
|
|
|
_spriteComponent.release();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Anchors the [Plunger] to the [PrismaticJoint] that controls its vertical
|
|
|
|
@ -77,24 +105,84 @@ class Plunger extends BodyComponent with InitialPosition, Layered {
|
|
|
|
|
Future<void> onLoad() async {
|
|
|
|
|
await super.onLoad();
|
|
|
|
|
await _anchorToJoint();
|
|
|
|
|
renderBody = false;
|
|
|
|
|
await add(_PlungerSpriteComponent());
|
|
|
|
|
|
|
|
|
|
_spriteComponent = _PlungerSpriteAnimationGroupComponent();
|
|
|
|
|
await add(_spriteComponent);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Animation states associated with a [Plunger].
|
|
|
|
|
enum _PlungerAnimationState {
|
|
|
|
|
/// Pull state.
|
|
|
|
|
pull,
|
|
|
|
|
|
|
|
|
|
/// Release state.
|
|
|
|
|
release,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Animations for pulling and releasing [Plunger].
|
|
|
|
|
class _PlungerSpriteAnimationGroupComponent
|
|
|
|
|
extends SpriteAnimationGroupComponent<_PlungerAnimationState>
|
|
|
|
|
with HasGameRef {
|
|
|
|
|
_PlungerSpriteAnimationGroupComponent()
|
|
|
|
|
: super(
|
|
|
|
|
anchor: Anchor.center,
|
|
|
|
|
position: Vector2(1.87, 14.9),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
void pull() {
|
|
|
|
|
if (current != _PlungerAnimationState.pull) {
|
|
|
|
|
animation?.reset();
|
|
|
|
|
}
|
|
|
|
|
current = _PlungerAnimationState.pull;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void release() {
|
|
|
|
|
if (current != _PlungerAnimationState.release) {
|
|
|
|
|
animation?.reset();
|
|
|
|
|
}
|
|
|
|
|
current = _PlungerAnimationState.release;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _PlungerSpriteComponent extends SpriteComponent with HasGameRef {
|
|
|
|
|
@override
|
|
|
|
|
Future<void> onLoad() async {
|
|
|
|
|
await super.onLoad();
|
|
|
|
|
final sprite = await gameRef.loadSprite(
|
|
|
|
|
|
|
|
|
|
final spriteSheet = await gameRef.images.load(
|
|
|
|
|
Assets.images.plunger.plunger.keyName,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
this.sprite = sprite;
|
|
|
|
|
size = sprite.originalSize / 10;
|
|
|
|
|
anchor = Anchor.center;
|
|
|
|
|
position = Vector2(1.5, 13.4);
|
|
|
|
|
angle = -0.008;
|
|
|
|
|
const amountPerRow = 20;
|
|
|
|
|
const amountPerColumn = 1;
|
|
|
|
|
|
|
|
|
|
final textureSize = Vector2(
|
|
|
|
|
spriteSheet.width / amountPerRow,
|
|
|
|
|
spriteSheet.height / amountPerColumn,
|
|
|
|
|
);
|
|
|
|
|
size = textureSize / 10;
|
|
|
|
|
|
|
|
|
|
// TODO(ruimiguel): we only need plunger pull animation, and release is just
|
|
|
|
|
// to reverse it, so we need to divide by 2 while we don't have only half of
|
|
|
|
|
// the animation (but amountPerRow and amountPerColumn needs to be correct
|
|
|
|
|
// in order of calculate textureSize correctly).
|
|
|
|
|
|
|
|
|
|
final pullAnimation = SpriteAnimation.fromFrameData(
|
|
|
|
|
spriteSheet,
|
|
|
|
|
SpriteAnimationData.sequenced(
|
|
|
|
|
amount: amountPerRow * amountPerColumn ~/ 2,
|
|
|
|
|
amountPerRow: amountPerRow ~/ 2,
|
|
|
|
|
stepTime: 1 / 24,
|
|
|
|
|
textureSize: textureSize,
|
|
|
|
|
texturePosition: Vector2.zero(),
|
|
|
|
|
loop: false,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
animations = {
|
|
|
|
|
_PlungerAnimationState.release: pullAnimation.reversed(),
|
|
|
|
|
_PlungerAnimationState.pull: pullAnimation,
|
|
|
|
|
};
|
|
|
|
|
current = _PlungerAnimationState.release;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|