You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
1.7 KiB
C++

#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()
{
std::cout << ">> ";
std::string input;
std::cin >> input;
std::cout << "\n";
std::transform(input.begin(), input.end(), input.begin(),
[](unsigned char c){ return std::tolower(c); });
if (input == "inventory")
{
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;
}
void Player::giveItem(const std::shared_ptr<Item>& item)
{
item->setReceived(true);
_inventory.push_back(item);
}
void Player::useItem(const std::shared_ptr<Item>& item)
{
item->setReceived(false);
_inventory.remove(item);
}
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;
}