Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
feat: adjusted camera
Browse files Browse the repository at this point in the history
  • Loading branch information
alestiago committed Mar 10, 2024
1 parent 84cc16d commit 00d7f21
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import 'package:flame/camera.dart';
import 'package:flame/components.dart';
import 'package:flame/effects.dart';
import 'package:trashy_road/game_settings.dart';

/// {@template CameraFollowBehavior}
/// An adaptation of [FollowBehavior] that makes some vertical adjustments so
/// that the [target] is not directly at the center of the screen.
/// {@endtemplate}
class CameraFollowBehavior extends Component with HasGameReference {
/// {@macro CameraFollowBehavior}
CameraFollowBehavior({
required ReadOnlyPositionProvider target,
required this.viewport,
PositionProvider? owner,
double maxSpeed = double.infinity,
super.priority,
}) : _target = target,
_owner = owner,
_speed = maxSpeed,
assert(maxSpeed > 0, 'maxSpeed must be positive: $maxSpeed');

ReadOnlyPositionProvider get target => _target;
final ReadOnlyPositionProvider _target;

PositionProvider get owner => _owner!;
PositionProvider? _owner;

double get maxSpeed => _speed;
final double _speed;

final Viewport viewport;

@override
void onMount() {
if (_owner == null) {
assert(
parent is PositionProvider,
'Can only apply this behavior to a PositionProvider',
);
_owner = parent! as PositionProvider;
}
}

final _deltaCache = Vector2.zero();

/// The amount of distance that there should be between the target and the
/// bottom of the screen.
static final _verticalAdjustment = GameSettings.gridDimensions.y * 4;

@override
void update(double dt) {
final delta = _deltaCache
..setFrom(target.position)
..sub(owner.position)
..y -= _verticalAdjustment;

final distance = delta.length;
if (distance > _speed * dt) {
delta.scale(_speed * dt / distance);
}
if (delta.x != 0 || delta.y != 0) {
owner.position = delta..add(owner.position);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

13 changes: 12 additions & 1 deletion packages/trashy_road/lib/src/game/game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import 'package:flame/cache.dart';
import 'package:flame/camera.dart';
import 'package:flame/components.dart';
import 'package:flame/events.dart';
import 'package:flame/experimental.dart';
import 'package:flame/game.dart';
import 'package:flame_bloc/flame_bloc.dart';
import 'package:flame_tiled/flame_tiled.dart';
import 'package:flutter/material.dart';
import 'package:trashy_road/game_settings.dart';
import 'package:trashy_road/src/game/entities/vehicle/behaviors/camera_follow_behavior.dart';
import 'package:trashy_road/src/game/game.dart';

export 'bloc/bloc.dart';
Expand Down Expand Up @@ -89,7 +91,16 @@ class TrashyRoadGame extends FlameGame
_player = trashyRoadWorld.tiled.children.whereType<Player>().first;
_player.children.register<PlayerDragMovingBehavior>();

camera.follow(_player);
camera.viewfinder.add(
CameraFollowBehavior(target: _player, viewport: camera.viewport),
);

camera.setBounds(
Rectangle.fromPoints(
trashyRoadWorld.bounds.topLeft,
trashyRoadWorld.bounds.bottomRight,
),
);
}

@override
Expand Down

0 comments on commit 00d7f21

Please sign in to comment.