src/weapon/submachine_gun.cpp

Go to the documentation of this file.
00001 /******************************************************************************
00002  *  Wormux is a convivial mass murder game.
00003  *  Copyright (C) 2001-2004 Lawrence Azzoug.
00004  *
00005  *  This program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2 of the License, or
00008  *  (at your option) any later version.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
00018  ******************************************************************************
00019  * Submachine gun. Don't fire bullet one by one but with burst fire (like
00020  * a submachine gun :)
00021  * The hack in order to firing multiple bullet at once consist in using a
00022  * std::list of projectile and overide the Refresh & HandleKeyEvent methods.
00023  *****************************************************************************/
00024 
00025 #include <sstream>
00026 #include "../map/map.h"
00027 #include "../game/time.h"
00028 #include "../object/objects_list.h"
00029 #include "../team/teams_list.h"
00030 #include "../tool/i18n.h"
00031 #include "../interface/game_msg.h"
00032 #include "../interface/game_msg.h"
00033 #include "../weapon/explosion.h"
00034 #include "../weapon/submachine_gun.h"
00035 #include "../network/randomsync.h"
00036 
00037 const uint    SUBMACHINE_BULLET_SPEED       = 30;
00038 const double  SUBMACHINE_TIME_BETWEEN_SHOOT = 70;
00039 const double  SUBMACHINE_RANDOM_ANGLE       = 0.01;
00040 
00041 SubMachineGunBullet::SubMachineGunBullet(ExplosiveWeaponConfig& cfg,
00042                                          WeaponLauncher * p_launcher) :
00043   WeaponBullet("m16_bullet", cfg, p_launcher)
00044 {
00045 }
00046 
00047 void SubMachineGunBullet::RandomizeShoot(double &angle,double &strength)
00048 {
00049   angle += M_PI * randomSync.GetDouble(-SUBMACHINE_RANDOM_ANGLE,SUBMACHINE_RANDOM_ANGLE);
00050 }
00051 
00052 void SubMachineGunBullet::ShootSound()
00053 {
00054   jukebox.Play("share", "weapon/m16");
00055 }
00056 
00057 //-----------------------------------------------------------------------------
00058 
00059 SubMachineGun::SubMachineGun() : WeaponLauncher(WEAPON_SUBMACHINE_GUN, "m16", new ExplosiveWeaponConfig())
00060 {
00061   m_name = _("Submachine Gun");
00062 
00063   override_keys = true ;
00064   ignore_collision_signal = true;
00065   ignore_explosion_signal = true;
00066   ignore_ghost_state_signal = true;
00067   ignore_drowning_signal = true;
00068   announce_missed_shots = false;
00069 
00070   m_weapon_fire = new Sprite(resource_manager.LoadImage(weapons_res_profile,m_id+"_fire"));
00071   m_weapon_fire->EnableRotationCache(32);
00072 
00073   ReloadLauncher();
00074 }
00075 
00076 // Return a projectile instance for the submachine gun
00077 WeaponProjectile * SubMachineGun::GetProjectileInstance()
00078 {
00079   return dynamic_cast<WeaponProjectile *>
00080       (new SubMachineGunBullet(cfg(),dynamic_cast<WeaponLauncher *>(this)));
00081 }
00082 
00083 void SubMachineGun::IncMissedShots()
00084 {
00085   if(missed_shots + 1 == ReadInitialNbUnit())
00086     announce_missed_shots = true;
00087   WeaponLauncher::IncMissedShots();
00088 }
00089 
00090 bool SubMachineGun::p_Shoot ()
00091 {
00092   if (m_is_active)
00093     return false;
00094 
00095   projectile->Shoot(SUBMACHINE_BULLET_SPEED);
00096   projectile = NULL;
00097   ReloadLauncher();
00098 
00099   Point2i pos = ActiveCharacter().GetHandPosition();
00100   double angle =  - M_PI_2 - ActiveCharacter().GetDirection()
00101                 * (float)(Time::GetInstance()->Read() % 100) * M_PI_4 / 100.0;
00102   particle.AddNow(pos, 1, particle_BULLET, true, angle,
00103                            5.0 + (Time::GetInstance()->Read() % 6));
00104 
00105   m_is_active = true;
00106   announce_missed_shots = false;
00107   return true;
00108 }
00109 
00110 // Overide regular Refresh method
00111 void SubMachineGun::RepeatShoot()
00112 {
00113   if ( m_is_active )
00114   {
00115     uint tmp = Time::GetInstance()->Read();
00116     uint time = tmp - m_last_fire_time;
00117 
00118     if (time >= SUBMACHINE_TIME_BETWEEN_SHOOT)
00119     {
00120       m_is_active = false;
00121       NewActionShoot();
00122       m_last_fire_time = tmp;
00123     }
00124   }
00125 }
00126 
00127 // Special handle to allow multiple shoot at a time
00128 void SubMachineGun::HandleKeyEvent(Action::Action_t action, Keyboard::Key_Event_t event_type)
00129 {
00130   switch (action) {
00131     case Action::ACTION_SHOOT:
00132       if (event_type == Keyboard::KEY_REFRESH)
00133         m_is_active = true;
00134       if (event_type ==  Keyboard::KEY_RELEASED)
00135         m_is_active = false;
00136       if (m_is_active) RepeatShoot();
00137       break;
00138     default:
00139       break;
00140   };
00141 }

Generated on Mon Jan 1 13:11:00 2007 for Wormux by  doxygen 1.4.7