-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChest.cpp
More file actions
176 lines (146 loc) · 4.97 KB
/
Chest.cpp
File metadata and controls
176 lines (146 loc) · 4.97 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
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
#include "Chest.h"
Chest::Chest() {
if (!font.loadFromFile("Fonts/light_pixel-7.ttf")) {
std::cout << "Could not load file" << std::endl;
}
if (!openSound.loadFromFile("SFX/Chest/ChestOpen.wav")) {
std::cout << "Could not load file" << std::endl;
}
sound.setBuffer(openSound);
std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
std::uniform_real_distribution<> dis(0.f,1.f);
float n = dis(gen);
if (n < .45f) {
fillChestWeapons();
}
else if(n<.9f){
fillChestPotions();
}
else {
fillChestWeapons();
fillChestPotions();
}
choicePrompt.setFont(font);
choicePrompt.setCharacterSize(16);
choicePrompt.setString("Press Y to swap weapons or leave room");
std::cout << "Chest size: " <<weaponContents.size()+potionContents.size()<< std::endl;
weaponAcquired.setFont(font);
weaponAcquired.setCharacterSize(16);
potionAcquired.setFont(font);
potionAcquired.setCharacterSize(16);
}
Chest::~Chest() {
}
void Chest::Open(Player *player) {
if(!opened) {
if (potionContents.size() == 1) {
opened = true;
potionAcquired.setString(potionContents[0]->getName() + " Acquired");
potionAcquired.setPosition(player->getPlayer().getPosition().x - potionAcquired.getGlobalBounds().width/2, player->getPlayer().getPosition().y - 60);
player->getPotions().push_back(potionContents[0]);
player->setCurrentPotion(potionContents[0]);
std::cout << potionContents[0]->getName() << " added" << std::endl;
}
if (weaponContents.size() == 1) {
if(player->getWeapons().size() == 1){
opened = true;
weaponAcquired.setString(weaponContents[0]->getName() + " Acquired");
weaponAcquired.setPosition(player->getPlayer().getPosition().x - weaponAcquired.getGlobalBounds().width/2, player->getPlayer().getPosition().y - 80);
std::cout << weaponContents[0]->getName() << " added" << std::endl;
player->getWeapons().push_back(*weaponContents[0]);
} else{
opened = false;
}
}
if(opened){
if (closedTexture.loadFromFile("Sprites/Map/openchest.png")) {
chest.setTexture(closedTexture);
}
}
}
sound.play();
}
void Chest::takeGun(Player* player){
opened = true;
weaponAcquired.setString(weaponContents[0]->getName() + " Acquired");
weaponAcquired.setPosition(player->getPlayer().getPosition().x - weaponAcquired.getGlobalBounds().width/2, player->getPlayer().getPosition().y - 80);
std::cout << weaponContents[0]->getName() << " added" << std::endl;
if(player->getCurrentWeapon().getName() != "M1911"){
player->setWeapon(*weaponContents[0]);
} else{
player->switchWeapons();
player->setWeapon(*weaponContents[0]);
}
if (closedTexture.loadFromFile("Sprites/Map/openchest.png")) {
chest.setTexture(closedTexture);
}
}
void Chest::leaveGun(Player* player){
}
std::vector<Weapon *>& Chest::getWeaponContents() {
return weaponContents;
}
std::vector<Potion *>& Chest::getPotionContents() {
return potionContents;
}
void Chest::fillChestWeapons() {
std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
std::uniform_real_distribution<> dis(0.f, 1.f);
float r = dis(gen);
int i = 0;
for(std::map<std::string,Weapon *>::iterator it = Weapon::weaponList.begin();it!=Weapon::weaponList.end()&&r>=0;it++) {
r -= it->second->getDropChance();
if (r<=0) {
weaponContents.push_back(it->second);
}
};
}
sf::Sprite Chest::getChestSprite(){
return chest;
}
void Chest::setData(float x, float y){
if (openTexture.loadFromFile("Sprites/Map/chest.png")) {
chest.setTexture(openTexture);
chest.setOrigin(chest.getGlobalBounds().width / 2, chest.getGlobalBounds().height / 2);
chest.setPosition(x, y);
}
}
void Chest::fillChestPotions() {
std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
std::uniform_real_distribution<> dis(0.f, 1.f);
float r = dis(gen);
if (r < .2f) {
potionContents.push_back(new HealthPotion());
}
else if (r < .4f) {
potionContents.push_back(new StaminaPotion());
}
else if (r < .6f) {
potionContents.push_back(new SpeedPotion());
}
else if (r<.8f) {
potionContents.push_back(new AttackPotion());
}
else {
potionContents.push_back(new TimePotion());
}
}
bool Chest::getIsOpen(){
return opened;
}
sf::Text Chest::getWeaponAcquired() {
return weaponAcquired;
}
sf::Text Chest::getPotionAcquired() {
return potionAcquired;
}
void Chest::setUI(Player* player, sf::RenderWindow& window){
if(weaponContents.size() == 1 && opened){
weaponAcquired.setPosition(player->getPlayer().getPosition().x - weaponAcquired.getGlobalBounds().width/2, player->getPlayer().getPosition().y - 85);
window.draw(weaponAcquired);
}
if(potionContents.size() == 1){
potionAcquired.setPosition(player->getPlayer().getPosition().x -potionAcquired.getGlobalBounds().width/2, player->getPlayer().getPosition().y - 60);
window.draw(potionAcquired);
}
}