Skip to content

Commit

Permalink
Merge pull request #519 from RafaelBarbosatec/develop
Browse files Browse the repository at this point in the history
Version 3.9.2
  • Loading branch information
RafaelBarbosatec authored May 30, 2024
2 parents 22677f9 + fd6a585 commit 6efc49d
Show file tree
Hide file tree
Showing 20 changed files with 146 additions and 139 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 3.9.2
- MiniMap improviments. Fix issue [#517](https://github.com/RafaelBarbosatec/bonfire/issues/517)
- Raname `BouncingObject` to `ElasticCollision`.
- Fix `SimpleDirectionAnimation` bug when render fastAnimation.

# 3.9.1
- `BlockMovementCollision` improvements.
- Create a `PinchGesture` mixin to listen pinch gestures.
Expand Down
2 changes: 1 addition & 1 deletion example/lib/pages/collision/collision_component.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:bonfire/bonfire.dart';

class CollisionComponent extends GameDecoration
with Movement, BlockMovementCollision, HandleForces, BouncingObject {
with Movement, BlockMovementCollision, HandleForces, ElasticCollision {
final bool isCircle;
CollisionComponent({
required Vector2 position,
Expand Down
3 changes: 2 additions & 1 deletion example/lib/pages/forces/forces_gem_bouncing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:bonfire/bonfire.dart';
import 'package:example/pages/mini_games/platform/platform_spritesheet.dart';

class ForcesGemBouncing extends GameDecoration
with Movement, HandleForces, BlockMovementCollision, BouncingObject {
with Movement, HandleForces, BlockMovementCollision, ElasticCollision {
ForcesGemBouncing({
required Vector2 position,
}) : super.withAnimation(
Expand All @@ -16,6 +16,7 @@ class ForcesGemBouncing extends GameDecoration
value: Vector2(0, 100),
),
);
setupElasticCollision(restitution: 3);
}

@override
Expand Down
2 changes: 2 additions & 0 deletions example/lib/pages/mini_games/platform/frog_enemy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class FrogEnemy extends PlatformEnemy with HandleForces {
@override
void onDie() {
super.onDie();
handleForcesEnabled = false;
velocity.setZero();
animation?.playOnce(
PlatformSpritesheet.enemyExplosion,
runToTheEnd: true,
Expand Down
2 changes: 1 addition & 1 deletion lib/bonfire.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export 'package:bonfire/map/tiled/world_map_by_tiled.dart';
export 'package:bonfire/map/world_map.dart';
export 'package:bonfire/mixins/attackable.dart';
export 'package:bonfire/mixins/automatic_random_movement.dart';
export 'package:bonfire/mixins/bouncing_object.dart';
export 'package:bonfire/mixins/elastic_collision.dart';
export 'package:bonfire/mixins/flip_render.dart';
export 'package:bonfire/mixins/follower.dart';
export 'package:bonfire/mixins/interval_checker.dart';
Expand Down
41 changes: 17 additions & 24 deletions lib/collision/block_movement_collision.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ enum BodyType {

/// Mixin responsible for adding stop the movement when happen collision
mixin BlockMovementCollision on Movement {
BodyType _bodyType = BodyType.static;
BodyType _bodyType = BodyType.dynamic;
bool _blockMovementCollisionEnabled = true;
bool get blockMovementCollisionEnabled => _blockMovementCollisionEnabled;
final Map<BlockMovementCollision, CollisionData> _collisionsResolution = {};
Expand Down Expand Up @@ -47,36 +47,29 @@ mixin BlockMovementCollision on Movement {
CollisionData collisionData,
) {
_lastCollisionData = collisionData;
Vector2 correction;
double depth = 0;
if (collisionData.depth > 0) {
depth = collisionData.depth + 0.08;
}
correction = (-collisionData.normal * depth);
if (!correction.isZero()) {
positionCorrectionFromCollision(position + correction);
}
onBlockMovementUpdateVelocity(other, collisionData);
}
if (_bodyType.isDynamic) {
Vector2 correction;
double depth = 0;
if (collisionData.depth > 0) {
depth = collisionData.depth + 0.08;
}

void onBlockMovementUpdateVelocity(
PositionComponent other,
CollisionData collisionData,
) {
if (_bodyType.isStatic) {
velocity -= Vector2(
velocity.x * collisionData.normal.x.abs(),
velocity.y * collisionData.normal.y.abs(),
);
} else {
velocity -= getCollisionVelocityReflection(other, collisionData);
correction = (-collisionData.normal * depth);
if ((other is BlockMovementCollision) && other._bodyType.isDynamic) {
correction = (-collisionData.normal * depth / 2);
}
correctPositionFromCollision(position + correction);
}
velocity -= getVelocityReflection(other, collisionData);
}

Vector2 getCollisionVelocityReflection(
Vector2 getVelocityReflection(
PositionComponent other,
CollisionData data,
) {
if (_bodyType.isStatic) {
return velocity;
}
return data.normal * velocity.dot(data.normal);
}

Expand Down
5 changes: 5 additions & 0 deletions lib/forces/handle_forces.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ mixin HandleForces on Movement {
/// Mass of the Component
double _mass = 1.0;

bool handleForcesEnabled = true;

set mass(double mass) {
assert(mass >= 1);
_mass = mass;
Expand All @@ -28,6 +30,9 @@ mixin HandleForces on Movement {

@override
Vector2 onVelocityUpdate(double dt, Vector2 velocity) {
if (!handleForcesEnabled) {
return super.onVelocityUpdate(dt, velocity);
}
final oldVelocity = velocity.clone();
List<Force2D> mergeForces = [..._forces, ...gameRef.globalForces];
final acceleration = mergeForces.whereType<AccelerationForce2D>();
Expand Down
4 changes: 2 additions & 2 deletions lib/mixins/automatic_random_movement.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ mixin RandomMovement on Movement {
}

@override
void positionCorrectionFromCollision(Vector2 position) {
super.positionCorrectionFromCollision(position);
void correctPositionFromCollision(Vector2 position) {
super.correctPositionFromCollision(position);
if (this is Jumper) {
if ((this is BlockMovementCollision)) {
final isV = (this as BlockMovementCollision)
Expand Down
37 changes: 0 additions & 37 deletions lib/mixins/bouncing_object.dart

This file was deleted.

50 changes: 50 additions & 0 deletions lib/mixins/elastic_collision.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import 'dart:math';

import 'package:bonfire/bonfire.dart';

// Mixin responsable to give the bounce behavior. (experimental)
mixin ElasticCollision on BlockMovementCollision {
double _restitution = 2;
bool _bouncingObjectEnabled = true;

void setupElasticCollision({
bool? enabled,
double? restitution,
}) {
_bouncingObjectEnabled = enabled ?? _bouncingObjectEnabled;
_restitution = restitution ?? _restitution;
}

// source https://chrishecker.com/images/e/e7/Gdmphys3.pdf
// Applying Impulse
@override
Vector2 getVelocityReflection(
PositionComponent other,
CollisionData data,
) {
if (_bouncingObjectEnabled) {
Vector2 otherVelocity =
(other is Movement) ? other.velocity : Vector2.zero();
Vector2 relativeVelocity = otherVelocity - velocity;

if (relativeVelocity.dot(data.normal) > 0) {
return super.getVelocityReflection(other, data);
}

double bRestitution =
(other is ElasticCollision) ? other._restitution : _restitution;

double e = min(_restitution, bRestitution);

double j = -(1 + e) * relativeVelocity.dot(data.normal);

double mass = (this is HandleForces) ? (this as HandleForces).mass : 1;
double massB = (other is HandleForces) ? (other).mass : 1;
j /= mass + massB;

Vector2 impulse = data.normal * j;
return impulse;
}
return super.getVelocityReflection(other, data);
}
}
18 changes: 9 additions & 9 deletions lib/mixins/jumper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ enum JumpingStateEnum {
/// )
mixin Jumper on Movement, BlockMovementCollision {
final double _defaultJumpSpeed = 150;
bool jumping = false;
bool isJumping = false;
JumpingStateEnum jumpingState = JumpingStateEnum.idle;
int _maxJump = 1;
int _currentJumps = 0;
Expand All @@ -30,10 +30,10 @@ mixin Jumper on Movement, BlockMovementCollision {
}

void jump({double? jumpSpeed, bool force = false}) {
if (!jumping || _currentJumps < _maxJump || force) {
if (!isJumping || _currentJumps < _maxJump || force) {
_currentJumps++;
moveUp(speed: jumpSpeed ?? _defaultJumpSpeed);
jumping = true;
isJumping = true;
}
}

Expand All @@ -43,26 +43,26 @@ mixin Jumper on Movement, BlockMovementCollision {
CollisionData collisionData,
) {
super.onBlockedMovement(other, collisionData);
if (jumping &&
if (isJumping &&
lastDirectionVertical.isDownSide &&
collisionData.direction.isDownSide) {
_currentJumps = 0;
jumping = false;
isJumping = false;
}
}

@override
void update(double dt) {
super.update(dt);
if (!jumping && displacement.y.abs() > 0.5) {
jumping = true;
if (!isJumping && displacement.y.abs() > 0.2) {
isJumping = true;
}
_notifyJump();
}

void _notifyJump() {
JumpingStateEnum newDirection;
if (jumping) {
if (isJumping) {
if (lastDirectionVertical == Direction.down) {
newDirection = JumpingStateEnum.down;
} else {
Expand All @@ -79,7 +79,7 @@ mixin Jumper on Movement, BlockMovementCollision {

@override
void stopMove({bool forceIdle = false, bool isX = true, bool isY = true}) {
if (!jumping) {
if (!isJumping) {
super.stopMove(forceIdle: forceIdle, isX: isX, isY: isY);
}
}
Expand Down
18 changes: 9 additions & 9 deletions lib/mixins/jumper_animation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ enum JumpAnimationsEnum {
mixin JumperAnimation on Jumper, DirectionAnimation {
@override
void onPlayRunDownAnimation() {
if (jumping) {
if (isJumping) {
if (lastDirectionHorizontal == Direction.left) {
_jumpDownLeft();
} else {
Expand All @@ -26,7 +26,7 @@ mixin JumperAnimation on Jumper, DirectionAnimation {

@override
void onPlayRunDownRightAnimation() {
if (jumping) {
if (isJumping) {
animation?.playOther(JumpAnimationsEnum.jumpDownRight, flipX: false);
} else {
super.onPlayRunDownRightAnimation();
Expand All @@ -35,7 +35,7 @@ mixin JumperAnimation on Jumper, DirectionAnimation {

@override
void onPlayRunDownLeftAnimation() {
if (jumping) {
if (isJumping) {
_jumpDownLeft();
} else {
super.onPlayRunDownLeftAnimation();
Expand All @@ -44,7 +44,7 @@ mixin JumperAnimation on Jumper, DirectionAnimation {

@override
void onPlayRunUpLeftAnimation() {
if (jumping) {
if (isJumping) {
_playJumpUpLeft();
} else {
super.onPlayRunUpLeftAnimation();
Expand All @@ -53,7 +53,7 @@ mixin JumperAnimation on Jumper, DirectionAnimation {

@override
void onPlayRunLeftAnimation() {
if (jumping) {
if (isJumping) {
_playJumpUpLeft();
} else {
super.onPlayRunLeftAnimation();
Expand All @@ -62,7 +62,7 @@ mixin JumperAnimation on Jumper, DirectionAnimation {

@override
void onPlayRunRightAnimation() {
if (jumping) {
if (isJumping) {
animation?.playOther(JumpAnimationsEnum.jumpUpRight, flipX: false);
} else {
super.onPlayRunRightAnimation();
Expand All @@ -71,7 +71,7 @@ mixin JumperAnimation on Jumper, DirectionAnimation {

@override
void onPlayRunUpRightAnimation() {
if (jumping) {
if (isJumping) {
animation?.playOther(JumpAnimationsEnum.jumpUpRight, flipX: false);
} else {
super.onPlayRunUpRightAnimation();
Expand All @@ -80,7 +80,7 @@ mixin JumperAnimation on Jumper, DirectionAnimation {

@override
void onPlayRunUpAnimation() {
if (jumping) {
if (isJumping) {
if (lastDirectionHorizontal == Direction.left) {
_playJumpUpLeft();
} else {
Expand Down Expand Up @@ -127,7 +127,7 @@ mixin JumperAnimation on Jumper, DirectionAnimation {

@override
void idle() {
if (!jumping) {
if (!isJumping) {
super.idle();
}
}
Expand Down
5 changes: 1 addition & 4 deletions lib/mixins/movement.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ mixin Movement on GameComponent {
}
}

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

Expand Down Expand Up @@ -216,9 +216,6 @@ mixin Movement on GameComponent {
bool enabledDiagonal = true,
double? speed,
}) {
if (direction != lastDirection) {
setZeroVelocity();
}
switch (direction) {
case Direction.left:
moveLeft(speed: speed);
Expand Down
Loading

0 comments on commit 6efc49d

Please sign in to comment.