Skip to content

Commit

Permalink
Merge pull request #481 from RafaelBarbosatec/develop
Browse files Browse the repository at this point in the history
Version 3.1.2
  • Loading branch information
RafaelBarbosatec authored Dec 15, 2023
2 parents 8479cb8 + 7486ff4 commit a56c34f
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 21 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 3.1.2
- fix bug `AutomaticRandomMovement`. (not stop movement in collision)
- fix bug `BlockMovementCollision`.(pushing other)
- fix bug `simpleAttackMeleeByDirection`.(pushing enemy to inside other collision)

## 3.1.1
- Fix flip render problem in `SimpleDirectionAnimation`.

Expand Down
25 changes: 16 additions & 9 deletions example/lib/pages/mini_games/simple_example/my_enemy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,31 @@ import 'package:example/shared/util/enemy_sprite_sheet.dart';
///
/// Rafaelbarbosatec
/// on 19/10/21
class MyEnemy extends SimpleEnemy with BlockMovementCollision {
class MyEnemy extends SimpleNpc
with BlockMovementCollision, AutomaticRandomMovement {
MyEnemy(Vector2 position)
: super(
animation: EnemySpriteSheet.simpleDirectionAnimation,
position: position,
size: Vector2.all(32),
life: 100,
);

@override
Future<void> onLoad() {
add(RectangleHitbox(size: size, isSolid: true));
return super.onLoad();
}

@override
void update(double dt) {
super.update(dt);
seeAndMoveToPlayer(
margin: -5,
closePlayer: (player) {
/// do anything when close to player
},
radiusVision: 64,
);
// seeAndMoveToPlayer(
// margin: -5,
// closePlayer: (player) {
// /// do anything when close to player
// },
// radiusVision: 64,
// );
runRandomMovement(dt);
}
}
30 changes: 21 additions & 9 deletions lib/collision/block_movement_collision.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export 'collision_data.dart';
/// Mixin responsible for adding stop the movement when happen collision
mixin BlockMovementCollision on Movement {
bool _isRigid = true;
bool _isStatic = false;
bool _blockMovementCollisionEnabled = true;
bool get blockMovementCollisionEnabled => _blockMovementCollisionEnabled;
bool get blockMovementCollisionReflectionEnabled => _isRigid;
Expand Down Expand Up @@ -43,16 +44,17 @@ mixin BlockMovementCollision on Movement {
depth = collisionData.depth + 0.05;
}
correction = (-collisionData.normal * depth);
superPosition = position + correction;

if (!correction.isZero()) {
positionCorrectionFromCollision(position + correction);
}
onBlockMovementUpdateVelocity(other, collisionData);
}

void onBlockMovementUpdateVelocity(
PositionComponent other,
CollisionData collisionData,
) {
if (_isRigid) {
if (_isStatic) {
velocity -= Vector2(
velocity.x * collisionData.normal.x.abs(),
velocity.y * collisionData.normal.y.abs(),
Expand All @@ -70,29 +72,34 @@ mixin BlockMovementCollision on Movement {
}

bool onCheckStaticCollision(BlockMovementCollision other) {
return _isRigid ? other.velocity.length > velocity.length : false;
return _isRigid ? velocity.isZero() : false;
}

@override
void onCollision(Set<Vector2> intersectionPoints, PositionComponent other) {
super.onCollision(intersectionPoints, other);
if (other is Sensor || !_blockMovementCollisionEnabled) return;
bool stopOtherMovement = true;
bool isStatic = false;
_isStatic = false;
bool stopMovement = other is GameComponent
? onBlockMovement(intersectionPoints, other)
: true;
if (other is BlockMovementCollision) {
stopOtherMovement = other.onBlockMovement(intersectionPoints, this);
isStatic = onCheckStaticCollision(other);
_isStatic = onCheckStaticCollision(other);
}

if (!stopMovement || !stopOtherMovement) {
return;
}

if (_collisionsResolution.containsKey(other)) {
onBlockedMovement(other, _collisionsResolution[other]!);
onBlockedMovement(
other,
_collisionsResolution[other]!.copyWith(
depth: _isStatic ? 0 : null,
),
);
_collisionsResolution.remove(other);
return;
}
Expand Down Expand Up @@ -124,11 +131,16 @@ mixin BlockMovementCollision on Movement {
if (colisionResult != null) {
final data = CollisionData(
normal: colisionResult.normal,
depth: isStatic ? 0 : colisionResult.depth,
depth: colisionResult.depth,
intersectionPoints: intersectionPoints.toList(),
direction: colisionResult.normal.toDirection(),
);
onBlockedMovement(other, data);
onBlockedMovement(
other,
data.copyWith(
depth: _isStatic ? 0 : null,
),
);
if (other is BlockMovementCollision) {
other.setCollisionResolution(this, data.inverted());
}
Expand Down
6 changes: 6 additions & 0 deletions lib/mixins/automatic_random_movement.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ mixin AutomaticRandomMovement on Movement {
}
}

@override
void positionCorrectionFromCollision(Vector2 position) {
super.positionCorrectionFromCollision(position);
stopMove();
}

@override
void stopMove({bool forceIdle = false, bool isX = true, bool isY = true}) {
super.stopMove(forceIdle: forceIdle, isX: isX, isY: isY);
Expand Down
2 changes: 1 addition & 1 deletion lib/mixins/movement.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ mixin Movement on GameComponent {
}
}

set superPosition(Vector2 position) {
void positionCorrectionFromCollision(Vector2 position) {
super.position = position;
}

Expand Down
5 changes: 4 additions & 1 deletion lib/util/extensions/game_component_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,10 @@ extension GameComponentExtensions on GameComponent {
.forEach((enemy) {
enemy.receiveDamage(attackFrom, damage, id);
if (withPush && enemy is Movement) {
(enemy as Movement).translate(diffBase);
if ((enemy as Movement).canMove(diffBase.toDirection(),
displacement: diffBase.maxValue())) {
(enemy as Movement).translate(diffBase);
}
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: bonfire
description: (RPG maker) Create RPG-style or similar games more simply with Flame.
version: 3.1.1
version: 3.1.2
homepage: https://bonfire-engine.github.io
repository: https://github.com/RafaelBarbosatec/bonfire
issue_tracker: https://github.com/RafaelBarbosatec/bonfire/issues
Expand Down

0 comments on commit a56c34f

Please sign in to comment.