-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cpp
More file actions
121 lines (109 loc) · 3.33 KB
/
Player.cpp
File metadata and controls
121 lines (109 loc) · 3.33 KB
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
/*******************************************************************************
* ** Author: Gavin Slusher
* ** Date: 6/2/2019
* ** Description: Player class implementation file. Contains logic for the
* main player within the game. The class inherits from the
* MapObjects class, but adds in an additional MapObjects
* vector to hold more MapObjects as items, along with methods
* to manipulate the container.
* ** *************************************************************************/
#include <iostream>
#include <vector>
#include <string>
#include "Items.hpp"
#include "MapObjects.hpp"
#include "Player.hpp"
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::vector;
/*******************************************************************************
* Player::Player()
* Constructor for the Player.
*******************************************************************************/
Player::Player()
:MapObjects('A', false, false, false, " ")
{
//player's icon is 't'
}
/*******************************************************************************
* Player::~Player()
* Destructor for the player. Needs to deallocate items.
*******************************************************************************/
Player::~Player()
{
for (int i = 0; i < items.size(); i++)
{
if (items[i] != nullptr)
{
delete items[i];
}
}
items.clear();
}
/*******************************************************************************
* void Player::addItem(MapObjects *&itemIn)
* Method to add an item to the item vector.
*******************************************************************************/
void Player::addItem(MapObjects *&itemIn)
{
if (items.size() < MAX_INVENTORY)
{
items.push_back(itemIn);
}
else
{
cout << "Inventory full." << endl;
}
}
/*******************************************************************************
* Player::displayInv()
* Method to display inventory
*******************************************************************************/
void Player::displayInv()
{
if (items.empty())
{
cout << "You're not carrying anything special" << endl;
}
else
{
cout << "Inventory" << endl;
cout << "---------" << endl;
for (int i = 0; i < items.size(); i++)
{
cout << i+1 << ": " << items[i]->getName() << endl;
}
}
}
/*******************************************************************************
* Player::searchInv()
* Method to search through inventory
*******************************************************************************/
bool Player::searchInv(string &stringIn)
{
if (items.empty())
{
cout << "You're not carrying anything" << endl;
return false;
}
else
{
for (int i = 0; i < items.size(); i++)
{
if (stringIn == items[i]->getName())
{
return true;
}
}
}
}
/*******************************************************************************
* Player::checkInvSize()
* Method to return inventory size
*******************************************************************************/
int Player::checkInvSize()
{
return items.size();
}