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

Commit

Permalink
feat: smoother camera movement (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
erickzanardo authored Dec 11, 2023
1 parent c0a22bd commit d599132
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 5 deletions.
131 changes: 126 additions & 5 deletions lib/game/components/player_camera_anchor.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,131 @@
import 'package:flame/components.dart';
import 'package:flame/effects.dart';
import 'package:flutter/material.dart';
import 'package:super_dash/game/super_dash_game.dart';

class CameraBounds extends PositionComponent {
CameraBounds({
required this.reference,
required this.bound,
this.showBounds = false,
});

final PositionComponent reference;
final double bound;
final bool showBounds;

late final Vector2 referenceOffset;

double translateValue = 0;
double translatedValue = 0;

void _updatePosition(double dt) {
position.x = reference.position.x + referenceOffset.x;

final playerPosition = reference.position.y + referenceOffset.y;
final difference = playerPosition - position.y;
if (difference.abs() > bound) {
translateValue = difference;
}

if (translateValue.abs() > 0) {
final translationDifference =
(translateValue.abs() - translatedValue.abs()) / translateValue.abs();
var speed = 1.0 - translationDifference;
speed = 1 - speed * speed * speed * speed;

final step = (800 * speed) * dt * translateValue.sign;

position.y += step;
translatedValue += step.abs();

if (translateValue.abs() - translatedValue.abs() < 10) {
translateValue = 0;
translatedValue = 0;
}
}
}

@override
void onMount() {
super.onMount();

referenceOffset = Vector2(
reference.size.x,
reference.size.y,
);
position.y = reference.position.y + referenceOffset.y;

_updatePosition(0);

if (showBounds) {
const referenceLine = 150.0;
add(
RectangleComponent(
position: Vector2(
-referenceLine / 2,
-bound,
),
size: Vector2(
referenceLine,
2,
),
paint: Paint()..color = Colors.red,
),
);

add(
RectangleComponent(
position: Vector2(
-referenceLine / 2,
0,
),
size: Vector2(
referenceLine,
2,
),
paint: Paint()..color = Colors.green,
),
);

add(
RectangleComponent(
position: Vector2(
-referenceLine / 2,
bound,
),
size: Vector2(
referenceLine,
2,
),
paint: Paint()..color = Colors.red,
),
);
}
}

@override
void update(double dt) {
super.update(dt);

_updatePosition(dt);
}
}

class PlayerCameraAnchor extends Component
with ParentIsA<PositionComponent>
with ParentIsA<PositionComponent>, HasGameRef<SuperDashGame>
implements ReadOnlyPositionProvider {
PlayerCameraAnchor({
required this.levelSize,
required this.cameraViewport,
this.showCameraBounds = false,
});

final Vector2 levelSize;
final Vector2 cameraViewport;
final Vector2 _anchor = Vector2.zero();
late final PositionComponent _bounds;
final bool showCameraBounds;

late final Vector2 _cameraMin = Vector2(
cameraViewport.x * .4,
Expand All @@ -36,8 +150,15 @@ class PlayerCameraAnchor extends Component
}

@override
Future<void> onLoad() async {
await super.onLoad();
void onMount() {
super.onMount();

_bounds = CameraBounds(
reference: parent,
bound: 128,
showBounds: showCameraBounds,
);
gameRef.world.add(_bounds);

final value = parent.position.clone();
_setAnchor(value.x, value.y);
Expand All @@ -48,8 +169,8 @@ class PlayerCameraAnchor extends Component
super.update(dt);

_setAnchor(
parent.position.x,
parent.position.y,
_bounds.position.x,
_bounds.position.y,
);
}
}
1 change: 1 addition & 0 deletions lib/game/entities/player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class Player extends JumperCharacter<SuperDashGame> {
cameraAnchor = PlayerCameraAnchor(
cameraViewport: cameraViewport,
levelSize: levelSize,
showCameraBounds: gameRef.inMapTester,
);

add(cameraAnchor);
Expand Down

0 comments on commit d599132

Please sign in to comment.