Files
flame/packages/flame_forge2d/lib/position_body_component.dart
Lukas Klingsbo cf19ec5e4e Migrate flame_forge2d to monorepo (#852)
* Migrate flame_forge2d to monorepo

* Put in the temporary publish to none

* Update docs

* Add dart_code_metrics

* Add analysis options

* Fix analyze issues

* Fix formatting

* Add dart_code_metrics to flame_forge2d example
2021-06-21 22:02:32 +02:00

54 lines
1.4 KiB
Dart

import 'package:flame/components.dart';
import 'package:flutter/cupertino.dart';
import 'body_component.dart';
/// A [PositionBodyComponent] handles a [PositionComponent] on top of a
/// [BodyComponent]. You have to keep track of the size of the
/// [PositionComponent] and it can only have its anchor in the center.
abstract class PositionBodyComponent extends BodyComponent {
PositionComponent positionComponent;
Vector2 size;
@override
bool debugMode = false;
/// Make sure that the [size] of the position component matches the bounding
/// shape of the body that is create in createBody()
PositionBodyComponent(
this.positionComponent,
this.size,
);
@mustCallSuper
@override
Future<void> onLoad() async {
await super.onLoad();
updatePositionComponent();
positionComponent..anchor = Anchor.center;
gameRef.add(positionComponent);
}
@override
void update(double dt) {
super.update(dt);
updatePositionComponent();
}
@override
void onRemove() {
super.onRemove();
// Since the PositionComponent was added to the game in this class it should
// also be removed by this class when the BodyComponent is removed.
positionComponent.remove();
}
void updatePositionComponent() {
positionComponent.position..setFrom(body.position);
positionComponent.position.y *= -1;
positionComponent
..angle = -angle
..size = size;
}
}