You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

222 lines
8.7 KiB
C++

#include "qw_eventfactory.h"
#include "qw_levelbuilder.h"
#include "qw_widgetdialoguemanager.h"
#include "qw_textdialoguemanager.h"
QWEventFactory::QWEventFactory(QWLevelBuilder *b) :
builder(b)
{}
std::shared_ptr<QWDeleteFromInventoryEvent> QWEventFactory::createDeleteItEvent(const QJsonObject &json_object)
{
std::unique_ptr<QWDeleteFromInventoryEvent> new_event;
qDebug() << " Found QWDeleteFromInventoryEvent. " << json_object["id"].toString() << ".";
Q_ASSERT(json_object.contains("target"));
Q_ASSERT(builder->hash_triggers.contains(json_object["target"].toString()));
new_event = std::make_unique<QWDeleteFromInventoryEvent>(builder->hash_triggers[json_object["target"].toString()]);
new_event->setInventoryManager(builder->ptr_inventory);
return std::shared_ptr<QWDeleteFromInventoryEvent>{std::move(new_event)};
}
std::shared_ptr<QWChangeLocationEvent> QWEventFactory::createChangeLocEvent(const QJsonObject &json_object)
{
std::unique_ptr<QWChangeLocationEvent> new_event;
qDebug() << " Found QWChangeLocationEvent. " << json_object["id"].toString() << ".";
Q_ASSERT(json_object.contains("location"));
Q_ASSERT(builder->hash_locations.contains(json_object["location"].toString()));
new_event = std::make_unique<QWChangeLocationEvent>(builder->hash_locations[json_object["location"].toString()]);
new_event->setScene(builder->ptr_scene);
return std::shared_ptr<QWChangeLocationEvent>{std::move(new_event)};
}
std::shared_ptr<QWChangeTriggerPropertiesEvent> QWEventFactory::createChangeTrProperts(const QJsonObject &json_object)
{
std::unique_ptr<QWChangeTriggerPropertiesEvent> new_event;
qDebug() << " Found QWChangeTriggerPropertiesEvent. " << json_object["id"].toString() << ".";
Q_ASSERT(json_object.contains("target"));
Q_ASSERT(builder->hash_triggers.contains(json_object["target"].toString()));
new_event = std::make_unique<QWChangeTriggerPropertiesEvent>(builder->hash_triggers[json_object["target"].toString()]);
if (json_object.contains("x") && json_object.contains("y"))
new_event->setParams(json_object["x"].toDouble(), json_object["y"].toDouble(),
json_object.contains("path") ? json_object["path"].toString() : "$no_pic");
else
new_event->setParams(json_object.contains("path") ? json_object["path"].toString() : "$no_pic");
return std::shared_ptr<QWChangeTriggerPropertiesEvent>{std::move(new_event)};
}
std::shared_ptr<QWSwitchEventsEvent> QWEventFactory::createSwitchEventsEvent(const QJsonObject &json_object)
{
std::unique_ptr<QWSwitchEventsEvent> new_event;
qDebug() << " Found QWSwitchEventsEvent. " << json_object["id"].toString() << ".";
Q_ASSERT(json_object.contains("target"));
Q_ASSERT(builder->hash_triggers.contains(json_object["target"].toString()));
new_event = std::make_unique<QWSwitchEventsEvent>(builder->hash_triggers[json_object["target"].toString()]);
Q_ASSERT(json_object.contains("enable_evs") && json_object.contains("disable_evs"));
// Linking events of enabled state
QJsonArray evs = json_object["enable_evs"].toArray();
QList<std::shared_ptr<QWAbstractEvent>> list_events;
for (const auto obj : evs) {
Q_ASSERT(builder->hash_events.contains(obj.toString()));
list_events.append(builder->hash_events[obj.toString()]);
}
new_event->setEventsOnEnable(list_events);
list_events.clear();
// Linking events of disabled state
evs = json_object["disable_evs"].toArray();
for (const auto obj : evs) {
Q_ASSERT(builder->hash_events.contains(obj.toString()));
list_events.append(builder->hash_events[obj.toString()]);
}
new_event->setEventsOnDisable(list_events);
new_event->init();
return std::shared_ptr<QWSwitchEventsEvent>{std::move(new_event)};
}
std::shared_ptr<QWPickupItemEvent> QWEventFactory::createPickupItEvent(const QJsonObject &json_object)
{
std::unique_ptr<QWPickupItemEvent> new_event;
qDebug() << " Found QWPickupItemEvent. " << json_object["id"].toString() << ".";
Q_ASSERT(json_object.contains("target"));
Q_ASSERT(builder->hash_triggers.contains(json_object["target"].toString()));
new_event = std::make_unique<QWPickupItemEvent>(builder->hash_triggers[json_object["target"].toString()]);
new_event->setInventoryManager(builder->ptr_inventory);
return std::shared_ptr<QWPickupItemEvent>{std::move(new_event)};
}
std::shared_ptr<QWEndLevelEvent> QWEventFactory::createEndLevelEvent(const QJsonObject &json_object)
{
std::unique_ptr<QWEndLevelEvent> new_event;
qDebug() << " Found QWEndLevelEvent. " << json_object["id"].toString() << ".";
Q_ASSERT(json_object.contains("new_level"));
new_event = std::make_unique<QWEndLevelEvent>(json_object["target"].toString());
new_event->setLevelBuilder(builder);
return std::shared_ptr<QWEndLevelEvent>{std::move(new_event)};
}
std::shared_ptr<QWPlaySoundEvent> QWEventFactory::createPlaySoundEvent(const QJsonObject &json_object)
{
std::unique_ptr<QWPlaySoundEvent> new_event;
qDebug() << " Found QWPlaySoundEvent. " << json_object["id"].toString() << ".";
Q_ASSERT(json_object.contains("sound"));
new_event = std::make_unique<QWPlaySoundEvent>(json_object["sound"].toString());
new_event->setSoundPlayer(builder->ptr_soundplayer);
return std::shared_ptr<QWPlaySoundEvent>{std::move(new_event)};
}
std::shared_ptr<QWNewGameEvent> QWEventFactory::createNewGameEvent(const QJsonObject &json_object)
{
std::unique_ptr<QWNewGameEvent> new_event;
qDebug() << " Found QWNewGameEvent. " << json_object["id"].toString() << ".";
Q_ASSERT(json_object.contains("save_file"));
new_event = std::make_unique<QWNewGameEvent>(json_object["save_file"].toString());
new_event->setLevelBuilder(builder);
return std::shared_ptr<QWNewGameEvent>{std::move(new_event)};
}
std::shared_ptr<QWQuitGameEvent> QWEventFactory::createQuitGameEvent(const QJsonObject &json_object)
{
std::unique_ptr<QWQuitGameEvent> new_event;
qDebug() << " Found QWQuitGameEvent. " << json_object["id"].toString() << ".";
Q_ASSERT(json_object.contains("save_game"));
new_event = std::make_unique<QWQuitGameEvent>(json_object["save_game"].toBool());
new_event->setLevelBuilder(builder);
return std::shared_ptr<QWQuitGameEvent>{std::move(new_event)};
}
std::shared_ptr<QWStartDialogueEvent> QWEventFactory::createStartDlgEvent(const QJsonObject &json_object)
{
std::unique_ptr<QWStartDialogueEvent> new_event;
qDebug() << " Found QWStartTextDialogueEvent. " << json_object["id"].toString() << ".";
Q_ASSERT(json_object.contains("dialogue"));
Q_ASSERT(json_object.contains("dialogue_type"));
Q_ASSERT(builder->hash_dialogues.contains(json_object["dialogue"].toString()));
new_event = std::make_unique<QWStartDialogueEvent>(builder->hash_dialogues[json_object["dialogue"].toString()]);
switch(json_object["dialogue_type"].toInt()) {
case 1:
new_event->setDialogueManager(builder->ptr_widget_dlg);
break;
case 0:
new_event->setDialogueManager(builder->ptr_text_dlg);
break;
default:
Q_ASSERT(false);
}
return std::shared_ptr<QWStartDialogueEvent>{std::move(new_event)};
}
std::shared_ptr<QWAddTriggerEvent> QWEventFactory::createAddTrEvent(const QJsonObject &json_object)
{
std::unique_ptr<QWAddTriggerEvent> new_event;
qDebug() << " Found QWAddTriggerEvent. " << json_object["id"].toString() << ".";
Q_ASSERT(json_object.contains("trigger"));
Q_ASSERT(builder->hash_triggers.contains(json_object["trigger"].toString()));
new_event = std::make_unique<QWAddTriggerEvent>(builder->hash_triggers[json_object["trigger"].toString()]);
Q_ASSERT(json_object.contains("location"));
Q_ASSERT(builder->hash_locations.contains(json_object["location"].toString()));
new_event->setLocation(builder->hash_locations[json_object["location"].toString()]);
return std::shared_ptr<QWAddTriggerEvent>{std::move(new_event)};
}
std::shared_ptr<QWRemoveTriggerEvent> QWEventFactory::createRemoveTrEvent(const QJsonObject &json_object)
{
std::unique_ptr<QWRemoveTriggerEvent> new_event;
qDebug() << " Found QWRemoveTriggerEvent. " << json_object["id"].toString() << ".";
Q_ASSERT(json_object.contains("trigger"));
Q_ASSERT(builder->hash_triggers.contains(json_object["trigger"].toString()));
new_event = std::make_unique<QWRemoveTriggerEvent>(builder->hash_triggers[json_object["trigger"].toString()]);
Q_ASSERT(json_object.contains("location"));
Q_ASSERT(builder->hash_locations.contains(json_object["location"].toString()));
new_event->setLocation(builder->hash_locations[json_object["location"].toString()]);
return std::shared_ptr<QWRemoveTriggerEvent>{std::move(new_event)};
}