2021-05-07 21:36:26 -04:00
|
|
|
#include <algorithm>
|
|
|
|
#include <iostream>
|
|
|
|
#include <cctype>
|
|
|
|
|
|
|
|
#include "player.h"
|
|
|
|
#include "location.h"
|
|
|
|
#include "controller.h"
|
|
|
|
#include "item.h"
|
|
|
|
|
|
|
|
Player::Player()
|
|
|
|
{}
|
|
|
|
|
|
|
|
Player::~Player()
|
|
|
|
{}
|
|
|
|
|
|
|
|
void Player::commitAction()
|
|
|
|
{
|
2021-05-16 11:20:57 -04:00
|
|
|
std::cout << ((_ready_item)
|
|
|
|
? (">> [" + _ready_item->label() + "] > ")
|
|
|
|
: (">> "));
|
|
|
|
|
2021-05-07 21:36:26 -04:00
|
|
|
std::string input;
|
2021-05-16 11:20:57 -04:00
|
|
|
std::getline(std::cin, input, '\n');
|
2021-05-07 21:36:26 -04:00
|
|
|
std::cout << "\n";
|
|
|
|
|
|
|
|
std::transform(input.begin(), input.end(), input.begin(),
|
|
|
|
[](unsigned char c){ return std::tolower(c); });
|
|
|
|
|
2021-05-16 11:20:57 -04:00
|
|
|
if (input.find("ready ") != std::string::npos)
|
|
|
|
{
|
|
|
|
findItemToReady(input.substr(6, input.size() - 1));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-07 21:36:26 -04:00
|
|
|
if (input == "inventory")
|
|
|
|
{
|
2021-05-16 11:20:57 -04:00
|
|
|
readyItem(nullptr);
|
2021-05-07 21:36:26 -04:00
|
|
|
std::cout << showInventory() << "\n\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& controller : _current_location->controllers())
|
|
|
|
{
|
|
|
|
if (controller->validateInput(input))
|
|
|
|
{
|
|
|
|
std::shared_ptr<Actor> player_as_actor = shared_from_this();
|
|
|
|
std::cout << controller->interact(player_as_actor) << "\n\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << "Nothing to do with that. Please think again." << "\n\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::moveToLocation(const std::shared_ptr<Location> &location)
|
|
|
|
{
|
|
|
|
_current_location = location;
|
2021-05-15 19:44:19 -04:00
|
|
|
_visited_locations.insert(location);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Player::isLocationVisited(const std::shared_ptr<Location> &location) const
|
|
|
|
{
|
|
|
|
return std::find(_visited_locations.begin(), _visited_locations.end(), location) != _visited_locations.end();
|
2021-05-07 21:36:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Player::giveItem(const std::shared_ptr<Item>& item)
|
|
|
|
{
|
|
|
|
_inventory.push_back(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::useItem(const std::shared_ptr<Item>& item)
|
|
|
|
{
|
|
|
|
_inventory.remove(item);
|
|
|
|
}
|
|
|
|
|
2021-05-15 19:44:19 -04:00
|
|
|
bool Player::hasItem(const std::shared_ptr<Item>& item) const
|
|
|
|
{
|
|
|
|
return std::find(_inventory.begin(), _inventory.end(), item) != _inventory.end();
|
|
|
|
}
|
|
|
|
|
2021-05-16 11:20:57 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-05-07 21:36:26 -04:00
|
|
|
std::string Player::showInventory() const
|
|
|
|
{
|
|
|
|
if (_inventory.empty())
|
|
|
|
{
|
|
|
|
return "Your pockets are empty.";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string inventory_message = "Currently you have ";
|
|
|
|
for (const auto& item : _inventory)
|
|
|
|
{
|
|
|
|
inventory_message += item->label();
|
|
|
|
inventory_message += ", ";
|
|
|
|
}
|
|
|
|
inventory_message.pop_back();
|
|
|
|
inventory_message.pop_back();
|
|
|
|
inventory_message += ".";
|
|
|
|
|
|
|
|
return inventory_message;
|
|
|
|
}
|
2021-05-16 11:20:57 -04:00
|
|
|
|
|
|
|
void Player::findItemToReady(const std::string& label)
|
|
|
|
{
|
|
|
|
for (const auto& item : _inventory)
|
|
|
|
{
|
|
|
|
if (item->label().find(label) != std::string::npos)
|
|
|
|
{
|
|
|
|
readyItem(item);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|