1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
|
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <utility>
#include <set>
using std::string;
using std::vector;
using std::cout;
using std::cin;
using std::map;
using std::pair;
using std::set;
//using std::iterator;
using std::make_pair;
class Character
{
public:
Character(const string& name, int health) : mName(name), mHealth(health) {}
string GetName() const {
return mName;
}
int GetHealth() const {
return mHealth;
}
private:
string mName { "Character Name" };
int mHealth { 100 };
};
class Item
{
public:
Item(const string& name, int slotsUsed, int currentStackSize, const int maxStackSize) : mName(name), mSlotsUsed(slotsUsed), mCurrentStackSize(currentStackSize), mMaxStackSize(maxStackSize) {}
Item() = default;
virtual void Reload(Item&) {
std::cout << "Cannot reload\n";
}
virtual int GetCurrentMagazineCount() const {
return -1;
}
string GetName() const {
return mName;
}
int GetCurrentStackSize() const {
return mCurrentStackSize;
}
int GetMaxStackSize() const {
return mMaxStackSize;
}
int GetSlotsUsed() const {
return mSlotsUsed;
}
void IncreaseStackSize(int amount) {
mCurrentStackSize += amount;
}
void DecreaseStackSize(int amount) {
mCurrentStackSize -= amount;
}
void SetStackSize(int amount) {
mCurrentStackSize = amount;
}
private:
string mName { "Weapon Name" };
int mSlotsUsed { 0 }; //Space taken up in container
int mCurrentStackSize { 0 };
const int mMaxStackSize { 0 }; //Max stack size for all items.
};
bool operator< (const Item& lhs, const Item& rhs) {
return lhs.GetName() < rhs.GetName();
}
std::ostream& operator<< (std::ostream& os, const Item& item) {
os << item.GetName();
return os;
}
//Should create a projectile weapon class and a melee weapon class that inherit from Weapon.
class Weapon : public Item
{
public:
Weapon
(
const string& name,
int size,
int currentStackSize,
const int maxStackSize,
const int magazineMaxSize,
const int ammoReserveMaxSize,
int magazine,
int ammoReserve)
: Item { name, size, currentStackSize, maxStackSize },
mMagazineMaxCapacity(magazineMaxSize),
mMagazine(magazine) {}
int GetMagazineMaxSize() const {
return mMagazineMaxCapacity;
}
int GetCurrentMagazineCount() const override {
return mMagazine;
}
//This may need some revision but I believe its working correctly.
void Reload(Item& item) override {
cout << "DEBUG: STACK SIZE: " << item.GetCurrentStackSize() << "\n\n";
if (item.GetCurrentStackSize() > 0) {
int ammoNeeded = mMagazineMaxCapacity - mMagazine;
cout << ammoNeeded << "\n\n";
if (item.GetCurrentStackSize() >= ammoNeeded) {
cout << "DEBUG: Weapon reloaded\n\n";
item.DecreaseStackSize(ammoNeeded);
mMagazine = mMagazineMaxCapacity;
} else {
mMagazine += item.GetCurrentStackSize();
item.SetStackSize(0);
}
} else {
cout << "You have no ammo left!\n\n";
}
}
void Fire() {
//For this we need to make sure the magazine has enough ammo in it to fire first, then if it does
//allow it to fire, then once the ammo reaches 0, then reload.
if (mMagazine > 0) {
mMagazine--;
cout << mMagazine << " rounds left\n\n";
} else {
cout << "Out of ammo, Reload!\n";
}
}
private:
const int mMagazineMaxCapacity { 0 };
int mMagazine { 0 };
};
class Container
{
public:
Container(const string& name, int containerSize, int amountStored) : mName(name), mContainerSize(containerSize), mAmountStored(amountStored) {}
void AddItem(Item& item) {
if (item.GetCurrentStackSize() <= item.GetMaxStackSize()) {
if (item.GetSlotsUsed() > mContainerSize) {
cout << "You do not have enough room in your inventory for " << item.GetName() << "\n\n";
} else {
mContainer.insert(&item);
}
} else {
cout << "Cannot pick up item " << item.GetName() << " as it has exceeded the max stack size...\n";
mContainer.insert(&item);
mContainerSize -= item.GetSlotsUsed();
}
}
void RemoveItem(const Item& item) {
mContainer.erase((Item*) & item);
}
int GetContainerSize() const {
return mContainerSize;
}
int GetAmountStored() const {
return mAmountStored;
}
Item* GetItem(const string& name) const {
for (const auto& i : mContainer)
if (i->GetName() == name)
return i;
return {};
}
void DisplayContents() {
int enumerate { 1 };
cout << "Inventory Slots Left: " << mContainerSize << "\n\n";
if (mContainer.empty()) {
cout << "Inventory is empty\n\n";
} else if (mContainer.size() > 0) {
for (const auto& i : mContainer) {
cout << enumerate++ << ") " << i->GetName() << " x " << i->GetCurrentStackSize();
if (const auto mag { i->GetCurrentMagazineCount() }; mag >= 0)
std::cout << " (magazine count: " << mag << ')';
std::cout << '\n';
}
}
cout << '\n';
}
int GetSize() const {
return mContainer.size();
}
private:
string mName { "Container Name" };
int mContainerSize { 0 };
int mAmountStored { 0 };
set<Item*> mContainer;
};
int main() {
Container PlayerInventory("Player Inventory", 50, 0);
Item NineMMAmmo("9mm Round", 1, 120, 9999);
Item FiveFiveSixAmmo("5.56mm Round", 1, 600, 9999);
Item ShotgunSlugAmmo("Shotgun Slug Round", 1, 50, 9999);
cout << "DEBUG 5.56MM AMMO CURRENT STACK SIZE: " << FiveFiveSixAmmo.GetCurrentStackSize() << "\n\n";
Weapon Rifle("Rifle", 4, 1, 1, 30, 9999, 7, FiveFiveSixAmmo.GetCurrentStackSize());
Weapon Handgun("Handgun", 2, 1, 1, 8, 9999, 8, NineMMAmmo.GetCurrentStackSize());
Weapon Shotgun("Shotgun", 5, 1, 1, 7, 9999, 8, ShotgunSlugAmmo.GetCurrentStackSize());
cout << "DEBUG: CURRENT RIFLE MAGAZINE COUNT: " << Rifle.GetCurrentMagazineCount() << "\n\n";
PlayerInventory.AddItem(Rifle);
PlayerInventory.AddItem(Handgun);
PlayerInventory.AddItem(Shotgun);
PlayerInventory.AddItem(ShotgunSlugAmmo);
PlayerInventory.AddItem(NineMMAmmo);
PlayerInventory.AddItem(FiveFiveSixAmmo);
cout << "DEBUG: RIFLE MAGAZINE SIZE: " << Rifle.GetCurrentMagazineCount() << "\n";
cout << "DEBUG: HANDGUN MAGAZINE SIZE: " << Handgun.GetCurrentMagazineCount() << "\n";
cout << "DEBUG: SHOTGUN MAGAZINE SIZE: " << Shotgun.GetCurrentMagazineCount() << "\n";
int choice { 0 };
int chooseItem { 0 };
PlayerInventory.GetItem("Rifle")->Reload(FiveFiveSixAmmo);
cout << "DEBUG: CURRENT RIFLE MAGAZINE COUNT: " << Rifle.GetCurrentMagazineCount() << "\n";
cout << "DEBUG 5.56MM AMMO CURRENT STACK SIZE: " << FiveFiveSixAmmo.GetCurrentStackSize() << "\n\n";
while (choice != 4) {
cout << "===MAIN MENU===\n\n";
cout << "1) Weapon Testing\n";
cout << "2) Remove Item\n";
cout << "3) Show Inventory\n";
cout << "4) Quit\n";
cout << ">";
cin >> choice;
string name { "" };
cout << '\n';
switch (choice) {
case 1:
cout << "Which weapon would you like to test?\n\n";
PlayerInventory.DisplayContents();
cin >> chooseItem;
break;
case 2:
cout << "Which item would you like to remove?\n\n";
PlayerInventory.DisplayContents();
getline(cin, name);
break;
case 3:
PlayerInventory.DisplayContents();
break;
case 4:
cout << "Goodbye!";
return 0;
break;
default:
cout << "You have made an invalid choice\n\n";
}
}
}
| |