ClientDefenseHandler.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <client/ScorchedClient.h>
00022 #include <client/ClientDefenseHandler.h>
00023 #include <tank/TankContainer.h>
00024 #include <tank/TankState.h>
00025 #include <tank/TankPosition.h>
00026 #include <tank/TankAccessories.h>
00027 #include <target/TargetLife.h>
00028 #include <target/TargetShield.h>
00029 #include <target/TargetParachute.h>
00030 #include <coms/ComsDefenseMessage.h>
00031 #include <common/Defines.h>
00032 #include <sound/SoundUtils.h>
00033 #include <weapons/AccessoryStore.h>
00034 
00035 ClientDefenseHandler *ClientDefenseHandler::instance_ = 0;
00036 
00037 ClientDefenseHandler *ClientDefenseHandler::instance()
00038 {
00039         if (!instance_)
00040         {
00041                 instance_ = new ClientDefenseHandler;
00042         }
00043         return instance_;
00044 }
00045 
00046 ClientDefenseHandler::ClientDefenseHandler()
00047 {
00048         ScorchedClient::instance()->getComsMessageHandler().addHandler(
00049                 "ComsDefenseMessage",
00050                 this);
00051 }
00052 
00053 ClientDefenseHandler::~ClientDefenseHandler()
00054 {
00055 }
00056 
00057 bool ClientDefenseHandler::processMessage(
00058         NetMessage &netMessage,
00059         const char *messageType,
00060         NetBufferReader &reader)
00061 {
00062         // Decode the connect message
00063         ComsDefenseMessage message;
00064         if (!message.readMessage(reader)) return false;
00065 
00066         // Check tank exists and is alive
00067         Tank *tank = ScorchedClient::instance()->getTankContainer().getTankById(message.getPlayerId());
00068         if (!tank || tank->getState().getState() != TankState::sNormal)
00069         {
00070                 return true;
00071         }
00072 
00073         // Actually perform the required action
00074         switch (message.getChange())
00075         {
00076         case ComsDefenseMessage::eBatteryUse:
00077                 {
00078                         Accessory *battery = 
00079                                 ScorchedClient::instance()->getAccessoryStore().
00080                                         findByAccessoryId(message.getInfoId());
00081                         if (battery)
00082                         {
00083                                 if (battery->getActivationSound() &&
00084                                         0 != strcmp("none", battery->getActivationSound()))
00085                                 {
00086                                         SoundBuffer *batSound = 
00087                                                 Sound::instance()->fetchOrCreateBuffer(
00088                                                         S3D::getDataFile(S3D::formatStringBuffer("data/wav/%s", battery->getActivationSound())));
00089                                         SoundUtils::playAbsoluteSound(VirtualSoundPriority::eAction,
00090                                                 batSound, tank->getPosition().getTankPosition().asVector());
00091                                 }
00092 
00093                                 if (tank->getDestinationId() != 
00094                                         ScorchedClient::instance()->getTankContainer().getCurrentDestinationId())
00095                                 {
00096                                         tank->getAccessories().add(battery, 1);
00097                                 }
00098 
00099                                 tank->getLife().setLife(tank->getLife().getLife() + 10);
00100                                 tank->getAccessories().rm(battery, 1);
00101                         }
00102                 }
00103                 break;
00104         case ComsDefenseMessage::eShieldUp:
00105                 {
00106                         Accessory *accessory = 
00107                                 ScorchedClient::instance()->getAccessoryStore().
00108                                         findByAccessoryId(message.getInfoId());
00109                         if (accessory->getType() == AccessoryPart::AccessoryShield)
00110                         {
00111                                 if (accessory->getActivationSound() &&
00112                                         0 != strcmp("none", accessory->getActivationSound()))
00113                                 {
00114                                         SoundBuffer *activateSound = 
00115                                                 Sound::instance()->fetchOrCreateBuffer(
00116                                                         S3D::getDataFile(S3D::formatStringBuffer("data/wav/%s", accessory->getActivationSound())));
00117                                         SoundUtils::playAbsoluteSound(VirtualSoundPriority::eAction,
00118                                                 activateSound, tank->getPosition().getTankPosition().asVector());
00119                                 }
00120 
00121                                 if (tank->getDestinationId() != 
00122                                         ScorchedClient::instance()->getTankContainer().getCurrentDestinationId())
00123                                 {
00124                                         // Make sure tank has this shield
00125                                         tank->getAccessories().add(accessory, 1);
00126                                 }
00127                                 tank->getAccessories().rm(accessory, 1);
00128                                 tank->getShield().setCurrentShield(accessory);
00129                         }
00130                 }
00131                 break;
00132         case ComsDefenseMessage::eShieldDown:
00133                 {
00134                         tank->getShield().setCurrentShield(0);
00135                 }       
00136                 break;
00137         case ComsDefenseMessage::eParachutesUp:
00138                 {
00139                         Accessory *parachute = 
00140                                 ScorchedClient::instance()->getAccessoryStore().
00141                                         findByAccessoryId(message.getInfoId());
00142                         if (parachute)
00143                         {
00144                                 if (parachute->getActivationSound() &&
00145                                         0 != strcmp("none", parachute->getActivationSound()))
00146                                 {
00147                                         SoundBuffer *paraSound = 
00148                                                 Sound::instance()->fetchOrCreateBuffer(
00149                                                         S3D::getDataFile(S3D::formatStringBuffer("data/wav/%s", parachute->getActivationSound())));
00150                                         SoundUtils::playAbsoluteSound(VirtualSoundPriority::eAction,
00151                                                 paraSound, tank->getPosition().getTankPosition().asVector());
00152                                 }
00153                         }
00154 
00155                         tank->getParachute().setCurrentParachute(parachute);
00156                 }
00157                 break;
00158         case ComsDefenseMessage::eParachutesDown:
00159                 {
00160                         tank->getParachute().setCurrentParachute(0);
00161                 }
00162                 break;
00163         }
00164 
00165         return true;
00166 }

Generated on Mon Feb 16 15:14:36 2009 for Scorched3D by  doxygen 1.5.3