Add ready items

This commit is contained in:
NaiJi ✨ 2021-05-16 18:20:57 +03:00
parent 5030b71e3f
commit f7517e0abd
13 changed files with 151 additions and 18 deletions

View File

@ -1,6 +1,8 @@
#include "actor.h"
Actor::Actor()
Actor::Actor() :
_current_location(nullptr),
_ready_item(nullptr)
{}
Actor::~Actor()

View File

@ -20,10 +20,13 @@ public:
virtual void giveItem(const std::shared_ptr<Item>& item) = 0;
virtual void useItem(const std::shared_ptr<Item>& item) = 0;
virtual bool hasItem(const std::shared_ptr<Item>& item) const = 0;
virtual void readyItem(const std::shared_ptr<Item>& item) = 0;
virtual bool isItemReady(const std::shared_ptr<Item>& item) const = 0;
protected:
std::shared_ptr<Location> _current_location;
std::list<std::shared_ptr<Item>> _inventory;
std::shared_ptr<Item> _ready_item;
std::set<std::shared_ptr<Location>> _visited_locations;
};

View File

@ -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>& item) const
return std::find(_inventory.begin(), _inventory.end(), item) != _inventory.end();
}
void Player::readyItem(const std::shared_ptr<Item>& item)
{
_ready_item = item;
}
bool Player::isItemReady(const std::shared_ptr<Item>& 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;
}
}
}

View File

@ -15,9 +15,12 @@ public:
virtual void giveItem(const std::shared_ptr<Item>& item) override;
virtual void useItem(const std::shared_ptr<Item>& item) override;
virtual bool hasItem(const std::shared_ptr<Item>& item) const override;
virtual void readyItem(const std::shared_ptr<Item>& item) override;
virtual bool isItemReady(const std::shared_ptr<Item>& item) const override;
private:
std::string showInventory() const;
void findItemToReady(const std::string &label);
};
#endif // PLAYER_H

View File

@ -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> &location,
const std::string &new_message)
{
_location = location;
_new_message = new_message;
}

View File

@ -0,0 +1,27 @@
#ifndef CHANGEINTERACTIONMESSAGEMODIFICATOR_H
#define CHANGEINTERACTIONMESSAGEMODIFICATOR_H
#include <memory>
#include <string>
#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>& location,
const std::string& new_message);
private:
std::shared_ptr<Location> _location;
std::string _new_message;
};
#endif // CHANGEINTERACTIONMESSAGEMODIFICATOR_H

View File

@ -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>& location,
const std::list<std::shared_ptr<Controller>>& controllers,
const std::string& new_message)
const std::list<std::shared_ptr<Controller>>& controllers)
{
_location = location;
_controllers_to_remove = controllers;
_new_location_message = new_message;
}

View File

@ -19,13 +19,11 @@ public:
virtual void run() const override;
void setDependentObjects(const std::shared_ptr<Location>& location,
const std::list<std::shared_ptr<Controller>>& controllers,
const std::string& new_message);
const std::list<std::shared_ptr<Controller>>& controllers);
private:
std::shared_ptr<Location> _location;
std::list<std::shared_ptr<Controller>> _controllers_to_remove;
std::string _new_location_message;
};
#endif // REMOVECONTROLLERSMODIFICATOR_H

View File

@ -1,7 +1,6 @@
#ifndef ITEMREQUIREDPOLICY_H
#define ITEMREQUIREDPOLICY_H
#include <memory>
#include "policy.h"
class Item;

View File

@ -1,7 +1,6 @@
#ifndef LOCATIONREQUIREDPOLICY_H
#define LOCATIONREQUIREDPOLICY_H
#include <memory>
#include "policy.h"
class Location;

View File

@ -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>& actor) const
{
bool success = actor->isItemReady(_item);
return composeMessageFromResult(success);
}
void ReadyItemRequiredPolicy::setRequiredReadyItem(const std::shared_ptr<Item> &item)
{
_item = item;
}

View File

@ -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>& actor) const override;
void setRequiredReadyItem(const std::shared_ptr<Item>& item);
private:
std::shared_ptr<Item> _item;
};
#endif // READYITEMREQUIREDPOLICY_H

View File

@ -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 <iostream>
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<LocationController> pleroman_cont = std::make_shared<LocationController>(std::move(pleroman_init));
std::shared_ptr<ItemRequiredPolicy> need_tenshi_policy = std::make_shared<ItemRequiredPolicy>("You give him the postcard of Tenshi.", "He doesn't want to talk to you. Make him trust you!!");
std::shared_ptr<ReadyItemRequiredPolicy> need_tenshi_policy = std::make_shared<ReadyItemRequiredPolicy>("You give him the postcard of Tenshi.", "He doesn't want to talk to you. Make him trust you!!");
std::shared_ptr<AllPoliciesValidator> pleroman_validator = std::make_shared<AllPoliciesValidator>(std::list<std::shared_ptr<Policy>>{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<Location> pleroman = std::make_shared<Location>(std::move(pleromanloc_init));
std::shared_ptr<Item> tenshi = std::make_shared<Item>("Postcard of Tenshi eating corndog");
need_tenshi_policy->setRequiredItem(tenshi);
// TENSHI POSTCARD ITEM
std::shared_ptr<Item> tenshi = std::make_shared<Item>("postcard of tenshi eating corndog");
need_tenshi_policy->setRequiredReadyItem(tenshi);
// WHAT HAPPENS WHEN YOU PICK TENSHI
std::shared_ptr<RemoveControllersModificator> remove_tenshi_modif = std::make_shared<RemoveControllersModificator>();
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<ChangeInteractionMessageModificator> change_table_desc_modif = std::make_shared<ChangeInteractionMessageModificator>();
change_table_desc_modif->setDependentObjects(table, "Boring table.");
tenshi_cont->setModificators({remove_tenshi_modif, change_table_desc_modif});
std::shared_ptr<ChangeInteractionMessageModificator> change_pleroman_desc_modif = std::make_shared<ChangeInteractionMessageModificator>();
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);