From f7517e0abd81d72530abb9ad0df9fef5c236e2a1 Mon Sep 17 00:00:00 2001 From: NaiJi Date: Sun, 16 May 2021 18:20:57 +0300 Subject: [PATCH] Add ready items --- actors/actor.cpp | 4 ++- actors/actor.h | 3 ++ actors/player.cpp | 36 +++++++++++++++++-- actors/player.h | 3 ++ .../changeinteractionmessagemodificator.cpp | 20 +++++++++++ .../changeinteractionmessagemodificator.h | 27 ++++++++++++++ modificators/removecontrollersmodificator.cpp | 5 +-- modificators/removecontrollersmodificator.h | 4 +-- policies/itemrequiredpolicy.h | 1 - policies/locationrequiredpolicy.h | 1 - policies/readyitemrequiredpolicy.cpp | 20 +++++++++++ policies/readyitemrequiredpolicy.h | 22 ++++++++++++ sandboxlevelbuilder.cpp | 23 ++++++++---- 13 files changed, 151 insertions(+), 18 deletions(-) create mode 100644 modificators/changeinteractionmessagemodificator.cpp create mode 100644 modificators/changeinteractionmessagemodificator.h create mode 100644 policies/readyitemrequiredpolicy.cpp create mode 100644 policies/readyitemrequiredpolicy.h diff --git a/actors/actor.cpp b/actors/actor.cpp index 1615f50..483c69c 100644 --- a/actors/actor.cpp +++ b/actors/actor.cpp @@ -1,6 +1,8 @@ #include "actor.h" -Actor::Actor() +Actor::Actor() : + _current_location(nullptr), + _ready_item(nullptr) {} Actor::~Actor() diff --git a/actors/actor.h b/actors/actor.h index 0625978..43bf04e 100644 --- a/actors/actor.h +++ b/actors/actor.h @@ -20,10 +20,13 @@ public: virtual void giveItem(const std::shared_ptr& item) = 0; virtual void useItem(const std::shared_ptr& item) = 0; virtual bool hasItem(const std::shared_ptr& item) const = 0; + virtual void readyItem(const std::shared_ptr& item) = 0; + virtual bool isItemReady(const std::shared_ptr& item) const = 0; protected: std::shared_ptr _current_location; std::list> _inventory; + std::shared_ptr _ready_item; std::set> _visited_locations; }; diff --git a/actors/player.cpp b/actors/player.cpp index 75f0921..e841904 100644 --- a/actors/player.cpp +++ b/actors/player.cpp @@ -15,16 +15,26 @@ Player::~Player() void Player::commitAction() { - std::cout << ">> "; + std::cout << ((_ready_item) + ? (">> [" + _ready_item->label() + "] > ") + : (">> ")); + std::string input; - std::cin >> input; + std::getline(std::cin, input, '\n'); std::cout << "\n"; std::transform(input.begin(), input.end(), input.begin(), [](unsigned char c){ return std::tolower(c); }); + if (input.find("ready ") != std::string::npos) + { + findItemToReady(input.substr(6, input.size() - 1)); + return; + } + if (input == "inventory") { + readyItem(nullptr); std::cout << showInventory() << "\n\n"; return; } @@ -68,6 +78,16 @@ bool Player::hasItem(const std::shared_ptr& item) const return std::find(_inventory.begin(), _inventory.end(), item) != _inventory.end(); } +void Player::readyItem(const std::shared_ptr& item) +{ + _ready_item = item; +} + +bool Player::isItemReady(const std::shared_ptr& item) const +{ + return item == _ready_item; +} + std::string Player::showInventory() const { if (_inventory.empty()) @@ -87,3 +107,15 @@ std::string Player::showInventory() const return inventory_message; } + +void Player::findItemToReady(const std::string& label) +{ + for (const auto& item : _inventory) + { + if (item->label().find(label) != std::string::npos) + { + readyItem(item); + return; + } + } +} diff --git a/actors/player.h b/actors/player.h index 42747e4..3b71a6b 100644 --- a/actors/player.h +++ b/actors/player.h @@ -15,9 +15,12 @@ public: virtual void giveItem(const std::shared_ptr& item) override; virtual void useItem(const std::shared_ptr& item) override; virtual bool hasItem(const std::shared_ptr& item) const override; + virtual void readyItem(const std::shared_ptr& item) override; + virtual bool isItemReady(const std::shared_ptr& item) const override; private: std::string showInventory() const; + void findItemToReady(const std::string &label); }; #endif // PLAYER_H diff --git a/modificators/changeinteractionmessagemodificator.cpp b/modificators/changeinteractionmessagemodificator.cpp new file mode 100644 index 0000000..56c2ee4 --- /dev/null +++ b/modificators/changeinteractionmessagemodificator.cpp @@ -0,0 +1,20 @@ +#include "changeinteractionmessagemodificator.h" +#include "location.h" + +ChangeInteractionMessageModificator::ChangeInteractionMessageModificator() +{} + +ChangeInteractionMessageModificator::~ChangeInteractionMessageModificator() +{} + +void ChangeInteractionMessageModificator::run() const +{ + _location->setInteractionMessage(_new_message); +} + +void ChangeInteractionMessageModificator::setDependentObjects(const std::shared_ptr &location, + const std::string &new_message) +{ + _location = location; + _new_message = new_message; +} diff --git a/modificators/changeinteractionmessagemodificator.h b/modificators/changeinteractionmessagemodificator.h new file mode 100644 index 0000000..49aac9c --- /dev/null +++ b/modificators/changeinteractionmessagemodificator.h @@ -0,0 +1,27 @@ +#ifndef CHANGEINTERACTIONMESSAGEMODIFICATOR_H +#define CHANGEINTERACTIONMESSAGEMODIFICATOR_H + +#include +#include + +#include "modificator.h" + +class Location; + +class ChangeInteractionMessageModificator : public Modificator +{ +public: + explicit ChangeInteractionMessageModificator(); + virtual ~ChangeInteractionMessageModificator() override; + + virtual void run() const override; + + void setDependentObjects(const std::shared_ptr& location, + const std::string& new_message); + +private: + std::shared_ptr _location; + std::string _new_message; +}; + +#endif // CHANGEINTERACTIONMESSAGEMODIFICATOR_H diff --git a/modificators/removecontrollersmodificator.cpp b/modificators/removecontrollersmodificator.cpp index f9ec20d..fbaf900 100644 --- a/modificators/removecontrollersmodificator.cpp +++ b/modificators/removecontrollersmodificator.cpp @@ -10,14 +10,11 @@ RemoveControllersModificator::~RemoveControllersModificator() void RemoveControllersModificator::run() const { _location->removeControllers(_controllers_to_remove); - _location->setInteractionMessage(_new_location_message); } void RemoveControllersModificator::setDependentObjects(const std::shared_ptr& location, - const std::list>& controllers, - const std::string& new_message) + const std::list>& controllers) { _location = location; _controllers_to_remove = controllers; - _new_location_message = new_message; } diff --git a/modificators/removecontrollersmodificator.h b/modificators/removecontrollersmodificator.h index 08f6ecb..bc7c9c8 100644 --- a/modificators/removecontrollersmodificator.h +++ b/modificators/removecontrollersmodificator.h @@ -19,13 +19,11 @@ public: virtual void run() const override; void setDependentObjects(const std::shared_ptr& location, - const std::list>& controllers, - const std::string& new_message); + const std::list>& controllers); private: std::shared_ptr _location; std::list> _controllers_to_remove; - std::string _new_location_message; }; #endif // REMOVECONTROLLERSMODIFICATOR_H diff --git a/policies/itemrequiredpolicy.h b/policies/itemrequiredpolicy.h index ee7874e..94bb186 100644 --- a/policies/itemrequiredpolicy.h +++ b/policies/itemrequiredpolicy.h @@ -1,7 +1,6 @@ #ifndef ITEMREQUIREDPOLICY_H #define ITEMREQUIREDPOLICY_H -#include #include "policy.h" class Item; diff --git a/policies/locationrequiredpolicy.h b/policies/locationrequiredpolicy.h index c1f6cc7..45548e4 100644 --- a/policies/locationrequiredpolicy.h +++ b/policies/locationrequiredpolicy.h @@ -1,7 +1,6 @@ #ifndef LOCATIONREQUIREDPOLICY_H #define LOCATIONREQUIREDPOLICY_H -#include #include "policy.h" class Location; diff --git a/policies/readyitemrequiredpolicy.cpp b/policies/readyitemrequiredpolicy.cpp new file mode 100644 index 0000000..b538703 --- /dev/null +++ b/policies/readyitemrequiredpolicy.cpp @@ -0,0 +1,20 @@ +#include "readyitemrequiredpolicy.h" +#include "actor.h" + +ReadyItemRequiredPolicy::ReadyItemRequiredPolicy(const std::string& satisfaction, const std::string& dissatisfaction) : + Policy(satisfaction, dissatisfaction) +{} + +ReadyItemRequiredPolicy::~ReadyItemRequiredPolicy() +{} + +Policy::CheckResult ReadyItemRequiredPolicy::check(const std::shared_ptr& actor) const +{ + bool success = actor->isItemReady(_item); + return composeMessageFromResult(success); +} + +void ReadyItemRequiredPolicy::setRequiredReadyItem(const std::shared_ptr &item) +{ + _item = item; +} diff --git a/policies/readyitemrequiredpolicy.h b/policies/readyitemrequiredpolicy.h new file mode 100644 index 0000000..755e02c --- /dev/null +++ b/policies/readyitemrequiredpolicy.h @@ -0,0 +1,22 @@ +#ifndef READYITEMREQUIREDPOLICY_H +#define READYITEMREQUIREDPOLICY_H + +#include "policy.h" + +class Item; + +class ReadyItemRequiredPolicy : public Policy +{ +public: + explicit ReadyItemRequiredPolicy(const std::string& satisfaction, const std::string& dissatisfaction); + virtual ~ReadyItemRequiredPolicy() override; + + virtual Policy::CheckResult check(const std::shared_ptr& actor) const override; + + void setRequiredReadyItem(const std::shared_ptr& item); + +private: + std::shared_ptr _item; +}; + +#endif // READYITEMREQUIREDPOLICY_H diff --git a/sandboxlevelbuilder.cpp b/sandboxlevelbuilder.cpp index 0aae81d..9d09a27 100644 --- a/sandboxlevelbuilder.cpp +++ b/sandboxlevelbuilder.cpp @@ -4,8 +4,11 @@ #include "locationcontroller.h" #include "itemcontroller.h" #include "itemrequiredpolicy.h" +#include "readyitemrequiredpolicy.h" #include "allpoliciesvalidator.h" #include "removecontrollersmodificator.h" +#include "changeinteractionmessagemodificator.h" + #include SandboxLevelBuilder::SandboxLevelBuilder() @@ -43,7 +46,7 @@ void SandboxLevelBuilder::init() // PLEROMAN CONTROLLER Controller::Initializer pleroman_init = {{"pleroman"}, "You talk to a pleroma user! What a happy and carefree creature. He even brew you some cofe!"}; std::shared_ptr pleroman_cont = std::make_shared(std::move(pleroman_init)); - std::shared_ptr need_tenshi_policy = std::make_shared("You give him the postcard of Tenshi.", "He doesn't want to talk to you. Make him trust you!!"); + std::shared_ptr need_tenshi_policy = std::make_shared("You give him the postcard of Tenshi.", "He doesn't want to talk to you. Make him trust you!!"); std::shared_ptr pleroman_validator = std::make_shared(std::list>{need_tenshi_policy}); pleroman_cont->setValidator(pleroman_validator); @@ -69,15 +72,23 @@ void SandboxLevelBuilder::init() // PLEROMAN LOCATION auto&& pleroman_msg = ". . . \n\n From the conversation you find out the quest is still in a very raw test state, the engine isn't even finished... Alright, just terminate the game process."; - Location::Initializer pleromanloc_init = {pleroman_msg, {}}; + Location::Initializer pleromanloc_init = {pleroman_msg, {table_cont, couch_cont}}; std::shared_ptr pleroman = std::make_shared(std::move(pleromanloc_init)); - std::shared_ptr tenshi = std::make_shared("Postcard of Tenshi eating corndog"); - need_tenshi_policy->setRequiredItem(tenshi); + // TENSHI POSTCARD ITEM + std::shared_ptr tenshi = std::make_shared("postcard of tenshi eating corndog"); + need_tenshi_policy->setRequiredReadyItem(tenshi); + // WHAT HAPPENS WHEN YOU PICK TENSHI std::shared_ptr remove_tenshi_modif = std::make_shared(); - remove_tenshi_modif->setDependentObjects(table, {tenshi_cont}, "Boring table."); - tenshi_cont->setModificators({remove_tenshi_modif}); + remove_tenshi_modif->setDependentObjects(table, {tenshi_cont}); + std::shared_ptr change_table_desc_modif = std::make_shared(); + change_table_desc_modif->setDependentObjects(table, "Boring table."); + tenshi_cont->setModificators({remove_tenshi_modif, change_table_desc_modif}); + + std::shared_ptr change_pleroman_desc_modif = std::make_shared(); + change_pleroman_desc_modif->setDependentObjects(pleroman, "He is happy."); + pleroman_cont->setModificators({change_pleroman_desc_modif}); the_first_and_only_trigger->setDependentLocation(start); door_cont->setDependentLocation(room);