src/weapon/jetpack.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  * Jet Pack :-)
00020  *****************************************************************************/
00021 
00022 #include "jetpack.h"
00023 #include "explosion.h"
00024 #include "../game/game.h"
00025 #include "../game/game_loop.h"
00026 #include "../game/game_mode.h"
00027 #include "../game/time.h"
00028 #include "../interface/game_msg.h"
00029 #include "../map/camera.h"
00030 #include "../network/network.h"
00031 #include "../object/physical_obj.h"
00032 #include "../sound/jukebox.h"
00033 #include "../team/teams_list.h"
00034 #include "../tool/i18n.h"
00035 #include "../character/move.h"
00036 #include "../include/action_handler.h"
00037 
00038 const double JETPACK_FORCE = 2000.0;
00039 
00040 const uint DELTA_FUEL_DOWN = 200 ;  // Delta time between 2 fuel unit consumption.
00041 
00042 JetPack::JetPack() : Weapon(WEAPON_JETPACK, "jetpack",
00043                             new WeaponConfig(),
00044                             NEVER_VISIBLE)
00045 {
00046   m_name = _("Jetpack");
00047   m_unit_visibility = VISIBLE_ONLY_WHEN_ACTIVE;
00048 
00049   override_keys = true ;
00050   use_unit_on_first_shoot = false;
00051 
00052   m_x_force = 0.0;
00053   m_y_force = 0.0;
00054   channel = -1;
00055 }
00056 
00057 void JetPack::Refresh()
00058 {
00059   if(!ActiveTeam().IsLocal())
00060     return;
00061 
00062   Point2d F;
00063 
00064   if (m_is_active)
00065   {
00066     F.x = m_x_force ;
00067     F.y = m_y_force ;
00068 
00069     ActiveCharacter().SetExternForceXY(F);
00070     ActiveCharacter().UpdatePosition();
00071     SendCharacterPosition();
00072     Action a(Action::ACTION_SET_CHARACTER_PHYSICS);
00073     a.StoreActiveCharacter();
00074     network.SendAction(&a);
00075 
00076     if( !F.IsNull() )
00077     {
00078       // We are using fuel !!!
00079       uint current = Time::GetInstance()->Read() ;
00080       double delta = (double)(current - m_last_fuel_down);
00081 
00082       while (delta >= DELTA_FUEL_DOWN)
00083       {
00084         if (EnoughAmmoUnit())
00085         {
00086           UseAmmoUnit();
00087           m_last_fuel_down += DELTA_FUEL_DOWN ;
00088           delta -= DELTA_FUEL_DOWN ;
00089         }
00090         else
00091         {
00092           p_Deselect();
00093           break;
00094         }
00095       }
00096     }
00097   }
00098 }
00099 
00100 void JetPack::p_Select()
00101 {
00102   ActiveCharacter().SetClothe("jetpack");
00103 }
00104 
00105 void JetPack::p_Deselect()
00106 {
00107   m_is_active = false;
00108   m_x_force = 0;
00109   m_y_force = 0;
00110   ActiveCharacter().SetExternForce(0,0);
00111   StopUse();
00112   camera.SetCloseFollowing(false);
00113   ActiveCharacter().SetClothe("normal");
00114   ActiveCharacter().SetMovement("walk");
00115 }
00116 
00117 void JetPack::StartUse()
00118 {
00119   ActiveCharacter().SetMovement("jetpack-fire");
00120   if ( (m_x_force == 0) && (m_y_force == 0))
00121     {
00122       m_last_fuel_down = Time::GetInstance()->Read();
00123       channel = jukebox.Play(ActiveTeam().GetSoundProfile(),"weapon/jetpack", -1);
00124 
00125       camera.FollowObject (&ActiveCharacter(),true, true, true);
00126       camera.SetCloseFollowing(true);
00127 //                           bool suit, bool recentre,
00128 //                           bool force_recentrage=false);
00129     }
00130 }
00131 
00132 void JetPack::StopUse()
00133 {
00134   ActiveCharacter().SetMovement("jetpack-nofire");
00135   if (m_x_force == 0.0 && m_y_force == 0.0)
00136   {
00137     if(channel != -1)
00138       jukebox.Stop(channel);
00139     channel = -1;
00140   }
00141 }
00142 
00143 void JetPack::GoUp()
00144 {
00145   StartUse();
00146   m_y_force = -(ActiveCharacter().GetMass() * GameMode::GetInstance()->gravity + JETPACK_FORCE) ;
00147 }
00148 
00149 void JetPack::GoLeft()
00150 {
00151   StartUse();
00152   m_x_force = - JETPACK_FORCE ;
00153   if(ActiveCharacter().GetDirection() == Body::DIRECTION_RIGHT)
00154     ActiveCharacter().SetDirection(Body::DIRECTION_LEFT);
00155 }
00156 
00157 void JetPack::GoRight()
00158 {
00159   StartUse();
00160   m_x_force = JETPACK_FORCE ;
00161   if(ActiveCharacter().GetDirection() == Body::DIRECTION_LEFT)
00162     ActiveCharacter().SetDirection(Body::DIRECTION_RIGHT);
00163 }
00164 
00165 void JetPack::StopUp()
00166 {
00167   m_y_force = 0.0 ;
00168   StopUse();
00169 }
00170 
00171 void JetPack::StopLeft()
00172 {
00173   m_x_force = 0.0 ;
00174   StopUse();
00175 }
00176 
00177 void JetPack::StopRight()
00178 {
00179   m_x_force = 0.0 ;
00180   StopUse();
00181 }
00182 
00183 void JetPack::HandleKeyEvent(Action::Action_t action, Keyboard::Key_Event_t event_type)
00184 {
00185   switch (action) {
00186     case Action::ACTION_UP:
00187       if (event_type == Keyboard::KEY_PRESSED)
00188         GoUp();
00189       else if (event_type == Keyboard::KEY_RELEASED)
00190         StopUp();
00191       break ;
00192 
00193     case Action::ACTION_MOVE_LEFT:
00194       if (event_type == Keyboard::KEY_PRESSED)
00195         GoLeft();
00196       else if (event_type == Keyboard::KEY_RELEASED)
00197         StopLeft();
00198       break ;
00199 
00200     case Action::ACTION_MOVE_RIGHT:
00201       if (event_type == Keyboard::KEY_PRESSED)
00202         GoRight();
00203       else if (event_type == Keyboard::KEY_RELEASED)
00204         StopRight();
00205       break ;
00206 
00207     case Action::ACTION_SHOOT:
00208       if (event_type == Keyboard::KEY_PRESSED)
00209         ActionHandler::GetInstance()->NewAction(new Action(Action::ACTION_WEAPON_STOP_USE));
00210       break ;
00211 
00212     default:
00213       break ;
00214   } ;
00215 }
00216 
00217 bool JetPack::p_Shoot()
00218 {
00219   m_is_active = true;
00220   ActiveCharacter().SetClothe("jetpack-fire");
00221 
00222   return true;
00223 }
00224 
00225 void JetPack::SignalTurnEnd()
00226 {
00227   p_Deselect();
00228 }
00229 
00230 void JetPack::ActionStopUse()
00231 {
00232   p_Deselect();
00233 }

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