mirror of https://github.com/flutter/pinball.git
feat: add dino wall (#115)
* feat: added dino walls * refactor: moved board * fix: undo board movement * feat: fixing shapes for wall with asset * fix: placed dino wall asset and shapes * fix: fixed top wall shape * feat: added dino bottom wall * fix: show background * fix: fixed geometry test * test: added golden tests for dino wall * test: removed unused import * refactor: moved dino walls to pinball_components * chore: analysis errors * refactor: more analysis errors * chore: analysis error * test: added tests for dino walls * chore: unused import * test: refactored golden test * refactor: refactored vertices shapes * test: make dino wall private and removed golden tests for them Co-authored-by: Alejandro Santiago <dev@alestiago.com>pull/133/head
parent
8b86656412
commit
48f831264e
After Width: | Height: | Size: 11 KiB |
After Width: | Height: | Size: 16 KiB |
@ -0,0 +1,225 @@
|
||||
// ignore_for_file: comment_references, avoid_renaming_method_parameters
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||
import 'package:pinball_components/gen/assets.gen.dart';
|
||||
import 'package:pinball_components/pinball_components.dart' hide Assets;
|
||||
|
||||
/// {@template dinowalls}
|
||||
/// A [Blueprint] which creates walls for the [ChromeDino].
|
||||
/// {@endtemplate}
|
||||
class DinoWalls extends Forge2DBlueprint {
|
||||
/// {@macro dinowalls}
|
||||
DinoWalls({required this.position});
|
||||
|
||||
/// The [position] where the elements will be created
|
||||
final Vector2 position;
|
||||
|
||||
@override
|
||||
void build(_) {
|
||||
addAll([
|
||||
_DinoTopWall()..initialPosition = position,
|
||||
_DinoBottomWall()..initialPosition = position,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/// {@template dino_top_wall}
|
||||
/// Wall segment located above [ChromeDino].
|
||||
/// {@endtemplate}
|
||||
class _DinoTopWall extends BodyComponent with InitialPosition {
|
||||
///{@macro dino_top_wall}
|
||||
_DinoTopWall() : super(priority: 2);
|
||||
|
||||
List<FixtureDef> _createFixtureDefs() {
|
||||
final fixturesDef = <FixtureDef>[];
|
||||
|
||||
final topStraightShape = EdgeShape()
|
||||
..set(
|
||||
Vector2(29.5, 35.1),
|
||||
Vector2(28.4, 35.1),
|
||||
);
|
||||
final topStraightFixtureDef = FixtureDef(topStraightShape);
|
||||
fixturesDef.add(topStraightFixtureDef);
|
||||
|
||||
final topCurveShape = BezierCurveShape(
|
||||
controlPoints: [
|
||||
topStraightShape.vertex1,
|
||||
Vector2(17.4, 26.38),
|
||||
Vector2(25.5, 20.7),
|
||||
],
|
||||
);
|
||||
fixturesDef.add(FixtureDef(topCurveShape));
|
||||
|
||||
final middleCurveShape = BezierCurveShape(
|
||||
controlPoints: [
|
||||
topCurveShape.vertices.last,
|
||||
Vector2(27.8, 20.1),
|
||||
Vector2(26.8, 19.5),
|
||||
],
|
||||
);
|
||||
fixturesDef.add(FixtureDef(middleCurveShape));
|
||||
|
||||
final bottomCurveShape = BezierCurveShape(
|
||||
controlPoints: [
|
||||
middleCurveShape.vertices.last,
|
||||
Vector2(21.15, 16),
|
||||
Vector2(25.6, 15.2),
|
||||
],
|
||||
);
|
||||
fixturesDef.add(FixtureDef(bottomCurveShape));
|
||||
|
||||
final bottomStraightShape = EdgeShape()
|
||||
..set(
|
||||
bottomCurveShape.vertices.last,
|
||||
Vector2(31, 14.5),
|
||||
);
|
||||
final bottomStraightFixtureDef = FixtureDef(bottomStraightShape);
|
||||
fixturesDef.add(bottomStraightFixtureDef);
|
||||
|
||||
return fixturesDef;
|
||||
}
|
||||
|
||||
@override
|
||||
Body createBody() {
|
||||
renderBody = false;
|
||||
|
||||
final bodyDef = BodyDef()
|
||||
..userData = this
|
||||
..position = initialPosition
|
||||
..type = BodyType.static;
|
||||
|
||||
final body = world.createBody(bodyDef);
|
||||
_createFixtureDefs().forEach(
|
||||
(fixture) => body.createFixture(
|
||||
fixture
|
||||
..restitution = 0.1
|
||||
..friction = 0,
|
||||
),
|
||||
);
|
||||
|
||||
return body;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> onLoad() async {
|
||||
await super.onLoad();
|
||||
await _loadSprite();
|
||||
}
|
||||
|
||||
Future<void> _loadSprite() async {
|
||||
final sprite = await gameRef.loadSprite(
|
||||
Assets.images.dino.dinoLandTop.keyName,
|
||||
);
|
||||
final spriteComponent = SpriteComponent(
|
||||
sprite: sprite,
|
||||
size: Vector2(10.6, 27.7),
|
||||
anchor: Anchor.center,
|
||||
position: Vector2(27, -28.2),
|
||||
);
|
||||
|
||||
await add(spriteComponent);
|
||||
}
|
||||
}
|
||||
|
||||
/// {@template dino_bottom_wall}
|
||||
/// Wall segment located below [ChromeDino].
|
||||
/// {@endtemplate}
|
||||
class _DinoBottomWall extends BodyComponent with InitialPosition {
|
||||
///{@macro dino_top_wall}
|
||||
_DinoBottomWall() : super(priority: 2);
|
||||
|
||||
List<FixtureDef> _createFixtureDefs() {
|
||||
final fixturesDef = <FixtureDef>[];
|
||||
|
||||
final topStraightControlPoints = [
|
||||
Vector2(32.4, 8.3),
|
||||
Vector2(25, 7.7),
|
||||
];
|
||||
final topStraightShape = EdgeShape()
|
||||
..set(
|
||||
topStraightControlPoints.first,
|
||||
topStraightControlPoints.last,
|
||||
);
|
||||
final topStraightFixtureDef = FixtureDef(topStraightShape);
|
||||
fixturesDef.add(topStraightFixtureDef);
|
||||
|
||||
final topLeftCurveControlPoints = [
|
||||
topStraightControlPoints.last,
|
||||
Vector2(21.8, 7),
|
||||
Vector2(29.5, -13.8),
|
||||
];
|
||||
final topLeftCurveShape = BezierCurveShape(
|
||||
controlPoints: topLeftCurveControlPoints,
|
||||
);
|
||||
fixturesDef.add(FixtureDef(topLeftCurveShape));
|
||||
|
||||
final bottomLeftStraightControlPoints = [
|
||||
topLeftCurveControlPoints.last,
|
||||
Vector2(31.8, -44.1),
|
||||
];
|
||||
final bottomLeftStraightShape = EdgeShape()
|
||||
..set(
|
||||
bottomLeftStraightControlPoints.first,
|
||||
bottomLeftStraightControlPoints.last,
|
||||
);
|
||||
final bottomLeftStraightFixtureDef = FixtureDef(bottomLeftStraightShape);
|
||||
fixturesDef.add(bottomLeftStraightFixtureDef);
|
||||
|
||||
final bottomStraightControlPoints = [
|
||||
bottomLeftStraightControlPoints.last,
|
||||
Vector2(37.8, -44.1),
|
||||
];
|
||||
final bottomStraightShape = EdgeShape()
|
||||
..set(
|
||||
bottomStraightControlPoints.first,
|
||||
bottomStraightControlPoints.last,
|
||||
);
|
||||
final bottomStraightFixtureDef = FixtureDef(bottomStraightShape);
|
||||
fixturesDef.add(bottomStraightFixtureDef);
|
||||
|
||||
return fixturesDef;
|
||||
}
|
||||
|
||||
@override
|
||||
Body createBody() {
|
||||
renderBody = false;
|
||||
|
||||
final bodyDef = BodyDef()
|
||||
..userData = this
|
||||
..position = initialPosition
|
||||
..type = BodyType.static;
|
||||
|
||||
final body = world.createBody(bodyDef);
|
||||
_createFixtureDefs().forEach(
|
||||
(fixture) => body.createFixture(
|
||||
fixture
|
||||
..restitution = 0.1
|
||||
..friction = 0,
|
||||
),
|
||||
);
|
||||
|
||||
return body;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> onLoad() async {
|
||||
await super.onLoad();
|
||||
await _loadSprite();
|
||||
}
|
||||
|
||||
Future<void> _loadSprite() async {
|
||||
final sprite = await gameRef.loadSprite(
|
||||
Assets.images.dino.dinoLandBottom.keyName,
|
||||
);
|
||||
final spriteComponent = SpriteComponent(
|
||||
sprite: sprite,
|
||||
size: Vector2(15.6, 54.8),
|
||||
anchor: Anchor.center,
|
||||
)..position = Vector2(31.7, 18);
|
||||
|
||||
await add(spriteComponent);
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
// ignore_for_file: cascade_invocations
|
||||
|
||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||
import 'package:flame_test/flame_test.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
|
||||
import '../../helpers/helpers.dart';
|
||||
|
||||
void main() {
|
||||
group('DinoWalls', () {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
final flameTester = FlameTester(TestGame.new);
|
||||
|
||||
flameTester.test(
|
||||
'loads correctly',
|
||||
(game) async {
|
||||
final dinoWalls = DinoWalls(position: Vector2.zero());
|
||||
await game.addFromBlueprint(dinoWalls);
|
||||
await game.ready();
|
||||
|
||||
for (final wall in dinoWalls.components) {
|
||||
expect(game.contains(wall), isTrue);
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
After Width: | Height: | Size: 38 KiB |
After Width: | Height: | Size: 44 KiB |
Loading…
Reference in new issue