From 685c74ef8306f7f157d2801ad012fa81fe0d7f05 Mon Sep 17 00:00:00 2001 From: alestiago Date: Mon, 21 Mar 2022 17:28:21 +0000 Subject: [PATCH] feat: enclosed ChromeDino --- lib/game/components/chrome_dino.dart | 57 +++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 5 deletions(-) diff --git a/lib/game/components/chrome_dino.dart b/lib/game/components/chrome_dino.dart index a297781c..1da3505e 100644 --- a/lib/game/components/chrome_dino.dart +++ b/lib/game/components/chrome_dino.dart @@ -16,7 +16,8 @@ class ChromeDino extends BodyComponent with InitialPosition { 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 /// motion. @@ -41,23 +42,69 @@ class ChromeDino extends BodyComponent with InitialPosition { removed.complete(true); } + @override + void update(double dt) { + super.update(dt); + // print(body.angle); + } + @override Future onLoad() async { await super.onLoad(); + await _anchorToJoint(); } + List _createFixtureDefs() { + final fixtureDefs = []; + + 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 Body createBody() { - final shape = PolygonShape()..setAsBoxXY(size.x, size.y); - final fixtureDef = FixtureDef(shape)..density = 1; - final bodyDef = BodyDef() ..gravityScale = 0 ..position = initialPosition ..type = BodyType.dynamic; - return world.createBody(bodyDef)..createFixture(fixtureDef); + final body = world.createBody(bodyDef); + _createFixtureDefs().forEach(body.createFixture); + + return body; } }