Skip to content

Commit

Permalink
docs: Move flame_forge2d examples to main examples (#1588)
Browse files Browse the repository at this point in the history
  • Loading branch information
spydon committed May 1, 2022
1 parent 7877579 commit 6dd0a97
Show file tree
Hide file tree
Showing 26 changed files with 498 additions and 406 deletions.
File renamed without changes
4 changes: 4 additions & 0 deletions examples/lib/main.dart
Expand Up @@ -2,6 +2,7 @@ import 'package:dashbook/dashbook.dart';
import 'package:flutter/material.dart';

import 'stories/animations/animations.dart';
import 'stories/bridge_libraries/forge2d/flame_forge2d.dart';
import 'stories/camera_and_viewport/camera_and_viewport.dart';
import 'stories/collision_detection/collision_detection.dart';
import 'stories/components/components.dart';
Expand Down Expand Up @@ -37,5 +38,8 @@ void main() async {
addUtilsStories(dashbook);
addWidgetsStories(dashbook);

// Bridge package examples
addForge2DStories(dashbook);

runApp(dashbook);
}
Expand Up @@ -4,51 +4,24 @@ import 'package:flame/components.dart';
import 'package:flame/input.dart';
import 'package:flame_forge2d/flame_forge2d.dart';

import 'boundaries.dart';
import 'utils/boundaries.dart';

class ChopperBody extends BodyComponent {
final Vector2 position;
final Vector2 size;

ChopperBody(
this.position,
PositionComponent component,
) : size = component.size {
renderBody = false;
add(component);
}

@override
Body createBody() {
final shape = CircleShape()..radius = size.x / 4;
final fixtureDef = FixtureDef(
shape,
userData: this, // To be able to determine object in collision
restitution: 0.8,
density: 1.0,
friction: 0.2,
);
class AnimatedBodyExample extends Forge2DGame with TapDetector {
static const String description = '''
In this example we show how to add an animated chopper, which is created
with a SpriteAnimationComponent, on top of a BodyComponent.
Tap the screen to add more choppers.
''';

final velocity = (Vector2.random() - Vector2.random()) * 200;
final bodyDef = BodyDef(
position: position,
angle: velocity.angleTo(Vector2(1, 0)),
linearVelocity: velocity,
type: BodyType.dynamic,
);
return world.createBody(bodyDef)..createFixture(fixtureDef);
}
}
AnimatedBodyExample() : super(gravity: Vector2.zero());

class PositionBodySample extends Forge2DGame with TapDetector {
late Image chopper;
late SpriteAnimation animation;

PositionBodySample() : super(gravity: Vector2.zero());

@override
Future<void> onLoad() async {
chopper = await images.load('chopper.png');
chopper = await images.load('animations/chopper.png');

animation = SpriteAnimation.fromFrameData(
chopper,
Expand Down Expand Up @@ -76,3 +49,37 @@ class PositionBodySample extends Forge2DGame with TapDetector {
add(ChopperBody(position, animationComponent));
}
}

class ChopperBody extends BodyComponent {
final Vector2 position;
final Vector2 size;

ChopperBody(
this.position,
PositionComponent component,
) : size = component.size {
renderBody = false;
add(component);
}

@override
Body createBody() {
final shape = CircleShape()..radius = size.x / 4;
final fixtureDef = FixtureDef(
shape,
userData: this, // To be able to determine object in collision
restitution: 0.8,
density: 1.0,
friction: 0.2,
);

final velocity = (Vector2.random() - Vector2.random()) * 200;
final bodyDef = BodyDef(
position: position,
angle: velocity.angleTo(Vector2(1, 0)),
linearVelocity: velocity,
type: BodyType.dynamic,
);
return world.createBody(bodyDef)..createFixture(fixtureDef);
}
}
Expand Up @@ -3,7 +3,40 @@ import 'dart:math' as math;
import 'package:flame/input.dart';
import 'package:flame_forge2d/flame_forge2d.dart';

import 'boundaries.dart';
import 'utils/boundaries.dart';

class BlobExample extends Forge2DGame with TapDetector {
static const String description = '''
In this example we show the power of joints by showing interactions between
bodies tied together.
Tap the screen to add boxes that will bounce on the "blob" in the center.
''';

@override
Future<void> onLoad() async {
final worldCenter = screenToWorld(size * camera.zoom / 2);
final blobCenter = worldCenter + Vector2(0, -30);
final blobRadius = Vector2.all(6.0);
addAll(createBoundaries(this));
add(Ground(worldCenter));
final jointDef = ConstantVolumeJointDef()
..frequencyHz = 20.0
..dampingRatio = 1.0
..collideConnected = false;

await addAll([
for (var i = 0; i < 20; i++) BlobPart(i, jointDef, blobRadius, blobCenter)
]);
world.createJoint(ConstantVolumeJoint(world, jointDef));
}

@override
void onTapDown(TapDownInfo details) {
super.onTapDown(details);
add(FallingBox(details.eventPosition.game));
}
}

class Ground extends BodyComponent {
final Vector2 worldCenter;
Expand All @@ -14,15 +47,16 @@ class Ground extends BodyComponent {
Body createBody() {
final shape = PolygonShape();
shape.setAsBoxXY(20.0, 0.4);
final fixtureDef = FixtureDef(shape, friction: 0.2);

final bodyDef = BodyDef(position: worldCenter.clone());
final ground = world.createBody(bodyDef);
ground.createFixtureFromShape(shape);
ground.createFixture(fixtureDef);

shape.setAsBox(0.4, 20.0, Vector2(-10.0, 0.0), 0.0);
ground.createFixtureFromShape(shape);
ground.createFixture(fixtureDef);
shape.setAsBox(0.4, 20.0, Vector2(10.0, 0.0), 0.0);
ground.createFixtureFromShape(shape);
ground.createFixture(fixtureDef);
return ground;
}
}
Expand Down Expand Up @@ -59,7 +93,7 @@ class BlobPart extends BodyComponent {
final fixtureDef = FixtureDef(
shape,
density: 1.0,
filter: Filter()..groupIndex = -2,
friction: 0.2,
);
body.createFixture(fixtureDef);
jointDef.addBody(body);
Expand All @@ -84,29 +118,3 @@ class FallingBox extends BodyComponent {
return body;
}
}

class BlobSample extends Forge2DGame with TapDetector {
@override
Future<void> onLoad() async {
final worldCenter = screenToWorld(size * camera.zoom / 2);
final blobCenter = worldCenter + Vector2(0, -30);
final blobRadius = Vector2.all(6.0);
addAll(createBoundaries(this));
add(Ground(worldCenter));
final jointDef = ConstantVolumeJointDef()
..frequencyHz = 20.0
..dampingRatio = 1.0
..collideConnected = false;

await addAll([
for (var i = 0; i < 20; i++) BlobPart(i, jointDef, blobRadius, blobCenter)
]);
world.createJoint(ConstantVolumeJoint(world, jointDef));
}

@override
void onTapDown(TapDownInfo details) {
super.onTapDown(details);
add(FallingBox(details.eventPosition.game));
}
}
21 changes: 21 additions & 0 deletions examples/lib/stories/bridge_libraries/forge2d/camera_example.dart
@@ -0,0 +1,21 @@
import 'package:flame/input.dart';
import 'package:flame_forge2d/flame_forge2d.dart';

import 'domino_example.dart';
import 'sprite_body_example.dart';

class CameraExample extends DominoExample {
static const String description = '''
This example showcases the possibility to follow BodyComponents with the
camera. When the screen is tapped a pizza is added, which the camera will
follow. Other than that it is the same as the domino example.
''';

@override
void onTapDown(TapDownInfo details) {
final position = details.eventPosition.game;
final pizza = Pizza(position);
add(pizza);
pizza.mounted.whenComplete(() => camera.followBodyComponent(pizza));
}
}
Expand Up @@ -5,18 +5,18 @@ import 'package:flame/input.dart';
import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flutter/material.dart';

import 'balls.dart';
import 'boundaries.dart';
import 'utils/balls.dart';
import 'utils/boundaries.dart';

const TextStyle _textStyle = TextStyle(color: Colors.white, fontSize: 2);

class CompositionSample extends Forge2DGame with HasTappables {
static const info = '''
This example shows how to compose a `BodyComponent` together with a normal Flame
component. Click the ball to see the number increment.
''';
class CompositionExample extends Forge2DGame with HasTappables {
static const description = '''
This example shows how to compose a `BodyComponent` together with a normal
Flame component. Click the ball to see the number increment.
''';

CompositionSample() : super(zoom: 20, gravity: Vector2(0, 10.0));
CompositionExample() : super(zoom: 20, gravity: Vector2(0, 10.0));

@override
Future<void> onLoad() async {
Expand Down
Expand Up @@ -3,17 +3,18 @@ import 'dart:math' as math;
import 'package:flame/input.dart';
import 'package:flame_forge2d/flame_forge2d.dart';

import 'balls.dart';
import 'boundaries.dart';
import 'utils/balls.dart';
import 'utils/boundaries.dart';

class ContactCallbacksSample extends Forge2DGame with TapDetector {
static const info = '''
This example shows how `BodyComponent`s can react to collisions with other
bodies.
Tap the screen to add balls, the white balls will give an impulse to the balls
that it collides with.
''';
ContactCallbacksSample() : super(gravity: Vector2(0, 10.0));
class ContactCallbacksExample extends Forge2DGame with TapDetector {
static const description = '''
This example shows how `BodyComponent`s can react to collisions with other
bodies.
Tap the screen to add balls, the white balls will give an impulse to the
balls that it collides with.
''';

ContactCallbacksExample() : super(gravity: Vector2(0, 10.0));

@override
Future<void> onLoad() async {
Expand Down
Expand Up @@ -3,8 +3,49 @@ import 'dart:ui';
import 'package:flame/input.dart';
import 'package:flame_forge2d/flame_forge2d.dart';

import 'boundaries.dart';
import 'sprite_body_sample.dart';
import 'sprite_body_example.dart';
import 'utils/boundaries.dart';

class DominoExample extends Forge2DGame with TapDetector {
static const description = '''
In this example we can see some domino tiles lined up.
If you tap on the screen a pizza is added which can tip the tiles over and
cause a chain reaction.
''';

DominoExample() : super(gravity: Vector2(0, 10.0));

late Image pizzaImage;

@override
Future<void> onLoad() async {
final boundaries = createBoundaries(this);
boundaries.forEach(add);
final center = screenToWorld(camera.viewport.effectiveSize / 2);

const numberOfRows = 7;
for (var i = 0; i < numberOfRows - 2; i++) {
final position = center + Vector2(0.0, 5.0 * i);
add(Platform(position));
}

const numberPerRow = 25;
for (var i = 0; i < numberOfRows; ++i) {
for (var j = 0; j < numberPerRow; j++) {
final position = center +
Vector2(-14.75 + j * (29.5 / (numberPerRow - 1)), -12.7 + 5 * i);
add(DominoBrick(position));
}
}
}

@override
void onTapDown(TapDownInfo details) {
super.onTapDown(details);
final position = details.eventPosition.game;
add(Pizza(position)..renderBody = true);
}
}

class Platform extends BodyComponent {
final Vector2 position;
Expand Down Expand Up @@ -41,38 +82,3 @@ class DominoBrick extends BodyComponent {
return world.createBody(bodyDef)..createFixture(fixtureDef);
}
}

class DominoSample extends Forge2DGame with TapDetector {
late Image pizzaImage;

DominoSample() : super(gravity: Vector2(0, 10.0));

@override
Future<void> onLoad() async {
final boundaries = createBoundaries(this);
boundaries.forEach(add);
final center = screenToWorld(camera.viewport.effectiveSize / 2);

const numberOfRows = 7;
for (var i = 0; i < numberOfRows - 2; i++) {
final position = center + Vector2(0.0, 5.0 * i);
add(Platform(position));
}

const numberPerRow = 25;
for (var i = 0; i < numberOfRows; ++i) {
for (var j = 0; j < numberPerRow; j++) {
final position = center +
Vector2(-14.75 + j * (29.5 / (numberPerRow - 1)), -12.7 + 5 * i);
add(DominoBrick(position));
}
}
}

@override
void onTapDown(TapDownInfo details) {
super.onTapDown(details);
final position = details.eventPosition.game;
add(Pizza(position)..renderBody = true);
}
}

0 comments on commit 6dd0a97

Please sign in to comment.