feat: fixing shapes for wall with asset

pull/115/head
RuiAlonso 4 years ago
parent d0a8d41231
commit b3b3b1dbbd

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

@ -1,11 +1,13 @@
// ignore_for_file: avoid_renaming_method_parameters // ignore_for_file: avoid_renaming_method_parameters
import 'dart:math' as math; import 'dart:math' as math;
import 'package:flame/components.dart';
import 'package:flame/extensions.dart'; import 'package:flame/extensions.dart';
import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:pinball/game/components/components.dart'; import 'package:pinball/game/game.dart';
import 'package:pinball/game/pinball_game.dart'; import 'package:pinball/gen/assets.gen.dart';
import 'package:pinball_components/pinball_components.dart'; import 'package:pinball_components/pinball_components.dart' hide Assets;
/// {@template wall} /// {@template wall}
/// A continuous generic and [BodyType.static] barrier that divides a game area. /// A continuous generic and [BodyType.static] barrier that divides a game area.
@ -101,34 +103,27 @@ class DinoTopWall extends BodyComponent with InitialPosition {
final wallPerspectiveAngle = final wallPerspectiveAngle =
PinballGame.boardPerspectiveAngle + math.pi / 2; PinballGame.boardPerspectiveAngle + math.pi / 2;
// TODO(ruimiguel): calculate final UI wall shape. final bottomCurveControlPoints = [
final topCurveShape = BezierCurveShape( Vector2(-4, 5),
controlPoints: [ Vector2(-5, 6.8),
Vector2(0, 0), Vector2(-9, 11.8),
Vector2(0, 8), Vector2(-9.5, 0),
Vector2(8, 8), ];
Vector2(15, 0), final bottomCurveShape = BezierCurveShape(
], controlPoints: bottomCurveControlPoints,
)..rotate(wallPerspectiveAngle); )..rotate(wallPerspectiveAngle);
final topFixtureDef = FixtureDef(topCurveShape) fixturesDef.add(FixtureDef(bottomCurveShape));
..restitution = 0.1
..friction = 0;
fixturesDef.add(topFixtureDef);
const bottomCurveShift = -5.0; final topCurveControlPoints = [
// TODO(ruimiguel): calculate final UI wall shape. Vector2(-4, 5),
final bottomCurveShape = BezierCurveShape( Vector2(2, 10),
controlPoints: [ Vector2(7, 10),
Vector2(0 + bottomCurveShift, 0), Vector2(17, 0),
Vector2(0 + bottomCurveShift, 8), ];
Vector2(3 + bottomCurveShift, 8), final topCurveShape = BezierCurveShape(
Vector2(7 + bottomCurveShift, 0), controlPoints: topCurveControlPoints,
],
)..rotate(wallPerspectiveAngle); )..rotate(wallPerspectiveAngle);
final bottomFixtureDef = FixtureDef(bottomCurveShape) fixturesDef.add(FixtureDef(topCurveShape));
..restitution = 0.1
..friction = 0;
fixturesDef.add(bottomFixtureDef);
return fixturesDef; return fixturesDef;
} }
@ -141,75 +136,35 @@ class DinoTopWall extends BodyComponent with InitialPosition {
..type = BodyType.static; ..type = BodyType.static;
final body = world.createBody(bodyDef); final body = world.createBody(bodyDef);
_createFixtureDefs().forEach(body.createFixture); _createFixtureDefs().forEach(
(fixture) => body.createFixture(
return body; fixture
}
}
/// {@template dino_bottom_wall}
/// Wall located below dino, at the right of the board.
/// {@endtemplate}
class DinoBottomWall extends BodyComponent with InitialPosition {
/// {@macro dino_bottom_wall}
DinoBottomWall() : super(priority: 2) {
// TODO(ruimiguel): remove color once sprites are added.
paint = Paint()
..color = const Color.fromARGB(255, 3, 188, 249)
..style = PaintingStyle.stroke;
}
List<FixtureDef> _createFixtureDefs() {
final fixturesDef = <FixtureDef>[];
final wallPerspectiveAngle =
PinballGame.boardPerspectiveAngle + math.pi / 2;
// TODO(ruimiguel): calculate final UI wall shape.
final topVertices = [
Vector2(0, 0),
Vector2(0, 5),
Vector2(-2, 8),
Vector2(-10, 6),
Vector2(-20, 4),
Vector2(-20, 0),
]..forEach((point) {
point.rotate(wallPerspectiveAngle);
});
final topWallShape = PolygonShape()..set(topVertices);
final topWallFixtureDef = FixtureDef(topWallShape)
..restitution = 0.1 ..restitution = 0.1
..friction = 0; ..friction = 0,
fixturesDef.add(topWallFixtureDef); ),
);
const bottomShift = -20.0;
final bottomVertices = [
Vector2(0 + bottomShift, 0),
Vector2(0 + bottomShift, 4),
Vector2(-40 + bottomShift, 4),
Vector2(-40 + bottomShift, 0),
]..forEach((point) {
point.rotate(wallPerspectiveAngle);
});
final bottomWallShape = PolygonShape()..set(bottomVertices);
final bottomWallFixtureDef = FixtureDef(bottomWallShape)
..restitution = 0.1
..friction = 0;
fixturesDef.add(bottomWallFixtureDef);
return fixturesDef; return body;
} }
@override @override
Body createBody() { Future<void> onLoad() async {
final bodyDef = BodyDef() await super.onLoad();
..userData = this await _loadBackground();
..position = initialPosition }
..type = BodyType.static;
final body = world.createBody(bodyDef);
_createFixtureDefs().forEach(body.createFixture);
return body; Future<void> _loadBackground() async {
final sprite = await gameRef.loadSprite(
Assets.images.components.dinoLandTop.path,
);
final spriteComponent = SpriteComponent(
sprite: sprite,
size: Vector2(10.6, 27.7),
anchor: Anchor.center,
)
..position = Vector2(-3.2, -4)
..priority = -1;
await add(spriteComponent);
} }
} }

