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

Commit

Permalink
feat: add CamerMan
Browse files Browse the repository at this point in the history
  • Loading branch information
alestiago committed Mar 11, 2024
1 parent 9633533 commit a6be451
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions packages/trashy_road/lib/src/game/game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ class TrashyRoadGame extends FlameGame

// TODO(alestiago): Refactor this and properly await loading.
unawaited(
trashyRoadWorld.loaded.then((_) {
trashyRoadWorld.loaded.then((_) async {
_player = trashyRoadWorld.children.whereType<Player>().first;
camera.follow(_player);
final cameraMan = _CameraMan(actor: _player);
await world.add(cameraMan);
camera.follow(cameraMan);
_updateBounds();
}),
);
Expand Down Expand Up @@ -154,3 +156,29 @@ class TrashyRoadGame extends FlameGame
camera.update(dt);
}
}

class _CameraMan extends PositionComponent {
_CameraMan({required this.actor});

final PositionComponent actor;

static final _offset = UnmodifiableVector2View(0, -200);

void _updatePosition() {
position
..setFrom(actor.position)
..add(_offset);
}

@override
FutureOr<void> onLoad() async {
await super.onLoad();
_updatePosition();
}

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

0 comments on commit a6be451

Please sign in to comment.