Modify examples to override createBody

This commit is contained in:
Lukas Klingsbo
2020-06-21 16:39:36 +02:00
parent f17ac614a5
commit b8c90a9ad3
3 changed files with 18 additions and 18 deletions

View File

@ -27,9 +27,7 @@ class Wall extends BodyComponent {
final Vector2 start; final Vector2 start;
final Vector2 end; final Vector2 end;
Wall(this.start, this.end, Box2DGame game) : super(game) { Wall(this.start, this.end, Box2DGame game) : super(game);
_createBody(start, end);
}
@override @override
void renderPolygon(Canvas canvas, List<Offset> coordinates) { void renderPolygon(Canvas canvas, List<Offset> coordinates) {
@ -38,7 +36,8 @@ class Wall extends BodyComponent {
canvas.drawLine(start, end, paint); canvas.drawLine(start, end, paint);
} }
void _createBody(Vector2 start, Vector2 end) { @override
Body createBody() {
final PolygonShape shape = PolygonShape(); final PolygonShape shape = PolygonShape();
shape.setAsEdge(start, end); shape.setAsEdge(start, end);
@ -52,6 +51,6 @@ class Wall extends BodyComponent {
..position = Vector2.zero() ..position = Vector2.zero()
..type = BodyType.STATIC; ..type = BodyType.STATIC;
body = world.createBody(bodyDef)..createFixtureFromFixtureDef(fixtureDef); return world.createBody(bodyDef)..createFixtureFromFixtureDef(fixtureDef);
} }
} }

View File

@ -19,12 +19,13 @@ void main() async {
class Ball extends BodyComponent { class Ball extends BodyComponent {
Paint originalPaint, currentPaint; Paint originalPaint, currentPaint;
bool giveNudge = false; bool giveNudge = false;
final double _radius = 5.0;
Vector2 _position;
Ball(Vector2 position, Box2DGame box2d) : super(box2d) { Ball(Vector2 position, Box2DGame box2d) : super(box2d) {
originalPaint = _randomPaint(); originalPaint = _randomPaint();
currentPaint = originalPaint; currentPaint = originalPaint;
final worldPosition = viewport.getScreenToWorld(position); _position = viewport.getScreenToWorld(position);
_createBody(5.0, worldPosition);
} }
Paint _randomPaint() { Paint _randomPaint() {
@ -39,9 +40,10 @@ class Ball extends BodyComponent {
).paint; ).paint;
} }
void _createBody(double radius, Vector2 position) { @override
Body createBody() {
final CircleShape shape = CircleShape(); final CircleShape shape = CircleShape();
shape.radius = radius; shape.radius = _radius;
final fixtureDef = FixtureDef() final fixtureDef = FixtureDef()
..shape = shape ..shape = shape
@ -52,10 +54,10 @@ class Ball extends BodyComponent {
final bodyDef = BodyDef() final bodyDef = BodyDef()
// To be able to determine object in collision // To be able to determine object in collision
..setUserData(this) ..setUserData(this)
..position = position ..position = _position
..type = BodyType.DYNAMIC; ..type = BodyType.DYNAMIC;
body = world.createBody(bodyDef)..createFixtureFromFixtureDef(fixtureDef); return world.createBody(bodyDef)..createFixtureFromFixtureDef(fixtureDef);
} }
@override @override

View File

@ -23,13 +23,12 @@ class MyPlanet extends BodyComponent {
// components that are added to the same Box2DGame/Box2DComponent. // components that are added to the same Box2DGame/Box2DComponent.
// After 20 seconds the circle will be removed, to show that we don't get // After 20 seconds the circle will be removed, to show that we don't get
// any concurrent modification exceptions. // any concurrent modification exceptions.
MyPlanet(Box2DGame game) : super(game) { MyPlanet(Box2DGame game) : super(game);
_createBody(50.0, Vector2.zero());
}
void _createBody(double radius, Vector2 position) { @override
Body createBody() {
final CircleShape shape = CircleShape(); final CircleShape shape = CircleShape();
shape.radius = radius; shape.radius = 50.0;
final fixtureDef = FixtureDef(); final fixtureDef = FixtureDef();
// To be able to determine object in collision // To be able to determine object in collision
@ -40,11 +39,11 @@ class MyPlanet extends BodyComponent {
fixtureDef.friction = 0.1; fixtureDef.friction = 0.1;
final bodyDef = BodyDef(); final bodyDef = BodyDef();
bodyDef.position = position; bodyDef.position = Vector2.zero();
bodyDef.angularVelocity = 4.0; bodyDef.angularVelocity = 4.0;
bodyDef.type = BodyType.DYNAMIC; bodyDef.type = BodyType.DYNAMIC;
body = world.createBody(bodyDef)..createFixtureFromFixtureDef(fixtureDef); return world.createBody(bodyDef)..createFixtureFromFixtureDef(fixtureDef);
} }
@override @override