C:/Documents and Settings/jegan/Desktop/projectX/Weapon.cpp

Go to the documentation of this file.
00001 #include "Weapon.h"
00002 #include "Level.h"
00003 #include "Parser.h"
00004 #include "Bullet.h"
00005 #include "Sprite.h"
00006 #include "glfuncs.h"
00007 #include "Command.h"
00008 #include "CommandManager.h"
00009 
00010 #include <sstream>
00011 
00012 using namespace std;
00013 
00014 Weapon::Weapon()
00015 {
00016 
00017 }
00018 
00019 Weapon::Weapon(std::string filename, Person *shooter)
00020 :_waitShoot(15), _framesSinceShoot(0), _bulletSpeed(500), _ammo(0), _ammoSize(0), _id(""), _shooter(shooter) ,_icon(NULL),
00021 _damage(1),_spray(false),_sprayCount(0)
00022 {
00023         List *info = Parser::parse(filename);
00024 
00025         load(info);
00026 
00027         delete info;
00028 }
00029 
00030 Weapon *Weapon::copy(Person *shooter)
00031 {
00032         Weapon *copy = new Weapon(*this);
00033         copy->_shooter = shooter;
00034         return copy;
00035 }
00036 
00037 void Weapon::load(List *info)
00038 {
00039         _id = info->firstString();
00040         info = info->rest();
00041 
00042         while(!info->empty()) {
00043                 List *current = info->firstList();
00044 
00045                 if(current->firstString() == "waitShoot")
00046                         _waitShoot = current->rest()->firstInt();
00047                 else if(current->firstString() == "bulletSpeed")
00048                         _bulletSpeed = current->rest()->firstInt();
00049                 else if(current->firstString() == "ammo")
00050                         _ammo = current->rest()->firstInt();
00051                 else if(current->firstString() == "ammoSize")
00052                         _ammoSize = current->rest()->firstInt();
00053                 else if(current->firstString() == "icon")
00054                         _icon = Sprite::loadSprite(current->rest()->firstString());
00055                 else if(current->firstString() == "damage")
00056                         _damage = current->rest()->firstInt();
00057                 else if(current->firstString() == "spray")
00058                         _spray = true;
00059 
00060                 info = info->rest();
00061         }
00062 }
00063 
00064 void Weapon::addAmmo(int dAmmo)
00065 {
00066         PlaySoundCommand("sounds/gun_boltclose.wav").execute();
00067         _ammo += dAmmo;
00068         PlaySoundCommand("sounds/gun_boltclose_b.wav").execute();
00069 }
00070 
00071 SDL_Rect Weapon::drawAmmo(SDL_Rect raster)
00072 {
00073         //glfuncs::instance()->glRasterPos2f(raster.x,raster.y);
00074         stringstream ammo;
00075         ammo << "Ammo: " << _ammo;
00076         glfuncs::instance()->glPrint(raster.x,raster.y,ammo.str());
00077         raster.y += 30;
00078 
00079         //glfuncs::instance()->glEnable(GL_TEXTURE_2D);
00080         _icon->draw(raster.x,raster.y,Sprite::SCREEN);
00081         //glfuncs::instance()->glDisable(GL_TEXTURE_2D);
00082 
00083         return raster;
00084 }
00085 
00086 void Weapon::shoot()
00087 {
00088         if(_ammoSize <= _ammo && _framesSinceShoot >= _waitShoot)
00089         {
00090                 //This is here to make sure that the sound effect only plays once per shot
00091                 //And we check to see that it's goudy because we'll want a much softer sound
00092                 //for enemy shots
00093                 if(_shooter->getId() == "goudy1")
00094                 {
00095                         if(_id == "goudyPistol")
00096                                 PlaySoundCommand("sounds/gunshot01.wav").execute();
00097                         else if(_id == "machineGun")
00098                                 PlaySoundCommand("sounds/goudymachinegun.wav").execute();
00099                 }
00100                 else
00101                 {
00102                         if(_id == "easyPistol")
00103                                 PlaySoundCommand("sounds/gun_glock9mm_02.wav").execute();
00104                         else if(_id == "machineGun")
00105                                 PlaySoundCommand("sounds/enemymachinegun_b.wav").execute();
00106                 }
00107 
00108                 if(_spray) {
00109                         _sprayCount++;
00110                         _sprayCount %= 4;
00111                         Level::instance()->registerObject( new Bullet( _shooter, _bulletSpeed, _damage, _sprayCount+1 ) );
00112                 }
00113                 else {
00114                         Level::instance()->registerObject( new Bullet( _shooter, _bulletSpeed, _damage ) );
00115                 }
00116                 _framesSinceShoot = 0;
00117                 _ammo -= _ammoSize;
00118         }
00119 }
00120 
00121 void Weapon::update()
00122 {
00123         _framesSinceShoot++;
00124 }

Generated on Fri May 5 00:20:19 2006 for ProjectX by  doxygen 1.4.6-NO