00001 #include "Player.h"
00002 #include "List.h"
00003 #include "Command.h"
00004 #include "CommandManager.h"
00005 #include "glfuncs.h"
00006 #include "Control.h"
00007 #include "Screen.h"
00008 #include "Weapon.h"
00009 #include "Level.h"
00010
00011 #include <sstream>
00012
00013 using namespace std;
00014
00015 Player::Player(List * playerInfo, int x, int y)
00016 :Person(playerInfo,x,y), _secondaryWeapon(NULL),_cDWait(30),_cDTime(30)
00017 {
00018
00019
00020 _depth = 2;
00021 _type = PLAYER;
00022 }
00023
00024 Player::~Player()
00025 {
00026 delete _secondaryWeapon;
00027 }
00028
00029 void Player::collide(Object *o)
00030 {
00031 Person::collide(o);
00032 if(o->getType() == BOSS && _cDTime > _cDWait) {
00033 AddHealthCommand(this,-2).execute();
00034 _cDTime = 0;
00035 }
00036 }
00037
00038 void Player::die()
00039 {
00040 Person::die();
00041 Level::instance()->setScore(Level::instance()->getScore()/2);
00042 }
00043
00044 Object *Player::copy()
00045 {
00046 Player *copy = new Player(*this);
00047 if(_primaryWeapon)
00048 copy->_primaryWeapon = _primaryWeapon->copy(copy);
00049 if(_punchWeapon)
00050 copy->_punchWeapon = _punchWeapon->copy(copy);
00051 if(_secondaryWeapon)
00052 copy->_secondaryWeapon = _secondaryWeapon->copy(copy);
00053 return copy;
00054 }
00055
00056 void Player::addSecondary(std::string filename)
00057 {
00058 Weapon* newSecondary = new Weapon(filename, this);
00059 if(_secondaryWeapon) {
00060
00061 if(_secondaryWeapon->id() == newSecondary->id()) {
00062 _secondaryWeapon->addAmmo(newSecondary->getAmmo());
00063 delete newSecondary;
00064 }
00065 else {
00066 delete _secondaryWeapon;
00067 _secondaryWeapon = newSecondary;
00068 }
00069 }
00070 else
00071 _secondaryWeapon = newSecondary;
00072 }
00073
00074 void Player::shootSecondary()
00075 {
00076 if(_secondaryWeapon)
00077 _secondaryWeapon->shoot();
00078 }
00079
00080 SDL_Rect Player::drawSecondaryAmmo(SDL_Rect raster)
00081 {
00082 if(_secondaryWeapon)
00083 return _secondaryWeapon->drawAmmo(raster);
00084
00085 return raster;
00086 }
00087
00088
00089 int saveTimer = 20;
00090
00091 void Player::update()
00092 {
00093 Person::update();
00094
00095 if(_secondaryWeapon) {
00096 _secondaryWeapon->update();
00097
00098 if(_secondaryWeapon->getAmmo() == 0) {
00099 delete _secondaryWeapon;
00100 _secondaryWeapon = NULL;
00101 }
00102 }
00103 saveTimer++;
00104 _cDTime++;
00105 }
00106
00107 void Player::control()
00108 {
00109 Uint8 *keystates = SDL_GetKeyState( NULL );
00110
00111
00112
00113
00114
00115
00116 if(keystates[Control::leftKey])
00117 {
00118 CommandManager::instance()->execute(new MoveCommand(this,LEFT));
00119 }
00120 if(keystates[Control::rightKey])
00121 {
00122 CommandManager::instance()->execute(new MoveCommand(this,RIGHT));
00123 }
00124
00125
00126 if(keystates[Control::jumpKey])
00127 {
00128 CommandManager::instance()->execute(new JumpCommand(this,_xDirection));
00129
00130
00131
00132
00133 }
00134 if(!keystates[Control::jumpKey])
00135
00136 CommandManager::instance()->execute(new JumpResetCommand(this));
00137
00138
00139
00140 if( ((!keystates[Control::upKey])&&(!keystates[Control::downKey]))||
00141 ( _crouching ))
00142
00143 CommandManager::instance()->execute(new AimCommand(this,MIDDLE));
00144
00145
00146
00147
00148 if( keystates[Control::upKey] ||
00149 (keystates[Control::downKey] && keystates[Control::upKey]) )
00150 CommandManager::instance()->execute(new AimCommand(this,UP));
00151
00152
00153 if(!_crouching && keystates[Control::downKey])
00154 CommandManager::instance()->execute(new AimCommand(this,DOWN));
00155
00156
00157 if( keystates[Control::shoot2Key ] || keystates[ Control::shoot1Key] )
00158 {
00159
00160 CommandManager::instance()->execute(new ShootCommand(this));
00161 }
00162
00163 else if( keystates[ Control::altShoot1Key ] || keystates[ Control::altShoot2Key ] )
00164 {
00165 CommandManager::instance()->execute(new SecondaryShootCommand(this));
00166 }
00167
00168
00169 if( _crouching && !keystates[ Control::downKey ] )
00170 {
00171 CommandManager::instance()->execute( new CrouchCommand( this, false ) );
00172 }
00173 if( keystates[ Control::downKey ] && !_crouching )
00174 {
00175 CommandManager::instance()->execute( new CrouchCommand( this, true ) );
00176 }
00177
00178 if( keystates[ SDLK_q ] && saveTimer > 20 ) {
00179 CommandManager::instance()->startRecording();
00180 CommandManager::instance()->execute(new StopPersonCommand(this));
00181 CommandManager::instance()->execute(new SetPositionCommand(this,29*32, 31*32));
00182 saveTimer = 0;
00183 }
00184 if( keystates[ SDLK_w ] && saveTimer > 20 ) {
00185 CommandManager::instance()->saveRecording("demo.txt");
00186 CommandManager::instance()->stopRecording();
00187 saveTimer = 0;
00188 }
00189 }
00190
00191 void Player::addHealth(int dHealth)
00192 {
00193 if( _health >= MAXHEALTH && dHealth > 0 )
00194 return;
00195 else
00196 Person::addHealth( dHealth );
00197 }