parent
17acf0a45a
commit
845835caa0
6 changed files with 43 additions and 31 deletions
@ -0,0 +1,13 @@ |
||||
#include "Listener.h" |
||||
|
||||
void GameListener::accept(GameEvent *event) { |
||||
if (auto coinEvent = dynamic_cast<CoinCollectEvent *>(event)) |
||||
OnCoinCollect(coinEvent); |
||||
|
||||
if (auto dmgEvent = dynamic_cast<DamageEvent *>(event)) |
||||
OnDamage(dmgEvent); |
||||
} |
||||
|
||||
void InputListener::accept(InputEvent *event) { |
||||
OnMouseClicked(); |
||||
} |
@ -1,17 +1,24 @@ |
||||
#pragma once |
||||
|
||||
#include "Event.h" |
||||
|
||||
template <typename EventType> |
||||
class Listener { |
||||
protected: |
||||
template <typename T> |
||||
bool isType(EventType * event) { |
||||
return cast<T>(event); |
||||
} |
||||
template <typename T> |
||||
T * cast(EventType * event) { |
||||
return dynamic_cast<T *>(event); |
||||
} |
||||
public: |
||||
virtual bool wantsEvent(EventType *) { return true; } |
||||
virtual void accept(EventType *) {} |
||||
}; |
||||
|
||||
class InputListener : public Listener<InputEvent> { |
||||
public: |
||||
void accept(InputEvent * event) override; |
||||
protected: |
||||
virtual void OnMouseClicked() {}; |
||||
}; |
||||
|
||||
class GameListener : public Listener<GameEvent> { |
||||
public: |
||||
void accept(GameEvent * event) override; |
||||
protected: |
||||
virtual void OnCoinCollect(CoinCollectEvent * event) {}; |
||||
virtual void OnDamage(DamageEvent * event) {}; |
||||
}; |
Loading…
Reference in new issue