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

Commit

Permalink
fix: stop player from teleporting into tile
Browse files Browse the repository at this point in the history
  • Loading branch information
OlliePugh committed Feb 16, 2024
1 parent 032212e commit 7150236
Showing 1 changed file with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ final class PlayerMovingBehavior extends Behavior<Player>
/// it will be lerped until [_targetPosition] is reached by the [Player].
final Vector2 _targetPosition = Vector2.zero();

final Vector2 _previousPosition = Vector2.zero();

/// The delay between player moves.
static const moveDelay = Duration(milliseconds: 100);
static const _moveDelay = Duration(milliseconds: 100);

/// The lerp time for player movement.
static const playerMoveAnimationSpeed = 15;
static const _playerMoveAnimationSpeed = 15;

/// A int that contains the time when the next move can be made.
DateTime _nextMoveTime = DateTime.fromMicrosecondsSinceEpoch(0);
Expand All @@ -29,16 +31,23 @@ final class PlayerMovingBehavior extends Behavior<Player>
Future<void> onLoad() async {
await super.onLoad();
_targetPosition.setFrom(parent.position);
_previousPosition.setFrom(parent.position);
}

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

if (parent.position.distanceTo(_targetPosition) > 0.01) {
parent.position.lerp(_targetPosition, playerMoveAnimationSpeed * dt);
parent.position.lerp(_targetPosition, _playerMoveAnimationSpeed * dt);
parent.priority = parent.position.y.floor();
}

if (parent.position.distanceTo(_targetPosition) <
GameSettings.gridDimensions.y / 2 &&
_previousPosition != _targetPosition) {
_previousPosition.setFrom(_targetPosition);
}
}

void move(Direction direction) {
Expand All @@ -62,10 +71,10 @@ final class PlayerMovingBehavior extends Behavior<Player>
} else if (direction == Direction.up) {
_targetPosition.y -= GameSettings.gridDimensions.y;
}
_nextMoveTime = now.add(moveDelay);
_nextMoveTime = now.add(_moveDelay);
}

void bounceBack() {
_targetPosition.setFrom(Player.snapToGrid(parent.position));
_targetPosition.setFrom(_previousPosition);
}
}

0 comments on commit 7150236

Please sign in to comment.