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

feat: onComponentTypeCheck support for ShapeHitbox #1981

Merged
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
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