From 6bc6917b0c2191bf9fd82bf0162bb56720edd5e4 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Sun, 23 Apr 2023 21:43:17 -0700 Subject: [PATCH] Parallel event --- src/dawn/games/vn/components/VNManager.cpp | 10 +---- src/dawn/games/vn/components/VNManager.hpp | 16 ++++---- src/dawn/games/vn/events/VNEvent.cpp | 18 +++++++-- src/dawn/games/vn/events/VNEvent.hpp | 23 +++++++++--- src/dawn/games/vn/events/VNParallelEvent.hpp | 39 ++++++++++++++++++++ src/dawn/games/vn/events/VNWaitEvent.hpp | 4 +- src/dawnliminal/game/LiminalGame.cpp | 4 +- src/dawnliminal/scenes/HelloWorldScene.hpp | 17 +++++++-- 8 files changed, 98 insertions(+), 33 deletions(-) create mode 100644 src/dawn/games/vn/events/VNParallelEvent.hpp diff --git a/src/dawn/games/vn/components/VNManager.cpp b/src/dawn/games/vn/components/VNManager.cpp index 169cd0f5..8aacf0a9 100644 --- a/src/dawn/games/vn/components/VNManager.cpp +++ b/src/dawn/games/vn/components/VNManager.cpp @@ -15,7 +15,7 @@ VNManager::VNManager(SceneItem *item) : void VNManager::onStart() { if(this->currentEvent != nullptr) { - this->currentEvent->start(nullptr); + this->currentEvent->start(this, nullptr); } } @@ -32,14 +32,6 @@ std::string VNManager::getFlag(std::string key) { return this->flags[key]; } -void VNManager::nextEvent() { - if(this->currentEvent == nullptr) return; - auto old = this->currentEvent; - old->end(); - this->currentEvent = old->getNextEvent(); - if(this->currentEvent != nullptr) this->currentEvent->start(old); -} - void VNManager::onDispose() { } \ No newline at end of file diff --git a/src/dawn/games/vn/components/VNManager.hpp b/src/dawn/games/vn/components/VNManager.hpp index 0485fea8..b000bb1d 100644 --- a/src/dawn/games/vn/components/VNManager.hpp +++ b/src/dawn/games/vn/components/VNManager.hpp @@ -9,10 +9,17 @@ namespace Dawn { class VNEvent; - class VNManager : public SceneItemComponent { + class IVNEventParent { + public: + VNEvent *currentEvent = nullptr; + }; + + class VNManager : + public SceneItemComponent, + public IVNEventParent + { protected: std::vector events; - VNEvent *currentEvent = nullptr; std::map flags; public: @@ -42,11 +49,6 @@ namespace Dawn { */ void setEvent(VNEvent *event); - /** - * Ends the current event, and moves to the next one. - */ - void nextEvent(); - /** * Sets a flag for the visual novel. * diff --git a/src/dawn/games/vn/events/VNEvent.cpp b/src/dawn/games/vn/events/VNEvent.cpp index 9212b795..a524be8f 100644 --- a/src/dawn/games/vn/events/VNEvent.cpp +++ b/src/dawn/games/vn/events/VNEvent.cpp @@ -11,7 +11,12 @@ void VNEvent::init(VNManager *manager) { this->manager = manager; } -void VNEvent::start(VNEvent *previous) { +void VNEvent::start( + IVNEventParent *parent, + VNEvent *previous +) { + this->parent = parent; + finished = false; this->onStart(); } @@ -25,13 +30,20 @@ VNEvent * VNEvent::getNextEvent() { void VNEvent::next() { assertNotNull(this->manager); - assertTrue(this->manager->currentEvent == this); - this->manager->nextEvent(); + assertNotNull(this->parent); + + this->end(); + auto next = this->getNextEvent(); + this->manager->currentEvent = next; + if(next != nullptr) next->start(this->parent, this); } void VNEvent::end() { + this->finished = true; this->unsubscribeAllEvents(); this->onEnd(); + + this->eventFinished.invoke(); } void VNEvent::onStart() {} diff --git a/src/dawn/games/vn/events/VNEvent.hpp b/src/dawn/games/vn/events/VNEvent.hpp index 50840513..4ced5e1c 100644 --- a/src/dawn/games/vn/events/VNEvent.hpp +++ b/src/dawn/games/vn/events/VNEvent.hpp @@ -9,24 +9,35 @@ namespace Dawn { class VNEvent : public StateOwner { + public: + StateEvent<> eventFinished; + + /** + * Invoked by a parent VN Manager. This is the start of the event. + * + * @param parent The VN Event Parent. Usually the manager but not always. + * @param previous The previous event that was running before this one. + */ + void start( + IVNEventParent *parent, + VNEvent *previous + ); + protected: VNManager *manager = nullptr; + IVNEventParent *parent = nullptr; VNEvent *doNext = nullptr; VNEvent *previous = nullptr; + bool_t finished = false; /** * Initializes the event. This is called by the VNManager, and should not * be called by anything else. + * * @param manager The VNManager that is running this event. */ void init(VNManager *manager); - /** - * Invoked by the VNManager, this is the start of the event. - * @param previous The previous event that was running before this one. - */ - void start(VNEvent *previous); - /** * Invoked by the VNManager, this is the end of the event. Perform the * necessary cleanup, but remember that events may be re-started again diff --git a/src/dawn/games/vn/events/VNParallelEvent.hpp b/src/dawn/games/vn/events/VNParallelEvent.hpp new file mode 100644 index 00000000..f751fddd --- /dev/null +++ b/src/dawn/games/vn/events/VNParallelEvent.hpp @@ -0,0 +1,39 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "VNEvent.hpp" + +namespace Dawn { + class VNParallelEvent : + public VNEvent, + public IVNEventParent + { + public: + std::vector events; + + protected: + int32_t eventCount; + int32_t eventCompleteCount; + + void onStart() override { + eventCount = 0; + eventCompleteCount = 0; + + auto itEvents = this->events.begin(); + while(itEvents != this->events.end()) { + auto event = *itEvents; + event->start(this, this); + eventCount++; + + useEvent([&]{ + eventCompleteCount++; + if(eventCompleteCount >= eventCount) this->next(); + }, event->eventFinished); + itEvents++; + } + } + }; +} \ No newline at end of file diff --git a/src/dawn/games/vn/events/VNWaitEvent.hpp b/src/dawn/games/vn/events/VNWaitEvent.hpp index e03f2302..a0233574 100644 --- a/src/dawn/games/vn/events/VNWaitEvent.hpp +++ b/src/dawn/games/vn/events/VNWaitEvent.hpp @@ -8,10 +8,8 @@ namespace Dawn { class VNWaitEvent : public VNAnimateEvent { - public: - protected: - void setValue(T value) override { + void setValue(float_t value) override { // Do nothing } }; diff --git a/src/dawnliminal/game/LiminalGame.cpp b/src/dawnliminal/game/LiminalGame.cpp index 7bec3cab..4c28a4cb 100644 --- a/src/dawnliminal/game/LiminalGame.cpp +++ b/src/dawnliminal/game/LiminalGame.cpp @@ -10,6 +10,6 @@ using namespace Dawn; Scene * Dawn::dawnGameGetInitialScene(DawnGame *game) { - // return new HelloWorldScene(game); - return new TestScene(game); + return new HelloWorldScene(game); + // return new TestScene(game); } \ No newline at end of file diff --git a/src/dawnliminal/scenes/HelloWorldScene.hpp b/src/dawnliminal/scenes/HelloWorldScene.hpp index 14359484..6daa3b5e 100644 --- a/src/dawnliminal/scenes/HelloWorldScene.hpp +++ b/src/dawnliminal/scenes/HelloWorldScene.hpp @@ -13,6 +13,8 @@ #include "games/vn/events/VNPositionEvent.hpp" #include "games/vn/events/VNSetEvent.hpp" #include "games/vn/events/VNChoiceEvent.hpp" +#include "games/vn/events/VNParallelEvent.hpp" +#include "games/vn/events/VNWaitEvent.hpp" namespace Dawn { class HelloWorldScene : public Scene { @@ -55,12 +57,21 @@ namespace Dawn { choiceEvent->choices["state2"] = "State 2"; choiceEvent->choices["state3"] = "State 3"; + auto parallelEvent = vnManager->createEvent(); + auto wait0 = vnManager->createEvent(); + wait0->duration = 1.0f; + parallelEvent->events.push_back(wait0); + + auto wait1 = vnManager->createEvent(); + wait1->duration = 3.0f; + parallelEvent->events.push_back(wait1); eventTest + ->then(parallelEvent) ->then(positionEvent) - ->then(vnTextEvent) - ->then(setPropertyEvent) - ->then(choiceEvent) + // ->then(vnTextEvent) + // ->then(setPropertyEvent) + // ->then(choiceEvent) ; vnManager->setEvent(eventTest); }