Skip to content
Snippets Groups Projects
Commit c1d90f6d authored by Mikhail Andrenkov's avatar Mikhail Andrenkov
Browse files

Added getInventory() to PlayerChar; Made failures silent in makefile

parent 7f17aa02
No related branches found
No related tags found
No related merge requests found
......@@ -9,10 +9,10 @@ class ItemZone {
ItemZone();
Item operator[](int);
void add(Item&);
bool contains(Item&);
bool contains(Item*);
bool contains(const std::string&);
std::vector<Item> getContents();
bool remove(Item&);
std::vector<Item*> getContents();
bool remove(Item*);
private:
std::vector<Item> contents;
......
#pragma once
#include <string>
#include <vector>
#include "armor.h"
#include "coord.h"
......@@ -30,6 +31,7 @@ class PlayerChar : public Mob {
void equipRingRight(Ring*);
void equipWeapon(Weapon*);
int getGold();
std::vector<std::pair<Item*, int>> getInventory();
int getStrength();
bool hasAmulet();
int maxDelved();
......
......@@ -5,12 +5,9 @@
#include "coord.h"
#include "item.h"
class Level;
class Weapon : public Item {
public:
Weapon(Coord, Item::Context, std::string, std::string, unsigned char, std::pair<int, int>, bool, bool);
bool activate(Level*);
int getChance();
int getDamage();
bool isMelee();
......
......@@ -17,7 +17,7 @@ Item::Item(char symbol, Coord location, Item::Context context, std::string class
type(type) {}
bool Item::operator==(const Item& other) const {
return this == &other;
return this->name.compare(other.name) == 0;
}
bool Item::operator<(const Item& other) const {
......
......@@ -13,8 +13,8 @@ void ItemZone::add(Item& item) {
contents.push_back(item);
}
bool ItemZone::contains(Item& item) {
return find(contents.begin(), contents.end(), item) != contents.end();
bool ItemZone::contains(Item* item) {
return find(contents.begin(), contents.end(), *item) != contents.end();
}
bool ItemZone::contains(const std::string& itemName) {
......@@ -25,13 +25,19 @@ bool ItemZone::contains(const std::string& itemName) {
return false;
}
std::vector<Item> ItemZone::getContents() {
return contents;
std::vector<Item*> ItemZone::getContents() {
std::vector<Item*> contentPointers;
for (auto itemIterator = contents.begin() ; itemIterator != contents.end() ; itemIterator++) {
contentPointers.push_back(&(*itemIterator));
}
return contentPointers;
}
bool ItemZone::remove(Item& item) {
bool ItemZone::remove(Item* item) {
for (auto itemIterator = contents.begin() ; itemIterator != contents.end() ; itemIterator++) {
if (*itemIterator == item) {
if (*itemIterator == *item) {
contents.erase(itemIterator);
return true;
}
......
......@@ -5,5 +5,5 @@ all: *.cpp
default: all
clean:
@rm RogueReborn.exe *.so
@rm scores.txt
@rm -f RogueReborn.exe *.so
@rm -f scores.txt
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include "include/armor.h"
#include "include/coord.h"
......@@ -40,7 +43,7 @@ bool PlayerChar::dropItem(Item* item) {
std::cout << "PlayerChar Dropped Item " << item->getName() << "\n";
this->inventory.remove(*item);
this->inventory.remove(item);
return true;
}
......@@ -48,7 +51,7 @@ void PlayerChar::eat(Food* food) {
std::cout << "PlayerChar Ate " << food->getName() << "\n";
food->activate(this);
this->inventory.remove(*food);
this->inventory.remove(food);
}
void PlayerChar::equipArmor(Armor* armor) {
......@@ -80,6 +83,30 @@ int PlayerChar::getGold() {
return this->gold;
}
std::vector<std::pair<Item*, int>> PlayerChar::getInventory() {
std::map<std::string, std::pair<Item*, int>> itemMap;
std::vector<Item*> contents = this->inventory.getContents();
std::vector<std::pair<Item*, int>> displayContents;
for (auto itemIt = contents.begin() ; itemIt != contents.end() ; itemIt++) {
std::string itemName = (*itemIt)->getName();
auto mapIt = itemMap.find(itemName);
if (mapIt != itemMap.end()) {
mapIt->second.second++;
} else {
itemMap[itemName] = std::make_pair(*itemIt, 1);
}
}
for (auto mapIt = itemMap.begin() ; mapIt != itemMap.end() ; mapIt++) {
displayContents.push_back(mapIt->second);
}
return displayContents;
}
int PlayerChar::getStrength() {
return this->currentStr;
}
......@@ -100,14 +127,14 @@ void PlayerChar::quaff(Potion* potion, Mob* mob) {
std::cout << "PlayerChar Quaffed " << potion->getName() << "\n";
potion->activate(mob);
this->inventory.remove(*potion);
this->inventory.remove(potion);
}
void PlayerChar::read(Scroll* scroll, Level* level) {
std::cout << "PlayerChar Read Scroll " << scroll->getName() << "\n";
scroll->activate(level);
this->inventory.remove(*scroll);
this->inventory.remove(scroll);
}
bool PlayerChar::removeArmor() {
......@@ -155,7 +182,9 @@ bool PlayerChar::throwItem(Item* item) {
if (!item->isThrowable()) return false;
std::cout << "PlayerChar Threw " << item->getName() << "\n";
// TODO
return true;
}
......@@ -167,5 +196,6 @@ bool PlayerChar::zap(Wand* wand, Level* level) {
wand->activate(level);
// TODO
return true;
}
\ No newline at end of file
......@@ -7,10 +7,6 @@ Weapon::Weapon(Coord location, Item::Context context, std::string name, std::str
enchantment(enchantment),
melee(melee) {}
bool Weapon::activate(Level* level) {
return false;
}
int Weapon::getChance() {
return this->enchantment.first;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment