Added IF Event Parsing

This commit is contained in:
2023-04-25 23:06:23 -07:00
parent ce2ac8ba1c
commit 60d4b53a33
10 changed files with 127 additions and 16 deletions

View File

@ -43,7 +43,6 @@
<!-- Here I am creating a marker -->
<marker name="craigCool" />
<choices character="eth" key="isCraigCool">
<title>
<string lang="en">Do you think Craig is cool?</string>
@ -67,7 +66,7 @@
If the user selects "Maybe", we basically just display a message and then
go back to the choices indefinitely.
-->
<!-- <if key="isCraigCool" value="maybe">
<if key="isCraigCool" value="maybe">
<text character="eth">
<string lang="en">Are you unsure?</string>
</text>
@ -83,7 +82,11 @@
<text character="eth">
<string lang="en">I disagree, Craig is cool.</string>
</text>
</if> -->
</if>
<text character="eth">
<string lang="en">bruh9000</string>
</text>
<!-- We can also modify values/set values too. -->
<choice-set key="isCraigCool" value="yes" />

View File

@ -10,7 +10,6 @@ namespace Dawn {
class VNDummyEvent : public VNEvent {
protected:
void onStart() override {
std::cout << "Dummy VN Event" << std::endl;
this->next();
}
};

View File

@ -34,7 +34,7 @@ void VNEvent::next() {
this->end();
auto next = this->getNextEvent();
this->manager->currentEvent = next;
this->parent->currentEvent = next;
if(next != nullptr) next->start(this->parent, this);
}

View File

@ -7,22 +7,29 @@
#include "VNEvent.hpp"
namespace Dawn {
class VNIfEvent : public VNEvent {
class VNIfEvent :
public VNEvent,
public IVNEventParent
{
public:
std::string key;
std::string value;
VNEvent *ifTrue = nullptr;
VNEvent * getNextEvent() override {
if(this->manager.getFlag(key) == value) {
return ifTrue;
}
return VNEvent::getNextEvent();
}
VNEvent *ifEnd = nullptr;
protected:
void onStart() override {
this->next();
assertNotNull(ifTrue);
assertNotNull(ifEnd);
if(this->manager->getFlag(key) == value) {
useEvent([&]{
if(ifEnd->getNextEvent() == nullptr) this->next();
}, ifEnd->eventFinished);
ifTrue->start(this, nullptr);
} else {
this->next();
}
}
}
};
}

View File

@ -103,6 +103,40 @@ void VNSceneGen::test(
line(&afterLines, eventName + "->value = \"" + event->choiceSet.value + "\";", "");
break;
case VN_SCENE_EVENT_TYPE_IF: {
initType = "VNIfEvent";
toInclude = "games/vn/events/VNIfEvent.hpp";
line(&afterLines, eventName + "->key = \"" + event->ifEvent.key + "\";", "");
line(&afterLines, eventName + "->value = \"" + event->ifEvent.value + "\";", "");
std::string ifPrevious = "";
std::string ifFirst = "";
auto itIf = event->ifEvent.events.events.begin();
while(itIf != event->ifEvent.events.events.end()) {
std::string ifEventName = "ifEvent" + std::to_string((*eventIndex)++);
VNSceneGen::test(
ifEventName,
&(*itIf),
eventIndex,
&afterLines,
includes
);
if(!ifPrevious.empty()) line(&afterLines, ifPrevious + "->then(" + ifEventName + ");", "");
ifPrevious = ifEventName;
if(ifFirst == "") ifFirst = ifEventName;
++itIf;
}
if(ifFirst == "" || ifPrevious == "") {
std::cout << "If event must have at least one event" << std::endl;
assertUnreachable();
}
line(&afterLines, eventName + "->ifTrue = " + ifFirst + ";", "");
line(&afterLines, eventName + "->ifEnd = " + ifPrevious + ";", "");
break;
}
default:
std::cout << "Unknown event type: " << event->type << std::endl;
assertUnreachable();

View File

@ -16,4 +16,5 @@ target_sources(vnscenetool
VNGoToMarkerEventParser.cpp
VNChoiceEventParser.cpp
VNChoiceSetEventParser.cpp
VNIfEventParser.cpp
)

View File

@ -0,0 +1,30 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "VNIfEventParser.hpp"
#include "VNSceneEventsParser.hpp"
using namespace Dawn;
std::vector<std::string> VNIfEventParser::getRequiredAttributes() {
return { "key", "value" };
}
std::map<std::string, std::string> VNIfEventParser::getOptionalAttributes() {
return {};
}
int32_t VNIfEventParser::onParse(
Xml *node,
std::map<std::string, std::string> values,
struct VNIfEvent *out,
std::string *error
) {
//Get the key and value
out->key = values["key"];
out->value = values["value"];
return (VNSceneEventsParser()).parse(node, &out->events, error);
}

View File

@ -0,0 +1,23 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "util/XmlParser.hpp"
namespace Dawn {
struct VNIfEvent;
class VNIfEventParser : public XmlParser<struct VNIfEvent> {
protected:
std::vector<std::string> getRequiredAttributes() override;
std::map<std::string, std::string> getOptionalAttributes() override;
int32_t onParse(
Xml *node,
std::map<std::string, std::string> values,
struct VNIfEvent *out,
std::string *error
) override;
};
}

View File

@ -75,6 +75,11 @@ int32_t VNSceneEventsParser::onParse(
ret = (VNChoiceSetEventParser()).parse(child, &event.choiceSet, error);
if(ret != 0) return ret;
} else if(child->node == "if") {
event.type = VN_SCENE_EVENT_TYPE_IF;
ret = (VNIfEventParser()).parse(child, &event.ifEvent, error);
if(ret != 0) return ret;
} else {
*error = "Unknown child node '" + child->node + "'";
return -1;

View File

@ -13,6 +13,7 @@
#include "VNGoToMarkerEventParser.hpp"
#include "VNChoiceEventParser.hpp"
#include "VNChoiceSetEventParser.hpp"
#include "VNIfEventParser.hpp"
namespace Dawn {
struct VNSceneEvent;
@ -30,12 +31,19 @@ namespace Dawn {
VN_SCENE_EVENT_TYPE_MARKER,
VN_SCENE_EVENT_TYPE_GOTO_MARKER,
VN_SCENE_EVENT_TYPE_CHOICES,
VN_SCENE_EVENT_TYPE_CHOICE_SET
VN_SCENE_EVENT_TYPE_CHOICE_SET,
VN_SCENE_EVENT_TYPE_IF
};
struct VNParallelEvent {
struct VNSceneEventList events;
};
struct VNIfEvent {
std::string key;
std::string value;
struct VNSceneEventList events;
};
struct VNSceneEvent {
enum VNSceneEventType type;
@ -49,6 +57,7 @@ namespace Dawn {
struct VNGoToMarkerEvent gotoMarker;
struct VNChoiceEvent choices;
struct VNChoiceSetEvent choiceSet;
struct VNIfEvent ifEvent;
};
class VNSceneEventsParser : public XmlParser<struct VNSceneEventList> {