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

Go to the documentation of this file.
00001 #include "bullet.h"
00002 #include "collisionhandler.h"
00003 #include "level.h"
00004 #include "Screen.h"
00005 #include "Command.h"
00006 #include "CommandManager.h"
00007 
00008 #include <iostream>
00009 
00010 using namespace std;
00011 
00012 
00013 Bullet::Bullet( Person* shooter, int bulletSpeed, int damage, int which )
00014 : _shooter( shooter ),
00015   _xVelocity( 0 ),
00016   _yVelocity( 0 ),
00017   _xDirection( shooter->getXDirection() ),
00018   _yDirection( shooter->getYDirection() ),
00019   _damage( damage )
00020 {
00021         _depth = 1;
00022                         
00023         _solid = 0;
00024         string filename;
00025         if (_yDirection == MIDDLE)
00026         {
00027                 _xVelocity = bulletSpeed * (int)_xDirection;
00028                 filename = "sprites/bullet.png";
00029         }
00030         else if ( _yDirection == DOWN)
00031         {
00032                 _yVelocity = bulletSpeed * (int)_yDirection;
00033                 filename = "sprites/bulletDown.png";
00034         }
00035         else
00036         {
00037                 _yVelocity = bulletSpeed * (int)_yDirection;
00038                 filename = "sprites/bulletUp.png";
00039         }
00040 
00041         _type = BULLET;
00042         _sprite = Sprite::loadSprite(filename);
00043         _sprite->setDirection( _xDirection );
00044         _box.w = _sprite->width();
00045         _box.h = _sprite->height();
00046 
00047         SDL_Rect shooterBox = _shooter->getBox();
00048 
00049         if( _xDirection == LEFT )
00050         {
00051                 _box.x = shooterBox.x - _box.w;
00052         }
00053         else
00054         {
00055                 _box.x = shooterBox.x + shooterBox.w;
00056         }
00057 
00058         if ( _yDirection == UP )
00059         {
00060                 _box.y = shooterBox.y - _box.h;
00061 
00062                 switch(which) {
00063                         case 0:
00064                                 _box.x = shooterBox.x + shooterBox.w/2;
00065                                 break;
00066                         case 1:
00067                                 _box.x = shooterBox.x;
00068                                 break;
00069                         case 2:
00070                                 _box.x = shooterBox.x + shooterBox.w/3;
00071                                 break;
00072                         case 3:
00073                                 _box.x = shooterBox.x + shooterBox.w*2.0/3.0;
00074                                 break;
00075                         case 4:
00076                                 _box.x = shooterBox.x + shooterBox.w;
00077                                 break;
00078                 }
00079         }
00080         else if ( _yDirection == MIDDLE )
00081         {
00082                 switch(which) {
00083                         case 0:
00084                                 _box.y = shooterBox.y + shooterBox.h/3;
00085                                 break;
00086                         case 1:
00087                                 _box.y = shooterBox.y;
00088                                 break;
00089                         case 2:
00090                                 _box.y = shooterBox.y + shooterBox.h/4;
00091                                 break;
00092                         case 3:
00093                                 _box.y = shooterBox.y + shooterBox.h/2;
00094                                 break;
00095                         case 4:
00096                                 _box.y = shooterBox.y + shooterBox.h*3.0/4.0;
00097                 }
00098         }
00099         else
00100         {
00101                 _box.y = shooterBox.y + shooterBox.h;
00102 
00103                 switch(which) {
00104                         case 0:
00105                                 _box.x = shooterBox.x + shooterBox.w/2;
00106                                 break;
00107                         case 1:
00108                                 _box.x = shooterBox.x;
00109                                 break;
00110                         case 2:
00111                                 _box.x = shooterBox.x + shooterBox.w/3;
00112                                 break;
00113                         case 3:
00114                                 _box.x = shooterBox.x + shooterBox.w*2.0/3.0;
00115                                 break;
00116                         case 4:
00117                                 _box.x = shooterBox.x + shooterBox.w;
00118                                 break;
00119                 }
00120         }
00121 }
00122 
00123 Object *Bullet::copy()
00124 {
00125         Bullet *copy = new Bullet(*this);
00126         // NOTE: Shooter may be incorrect at this point, hopefully it gets set later.
00127         // FIXME: Lord, is there a better way?!
00128         return copy;
00129 }
00130 
00131 void Bullet::draw()
00132 {
00133         _sprite->setDirection( _xDirection );
00134         _sprite->draw(_box.x,_box.y);
00135 }
00136 
00137 void Bullet::update()
00138 {
00139         // TODO: Fix collisions
00140         _box.x += ((double)_xVelocity/Screen::FRAMERATE);
00141         CollisionHandler::instance()->checkCollision(this);
00142         
00143         _box.y += ((double)_yVelocity/Screen::FRAMERATE);
00144         CollisionHandler::instance()->checkCollision(this);
00145 
00146         SDL_Rect screenBox = Screen::instance()->getScreenRect();
00147 
00148         if(!glfuncs::intersect(_box, screenBox))
00149                 Level::instance()->scheduleDelete(this);
00150 }
00151 
00152 void Bullet::collide( Object* o )
00153 {
00154         if( o == _shooter || o->getType() == BULLET )
00155                 return;
00156 
00157         if( o->getType() == PLAYER || o->getType() == ENEMY || o->getType() == BOSS )
00158         {
00159                 ((Person*)o)->addHealth( -_damage );
00160                 //SOUNDFX
00161                 if((o->getType() == PLAYER) && (((Person*)o)->getHealth() > 0))
00162                         PlaySoundCommand("sounds/grunt_male30.wav").execute();
00163         }
00164 
00165         _collidable = false;
00166         Level::instance()->scheduleDelete( this );
00167 }

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