Skip to content

Commit

Permalink
Merge pull request #568 from RafaelBarbosatec/develop
Browse files Browse the repository at this point in the history
Version 3.12.2
  • Loading branch information
RafaelBarbosatec authored Nov 22, 2024
2 parents da7fecf + a8188e1 commit f444b63
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 40 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 3.12.2
- `RandomMovement` improvements

# 3.12.1
- Resolve the alignment and visibility issues when using the MiniMap widget with non-1.0 zoom values. Thanks [qulvmp6](https://github.com/qulvmp6)
- Adds `randomMovementArea` param in `RandomMovement` mixin.
Expand Down
21 changes: 12 additions & 9 deletions example/lib/pages/enemy/melee_enemy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@ class MeleeEnemy extends SimpleEnemy {

@override
void update(double dt) {
seeAndMoveToPlayer(closePlayer: (p) {
animation?.showStroke(Colors.white, 1);
if (checkInterval('attack', 600, dt)) {
_playAttackAnimation();
}
}, notObserved: () {
animation?.hideStroke();
return true;
});
seeAndMoveToPlayer(
closePlayer: (p) {
animation?.showStroke(Colors.white, 1);
if (checkInterval('attack', 600, dt)) {
_playAttackAnimation();
}
},
notObserved: () {
animation?.hideStroke();
return true;
},
);
super.update(dt);
}

Expand Down
101 changes: 72 additions & 29 deletions lib/mixins/random_movement.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ mixin RandomMovement on Movement {
Direction _currentDirection = Direction.left;
Vector2 _originPosition = Vector2.zero();

double _lastMinDistance = 0;
double _travelledDistance = 0;

// Area where the random movement will be made
Expand All @@ -67,42 +66,33 @@ mixin RandomMovement on Movement {
Function(Direction direction)? onStartMove,
Function()? onStopMove,
}) {
_lastMinDistance = minDistance;
_onStartMove = onStartMove;
_onStopMove = onStopMove;

if (_distanceToArrived == null) {
if (checkInterval(_KEY_INTERVAL_KEEP_STOPPED, timeKeepStopped, dt)) {
_distanceToArrived = _getDistance(minDistance, maxDistance);
_currentDirection = _getDirection(directions);
_originPosition = absoluteCenter.clone();
if (randomMovementArea != null) {
final targetPosition = _getTargetPosition(
_currentDirection,
_distanceToArrived,
);
final insideArea = randomMovementArea!.containsLocalPoint(
targetPosition,
);
if (!insideArea) {
_stop();
return;
}
}
if (checkDirectionWithRayCast) {
if (!canMove(_currentDirection, displacement: _distanceToArrived)) {
_stop();
return;
}
final target = _getTarget(
minDistance,
maxDistance,
checkDirectionWithRayCast,
);
if (target == null) {
_stop();
return;
}
_currentDirection = target.direction;
_distanceToArrived = target.distance;
_originPosition = absoluteCenter.clone();
_onStartMove?.call(_currentDirection);
}
} else {
_travelledDistance = absoluteCenter.distanceTo(_originPosition);
if (_travelledDistance >= _distanceToArrived!) {
final isCanMove = canMove(_currentDirection, displacement: speed);
if (_travelledDistance >= _distanceToArrived! || !isCanMove) {
_stop();
return;
}

moveFromDirection(_currentDirection, speed: speed);
if (updateAngle) {
angle = _currentDirection.toRadians();
Expand Down Expand Up @@ -130,9 +120,6 @@ mixin RandomMovement on Movement {

void _stop() {
_onStopMove?.call();
if (_travelledDistance < _lastMinDistance) {
resetInterval(_KEY_INTERVAL_KEEP_STOPPED);
}
_onStopMove = null;
_onStartMove = null;
_distanceToArrived = null;
Expand All @@ -146,7 +133,7 @@ mixin RandomMovement on Movement {
super.onMount();
}

double? _getDistance(double minDistance, double maxDistance) {
double _getDistance(double minDistance, double maxDistance) {
final diffDistane = maxDistance - minDistance;
return minDistance + _random.nextDouble() * diffDistane;
}
Expand All @@ -157,7 +144,63 @@ mixin RandomMovement on Movement {
}

Vector2 _getTargetPosition(
Direction currentDirection, double? distanceToArrived) {
Direction currentDirection,
double? distanceToArrived,
) {
return absoluteCenter + currentDirection.toVector2() * distanceToArrived!;
}

_RandomPositionTarget? _getTarget(
double minDistance,
double maxDistance,
bool checkDirectionWithRayCast,
) {
int index = 0;
while (index < 100) {
final distance = _getDistance(minDistance, maxDistance);
final direction = _getDirection(RandomMovementDirections.all);
final targetPosition = _getTargetPosition(direction, distance);
bool isRaycastOk = true;

if (checkDirectionWithRayCast) {
isRaycastOk = canMove(
_currentDirection,
displacement: _distanceToArrived,
);
}

if (randomMovementArea != null) {
final insideArea = randomMovementArea!.containsPoint(
targetPosition,
);
if (insideArea && isRaycastOk) {
return _RandomPositionTarget(
position: targetPosition,
direction: direction,
distance: distance,
);
}
} else if (isRaycastOk) {
return _RandomPositionTarget(
position: targetPosition,
direction: direction,
distance: distance,
);
}

index++;
}
return null;
}
}

class _RandomPositionTarget {
final Vector2 position;
final Direction direction;
final double distance;

_RandomPositionTarget(
{required this.position,
required this.direction,
required this.distance});
}
3 changes: 1 addition & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
name: bonfire
description: (RPG maker) Create RPG-style or similar games more simply with Flame.
version: 3.12.1
version: 3.12.2
homepage: https://bonfire-engine.github.io
repository: https://github.com/RafaelBarbosatec/bonfire
issue_tracker: https://github.com/RafaelBarbosatec/bonfire/issues
# documentation: https://bonfire-engine.github.io

environment:
sdk: ">=3.4.0 <4.0.0"
Expand Down

0 comments on commit f444b63

Please sign in to comment.