feat: enclosed ChromeDino

pull/76/head
alestiago 4 years ago
parent 4ceb0cd2c2
commit 685c74ef83

@ -16,7 +16,8 @@ class ChromeDino extends BodyComponent with InitialPosition {
paint = Paint()..color = Colors.blue; paint = Paint()..color = Colors.blue;
} }
static final size = Vector2(3, 1); /// The size of the dinosour mouth.
static final size = Vector2(5, 2.5);
/// Anchors the [ChromeDino] to the [RevoluteJoint] that controls its arc /// Anchors the [ChromeDino] to the [RevoluteJoint] that controls its arc
/// motion. /// motion.
@ -41,23 +42,69 @@ class ChromeDino extends BodyComponent with InitialPosition {
removed.complete(true); removed.complete(true);
} }
@override
void update(double dt) {
super.update(dt);
// print(body.angle);
}
@override @override
Future<void> onLoad() async { Future<void> onLoad() async {
await super.onLoad(); await super.onLoad();
await _anchorToJoint(); await _anchorToJoint();
} }
List<FixtureDef> _createFixtureDefs() {
final fixtureDefs = <FixtureDef>[];
final box = PolygonShape()..setAsBoxXY(size.x / 2, size.y / 2);
final fixtureDef = FixtureDef(box)
..shape = box
..density = 1
..friction = 0.3
..restitution = 0.1
..isSensor = true;
fixtureDefs.add(fixtureDef);
final upperEdge = EdgeShape()
..set(
Vector2(-size.x / 2, -size.y / 2),
Vector2(size.x / 2, -size.y / 2),
);
final upperEdgeDef = FixtureDef(upperEdge)..density = 0.5;
fixtureDefs.add(upperEdgeDef);
final lowerEdge = EdgeShape()
..set(
Vector2(-size.x / 2, size.y / 2),
Vector2(size.x / 2, size.y / 2),
);
final lowerEdgeDef = FixtureDef(lowerEdge)..density = 0.5;
fixtureDefs.add(lowerEdgeDef);
final rightEdge = EdgeShape()
..set(
Vector2(size.x / 2, -size.y / 2),
Vector2(size.x / 2, size.y / 2),
);
final rightEdgeDef = FixtureDef(rightEdge)..density = 0.5;
fixtureDefs.add(rightEdgeDef);
return fixtureDefs;
}
@override @override
Body createBody() { Body createBody() {
final shape = PolygonShape()..setAsBoxXY(size.x, size.y);
final fixtureDef = FixtureDef(shape)..density = 1;
final bodyDef = BodyDef() final bodyDef = BodyDef()
..gravityScale = 0 ..gravityScale = 0
..position = initialPosition ..position = initialPosition
..type = BodyType.dynamic; ..type = BodyType.dynamic;
return world.createBody(bodyDef)..createFixture(fixtureDef); final body = world.createBody(bodyDef);
_createFixtureDefs().forEach(body.createFixture);
return body;
} }
} }

Loading…
Cancel
Save