Skip to content

Apply Observer pattern to handle collision#1

Merged
AmberPotion merged 3 commits intothoth-tech:mainfrom
lexlam1524:main
Oct 1, 2024
Merged

Apply Observer pattern to handle collision#1
AmberPotion merged 3 commits intothoth-tech:mainfrom
lexlam1524:main

Conversation

@lexlam1524
Copy link

@lexlam1524 lexlam1524 commented Sep 15, 2024

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:

  1. call the handle_collision(player, obstacle);. // the player is subject , obstacle is observer
  2. check is there any collision by calling is_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

skm g++ program.cpp player.cpp obstacle.cpp -o game.exe

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

@AmberPotion AmberPotion merged commit 24fb3f2 into thoth-tech:main Oct 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants