-
-
Notifications
You must be signed in to change notification settings - Fork 945
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: Add collision completed listener #2896
feat: Add collision completed listener #2896
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some comments, and missing tests
@@ -161,3 +166,24 @@ abstract class CollisionDetection<T extends Hitbox<T>, | |||
List<RaycastResult<T>>? out, | |||
}); | |||
} | |||
|
|||
/// Add a basic class which implements a Listener. | |||
class CollisionDetectionCompletionNotifier implements Listenable { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
class CollisionDetectionCompletionNotifier implements Listenable { | |
class CollisionDetectionCompletionNotifier with ChangeNotifier { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to manually implement Listenable
, check https://api.flutter.dev/flutter/foundation/ChangeNotifier-class.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the suggestion to implement Listenable
came from you, to avoid the warning about unnecessary overrides. Happy to do whichever you think best.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, now I remember. 🤦♂️
I think it is probably better to go with with ChangeNotifier
anyways, so that we don't have to do the re-implementation, and then we can just put an ignore lint lint above the overridden method.
comment change Co-authored-by: Lukas Klingsbo <[email protected]>
I appreciate the need for tests, but the testing framework is very confusing to me. @spydon would you be able to suggest a change for this? |
Yeah, the collision detection tests can be a bit confusing, this one can be fairly simple though, something like this: group('collisionsCompletedNotifier', {
runCollisionTestRegistry({
'collisionsCompletedNotifier calls listeners': (game) async {
var calledTimes = 0;
final listeners = List.generate(10, (_) => () => calledTimes++);
for(final listener in listeners) {
game.collisionDetection.collisionsCompletedNotifier.addListener(listener);
}
game.update(0);
expect(calledTimes, listeners.length);
},
});
}); and then just adding that in the end of this file: |
Thanks. Done that and managed to get the test to run, but fails: |
@christian-mindfulness did you add the |
Thanks the one, thank you. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few final comments
doc/flame/collision_detection.md
Outdated
(game as HasCollisionDetection).collisionDetection.collisionsCompletedNotifier. | ||
addListener(() { | ||
resolveCollisions(); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Formatting
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have to break the line (as it's too long otherwise). I've now done it in a way that looks nice to me, happy to hear your thoughts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you put it in an IDE and or https://dartpad.dev it will correctly format it for you, since it follows 80 width by default
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couldn't get VScode, dartpad or tutorialspoint to do it automatically for me. :-(
Thanks @spydon for the review. I think my latest commit addresses all the issues. Let me know if I've missed anything. I've also simplified the extension of |
You have to override |
Curious that it worked for me - I've changed back. |
It works, but you get an analyzer warning. That was why the pipeline didn't pass. |
doc/flame/collision_detection.md
Outdated
(game as HasCollisionDetection).collisionDetection | ||
.collisionsCompletedNotifier | ||
.addListener(() { | ||
resolveCollisions(); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(game as HasCollisionDetection).collisionDetection | |
.collisionsCompletedNotifier | |
.addListener(() { | |
resolveCollisions(); | |
}); | |
(game as HasCollisionDetection) | |
.collisionDetection | |
.collisionsCompletedNotifier | |
.addListener(() { | |
resolveCollisions(); | |
}); |
This is the correct formatting
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your contribution! :)
Description
Add a new variable to the
CollisionDetection
class. CallnotifyListeners
at the end of the collision detection step, so that users can add a listener to their code to know when this step has finished. The variable is a basic implementation of aListenable
class, since it needs no more complexity.Checklist
docs
and added dartdoc comments with///
.examples
ordocs
.Breaking Change?
Related Issues
Closes #2849