Compare commits
1 commit
libzork-57
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2bc2fe035d |
3 changed files with 33 additions and 11 deletions
|
|
@ -1,9 +1,12 @@
|
||||||
#include <libzork/story/story.hh>
|
#include <libzork/story/story.hh>
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <unordered_map>
|
||||||
#include <yaml-cpp/yaml.h>
|
#include <yaml-cpp/yaml.h>
|
||||||
|
|
||||||
#include "exceptions.hh"
|
#include "exceptions.hh"
|
||||||
|
#include "libzork/story/node.hh"
|
||||||
#include "story/node_impl.hh"
|
#include "story/node_impl.hh"
|
||||||
#include "story/story_impl.hh"
|
#include "story/story_impl.hh"
|
||||||
|
|
||||||
|
|
@ -13,17 +16,20 @@ namespace libzork::story
|
||||||
std::unique_ptr<Story> make_story(const fs::path& path)
|
std::unique_ptr<Story> make_story(const fs::path& path)
|
||||||
{
|
{
|
||||||
YAML::Node story_node = YAML::LoadFile(path);
|
YAML::Node story_node = YAML::LoadFile(path);
|
||||||
|
if (!story_node)
|
||||||
|
throw std::runtime_error("Coult not load YAML file");
|
||||||
|
|
||||||
// Check fields
|
// Check fields
|
||||||
if (!story_node["title"] || !story_node["scripts-path"]
|
if (!story_node["title"] || !story_node["scripts-path"]
|
||||||
|| !story_node["story"])
|
|| !story_node["story"])
|
||||||
return nullptr;
|
throw std::runtime_error("Missing fields in YAML file");
|
||||||
|
|
||||||
const std::string title = story_node["title"].as<std::string>();
|
const std::string title = story_node["title"].as<std::string>();
|
||||||
const std::string scripts_path =
|
const std::string scripts_path =
|
||||||
story_node["scripts-path"].as<std::string>();
|
story_node["scripts-path"].as<std::string>();
|
||||||
|
|
||||||
// Stories
|
// Stories (get nodes)
|
||||||
|
std::unordered_map<std::string, std::unique_ptr<Node>> nodes;
|
||||||
auto stories = story_node["story"];
|
auto stories = story_node["story"];
|
||||||
for (auto story : stories)
|
for (auto story : stories)
|
||||||
{
|
{
|
||||||
|
|
@ -31,11 +37,21 @@ namespace libzork::story
|
||||||
if (!story["name"] || story["script"] || story["choices"])
|
if (!story["name"] || story["script"] || story["choices"])
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
// Get values
|
||||||
const std::string story_name = story["name"].as<std::string>();
|
const std::string story_name = story["name"].as<std::string>();
|
||||||
const std::string story_script_path =
|
const std::string story_script_path =
|
||||||
scripts_path + story["script"].as<std::string>();
|
scripts_path + story["script"].as<std::string>();
|
||||||
|
|
||||||
// Choices
|
// Create node
|
||||||
|
auto n = make_node(story_name, story_script_path);
|
||||||
|
nodes.emplace(story_name, std::move(n));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stories (get choices)
|
||||||
|
for (auto story : stories)
|
||||||
|
{
|
||||||
|
// Get values
|
||||||
|
const std::string story_name = story["name"].as<std::string>();
|
||||||
auto story_choices = story["choices"];
|
auto story_choices = story["choices"];
|
||||||
auto choices_list =
|
auto choices_list =
|
||||||
std::list<std::pair<const std::string, const std::string>>();
|
std::list<std::pair<const std::string, const std::string>>();
|
||||||
|
|
@ -48,12 +64,16 @@ namespace libzork::story
|
||||||
std::make_pair(choice["text"].as<std::string>(),
|
std::make_pair(choice["text"].as<std::string>(),
|
||||||
choice["target"].as<std::string>()));
|
choice["target"].as<std::string>()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO add choices
|
||||||
|
auto it = nodes.find(story_name);
|
||||||
|
if (it == nodes.end())
|
||||||
|
throw std::runtime_error(
|
||||||
|
"Could not find the story back to add choices");
|
||||||
|
NodeImpl n = it.second;
|
||||||
}
|
}
|
||||||
|
|
||||||
// auto res = StoryImpl{ title, nullptr, nullptr };
|
|
||||||
// return std::unique_ptr<StoryImpl>();
|
|
||||||
return std::make_unique<StoryImpl>(title, nullptr, nullptr);
|
return std::make_unique<StoryImpl>(title, nullptr, nullptr);
|
||||||
// return nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace libzork::story
|
} // namespace libzork::story
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@
|
||||||
namespace libzork::story
|
namespace libzork::story
|
||||||
{
|
{
|
||||||
StoryImpl::StoryImpl(const std::string& title, const Node* current_node,
|
StoryImpl::StoryImpl(const std::string& title, const Node* current_node,
|
||||||
const store::Store* store)
|
store::Store* store)
|
||||||
: title_{ title }
|
: title_{ title }
|
||||||
, current_{ current_node }
|
, current_{ current_node }
|
||||||
, store_{ store }
|
, store_{ store }
|
||||||
|
|
@ -18,12 +18,12 @@ namespace libzork::story
|
||||||
|
|
||||||
const Node* StoryImpl::get_current() const
|
const Node* StoryImpl::get_current() const
|
||||||
{
|
{
|
||||||
return current_;
|
return store_->get_active_node();
|
||||||
}
|
}
|
||||||
|
|
||||||
void StoryImpl::set_current(const Node* node)
|
void StoryImpl::set_current(const Node* node)
|
||||||
{
|
{
|
||||||
current_ = node;
|
store_->set_active_node(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
const store::Store* StoryImpl::get_store() const
|
const store::Store* StoryImpl::get_store() const
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
#define STORY_IMPL_HH
|
#define STORY_IMPL_HH
|
||||||
|
|
||||||
#include <libzork/story/story.hh>
|
#include <libzork/story/story.hh>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
namespace libzork::story
|
namespace libzork::story
|
||||||
{
|
{
|
||||||
|
|
@ -11,7 +12,7 @@ namespace libzork::story
|
||||||
public:
|
public:
|
||||||
StoryImpl() = default;
|
StoryImpl() = default;
|
||||||
StoryImpl(const std::string& title, const Node* current_node,
|
StoryImpl(const std::string& title, const Node* current_node,
|
||||||
const store::Store* store);
|
store::Store* store);
|
||||||
~StoryImpl() override = default;
|
~StoryImpl() override = default;
|
||||||
|
|
||||||
const std::string& get_title() const override;
|
const std::string& get_title() const override;
|
||||||
|
|
@ -22,8 +23,9 @@ namespace libzork::story
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const std::string title_;
|
const std::string title_;
|
||||||
|
const std::vector<Node*> nodes_;
|
||||||
const Node* current_;
|
const Node* current_;
|
||||||
const store::Store* store_;
|
store::Store* store_;
|
||||||
};
|
};
|
||||||
|
|
||||||
const StoryImpl& to_impl(const Story& story);
|
const StoryImpl& to_impl(const Story& story);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue