/
Player.cpp
105 lines (88 loc) · 2.58 KB
/
Player.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
#include "Player.h"
#include <iostream>
Player::Player(float X, float Y, int W, int H, char* name, int MAXHP, int MAXMP, char* filename)
: Character(X, Y, W, H, name, 0, 0, filename)
{
MaxHP = MAXHP;
HP = MaxHP;
MaxMP = MAXMP;
MP = MaxMP;
MaxXP = 100;
XP = 20;
MaxHP = 100;
level = 1;
monies = 1500;
invSize = 0;
maxInvSize = 50;
invActive = true;
maxVel = 2.5;
for (int i = 0; i < maxInvSize; i++)
inventory[i] = NULL;
/* Head = NULL;
Torso = NULL;
Gloves = NULL;
Legs = NULL;
Feet = NULL;
Necklace = NULL;
LeftHand = NULL;
RightHand = NULL;
spellBook.push_back(new SpellInfo("Ice Frost", 17, eIce, 5));*/
targetNPC = NULL;
addItem(new Item(60, 60, 32, 32, "Potion", "Increases your Health by 100.", "test/item/gPotion.png"));
addItem(new Item(60, 60, 32, 32, "Mana", "Increases your Mana by 100.", "test/item/gManaPotion.png"));
addItem(new Item(60, 60, 32, 32, "Carbon", "Atomic No. 6.", "test/item/element/carbon.png"));
addItem(new Item(60, 60, 32, 32, "Oxygen", "Atomic No. 8.", "test/item/element/oxygen.png"));
addItem(new Item(420, 69, 32, 32, "Hydrogen", "Atomic No. 1", "test/item/element/hydrogen.png"));
}
void Player::addItem(Item* i){
//inventory.push_back(i);
for (int j = 0; j < invSize; j++){
// std::cout << i->getName() << " " << inventory[j]->getName() << std::endl;
//Need to cast them to strings to have them equal each other. Lua string to char* doesn't seem to work.
if (std::string(i->getName()) == std::string(inventory[j]->getName()) && i->isStackable())
{
// std::cout << inventory[j]->getNumberOf() << std::endl;
inventory[j]->incrNum();
EntityManager::getInstance()->Remove(i->getIndex());
// std::cout << inventory[j]->getNumberOf() << std::endl;
return;
}
}
if (invSize < 45){
inventory[invSize] = i;
inventory[invSize]->setHardPosition((float)((invSize % 4)*getW()), (float)(invSize / 4 * i->getH()));
invSize++;
}
else
{
delete i;
}
}
void Player::removeItem(int index){ //index = 15; size 16
EntityManager::getInstance()->Remove(index);
//if (index < inventory.size())
// inventory.erase(inventory.begin()+index);
//if (index < invSize)
//return inventory[index];
}
void Player::useItem(lua_State* L, Item* i){
luaL_dofile(L, i->getScript());
for (int j = 0; j < invSize; j++){
if (i == getInv()[j])
index = j;
}
i->decrNum();
if (i->getNumberOf() <= 0){
for (int j = index + 1; j < invSize; j++){
inventory[j - 1] = inventory[j];
}
inventory[invSize - 1] = NULL;
invSize--;
}
}
void Player::toggleInventory(){
if (invActive)
invActive = false;
else if (!invActive)
invActive = true;
}