202 lines
7.3 KiB
C++
202 lines
7.3 KiB
C++
// Copyright (c) 2023 Dominic Masters
|
|
//
|
|
// This software is released under the MIT License.
|
|
// https://opensource.org/licenses/MIT
|
|
|
|
#include "VNSceneGen.hpp"
|
|
|
|
using namespace Dawn;
|
|
|
|
void VNSceneGen::test(
|
|
std::string eventName,
|
|
struct VNSceneEvent *event,
|
|
int32_t *eventIndex,
|
|
std::vector<std::string> *body,
|
|
std::vector<std::string> *includes
|
|
) {
|
|
std::string initType = "";
|
|
std::string toInclude = "";
|
|
std::string initArgs = "";
|
|
std::vector<std::string> afterLines;
|
|
|
|
switch(event->type) {
|
|
case VN_SCENE_EVENT_TYPE_TEXT:
|
|
initType = "VNTextEvent";
|
|
toInclude = "games/vn/events/VNTextEvent.hpp";
|
|
line(&afterLines, eventName + "->" + "text = \"" + event->text.texts.begin()->text + "\";", "");
|
|
break;
|
|
|
|
case VN_SCENE_EVENT_TYPE_POSITION:
|
|
initType = "VNPositionEvent";
|
|
toInclude = "games/vn/events/VNPositionEvent.hpp";
|
|
line(&afterLines, eventName + "->item = " + event->position.item + ";", "");
|
|
if(event->position.x != "") line(&afterLines, eventName + "->" + "to.x = " + event->position.x + ";", "");
|
|
if(event->position.y != "") line(&afterLines, eventName + "->" + "to.y = " + event->position.y + ";", "");
|
|
if(event->position.z != "") line(&afterLines, eventName + "->" + "to.z = " + event->position.z + ";", "");
|
|
break;
|
|
|
|
case VN_SCENE_EVENT_TYPE_SET:
|
|
initType = "VNSetEvent<" + event->set.type + ">";
|
|
toInclude = "games/vn/events/VNSetEvent.hpp";
|
|
line(&afterLines, eventName + "->modifies = &" + event->set.property + ";", "");
|
|
line(&afterLines, eventName + "->to = " + event->set.to + ";", "");
|
|
if(event->set.from != "") line(&afterLines, eventName + "->from = " + event->set.from + ";", "");
|
|
if(event->set.duration != "") line(&afterLines, eventName + "->duration = " + event->set.duration + ";", "");
|
|
break;
|
|
|
|
case VN_SCENE_EVENT_TYPE_WAIT:
|
|
initType = "VNWaitEvent";
|
|
toInclude = "games/vn/events/VNWaitEvent.hpp";
|
|
line(&afterLines, eventName + "->duration = " + event->wait.duration + ";", "");
|
|
break;
|
|
|
|
case VN_SCENE_EVENT_TYPE_PARALLEL: {
|
|
initType = "VNParallelEvent";
|
|
toInclude = "games/vn/events/VNParallelEvent.hpp";
|
|
|
|
auto itParallel = event->parallel.events.events.begin();
|
|
while(itParallel != event->parallel.events.events.end()) {
|
|
std::string pEventName = "pEvent" + std::to_string((*eventIndex)++);
|
|
VNSceneGen::test(
|
|
pEventName,
|
|
&(*itParallel),
|
|
eventIndex,
|
|
&afterLines,
|
|
includes
|
|
);
|
|
line(&afterLines, eventName + "->events.push_back(" + pEventName + ");", "");
|
|
line(&afterLines, "", "");
|
|
++itParallel;
|
|
}
|
|
break;
|
|
}
|
|
|
|
case VN_SCENE_EVENT_TYPE_MARKER:
|
|
initType = "VNDummyEvent";
|
|
toInclude = "games/vn/events/VNDummyEvent.hpp";
|
|
line(&afterLines, "auto marker_" + event->marker.name + " = " + eventName + ";", "");
|
|
break;
|
|
|
|
case VN_SCENE_EVENT_TYPE_GOTO_MARKER:
|
|
initType = "VNDummyEvent";
|
|
toInclude = "games/vn/events/VNDummyEvent.hpp";
|
|
line(&afterLines, eventName + "->then(marker_" + event->gotoMarker.name + ");", "");
|
|
break;
|
|
|
|
case VN_SCENE_EVENT_TYPE_CHOICES: {
|
|
initType = "VNChoiceEvent";
|
|
toInclude = "games/vn/events/VNChoiceEvent.hpp";
|
|
line(&afterLines, eventName + "->key = \"" + event->choices.key+ "\";", "");
|
|
line(&afterLines, eventName + "->text = \"" + event->choices.titles.begin()->text + "\";", "");
|
|
auto itChoices = event->choices.choices.begin();
|
|
while(itChoices != event->choices.choices.end()) {
|
|
line(&afterLines, eventName + "->choices[\"" + itChoices->value + "\"] = \"" + itChoices->texts.begin()->text + "\";", "");
|
|
++itChoices;
|
|
}
|
|
break;
|
|
}
|
|
|
|
case VN_SCENE_EVENT_TYPE_CHOICE_SET:
|
|
initType = "VNChoiceSetEvent";
|
|
toInclude = "games/vn/events/VNChoiceSetEvent.hpp";
|
|
line(&afterLines, eventName + "->key = \"" + event->choiceSet.key + "\";", "");
|
|
line(&afterLines, eventName + "->value = \"" + event->choiceSet.value + "\";", "");
|
|
break;
|
|
|
|
default:
|
|
std::cout << "Unknown event type: " << event->type << std::endl;
|
|
assertUnreachable();
|
|
}
|
|
|
|
if(!toInclude.empty()) includes->push_back(toInclude);
|
|
|
|
line(body, "auto " + eventName + " = vnManager->createEvent<" + initType + ">(" + initArgs + ");", "");
|
|
lines(body, afterLines, "");
|
|
}
|
|
|
|
void VNSceneGen::generate(
|
|
std::vector<std::string> *out,
|
|
struct VNScene *scene,
|
|
std::string tabs
|
|
) {
|
|
struct ClassGenInfo classInfo;
|
|
classInfo.clazz = "TestScene";
|
|
classInfo.extend = "Scene";
|
|
classInfo.constructorArgs = "DawnGame *game";
|
|
classInfo.extendArgs = "game";
|
|
|
|
classInfo.includes.push_back("scene/Scene.hpp");
|
|
|
|
struct MethodGenInfo methodAssets;
|
|
methodAssets.name = "getRequiredAssets";
|
|
methodAssets.isOverride = true;
|
|
methodAssets.type = "std::vector<Asset*>";
|
|
line(&methodAssets.body, "auto assMan = &this->game->assetManager;", "");
|
|
line(&methodAssets.body, "std::vector<Asset*> assets;", "");
|
|
line(&methodAssets.body, "return assets;", "");
|
|
|
|
struct MethodGenInfo methodStage;
|
|
methodStage.name = "stage";
|
|
methodStage.isOverride = "true";
|
|
line(&methodStage.body, "// test", "");
|
|
|
|
// TEST
|
|
line(&methodStage.body, "auto canvas = UICanvas::create(this);", "");
|
|
line(&methodStage.body, "", "");
|
|
|
|
classInfo.includes.push_back("scene/components/display/Camera.hpp");
|
|
line(&methodStage.body, "auto camera = Camera::create(this);", "");
|
|
line(&methodStage.body, "camera->fov = 0.436332f;", "");
|
|
line(&methodStage.body, "camera->transform->lookAt(glm::vec3(10, 10, 10), glm::vec3(0, 0, 0));", "");
|
|
|
|
// Items
|
|
int32_t itemRefIndex = 0;
|
|
auto itItems = scene->items.begin();
|
|
while(itItems != scene->items.end()) {
|
|
struct VNSceneItem item = *itItems;
|
|
if(item.ref.empty()) item.ref = "item" + std::to_string(itemRefIndex++);
|
|
classInfo.includes.push_back(item.prefab + ".hpp");
|
|
|
|
line(&methodStage.body, "", "");
|
|
line(&methodStage.body, "auto " + item.ref + " = " + item.className + "::create(this);", "");
|
|
|
|
++itItems;
|
|
}
|
|
|
|
line(&methodStage.body, "", "");
|
|
|
|
// Events
|
|
classInfo.includes.push_back("games/vn/components/VNManager.hpp");
|
|
line(&methodStage.body, "auto vnItem = this->createSceneItem();", "");
|
|
line(&methodStage.body, "auto vnManager = vnItem->addComponent<VNManager>();", "");
|
|
line(&methodStage.body, "VNEvent *previous = vnManager->createEvent<VNDummyEvent>();", "");
|
|
line(&methodStage.body, "auto eventStart = previous;", "");
|
|
|
|
int32_t eventIndex = 0;
|
|
auto itEvents = scene->events.events.begin();
|
|
std::string previous = "eventStart";
|
|
while(itEvents != scene->events.events.end()) {
|
|
line(&methodStage.body, "", "");
|
|
std::string eventName = "event" + std::to_string(eventIndex++);
|
|
VNSceneGen::test(
|
|
eventName,
|
|
&(*itEvents),
|
|
&eventIndex,
|
|
&methodStage.body,
|
|
&classInfo.includes
|
|
);
|
|
line(&methodStage.body, previous + "->then(" + eventName + ");", "");
|
|
previous = eventName;
|
|
++itEvents;
|
|
}
|
|
|
|
line(&methodStage.body, "", "");
|
|
line(&methodStage.body, "vnManager->setEvent(eventStart);", "");
|
|
|
|
// Add in methods
|
|
CodeGen::methodGen(&classInfo.protectedCode, methodAssets);
|
|
line(&classInfo.protectedCode, "", "");
|
|
CodeGen::methodGen(&classInfo.protectedCode, methodStage);
|
|
|
|
CodeGen::classGen(out, classInfo);
|
|
} |