This repository has been archived by the owner on Mar 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
79 additions
and
1 deletion.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
packages/trashy_road/lib/src/game/entities/vehicle/behaviors/camera_follow_behavior.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
packages/trashy_road/lib/src/game/entities/vehicle/behaviors/translated_follow_behavior.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters