From 33ac8ab1b662751862c23709a8245046184e4cea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Reinert?= Date: Thu, 7 Mar 2024 16:18:06 +0100 Subject: [PATCH] publisher / dispatcher pattern --- src/Event/FightStartingEvent.php | 16 +++++++++++++ src/Event/OutputFightStartingSubscriber.php | 26 +++++++++++++++++++++ src/Game.php | 5 ++++ 3 files changed, 47 insertions(+) create mode 100644 src/Event/FightStartingEvent.php create mode 100644 src/Event/OutputFightStartingSubscriber.php diff --git a/src/Event/FightStartingEvent.php b/src/Event/FightStartingEvent.php new file mode 100644 index 0000000..10fa78e --- /dev/null +++ b/src/Event/FightStartingEvent.php @@ -0,0 +1,16 @@ + 'onFightStart', + ]; + } + + public function onFightStart(FightStartingEvent $event): void + { + $io = new SymfonyStyle(new ArrayInput([]), new ConsoleOutput()); + $io->note('Fight is starting against: ' . $event->ai->getNickname()); + } +} diff --git a/src/Game.php b/src/Game.php index 43345c7..fdac92f 100644 --- a/src/Game.php +++ b/src/Game.php @@ -13,8 +13,10 @@ use App\Builder\CharacterBuilderFactory; use App\Character\Character; use App\Character\CharacterType; +use App\Event\FightStartingEvent; use App\Observer\CanObserverFight; use Random\RandomException; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; class Game { @@ -23,11 +25,14 @@ class Game public function __construct( private CharacterBuilderFactory $characterBuilderFactory, + private EventDispatcherInterface $eventDispatcher, ) { } public function play(Character $player, Character $enemy): Fight { + $this->eventDispatcher->dispatch(new FightStartingEvent($player, $enemy)); + $player->rest(); $fight = new Fight();