Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | Related Pages | Examples

testserver/testserversnakebite.cpp

/// @cond EXCLUDEDTESTSOURCES
/***************************************************************************
 * The contents of this file are subject to the Mozilla Public             *
 * License Version 1.1 (the "License"); you may not use this file          *
 * except in compliance with the License. You may obtain a copy of         *
 * the License at http://www.mozilla.org/MPL/                              *
 *                                                                         *
 * Software distributed under the License is distributed on an "AS         *
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or              *
 * implied. See the License for the specific language governing            *
 * rights and limitations under the License.                               *
 *                                                                         *
 * The Original Code is Game Network Framework (GaNeF).                    *
 *                                                                         *
 * The Initial Developers of the Original Code are                         *
 * Lars Langer and Emanuel Greisen                                         *
 * Copyright (C) 2005. Lars Langer & Emanuel Greisen                       *
 * All Rights Reserved.                                                    *
 *                                                                         *
 * Contributor(s):                                                         *
 *   none yet....                                                          *
 *                                                                         *
 ***************************************************************************/
#include "testserversnakebite.h"

#include <cmath>


TestServerSnakeBite::TestServerSnakeBite(
      ServerFramework * f,
      int x,
      int y,
      double angle,
      TestServerSnakeBite * nextbite,
      bool is_head,
      Client * owner) :
   ServerData(f, 15, false),
   m_x(x),
   m_y(y),
   m_angle(angle),
   m_nextbite(nextbite),
   m_is_head(is_head),
   m_owner(owner)
{
   if(m_is_head)
   {
      m_x_dest = m_x + 50;
      m_y_dest = m_y + 50;
   }
}


TestServerSnakeBite::~TestServerSnakeBite()
{
   // Tell everyone that I have left the building
   std::cout << "Sending DestroyObject("<<getFrameworkId()<<" to all\n";
   sendDestroyToAll();
}

void TestServerSnakeBite::fillUpdatePacket( ServerPacket * packet, unsigned char type ) const
{
   switch(type)
   {
      case 10:
         packet->writeInt32((int)m_x);
         packet->writeInt32((int)m_y);
         packet->writeDouble64(m_angle);
         break;
      case 20:
         packet->writePtr(m_nextbite);
         break;
      case 30:
      case 40:
         // Do nothing, this is just to let one player know, that this is his snake-bite or not any more
         break;
      default:
         std::cout << "damn unknown update-type:" << type << std::endl;
         break;
   }
}

void TestServerSnakeBite::fillCreateObjectPacket( ServerPacket * packet ) const
{
   packet->writeInt32((int)m_x);
   packet->writeInt32((int)m_y);
   packet->writeDouble64(m_angle);
   packet->writePtr(m_nextbite);
   packet->writeBool8(m_is_head);
}

void TestServerSnakeBite::clientPacket( Client * client, unsigned char type, ServerPacket * packet )
{
   //std::cout << "MyData received a packet from client:" << client << std::endl;
   switch(type)
   {
      case 10:
         m_x_dest = packet->readInt32();
         m_y_dest = packet->readInt32();
         break;
   }
}

void TestServerSnakeBite::updateMovement( )
{
   // First update angle
   if(m_nextbite)
   {
      double dx,dy;
      dx = m_x - m_nextbite->m_x;
      dy = m_y - m_nextbite->m_y;
      double dist = std::sqrt(dx * dx + dy * dy);
      if(dist > 5.0)
      {
         // Then move
         m_x -= dx * 0.3;
         m_y -= dy * 0.3;
      }
   }
   else
   {
      m_x = m_x_dest;
      m_y = m_y_dest;
   }

}

double TestServerSnakeBite::dist( const TestServerSnakeBite & bite ) const
{
   double dx = m_x - bite.m_x;
   double dy = m_y - bite.m_y;
   return std::sqrt(dx * dx + dy * dy);
}

/// @endcond


Generated on Mon Feb 6 12:24:50 2006 for Ganef by  doxygen 1.4.4