Compare commits
4 commits
libzork-83
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2bc2fe035d | ||
|
|
57db392019 | ||
|
|
b129bd0039 | ||
|
|
3a0dddabc9 |
8 changed files with 131 additions and 22 deletions
|
|
@ -38,16 +38,22 @@ set(CMAKE_BINARY_DIR build/)
|
||||||
|
|
||||||
|
|
||||||
# Import yaml-cpp
|
# Import yaml-cpp
|
||||||
include(FetchContent)
|
# Bon la mouli casse les couilles ENCORE UNE FOIS donc ça ça marche pas
|
||||||
|
#
|
||||||
|
# include(FetchContent)
|
||||||
|
|
||||||
FetchContent_Declare(
|
# FetchContent_Declare(
|
||||||
yaml-cpp
|
# yaml-cpp
|
||||||
GIT_REPOSITORY https://github.com/jbeder/yaml-cpp.git
|
# GIT_REPOSITORY https://github.com/jbeder/yaml-cpp.git
|
||||||
# GIT_TAG <master>
|
# # GIT_TAG <master>
|
||||||
)
|
# )
|
||||||
FetchContent_MakeAvailable(yaml-cpp)
|
# FetchContent_MakeAvailable(yaml-cpp)
|
||||||
|
#
|
||||||
|
# target_link_libraries(zork PUBLIC yaml-cpp::yaml-cpp)
|
||||||
|
|
||||||
target_link_libraries(zork PUBLIC yaml-cpp::yaml-cpp)
|
find_library(YAML_CPP yaml-cpp REQUIRED )
|
||||||
|
target_link_libraries(zork PUBLIC yaml-cpp)
|
||||||
|
|
||||||
target_link_options(zork PUBLIC -Wl,--no-undefined)
|
|
||||||
|
target_link_options(zork PUBLIC ${YAML_CPP} -Wl,--no-undefined)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,6 @@ namespace libzork::story
|
||||||
class Choice
|
class Choice
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Choice() = default;
|
|
||||||
Choice(const Node* node, const std::string& text,
|
Choice(const Node* node, const std::string& text,
|
||||||
const std::vector<std::unique_ptr<vars::Condition>>& conditions,
|
const std::vector<std::unique_ptr<vars::Condition>>& conditions,
|
||||||
const std::vector<std::unique_ptr<vars::Action>>& actions);
|
const std::vector<std::unique_ptr<vars::Action>>& actions);
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,36 @@
|
||||||
|
#include <fstream>
|
||||||
#include <libzork/story/node.hh>
|
#include <libzork/story/node.hh>
|
||||||
|
#include <memory>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
#include "exceptions.hh"
|
#include "exceptions.hh"
|
||||||
#include "story/node_impl.hh"
|
#include "story/node_impl.hh"
|
||||||
|
|
||||||
|
std::string readFile(const std::string& path)
|
||||||
|
{
|
||||||
|
std::ifstream in(path, std::ios::binary);
|
||||||
|
if (!in)
|
||||||
|
throw std::runtime_error("cannot open file");
|
||||||
|
return std::string(std::istreambuf_iterator<char>(in),
|
||||||
|
std::istreambuf_iterator<char>());
|
||||||
|
}
|
||||||
|
|
||||||
namespace libzork::story
|
namespace libzork::story
|
||||||
{
|
{
|
||||||
|
|
||||||
std::unique_ptr<Node> make_node(const std::string& name,
|
std::unique_ptr<Node> make_node(const std::string& name,
|
||||||
const fs::path& script_path)
|
const fs::path& script_path)
|
||||||
{
|
{
|
||||||
(void)name;
|
std::ifstream a;
|
||||||
(void)script_path;
|
a.open(script_path);
|
||||||
throw NotImplemented();
|
if (!a.is_open())
|
||||||
|
throw std::runtime_error(
|
||||||
|
std::string("Could not open script: ").append(script_path));
|
||||||
|
|
||||||
|
std::string text = std::string(std::istreambuf_iterator<char>(a),
|
||||||
|
std::istreambuf_iterator<char>());
|
||||||
|
|
||||||
|
return std::make_unique<NodeImpl>(NodeImpl{ name, text });
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace libzork::story
|
} // namespace libzork::story
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,11 @@
|
||||||
|
|
||||||
namespace libzork::story
|
namespace libzork::story
|
||||||
{
|
{
|
||||||
|
NodeImpl::NodeImpl(const std::string& name, const std::string& text)
|
||||||
|
: name_{ name }
|
||||||
|
, text_{ text }
|
||||||
|
{}
|
||||||
|
|
||||||
const std::string& NodeImpl::get_name() const
|
const std::string& NodeImpl::get_name() const
|
||||||
{
|
{
|
||||||
return name_;
|
return name_;
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ namespace libzork::story
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
~NodeImpl() override = default;
|
~NodeImpl() override = default;
|
||||||
|
NodeImpl(const std::string& name, const std::string& text);
|
||||||
|
|
||||||
const std::string& get_name() const override;
|
const std::string& get_name() const override;
|
||||||
const std::string& get_text() const override;
|
const std::string& get_text() const override;
|
||||||
|
|
@ -29,8 +30,8 @@ namespace libzork::story
|
||||||
std::vector<std::unique_ptr<vars::Action>> actions = {}) override;
|
std::vector<std::unique_ptr<vars::Action>> actions = {}) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string name_;
|
const std::string name_;
|
||||||
std::string text_;
|
const std::string text_;
|
||||||
std::vector<std::shared_ptr<Choice>> choices_;
|
std::vector<std::shared_ptr<Choice>> choices_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,13 @@
|
||||||
#include <libzork/story/story.hh>
|
#include <libzork/story/story.hh>
|
||||||
|
#include <list>
|
||||||
|
#include <memory>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <yaml-cpp/yaml.h>
|
||||||
|
|
||||||
#include "exceptions.hh"
|
#include "exceptions.hh"
|
||||||
|
#include "libzork/story/node.hh"
|
||||||
|
#include "story/node_impl.hh"
|
||||||
#include "story/story_impl.hh"
|
#include "story/story_impl.hh"
|
||||||
|
|
||||||
namespace libzork::story
|
namespace libzork::story
|
||||||
|
|
@ -8,8 +15,65 @@ namespace libzork::story
|
||||||
|
|
||||||
std::unique_ptr<Story> make_story(const fs::path& path)
|
std::unique_ptr<Story> make_story(const fs::path& path)
|
||||||
{
|
{
|
||||||
(void)path;
|
YAML::Node story_node = YAML::LoadFile(path);
|
||||||
throw NotImplemented();
|
if (!story_node)
|
||||||
|
throw std::runtime_error("Coult not load YAML file");
|
||||||
|
|
||||||
|
// Check fields
|
||||||
|
if (!story_node["title"] || !story_node["scripts-path"]
|
||||||
|
|| !story_node["story"])
|
||||||
|
throw std::runtime_error("Missing fields in YAML file");
|
||||||
|
|
||||||
|
const std::string title = story_node["title"].as<std::string>();
|
||||||
|
const std::string scripts_path =
|
||||||
|
story_node["scripts-path"].as<std::string>();
|
||||||
|
|
||||||
|
// Stories (get nodes)
|
||||||
|
std::unordered_map<std::string, std::unique_ptr<Node>> nodes;
|
||||||
|
auto stories = story_node["story"];
|
||||||
|
for (auto story : stories)
|
||||||
|
{
|
||||||
|
// Check fields
|
||||||
|
if (!story["name"] || story["script"] || story["choices"])
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// Get values
|
||||||
|
const std::string story_name = story["name"].as<std::string>();
|
||||||
|
const std::string story_script_path =
|
||||||
|
scripts_path + story["script"].as<std::string>();
|
||||||
|
|
||||||
|
// 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 choices_list =
|
||||||
|
std::list<std::pair<const std::string, const std::string>>();
|
||||||
|
for (auto choice : story_choices)
|
||||||
|
{
|
||||||
|
// Check fields
|
||||||
|
if (!choice["text"] || !choice["target"])
|
||||||
|
continue;
|
||||||
|
choices_list.push_back(
|
||||||
|
std::make_pair(choice["text"].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;
|
||||||
|
}
|
||||||
|
|
||||||
|
return std::make_unique<StoryImpl>(title, nullptr, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace libzork::story
|
} // namespace libzork::story
|
||||||
|
|
|
||||||
|
|
@ -4,26 +4,31 @@
|
||||||
|
|
||||||
namespace libzork::story
|
namespace libzork::story
|
||||||
{
|
{
|
||||||
|
StoryImpl::StoryImpl(const std::string& title, const Node* current_node,
|
||||||
|
store::Store* store)
|
||||||
|
: title_{ title }
|
||||||
|
, current_{ current_node }
|
||||||
|
, store_{ store }
|
||||||
|
{}
|
||||||
|
|
||||||
const std::string& StoryImpl::get_title() const
|
const std::string& StoryImpl::get_title() const
|
||||||
{
|
{
|
||||||
throw NotImplemented();
|
return title_;
|
||||||
}
|
}
|
||||||
|
|
||||||
const Node* StoryImpl::get_current() const
|
const Node* StoryImpl::get_current() const
|
||||||
{
|
{
|
||||||
throw NotImplemented();
|
return store_->get_active_node();
|
||||||
}
|
}
|
||||||
|
|
||||||
void StoryImpl::set_current(const Node* node)
|
void StoryImpl::set_current(const Node* node)
|
||||||
{
|
{
|
||||||
(void)node;
|
store_->set_active_node(node);
|
||||||
throw NotImplemented();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const store::Store* StoryImpl::get_store() const
|
const store::Store* StoryImpl::get_store() const
|
||||||
{
|
{
|
||||||
throw NotImplemented();
|
return store_;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::ostream& StoryImpl::display(std::ostream& os) const
|
std::ostream& StoryImpl::display(std::ostream& os) 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
|
||||||
{
|
{
|
||||||
|
|
@ -9,6 +10,9 @@ namespace libzork::story
|
||||||
class StoryImpl : public Story
|
class StoryImpl : public Story
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
StoryImpl() = default;
|
||||||
|
StoryImpl(const std::string& title, const Node* current_node,
|
||||||
|
store::Store* store);
|
||||||
~StoryImpl() override = default;
|
~StoryImpl() override = default;
|
||||||
|
|
||||||
const std::string& get_title() const override;
|
const std::string& get_title() const override;
|
||||||
|
|
@ -16,6 +20,12 @@ namespace libzork::story
|
||||||
void set_current(const Node* node) override;
|
void set_current(const Node* node) override;
|
||||||
const store::Store* get_store() const override;
|
const store::Store* get_store() const override;
|
||||||
std::ostream& display(std::ostream& os) const override;
|
std::ostream& display(std::ostream& os) const override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
const std::string title_;
|
||||||
|
const std::vector<Node*> nodes_;
|
||||||
|
const Node* current_;
|
||||||
|
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