allow camera zoom
MODIFY
example/lib/main.dart
example/lib/main.dart
@@ -62,6 +62,7 @@ class Game extends StatelessWidget implements GameListener {
gameController: _controller..setListener(this),
lightingColorGame: Colors.black.withOpacity(0.5),
showFPS: true,
+ zoom: 1, // you can change this to change the game zoom or directly on the camera
);
}

MODIFY
lib/bonfire_widget.dart
lib/bonfire_widget.dart
@@ -24,6 +24,7 @@ class BonfireWidget extends StatefulWidget {
final Color constructionModeColor;
final Color collisionAreaColor;
final Color lightingColorGame;
+ final double zoom;
const BonfireWidget({
Key key,
@@ -41,6 +42,7 @@ class BonfireWidget extends StatefulWidget {
this.constructionModeColor,
this.collisionAreaColor,
this.lightingColorGame,
+ this.zoom,
}) : super(key: key);
@override
@@ -88,6 +90,7 @@ class _BonfireWidgetState extends State<BonfireWidget>
collisionAreaColor:
widget.collisionAreaColor ?? Colors.lightGreenAccent.withOpacity(0.5),
lightingColorGame: widget.lightingColorGame,
+ zoom: widget.zoom,
);
super.initState();
}

MODIFY
lib/rpg_game.dart
lib/rpg_game.dart
@@ -4,6 +4,7 @@ import 'package:bonfire/joystick/joystick_controller.dart';
import 'package:bonfire/map/map_game.dart';
import 'package:bonfire/player/player.dart';
import 'package:bonfire/util/base_game_point_detector.dart';
+import 'package:bonfire/util/camera.dart';
import 'package:bonfire/util/game_component.dart';
import 'package:bonfire/util/game_controller.dart';
import 'package:bonfire/util/game_interface/game_interface.dart';
@@ -59,9 +60,11 @@ class RPGGame extends BaseGamePointerDetector with KeyboardEvents {
this.constructionModeColor,
this.collisionAreaColor,
this.lightingColorGame,
+ double zoom,
}) : assert(map != null),
assert(context != null),
assert(joystickController != null) {
+ if (zoom != null) gameCamera = Camera(zoom: zoom);
gameCamera.gameRef = this;
joystickController.joystickListener = player ?? MapExplorer(gameCamera);
if (gameController != null) gameController.setGame(this);

MODIFY
lib/util/base_game_point_detector.dart
lib/util/base_game_point_detector.dart
@@ -15,7 +15,7 @@ import 'package:ordered_set/ordered_set.dart';
abstract class BaseGamePointerDetector extends Game with PointerDetector {
bool _isPause = false;
final CustomWidgetBuilder widgetBuilder = CustomWidgetBuilder();
- final Camera gameCamera = Camera();
+ Camera gameCamera = Camera();
/// The list of components to be updated and rendered by the base game.
OrderedSet<Component> components =
@@ -115,7 +115,11 @@ abstract class BaseGamePointerDetector extends Game with PointerDetector {
@override
void render(Canvas canvas) {
canvas.save();
+
+ canvas.translate(size.width/2, size.height/2);
+ canvas.scale(gameCamera.zoom);
canvas.translate(-gameCamera.position.x, -gameCamera.position.y);
+
components.forEach((comp) => renderComponent(canvas, comp));
canvas.restore();
}
@@ -130,14 +134,16 @@ abstract class BaseGamePointerDetector extends Game with PointerDetector {
} else if (comp is GameComponent) {
if (!comp.isHud() && !comp.isVisibleInCamera()) return;
}
+ canvas.save();
if (comp.isHud()) {
- canvas.save();
canvas.translate(gameCamera.position.x, gameCamera.position.y);
+ canvas.scale(1/gameCamera.zoom);
+ canvas.translate(-size.width/2, -size.height/2);
}
+
comp.render(canvas);
canvas.restore();
- canvas.save();
}
/// This implementation of update updates every component in the list.

MODIFY
lib/util/camera.dart
lib/util/camera.dart
@@ -8,12 +8,14 @@ class Camera with HasGameRef<RPGGame> {
double maxTop = 0;
double maxLeft = 0;
Position position = Position.empty();
+ double zoom;
- Rect get cameraRect => Rect.fromLTWH(
- position.x - 40,
- position.y - 40,
- gameRef.size.width + 80,
- gameRef.size.height + 80,
+ Camera({this.zoom = 1.0});
+
+ Rect get cameraRect => Rect.fromCenter(
+ center: Offset(position.x, position.y),
+ width: gameRef.size.width / zoom + 80,
+ height: gameRef.size.height / zoom + 80,
);
void moveTop(double displacement) {
@@ -66,12 +68,10 @@ class Camera with HasGameRef<RPGGame> {
Curve curve = Curves.decelerate,
}) {
if (gameRef?.size == null) return;
- final screenCenter =
- Position(gameRef.size.width / 2, gameRef.size.height / 2);
if (gameRef?.player != null) gameRef.player.focusCamera = false;
- double diffX = this.position.x - (position.x - screenCenter.x);
- double diffY = this.position.y - (position.y - screenCenter.y);
+ double diffX = this.position.x - (position.x);
+ double diffY = this.position.y - (position.y);
double originX = this.position.x;
double originY = this.position.y;
@@ -88,9 +88,8 @@ class Camera with HasGameRef<RPGGame> {
void moveToPosition(Position position) {
if (gameRef?.size == null) return;
if (gameRef?.player != null) gameRef.player.focusCamera = false;
- final screenCenter =
- Position(gameRef.size.width / 2, gameRef.size.height / 2);
- this.position = position - screenCenter;
+
+ this.position = position;
}
void moveToPlayerAnimated({Duration duration, VoidCallback finish}) {
@@ -112,8 +111,10 @@ class Camera with HasGameRef<RPGGame> {
Offset(gameRef.size.width / 2, gameRef.size.height / 2);
final positionPlayer =
worldPositionToScreen(gameRef.player.position.center);
+
final horizontalDistance = screenCenter.dx - positionPlayer.dx;
final verticalDistance = screenCenter.dy - positionPlayer.dy;
+
if (horizontalDistance.abs() > horizontal) {
this.gameRef.gameCamera.position.x += horizontalDistance > 0
? horizontal - horizontalDistance
@@ -135,11 +136,15 @@ class Camera with HasGameRef<RPGGame> {
Offset worldPositionToScreen(Offset position) {
return position.translate(
- -gameRef.gameCamera.position.x, -gameRef.gameCamera.position.y);
+ -gameRef.gameCamera.position.x + gameRef.size.width / 2,
+ -gameRef.gameCamera.position.y + gameRef.size.height / 2,
+ );
}
Offset cameraPositionToWorld(Offset position) {
return position.translate(
- gameRef.gameCamera.position.x, gameRef.gameCamera.position.y);
+ gameRef.gameCamera.position.x - gameRef.size.width / 2,
+ gameRef.gameCamera.position.y - gameRef.size.height / 2,
+ );
}
}

MODIFY
lib/util/lighting/lighting.dart
lib/util/lighting/lighting.dart
@@ -28,10 +28,11 @@ class Lighting extends GameComponent {
_visibleLight.forEach((light) {
light.update(_dtUpdate);
canvas.save();
- canvas.translate(
- -gameRef.gameCamera.position.x,
- -gameRef.gameCamera.position.y,
- );
+
+ canvas.translate(size.width/2, size.height/2);
+ canvas.scale(gameRef.gameCamera.zoom);
+ canvas.translate(-gameRef.gameCamera.position.x, -gameRef.gameCamera.position.y);
+
canvas.drawCircle(
Offset(light.gameComponent.position.center.dx,
light.gameComponent.position.center.dy),

allow camera zoom