Skip to content

Commit

Permalink
feat: onComponentTypeCheck support for ShapeHitbox (#1981)
Browse files Browse the repository at this point in the history
onComponentTypeCheck was only supported for components but not for hitboxes, because ShapeHitbox implements GenericCollisionCallbacks instead CollisionCallbacks.

Specifying onComponentTypeCheck for individual hitboxes might be useful in cases where Component have hitboxes for collisions and also some utility hitboxes (for example, to check available directions for pathfinding). Colliding between this two types of hitboxes should not lead to movement restrictions and so on. But without onComponentTypeCheck in ShapeHitbox we can not to perform such check.
  • Loading branch information
ASGAlex authored Oct 1, 2022
1 parent 2ed1403 commit f840210
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,16 @@ mixin HasQuadTreeCollisionDetection on FlameGame

bool onComponentTypeCheck(PositionComponent one, PositionComponent another) {
var checkParent = false;
if (one is CollisionCallbacks) {
if (!(one as CollisionCallbacks).onComponentTypeCheck(another)) {
if (one is GenericCollisionCallbacks) {
if (!(one as GenericCollisionCallbacks).onComponentTypeCheck(another)) {
return false;
}
} else {
checkParent = true;
}

if (another is CollisionCallbacks) {
if (!(another as CollisionCallbacks).onComponentTypeCheck(one)) {
if (another is GenericCollisionCallbacks) {
if (!(another as GenericCollisionCallbacks).onComponentTypeCheck(one)) {
return false;
}
} else {
Expand Down
14 changes: 9 additions & 5 deletions packages/flame/lib/src/collisions/collision_callbacks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ mixin GenericCollisionCallbacks<T> {
onCollisionEndCallback?.call(other);
}

/// Works only for the QuadTree collision detection.
/// If you need to prevent collision of items of different types -
/// reimplement [onComponentTypeCheck]. The result of calculation is cached
/// so you should not check any dynamical parameters here, the function
/// intended to be used as pure type checker.
@mustCallSuper
bool onComponentTypeCheck(PositionComponent other);

/// Assign your own [CollisionCallback] if you want a callback when this
/// shape collides with another [T].
CollisionCallback<T>? onCollisionCallback;
Expand Down Expand Up @@ -127,11 +135,7 @@ mixin CollisionCallbacks on Component
onCollisionEndCallback?.call(other);
}

/// Works only for the QuadTree collision detection.
/// If you need to prevent collision of items of different types -
/// reimplement [onComponentTypeCheck]. The result of calculation is cached
/// so you should not check any dynamical parameters here, the function
/// intended to be used as pure type checker.
@override
@mustCallSuper
bool onComponentTypeCheck(PositionComponent other) {
final myParent = parent;
Expand Down
12 changes: 12 additions & 0 deletions packages/flame/lib/src/collisions/hitboxes/shape_hitbox.dart
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,18 @@ mixin ShapeHitbox on ShapeComponent implements Hitbox<ShapeHitbox> {
}
}

@override
@mustCallSuper
bool onComponentTypeCheck(PositionComponent other) {
final myParent = parent;
final otherParent = other.parent;
if (myParent is CollisionCallbacks && otherParent is PositionComponent) {
return myParent.onComponentTypeCheck(otherParent);
}

return true;
}

@override
CollisionCallback<ShapeHitbox>? onCollisionCallback;

Expand Down

0 comments on commit f840210

Please sign in to comment.