Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 3.1.2 #481

Merged
merged 5 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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