1 #include <sstream>
2
3 #include "WeaponManager.h"
4
5 namespace violetland {
6
WeaponManager(FileUtility * fileUtility,SoundManager * sndManager)7 WeaponManager::WeaponManager(FileUtility* fileUtility, SoundManager* sndManager) {
8 std::cout << "Loading weapons..." << std::endl;
9
10 m_fileUtility = fileUtility;
11 m_sndManager = sndManager;
12
13 boost::filesystem::path weaponsPath =
14 m_fileUtility->getFullPath(FileUtility::weapon, "");
15 std::vector<std::string> weapons = m_fileUtility->getSubDirsFromDir(
16 weaponsPath);
17
18 std::cout << "Total weapons found: " << weapons.size() << std::endl;
19
20 if (weapons.size() == 0) {
21 std::cout << "Couldn't load weapons, the program won't run!"
22 << std::endl;
23 exit(6);
24 }
25
26 for (int i = 0; i < (int) weapons.size(); i++) {
27 if (weapons.at(i) == "PM") {
28 weapons.at(i) = weapons.back();
29 weapons.back() = "PM";
30 std::cout << "EDIT: Putting PM to the end of list!" << std::endl;
31 break;
32 }
33 }
34
35 for (unsigned int j = 0; j < weapons.size(); j++) {
36 boost::filesystem::path weaponPath(weaponsPath);
37 weaponPath /= weapons[j];
38 Texture* wImage = new Texture(ImageUtility::loadImage(
39 boost::filesystem::path(weaponPath) /= "image.png"),
40 GL_TEXTURE_2D, GL_LINEAR, true);
41
42 Texture* pImage = new Texture(ImageUtility::loadImage(
43 boost::filesystem::path(weaponPath) /= "player.png"),
44 GL_TEXTURE_2D, GL_LINEAR, true);
45
46 Weapon *weapon = new Weapon(wImage, pImage,
47 sndManager->create(boost::filesystem::path(weaponPath) /= "shot.ogg"),
48 sndManager->create(boost::filesystem::path(weaponPath) /= "reload.ogg"));
49
50 weapon->Name = weapons[j];
51
52 boost::filesystem::ifstream in;
53 in.open(boost::filesystem::path(weaponPath) /= "stats");
54 if (!in) {
55 std::cerr << "Couldn't load stats of weapon " << weapons[j] << '.'
56 << std::endl;
57 exit(4);
58 }
59
60 std::string shellName;
61 std::string strbuf;
62
63 getline(in, strbuf, ' ');
64 weapon->Type = (BulletType) strtol(strbuf.c_str(), NULL, 10);
65
66 if (weapon->Type == BULLET_FLAME)
67 weapon->setBulletImage(new Texture(ImageUtility::loadImage(
68 boost::filesystem::path(weaponPath) /= "bullet.png"),
69 GL_TEXTURE_2D, GL_LINEAR, true));
70
71 getline(in, shellName, ' ');
72 in >> weapon->AmmoClipSize;
73 weapon->Ammo = weapon->AmmoClipSize;
74 in >> weapon->Damage;
75 in >> weapon->FireDelayTime;
76 in >> weapon->ReloadTime;
77 in >> weapon->FireRange;
78 in >> weapon->BulletSpeed;
79 in >> weapon->ReturnForce;
80 in >> weapon->BulletsAtOnce;
81 in >> weapon->XDiff;
82 in >> weapon->YDiff;
83
84 in.close();
85
86 if (shellName != "no")
87 loadShellSprite(weapon, shellName);
88
89 Weapons.push_back(weapon);
90 }
91
92 std::cout << "Loading of weapons is completed." << std::endl;
93 }
94
loadShellSprite(Weapon * _weapon,std::string & _shellName)95 void WeaponManager::loadShellSprite(Weapon* _weapon, std::string& _shellName)
96 {
97 std::vector<SDL_Surface*> animSurfaces;
98
99 boost::filesystem::path shellDir =
100 m_fileUtility->getFullPath(FileUtility::anima, "shells");
101 shellDir /= _shellName;
102 unsigned framesCount =
103 m_fileUtility->getFilesCountFromDir(shellDir);
104
105 std::cout << "Shell animation of " << _weapon->Name << " - " << _shellName
106 << ", frames count: " << framesCount << '.' << std::endl;
107
108 for (unsigned i = 0; i < framesCount; i++) {
109 std::ostringstream filename;
110 filename << i << ".png";
111
112 SDL_Surface *surface =
113 ImageUtility::loadImage(boost::filesystem::path(shellDir) /= filename.str());
114
115 animSurfaces.push_back(surface);
116 }
117
118 _weapon->ShellSprite = new Sprite(animSurfaces);
119 }
120
getWeaponByName(std::string name)121 Weapon* WeaponManager::getWeaponByName(std::string name) {
122 for (unsigned int i = 0; i < Weapons.size(); i++) {
123 if (Weapons[i]->Name == name)
124 return Weapons[i];
125 }
126 return NULL;
127 }
128
~WeaponManager()129 WeaponManager::~WeaponManager() {
130 for (unsigned int i = 0; i < Weapons.size(); i++) {
131 Weapons[i]->deleteResources();
132 delete Weapons[i];
133 }
134 Weapons.clear();
135 }
136
137 }
138