ClientAddPlayerHandler.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/ClientAddPlayerHandler.h>
00023 #include <client/ClientChannelManager.h>
00024 #include <coms/ComsAddPlayerMessage.h>
00025 #include <common/Logger.h>
00026 #include <tank/TankContainer.h>
00027 #include <tank/TankState.h>
00028 #include <tank/TankAvatar.h>
00029 #include <tankgraph/TargetRendererImplTank.h>
00030 
00031 ClientAddPlayerHandler *ClientAddPlayerHandler::instance_ = 0;
00032 
00033 ClientAddPlayerHandler *ClientAddPlayerHandler::instance()
00034 {
00035         if (!instance_)
00036         {
00037           instance_ = new ClientAddPlayerHandler();
00038         }
00039 
00040         return instance_;
00041 }
00042 
00043 ClientAddPlayerHandler::ClientAddPlayerHandler()
00044 {
00045         ScorchedClient::instance()->getComsMessageHandler().addHandler(
00046                 "ComsAddPlayerMessage",
00047                 this);
00048 }
00049 
00050 ClientAddPlayerHandler::~ClientAddPlayerHandler()
00051 {
00052 
00053 }
00054 
00055 bool ClientAddPlayerHandler::processMessage(
00056         NetMessage &netMessage,
00057         const char *messageType,
00058         NetBufferReader &reader)
00059 {
00060         ComsAddPlayerMessage message;
00061         if (!message.readMessage(reader)) return false;
00062 
00063         Tank *tank = ScorchedClient::instance()->getTankContainer().getTankById(
00064                 message.getPlayerId());
00065         if (!tank)
00066         {
00067                 // Create the new tank and add it to the tank container
00068                 // Collections
00069                 tank = new Tank(
00070                         ScorchedClient::instance()->getContext(),
00071                         message.getPlayerId(),
00072                         message.getDestinationId(),
00073                         message.getPlayerName(),
00074                         message.getPlayerColor(),
00075                         message.getModelName(),
00076                         message.getTankType());
00077                 tank->setTeam(message.getPlayerTeam());
00078                 tank->getAvatar().setFromBuffer(
00079                         message.getPlayerIconName(),
00080                         message.getPlayerIcon(),
00081                         true);
00082                 tank->setRenderer(new TargetRendererImplTank(tank));
00083                 tank->getState().setSpectator(true);
00084                 ScorchedClient::instance()->getTankContainer().addTank(tank);
00085 
00086                 if (ClientChannelManager::instance()->getMutedPlayers().find(tank->getPlayerId())
00087                         != ClientChannelManager::instance()->getMutedPlayers().end())
00088                 {
00089                         tank->getState().setMuted(true);
00090                 }
00091 
00092                 Logger::log(S3D::formatStringBuffer("Tank connected \"%s\"", 
00093                         tank->getCStrName().c_str()));
00094         }
00095         else
00096         {
00097                 // Update the tanks icon
00098                 tank->getAvatar().setFromBuffer(
00099                         message.getPlayerIconName(),
00100                         message.getPlayerIcon(),
00101                         true);
00102         }
00103 
00104         return true;
00105 }

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