-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeapon.cpp
More file actions
346 lines (298 loc) · 11.5 KB
/
Weapon.cpp
File metadata and controls
346 lines (298 loc) · 11.5 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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include "GameManager.h"
#include "Player.h"
#include "Weapon.h"
#include <exception>
#include <cmath>
std::map<std::string,Weapon *> Weapon::weaponList;
bool Weapon::gunsLoaded;
//Base Constructor
Weapon::Weapon() {
if (!gunsLoaded) {
weaponList["defaultKnife"] = new Weapon("Melee", "Knife", 100000.f, 2, .5f, 0.f);
weaponList["defaultPistol"]= new Weapon("Pistol", "M1911", 88, 88, 8, 8, 10.f, 0.25f, 1.f, 2.5f, 0.f, "SFX/Guns/DefaultPistol.wav", "Sprites/Weapons/m1911.png");
weaponList["heavyPistol"] = new Weapon("Heavy Pistol", "Desert Eagle", 35, 35, 7, 7, 50.f, .5f, 2.f, 2.f, 0.3f, "SFX/Guns/DesertEagle.wav", "Sprites/Weapons/Deagle.png");
weaponList["boltSniper"] = new Weapon("Sniper", "L96A1", 15, 15, 5, 5, 100.f, 2.f, 4.f, 0.f, 0.04f, "SFX/Guns/Sniper.wav","Sprites/Weapons/L9.png");
weaponList["semiAuto"]= new Weapon("SemiAuto", "M14", 60, 60, 10, 10, 25.f, .4f, 2.f, 1.f, 0.1f, "SFX/Guns/AssaultRifle.wav","Sprites/Weapons/m14.png");
weaponList["shotgun"] = new Weapon("Shotgun", "KSG", 30, 30, 6, 6, 20.f, 1.f, 0.f, 5.f, 0.1f, "SFX/Guns/Shotgun.wav", "Sprites/Weapons/KSG.png");
weaponList["assaultRifle"] = new Weapon("Assault Rifle", "AK47", 90, 90, 30, 30, 30.f, .1f, 2.f, 5.f, .1f, "SFX/Guns/AssaultRifle.wav", "Sprites/Weapons/AK47.png");
weaponList["minigun"]= new Weapon("Minigun", "Minigun", 180, 180, 180, 180, 20.f, .05f, 5.f, 7.5f, .06f, "SFX/Guns/Minigun.wav", "Sprites/Weapons/minigun.png");
weaponList["semiSninper"] = new Weapon("Sniper", "Barrett .50 Cal", 30, 30, 10, 10, 80.f, .5f, 4.f, 0.f, .1f, "SFX/Guns/Sniper.wav", "Sprites/Weapons/50cal.png");
weaponList["submachine"] = new Weapon("Submachine Gun", "MP40", 96, 96, 32, 32, 20.f, .2f, 2.f, 2.5f, .2f, "SFX/Guns/Submachine.wav", "Sprites/Weapons/mp40.png");
gunsLoaded = true;
}
}
//Weapon(Melee) Constructor
Weapon::Weapon(std::string type, std::string meleeName, float damage, int range, float attackspeed, float dropChance):name(meleeName),maxAmmo(0),range(range),dropChance(dropChance) {
setDamage(damage);
setAttackSpeed(attackspeed);
}
//Weapon(Ranged) Construtor
Weapon::Weapon(std::string weaponType, std::string Name, int MaxAmmo, int currentMax, int currentClip, int maxClip, float Damage,
float attackSpeed, float reloadTime, float Deviation, float DropChance, std::string soundFX, std::string weaponUI):type(weaponType),name(Name),maxAmmo(MaxAmmo),deviation(Deviation),dropChance(DropChance){
setCurrentClip(currentClip);
setCurrentMax(currentMax);
setDamage(Damage);
setAttackSpeed(attackSpeed);
setMaxClip(maxClip);
setReloadTime(reloadTime);
loadBuffer(FXgunshot, soundFX);
loadBuffer(FXreload, "SFX/Guns/Reload.wav");
fireSound = new sf::Sound(FXgunshot);
reloadSound = new sf::Sound(FXreload);
setUI(weaponUI);
}
//Shoots bullet
void Weapon::Shoot(Player *player,sf::RenderWindow &window) {
if (player->getCurrentWeapon().getCurrentClip() > 0&&!bIsReloading) {
if (player->getCurrentWeapon().getType() == "Shotgun") {
for (int i = 0;i < 12;i++) {
bullets.push_back(createBullet(player, window));
}
}
else {
bullets.push_back(createBullet(player, window));
}
//Subtracting from current ammo
player->getCurrentWeapon().setCurrentClip(player->getCurrentWeapon().getCurrentClip() - 1);
std::thread(&Weapon::playAudioFire, this).detach();
}
else if (player->getCurrentWeapon().getCurrentClip()==0) {
if (!bIsReloading) {
bIsReloading = true;
std::thread(&Weapon::playAudioReload, this).detach();
}
}
}
//Plays Audio
void Weapon::playAudioFire() {
fireSound->play();
}
void Weapon::playAudioReload() {
reloadSound->play();
}
//Creates bullet
Weapon::Bullet Weapon::createBullet(Player* player, sf::RenderWindow &window) {
//Instantiating bullet
Bullet b;
if (player->empowered) {
b.bullet.setFillColor(sf::Color::Red);
}
if (type == "Sniper") {
b.velocity *= 5;
}
b.damage = (player->empowered)?damage*1.5f:damage;
b.bullet.setRadius(2.5f);
b.bullet.setPosition(player->getPlayer().getPosition());
//Setting accuracy
sf::Vector2f WorldCoords = window.mapPixelToCoords(sf::Mouse::getPosition(window));
sf::Vector2f dVector = WorldCoords - player->getPlayer().getPosition();
float newX1 = dVector.x*cos(deviation * 3.14f / 180) - dVector.y*sin(deviation * 3.14f / 180);
float newY1 = dVector.x*sin(deviation * 3.14f / 180) + dVector.y*cos(deviation * 3.14f / 180);
float newX2 = dVector.x*cos(-deviation * 3.14f / 180) - dVector.y*sin(-deviation * 3.14f / 180);
float newY2 = dVector.x*sin(-deviation * 3.14f / 180) + dVector.y*cos(-deviation * 3.14f / 180);
float n = ((newX2 - newX1) * ((float)rand() / RAND_MAX)) + newX1;
float n2 = ((newY2 - newY1) * ((float)rand() / RAND_MAX)) + newY1;
sf::Vector2f newDir = sf::Vector2f(n, n2);
float mag = sqrt(pow(newDir.x, 2) + pow(newDir.y, 2));
b.direction = sf::Vector2f(newDir.x / mag, newDir.y / mag);
return b;
}
//Reloads current weapon
void Weapon::Reload(Player *player) {
int amountToReload = player->getCurrentWeapon().getMaxClip() - player->getCurrentWeapon().getCurrentClip();
if (player->getCurrentWeapon().getCurrentClip() == player->getCurrentWeapon().getMaxClip()) {
std::cout << "Clip Full" << std::endl;
}
else if(player->getCurrentWeapon().getCurrentMax()-player->getCurrentWeapon().getMaxClip()>=0) {
if(player->getCurrentWeapon().getType() != "Pistol") {
player->getCurrentWeapon().setCurrentMax(player->getCurrentWeapon().getCurrentMax() - amountToReload);
}
player->getCurrentWeapon().setCurrentClip(player->getCurrentWeapon().getMaxClip());
std::cout << "Reloading" << std::endl;
}
else if((player->getCurrentWeapon().getCurrentMax() - player->getCurrentWeapon().getMaxClip()<0)){
if (player->getCurrentWeapon().getCurrentMax() - amountToReload > 0) {
player->getCurrentWeapon().setCurrentClip(player->getCurrentWeapon().getMaxClip());
if(player->getCurrentWeapon().getType() != "Pistol") {
player->getCurrentWeapon().setCurrentMax(player->getCurrentWeapon().getCurrentMax() - amountToReload);
}
}
else {
player->getCurrentWeapon().setCurrentClip(player->getCurrentWeapon().getCurrentClip()+player->getCurrentWeapon().getCurrentMax());
player->getCurrentWeapon().setCurrentMax(0);
}
}
else {
std::cout << "Out of ammo" << std::endl;
}
}
//Checks if player is able to reload
void Weapon::canReload(Player *player) {
if (player->getCurrentWeapon().bIsReloading&&player->getCurrentWeapon().bCanReload) {
reloadTimer.restart();
bCanReload = false;
if (type == "Shotgun") {
for (int i = 0; i < maxClip - currentClip;i++) {
reloadTime += .5f;
}
}
}
if (player->getCurrentWeapon().bIsReloading) {
if (player->getCurrentWeapon().reloadTimer.getElapsedTime().asSeconds() >= ((player->triggerhappy)?player->getCurrentWeapon().reloadTime/2:player->getCurrentWeapon().reloadTime)) {
Reload(player);
bCanReload = true;
bIsReloading = false;
if (type == "Shotgun") {
reloadTime = 0.f;
}
}
}
}
//Sets weapon firing audio
void Weapon::loadBuffer(sf::SoundBuffer &buffer, std::string soundfile) {
if (!buffer.loadFromFile(soundfile)) {
std::cout << "Could not load sound from file" << std::endl;
}
}
//Returns weapon max clip
int Weapon::getMaxClip() const {
return maxClip;
}
//Sets weapon reload time
void Weapon::setReloadTime(float reloadTime) {
this->reloadTime = reloadTime;
}
//Updates weapon position
void Weapon::Update(sf::RenderWindow &window, Player *player,float dt){
for (unsigned i = 0;i < bullets.size();i++) {
bullets[i].bullet.move(bullets[i].direction.x*dt*bullets[i].velocity, bullets[i].direction.y*dt*bullets[i].velocity);
if (bullets[i].deleteTime.getElapsedTime().asSeconds() > 3) {
std::cout << "Deleting bullet" << std::endl;
bullets.erase(bullets.begin() + i);
}
}
//weapon.setPosition(player->getPlayer().getPosition().x, player->getPlayer().getPosition().y);
canReload(player);
Draw(window);
}
//Draws weapon to screen
void Weapon::Draw(sf::RenderWindow &window) {
window.draw(weapon);
for (unsigned i = 0;i < bullets.size();i++) {
window.draw(bullets[i].bullet);
}
}
//Sets current clip of weapon
void Weapon::setCurrentClip(int currentClip) {
this->currentClip = currentClip;
}
//Sets current max clip of weapon
void Weapon::setCurrentMax(int currentMax) {
this->currentMax = currentMax;
}
//Sets max clip of weapon
void Weapon::setMaxClip(int maxClip) {
this->maxClip = maxClip;
}
//Sets weapon damage
void Weapon::setDamage(float damage) {
this->damage = damage;
}
//Sets weapon attackspeed
void Weapon::setAttackSpeed(float attackSpeed) {
this->attackSpeed = attackSpeed;
}
//Returns weapon type
std::string Weapon::getType() const{
return type;
}
//Returns weapon name
std::string Weapon::getName() const{
return name;
}
//Returns weapon's max ammo
int Weapon::getMaxAmmo() const{
return maxAmmo;
}
//Returns weapon's current ammo
int Weapon::getCurrentMax() const{
return currentMax;
}
//Returns weapon's current clip
int Weapon::getCurrentClip() const{
return currentClip;
}
//Returns weapon's damage
float Weapon::getDamage() const{
return damage;
}
//Returns weapons' attackspeed
float Weapon::getAttackSpeed() const{
return attackSpeed;
}
//Returns weapon's drop chance
float Weapon::getDropChance() const {
return dropChance;
}
//Sets ammo UI
void Weapon::setUI(std::string weaponUI) {
if (!font.loadFromFile("Fonts/light_pixel-7.ttf")) {
std::cout << "Could not open font" << std::endl;
}
if(weaponTexture.loadFromFile(weaponUI)) {
weaponImage.setTexture(weaponTexture);
}
weaponImage.setOrigin(weaponImage.getGlobalBounds().width/2,weaponImage.getGlobalBounds().height/2);
weaponImage.setScale(0.2,0.2);
ammoCount.setCharacterSize(20);
ammoCount.setFont(font);
gunName.setString(name);
gunName.setFont(font);
gunName.setCharacterSize(15);
}
//Displays ammo UI
void Weapon::displayWeaponInfo(sf::RenderWindow &window) {
//Updates current clip numbers
ammoCount.setString(std::to_string(getCurrentClip()) + "/" + std::to_string(getCurrentMax()));
//sets x location of ammoCount based on current view
int infoLocation = (int)window.getView().getCenter().x + (int)window.getView().getSize().x/2 - 130;
//sets location of ammoCount, gunName and ammoBlocks rely on this for location
ammoCount.setPosition(infoLocation, window.getView().getCenter().y + (window.getView().getSize().y/2) - 50);
gunName.setPosition(ammoCount.getPosition().x - 50, ammoCount.getPosition().y -40);
float offsetX = 0, offsetY = -getMaxClip()/2.f+4, rectSizeX = 5.f, rectSizeY = 15.f, size = 0.f, standardOffset = 40.f, desiredOffset = 200.f;
for (int i = 8;i < getMaxClip();i += 8) {
rectSizeX -= 4.f / 23.f;
}
for (int i = 32;i < getMaxClip();i+=32) {
if (i < i + 32) {
desiredOffset += 100;
}
}
for (int i = 0; i < getMaxClip();i++) {
sf::RectangleShape temp(sf::Vector2f(rectSizeX, rectSizeY));
temp.setPosition(ammoCount.getPosition().x-standardOffset-offsetX,ammoCount.getPosition().y+offsetY);
ammoBlocks.push_back(temp);
offsetX += 20;
if (standardOffset + offsetX>desiredOffset) {
offsetX = 0;
offsetY += temp.getSize().y+5;
}
}
for (int i = ammoBlocks.size()-1;i >= 0;i--) {
if ((unsigned)getCurrentClip()<ammoBlocks.size()&&i==ammoBlocks.size()-1) {
i -= (getMaxClip() - getCurrentClip());
}
if ((unsigned)i < ammoBlocks.size()) {
window.draw(ammoBlocks[i]);
}
}
//clears ammo blocks for next time looped around to get new location.
ammoBlocks.clear();
window.draw(ammoCount);
window.draw(gunName);
}
//Weapon Deconstructor
Weapon::~Weapon() {
delete fireSound;
delete reloadSound;
}