/
BackPack.cpp
209 lines (186 loc) · 5.55 KB
/
BackPack.cpp
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
#include "BackPack.h"
using namespace cocos2d;
BackPack::m_sItems BackPack::m_asItems[TOTAL_ITEMS];
const char* BackPack::m_aActionList[TOTAL_ACTIONS];
// Store the category data in a char array,
// then load them into a ccarray when we need them!
CCArray* BackPack::getItmArray(bool zeros)
{
// create array for transferring purposes
CCArray* aItm = CCArray::create();
for (int i=0 ; i < sizeof(m_asItems)/TOTAL_ITEMS ; i++) // divide by the size of the struct to get length
{
if (zeros == true) // items with no stock will be included in array
{
if (m_asItems[i].filename == NULL);
else
{
aItm->addObject(CCString::create(m_asItems[i].filename));
//CCLog("filename object added to array");
}
}
else if (zeros == false) // items with no stock will NOT be included in array
if (m_asItems[i].filename == NULL || m_asItems[i].stock <= 0);
else
{
aItm->addObject(CCString::create(m_asItems[i].filename));
//CCLog("filename object added to array");
}
}
return aItm;
}
CCArray* BackPack::getAxnArray()
{
const int BASIC_SIZE = 4;
const char* basic_actions[BASIC_SIZE] = {"Break","Climb","Push","Leap"};
for (int i=0 ; i < BASIC_SIZE ; i++)
m_aActionList[i] = basic_actions[i];
// create array for transferring purposes
CCArray* actionArray = CCArray::create();
// load string objects into array
for (int i=0 ; i < sizeof(m_aActionList)/BASIC_SIZE ; i++)
{
if (m_aActionList[i] == NULL);
else
actionArray->addObject(CCString::create(m_aActionList[i]));
}
return actionArray;
}
void BackPack::addAxn(int position, const char* text)
{
// store action names in array
// bypass first four default actions before adding
m_aActionList[position+4] = text;
//CCLog("added to action array");
}
int BackPack::getItmStock(const char* filename, bool zeros)
{
//return item amount currently in stock
int check = 0;
CCString* nameCheck;
//CCLog("filename is %s",filename);
// run loop to find matching filename in m_asItems
for (int i=0 ; i < sizeof(m_asItems)/TOTAL_ITEMS ; i++) // divide by the size of a struct to get length
{
nameCheck = CCString::createWithFormat("%s",m_asItems[i].filename);
//CCLog("%s",m_asItems[i].filename);
if (zeros == true)
if (m_asItems[i].filename == NULL);
else
{
// use compare fxn for strings
if (nameCheck->compare(filename) == 0)
{
//CCLog("got a match");
check = m_asItems[i].stock;
}
}
else if (zeros == false)
if (m_asItems[i].filename == NULL || m_asItems[i].stock == 0);
else
{
// use compare fxn for strings
if (nameCheck->compare(filename) == 0)
{
//CCLog("got a match");
check = m_asItems[i].stock;
}
}
}
return check;
}
void BackPack::useItmStock(const char* filename)
{
// Use an item
CCString* nameCheck;
//CCLog("filename is %s",filename);
// run loop to find matching filename in m_asItems
for (int i=0 ; i < sizeof(m_asItems)/TOTAL_ITEMS ; i++) // divide by the size of the struct to get length
{
nameCheck = CCString::createWithFormat("%s",m_asItems[i].filename);
//CCLog("%s",m_asItems[i].filename);
//if (m_asItems[i].filename == NULL || m_asItems[i].stock == 0);
// use compare fxn for strings
if (nameCheck->compare(filename) == 0)
{
//CCLog("got a match");
// reduce stock by 1
m_asItems[i].stock-=1;
}
}
}
const char* BackPack::getItmType(const char* filename)
{
// return monster, obstacle, or health type item
CCString* nameCheck;
//CCLog("filename is %s",filename);
// run loop to find matching filename in m_asItems
for (int i=0 ; i < sizeof(m_asItems)/TOTAL_ITEMS ; i++) // divide by the size of the struct to get length
{
nameCheck = CCString::createWithFormat("%s",m_asItems[i].filename);
//CCLog("%s",m_asItems[i].filename);
//if (m_asItems[i].filename == NULL || m_asItems[i].stock == 0);
// use compare fxn for strings
if (nameCheck->compare(filename) == 0)
{
//CCLog("got a match");
// return type (monster, obstacle, or "")
if (m_asItems[i].type == NULL)
return "";
else
return m_asItems[i].type;
}
}
return NULL;
}
int BackPack::getItmDice(const char* filename)
{
// return dice bonus amount
CCString* nameCheck;
//CCLog("filename is %s",filename);
// run loop to find matching filename in m_asItems
for (int i=0 ; i < sizeof(m_asItems)/TOTAL_ITEMS ; i++) // divide by the size of the struct to get length
{
nameCheck = CCString::createWithFormat("%s",m_asItems[i].filename);
//CCLog("%s",m_asItems[i].filename);
//if (m_asItems[i].filename == NULL || m_asItems[i].stock == 0);
// use compare fxn for strings
if (nameCheck->compare(filename) == 0)
{
//CCLog("got a match");
// return dice number
return m_asItems[i].dice;
}
}
return NULL;
}
void BackPack::addItm(int position, const char* filename, const char* type, int dice)
{
// add to item inventory array
// determines if item needs to be created or it's stock increased
int inStock = 0;
// if filename is in m_asItems array, increase stock by 1
for (int i=0 ; i < TOTAL_ITEMS ; i++)
{
if (m_asItems[i].filename == filename)
{
m_asItems[i].stock++;
//CCLog("item's stock increased by 1");
inStock++;
}
}
// if item's filename is not in m_asItems array, create and add it
if (inStock == 0)
{
m_sItems item;
item.filename = filename;
item.type = type;
item.dice = dice;
item.stock = 1;
// create and store item information in struct array
m_asItems[position] = item;
//CCLog("position is %i",position);
//CCLog("added to temporary item array");
}
//CCLog("item's stock is currently: %i",m_asItems[position].stock);
}