@ -70,19 +70,7 @@ class PinballGame extends Forge2DGame
await add(BottomWall()); await add(BottomWall());
createBoundaries(this).forEach(add); createBoundaries(this).forEach(add);
await add( await add(
DinoTopWall() DinoTopWall()..initialPosition = Vector2(31.5, 24.5), //Vector2(27, 24.5),
..initialPosition = Vector2(
boardBounds.center.dx + 30,
boardBounds.center.dy + 20,
),
);
await add(
DinoBottomWall()
..initialPosition = Vector2(
boardBounds.center.dx + 31.5,
boardBounds.center.dy + 8,
),
); );
} }
@ -124,7 +112,7 @@ class DebugPinballGame extends PinballGame with TapDetector {
@override @override
Future<void> onLoad() async { Future<void> onLoad() async {
await super.onLoad(); await super.onLoad();
await _loadBackground(); //await _loadBackground();
} }
// TODO(alestiago): Move to PinballGame once we have the real background // TODO(alestiago): Move to PinballGame once we have the real background

@ -3,6 +3,8 @@
/// FlutterGen /// FlutterGen
/// ***************************************************** /// *****************************************************
// ignore_for_file: directives_ordering,unnecessary_import
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
class $AssetsImagesGen { class $AssetsImagesGen {
@ -15,8 +17,13 @@ class $AssetsImagesGen {
class $AssetsImagesComponentsGen { class $AssetsImagesComponentsGen {
const $AssetsImagesComponentsGen(); const $AssetsImagesComponentsGen();
/// File path: assets/images/components/background.png
AssetGenImage get background => AssetGenImage get background =>
const AssetGenImage('assets/images/components/background.png'); const AssetGenImage('assets/images/components/background.png');
/// File path: assets/images/components/dino-land-top.png
AssetGenImage get dinoLandTop =>
const AssetGenImage('assets/images/components/dino-land-top.png');
} }
class Assets { class Assets {

Loading…
Cancel
Save