Apply Observer pattern to handle collision#1
Merged
AmberPotion merged 3 commits intothoth-tech:mainfrom Oct 1, 2024
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
implementation of Observer pattern to handle the collision between bee and box.
the Observer pattern is used to handle the interaction between one and many relations, in this case, subject (bee) is one, and observer (box) is many.
using the Observer pattern, when there is a collision , subject (bee) will send the notify to observer (box)
code:
notify(Observer* observer, bool is_collision)then observer will update their/ his status, in this case, it is is_collision, also it can perform some action according to the status, in my code, it is the debug message to indicate there is a collision.
code:
CollisionUpdate(bool is_collision)using the Observer pattern, we can handle the collision between bee and different type of object (not only the box) by calling the function.
code:
handle_collision(T& subject, U& observer)here is the flow of handling the collision:
handle_collision(player, obstacle);. // the player is subject , obstacle is observeris_colliding(subject, observer)a) if there is collision, subject send notify to observer to update collision status to True
subject.notify(&observer,true);b) if the collision end, subject send notify to observer to update the collision status to False
subject.notify(&observer,false);Type of change
code
How Has This Been Tested?
Compiled and ran with
###Checklist
My code follows the style guidelines of this project
I have performed a self-review of my own code
I have commented my code in hard-to-understand areas
I have made corresponding changes to the documentation
My changes generate no new warnings
I have requested a review from team members on the Pull Request