C:/Documents and Settings/mtauraso/Desktop/proj3/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 
00006 #include <iostream>
00007 
00008 using namespace std;
00009 
00010 
00011 Bullet::Bullet( Person* shooter, int bulletSpeed, xDirection xDir, yDirection yDir )
00012 : _shooter( shooter ),
00013   _xVelocity( 0 ),
00014   _yVelocity( 0 ),
00015   _xDirection( xDir ),
00016   _yDirection( yDir ),
00017   _damage( 1 )
00018 {
00019                         
00020         string filename;
00021         if (_yDirection == MIDDLE)
00022         {
00023                 _xVelocity = bulletSpeed * (int)_xDirection;
00024                 filename = "sprites/bullet.png";
00025         }
00026         else if ( _yDirection == DOWN)
00027         {
00028                 _yVelocity = bulletSpeed * (int)_yDirection;
00029                 filename = "sprites/bulletDown.png";
00030         }
00031         else
00032         {
00033                 _yVelocity = bulletSpeed * (int)_yDirection;
00034                 filename = "sprites/bulletUp.png";
00035         }
00036 
00037         _type = BULLET;
00038         _sprite = Sprite::loadSprite(filename);
00039         _sprite->setDirection( xDir );
00040         _box.w = _sprite->width();
00041         _box.h = _sprite->height();
00042 
00043         SDL_Rect shooterBox = _shooter->getBox();
00044 
00045         if( xDir == LEFT )
00046         {
00047                 _box.x = shooterBox.x - _box.w;
00048         }
00049         else
00050         {
00051                 _box.x = shooterBox.x + shooterBox.w;
00052         }
00053 
00054         if ( yDir == UP )
00055         {
00056                 _box.y = shooterBox.y - _box.h;
00057                 _box.x = shooterBox.x + shooterBox.w/2;
00058         }
00059         else if ( yDir == MIDDLE )
00060         {
00061                 _box.y = shooterBox.y + shooterBox.h/3;
00062         }
00063         else
00064         {
00065                 _box.y = shooterBox.y + shooterBox.h;
00066                 _box.x = shooterBox.x + shooterBox.w/2;
00067         }
00068 }
00069 
00070 void Bullet::draw()
00071 {
00072         _sprite->setDirection( _xDirection );
00073         _sprite->draw(_box.x,_box.y);
00074 }
00075 
00076 void Bullet::update()
00077 {
00078         // TODO: Fix collisions
00079         _box.x += ((double)_xVelocity/Screen::FRAMERATE);
00080         CollisionHandler::instance()->checkCollision(this);
00081         
00082         _box.y += ((double)_yVelocity/Screen::FRAMERATE);
00083         CollisionHandler::instance()->checkCollision(this);
00084 
00085         SDL_Rect screenBox = Screen::instance()->getScreenRect();
00086 
00087         if(!glfuncs::intersect(_box, screenBox))
00088                 Level::instance()->scheduleDelete(this);
00089 }
00090 
00091 void Bullet::collide( Object* o )
00092 {
00093         if( o == _shooter || o->getType() == BULLET )
00094                 return;
00095 
00096         if( o->getType() == PLAYER || o->getType() == ENEMY )
00097                 ((Person*)o)->addHealth( -_damage );
00098 
00099         _collidable = false;
00100         Level::instance()->scheduleDelete( this );
00101 }

Generated on Sat Apr 22 15:05:20 2006 for ProjectX by  doxygen 1.4.6-NO