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
|
//============================================================================
// Name : Console Text Shooter
// Author : Chay Hawk
// Version : 0.1.5
// Version Date : December 14th 2022 @ 2:06 PM
// Date Created : October 31st 2022 @ 12:28 PM
// Lines of Code : 273
// Description :
//============================================================================
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <utility>
using std::string;
using std::vector;
using std::cout;
using std::cin;
using std::map;
using std::pair;
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 size, int maxStackSize) : mName(name), mSize(size), mMaxStackSize(maxStackSize)
{}
Item() = default;
string GetName() const { return mName; }
int GetMaxStackSize() const { return mMaxStackSize; }
private:
string mName{ "Weapon Name" };
int mSize{ 0 }; //Space taken up in container
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,
const int maxStackSize,
const int magazineMaxSize,
const int ammoReserveMaxSize,
int magazineCount,
int ammoReserveCount)
: Item{ name, size, maxStackSize },
mMagazineMaxSize(magazineMaxSize),
mAmmoReserveMaxSize(ammoReserveMaxSize),
mMagazineCount(magazineCount),
mAmmoReserveCount(ammoReserveCount)
{}
int GetMagazineMaxSize() const { return mMagazineMaxSize; }
int GetAmmoReserveMaxSize() const { return mAmmoReserveMaxSize; }
int GetMagazineCount() const { return mMagazineCount; }
int GetAmmoReserveCount() const { return mAmmoReserveCount; }
//This may need some revision but I believe its working correctly.
void Reload()
{
if (mAmmoReserveCount > 0)
{
int ammoNeeded = mMagazineMaxSize - mMagazineCount;
cout << ammoNeeded << "\n\n";
if (mAmmoReserveCount >= ammoNeeded)
{
cout << "DEBUG: Weapon reloaded\n\n";
mAmmoReserveCount -= ammoNeeded;
mMagazineCount = mMagazineMaxSize;
}
else
{
mMagazineCount += mAmmoReserveCount;
mAmmoReserveCount = 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 (mMagazineCount > 0)
{
mMagazineCount--;
cout << mMagazineCount << " rounds left\n\n";
}
else
{
cout << "Out of ammo, Reload!\n";
}
}
private:
const int mMagazineMaxSize{ 0 };
const int mAmmoReserveMaxSize{ 0 };
int mMagazineCount{ 0 };
int mAmmoReserveCount{ 0 };
};
class Container
{
public:
Container(const string& name, int containerSize) : mName(name), mContainerSize(containerSize)
{}
void AddItem(Item& item, int amount)
{
if (amount <= item.GetMaxStackSize())
{
mContainer.insert(pair<Item, int>(item, amount));
}
else if(amount > item.GetMaxStackSize())
{
cout << "DEBUG: Item " << item.GetName() << " has exceeded the max stack size, resizing...\n";
mContainer.insert(pair<Item, int>(item, item.GetMaxStackSize()));
}
}
//Remove item by key
void RemoveItem(Item& item)
{
mContainer.erase(item);
}
//Remove item by element
void RemoveItem(int element)
{
//mContainer.erase(element);
}
void DecrementAmountHeld()
{
}
void IncrementAmountHeld()
{
}
void DisplayContents()
{
int enumerate{ 1 };
if (mContainer.empty())
{
cout << "Inventory is empty\n\n";
}
else if (mContainer.size() > 0)
{
for (const auto& i : mContainer)
{
cout << enumerate++ << ") " << i.first << " x " << i.second << "\n";
}
}
cout << '\n';
}
int GetSize() const
{
return mContainer.size();
}
private:
string mName{ "Container Name" };
int mContainerSize{ 0 };
map<Item, int> mContainer;
};
int main()
{
Container PlayerInventory("Player Inventory", 50);
Item NineMMAmmo("9mm Round", 1, 999);
Item FiveFiveSixAmmo("5.56mm Round", 1, 999);
Item ShotgunSlugAmmo("Shotgun Slug Round", 1, 999);
Weapon Rifle("Rifle", 4, 1, 30, 9999, 7, 50);
Weapon Handgun("Handgun", 2, 1, 8, 999, 8, 16);
Weapon Shotgun("Shotgun", 5, 1, 8, 999, 8, 24);
cout << "Rifle Reserve Ammo: " << Rifle.GetAmmoReserveCount() << "\n";
cout << "Rifle Magazine Size: " << Rifle.GetMagazineCount() << "\n\n";
Rifle.Reload();
cout << "Rifle Reserve Ammo: " << Rifle.GetAmmoReserveCount() << "\n";
cout << "Rifle Magazine Size: " << Rifle.GetMagazineCount() << "\n\n";
//Rifle.Fire();
//Rifle.Fire();
//Rifle.Fire();
Rifle.Reload();
cout << "Rifle Reserve Ammo: " << Rifle.GetAmmoReserveCount() << "\n";
cout << "Rifle Magazine Size: " << Rifle.GetMagazineCount() << "\n\n";
//TESTING
cout << "TESTING\n\n";
PlayerInventory.AddItem(Rifle, 1);
PlayerInventory.AddItem(Handgun, 3);
PlayerInventory.AddItem(Shotgun, 2);
PlayerInventory.AddItem(ShotgunSlugAmmo, 500);
PlayerInventory.AddItem(NineMMAmmo, 200);
PlayerInventory.AddItem(FiveFiveSixAmmo, 856);
cout << "\n\nInventory Size: " << PlayerInventory.GetSize() << " items.\n\n";
int choice{ 0 };
int chooseItem{ 0 };
while (choice != 4)
{
cout << "1) Weapon Testing\n";
cout << "2) Remove Item\n";
cout << "3) Show Inventory\n";
cout << "4) Quit\n";
cout << ">";
cin >> choice;
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();
cin >> chooseItem;
PlayerInventory.RemoveItem(chooseItem);
break;
case 3:
PlayerInventory.DisplayContents();
break;
case 4:
cout << "Goodbye!";
return 0;
break;
default:
cout << "You have made an invalid choice\n\n";
}
}
}
| |