From f077e132a3b2ecd78f54e4c6d8c28e8b3ff33e0b Mon Sep 17 00:00:00 2001 From: Rafael Almeida Barbosa Date: Mon, 29 Jul 2024 09:18:20 -0300 Subject: [PATCH 1/6] Fix bug when hitbox anchor is center. --- CHANGELOG.md | 3 +++ example/lib/pages/player/simple/human.dart | 7 ++++++- lib/collision/block_movement_collision.dart | 6 +++--- lib/collision/collision_util.dart | 2 +- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 19c8e7c7b..b54c41d86 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# NEXT +- Fix bug when hitbox anchor is center. + # 3.9.7 - Update `tiledjsonreader` - Bugfix/tile rotation collision. [#535](https://github.com/RafaelBarbosatec/bonfire/pull/535) diff --git a/example/lib/pages/player/simple/human.dart b/example/lib/pages/player/simple/human.dart index 90b79aceb..7a3d4349f 100644 --- a/example/lib/pages/player/simple/human.dart +++ b/example/lib/pages/player/simple/human.dart @@ -14,7 +14,12 @@ class HumanPlayer extends SimplePlayer with BlockMovementCollision { @override Future onLoad() { /// Adds rectangle collision - add(RectangleHitbox(size: size / 2, position: size / 4)); + add( + RectangleHitbox( + size: size / 2, + position: size / 4, + ), + ); return super.onLoad(); } } diff --git a/lib/collision/block_movement_collision.dart b/lib/collision/block_movement_collision.dart index 2b131e3ec..f6218a829 100644 --- a/lib/collision/block_movement_collision.dart +++ b/lib/collision/block_movement_collision.dart @@ -50,9 +50,9 @@ mixin BlockMovementCollision on Movement { if (_bodyType.isDynamic) { Vector2 correction; - double depth = collisionData.depth; - if (depth != 0) { - depth = collisionData.depth.abs() + 0.08; + double depth = collisionData.depth.abs(); + if (depth > 0) { + depth += 0.08; } correction = (-collisionData.normal * depth); diff --git a/lib/collision/collision_util.dart b/lib/collision/collision_util.dart index 4ce378216..e46e57837 100644 --- a/lib/collision/collision_util.dart +++ b/lib/collision/collision_util.dart @@ -101,7 +101,7 @@ final _cachedGlobalVertices = ValueCache>(); extension PolygonComponentExt on PolygonComponent { List get absoluteVertices { - final Vector2 p = absolutePosition; + final Vector2 p = absoluteTopLeftPosition; final adjustedVerticies = absoluteAngle == 0 ? vertices : rotatedVerticesBonfire(absoluteAngle); From 3b2d63990f5aa5ae34cd2da917d2dd2f39586ffd Mon Sep 17 00:00:00 2001 From: Rafael Almeida Barbosa Date: Mon, 29 Jul 2024 09:41:39 -0300 Subject: [PATCH 2/6] rename receiveDamage to handleAttack --- CHANGELOG.md | 1 + example/lib/shared/decoration/spikes.dart | 4 ++-- lib/mixins/attackable.dart | 18 +++++++++++++----- lib/objects/flying_attack_game_object.dart | 2 +- lib/util/damage_hitbox.dart | 2 +- 5 files changed, 18 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b54c41d86..567cdad2e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # NEXT - Fix bug when hitbox anchor is center. +- Update `bool receiveDamage` to `void onReceiveDamage`. Now to perform attack use `handleAttack` method. # 3.9.7 - Update `tiledjsonreader` diff --git a/example/lib/shared/decoration/spikes.dart b/example/lib/shared/decoration/spikes.dart index d7fb4e002..bfacbca58 100644 --- a/example/lib/shared/decoration/spikes.dart +++ b/example/lib/shared/decoration/spikes.dart @@ -15,9 +15,9 @@ class Spikes extends GameDecoration with Sensor { @override void onContact(Attackable component) { if (component is Player) { - component.receiveDamage(AttackOriginEnum.ENEMY, 10, 1); + component.handleAttack(AttackOriginEnum.ENEMY, 10, 1); } else { - component.receiveDamage(AttackOriginEnum.PLAYER_OR_ALLY, 10, 1); + component.handleAttack(AttackOriginEnum.PLAYER_OR_ALLY, 10, 1); } } } diff --git a/lib/mixins/attackable.dart b/lib/mixins/attackable.dart index f551d8346..57156edfe 100644 --- a/lib/mixins/attackable.dart +++ b/lib/mixins/attackable.dart @@ -75,16 +75,24 @@ mixin Attackable on GameComponent { /// This method is called to give damage a this component. /// Only receive damage if the method [checkCanReceiveDamage] return `true`. - bool receiveDamage( + bool handleAttack( AttackOriginEnum attacker, double damage, dynamic identify, ) { - if (checkCanReceiveDamage(attacker)) { - removeLife(damage); - return true; + final canReceive = checkCanReceiveDamage(attacker); + if (canReceive) { + onReceiveDamage(attacker, damage, identify); } - return false; + return canReceive; + } + + void onReceiveDamage( + AttackOriginEnum attacker, + double damage, + dynamic identify, + ) { + removeLife(damage); } /// This method is used to check if this component can receive damage from any attacker. diff --git a/lib/objects/flying_attack_game_object.dart b/lib/objects/flying_attack_game_object.dart index 3f2ce6cee..3c15b42b9 100644 --- a/lib/objects/flying_attack_game_object.dart +++ b/lib/objects/flying_attack_game_object.dart @@ -123,7 +123,7 @@ class FlyingAttackGameObject extends AnimatedGameObject @override void onCollision(Set intersectionPoints, PositionComponent other) { if (other is Attackable && animationDestroy == null) { - other.receiveDamage(attackFrom, damage, id); + other.handleAttack(attackFrom, damage, id); } if (other is GameComponent) { _destroyObject(other); diff --git a/lib/util/damage_hitbox.dart b/lib/util/damage_hitbox.dart index 4a32c6e0f..f1ed2e4de 100644 --- a/lib/util/damage_hitbox.dart +++ b/lib/util/damage_hitbox.dart @@ -52,7 +52,7 @@ class DamageHitbox extends GameComponent { .attackables(onlyVisible: true) .where((a) => a.rectAttackable().overlaps(toAbsoluteRect())) .forEach((attackable) { - final receiveDamage = attackable.receiveDamage(origin, damage, id); + final receiveDamage = attackable.handleAttack(origin, damage, id); if (receiveDamage) { onDamage?.call(attackable); } From 713329d803d5bac9d07ee562eb676a1f3ca18ae6 Mon Sep 17 00:00:00 2001 From: Rafael Almeida Barbosa Date: Mon, 29 Jul 2024 09:55:52 -0300 Subject: [PATCH 3/6] increment version --- CHANGELOG.md | 2 +- pubspec.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 567cdad2e..6492615f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -# NEXT +# 3.9.8 - Fix bug when hitbox anchor is center. - Update `bool receiveDamage` to `void onReceiveDamage`. Now to perform attack use `handleAttack` method. diff --git a/pubspec.yaml b/pubspec.yaml index 711ac16a3..752835669 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: bonfire description: (RPG maker) Create RPG-style or similar games more simply with Flame. -version: 3.9.7 +version: 3.9.8 homepage: https://bonfire-engine.github.io repository: https://github.com/RafaelBarbosatec/bonfire issue_tracker: https://github.com/RafaelBarbosatec/bonfire/issues From afc4abf55d535bd20ec8b95ac30fed030af77c5b Mon Sep 17 00:00:00 2001 From: Rafael Almeida Barbosa Date: Mon, 29 Jul 2024 09:58:12 -0300 Subject: [PATCH 4/6] update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6492615f2..70c26c12d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # 3.9.8 - Fix bug when hitbox anchor is center. -- Update `bool receiveDamage` to `void onReceiveDamage`. Now to perform attack use `handleAttack` method. +- Update `bool receiveDamage` to `void onReceiveDamage`. Now to perform receive of attack use `handleAttack` method. # 3.9.7 - Update `tiledjsonreader` From 77bf18595b9bb4d958e729dec3f230617505f62b Mon Sep 17 00:00:00 2001 From: Rafael Almeida Barbosa Date: Mon, 29 Jul 2024 10:04:56 -0300 Subject: [PATCH 5/6] update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 70c26c12d..b31d7fa54 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # 3.9.8 - Fix bug when hitbox anchor is center. -- Update `bool receiveDamage` to `void onReceiveDamage`. Now to perform receive of attack use `handleAttack` method. +- BREAKING CHANGE: Update `bool receiveDamage` to `void onReceiveDamage`. Now to perform receive of attack use `handleAttack` method. # 3.9.7 - Update `tiledjsonreader` From 50abb28155abf283ae20cfa0a65de761875d5004 Mon Sep 17 00:00:00 2001 From: Rafael Almeida Barbosa Date: Mon, 29 Jul 2024 10:07:50 -0300 Subject: [PATCH 6/6] adds comments --- lib/mixins/attackable.dart | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/mixins/attackable.dart b/lib/mixins/attackable.dart index 57156edfe..1fd4d2ea5 100644 --- a/lib/mixins/attackable.dart +++ b/lib/mixins/attackable.dart @@ -25,12 +25,13 @@ mixin Attackable on GameComponent { double get life => _life; + /// Set initial life void initialLife(double life) { _life = life; _maxLife = life; } - /// increase life + /// Increase life void addLife(double life) { double newLife = _life + life; @@ -43,6 +44,7 @@ mixin Attackable on GameComponent { _verifyLimitsLife(); } + // Update life void updateLife(double life, {bool verifyDieOrRevive = true}) { _life = life; if (verifyDieOrRevive) { @@ -62,7 +64,10 @@ mixin Attackable on GameComponent { _verifyLimitsLife(); } + // Called when life is removed void onRemoveLife(double life) {} + + // Called when life is restored void onRestoreLife(double life) {} void _verifyLimitsLife() { @@ -87,6 +92,7 @@ mixin Attackable on GameComponent { return canReceive; } + // Called when the component receives damage void onReceiveDamage( AttackOriginEnum attacker, double damage, @@ -120,15 +126,18 @@ mixin Attackable on GameComponent { return false; } + // Called when the component dies void onDie() { _isDead = true; } + // Called when the component revives void onRevive() { _isDead = false; } bool get isDead => _isDead; + // Get rect collision of the component used to receive damage Rect rectAttackable() => rectCollision; }