flame_forge2d 0.14.1+1 flame_forge2d: ^0.14.1+1 copied to clipboard
Forge2D (Box2D) support for the Flame game engine. This uses the forge2d package and provides wrappers and components to be used inside Flame.
import 'package:flame/camera.dart' as camera;
import 'package:flame/components.dart';
import 'package:flame/events.dart';
import 'package:flame/game.dart';
import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flutter/widgets.dart';
void main() {
runApp(GameWidget(game: Forge2DExample()));
}
class Forge2DExample extends Forge2DGame {
final cameraWorld = camera.World();
late final CameraComponent cameraComponent;
@override
Future<void> onLoad() async {
cameraComponent = CameraComponent(world: cameraWorld);
cameraComponent.viewfinder.anchor = Anchor.topLeft;
addAll([cameraComponent, cameraWorld]);
cameraWorld.add(Ball(size / 2));
cameraWorld.addAll(createBoundaries());
}
List<Component> createBoundaries() {
final topLeft = Vector2.zero();
final bottomRight = screenToWorld(cameraComponent.viewport.size);
final topRight = Vector2(bottomRight.x, topLeft.y);
final bottomLeft = Vector2(topLeft.x, bottomRight.y);
return [
Wall(topLeft, topRight),
Wall(topRight, bottomRight),
Wall(bottomLeft, bottomRight),
Wall(topLeft, bottomLeft)
];
}
}
class Ball extends BodyComponent with TapCallbacks {
final Vector2 _position;
Ball(this._position);
@override
Body createBody() {
final shape = CircleShape();
shape.radius = 5;
final fixtureDef = FixtureDef(
shape,
restitution: 0.8,
density: 1.0,
friction: 0.4,
);
final bodyDef = BodyDef(
userData: this,
angularDamping: 0.8,
position: _position,
type: BodyType.dynamic,
);
return world.createBody(bodyDef)..createFixture(fixtureDef);
}
@override
void onTapDown(_) {
body.applyLinearImpulse(Vector2.random() * 5000);
}
}
class Wall extends BodyComponent {
final Vector2 _start;
final Vector2 _end;
Wall(this._start, this._end);
@override
Body createBody() {
final shape = EdgeShape()..set(_start, _end);
final fixtureDef = FixtureDef(shape, friction: 0.3);
final bodyDef = BodyDef(
userData: this,
position: Vector2.zero(),
);
return world.createBody(bodyDef)..createFixture(fixtureDef);
}
}