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

testserversnakebite.cpp

00001 /// @cond EXCLUDEDTESTSOURCES
00002 /***************************************************************************
00003  * The contents of this file are subject to the Mozilla Public             *
00004  * License Version 1.1 (the "License"); you may not use this file          *
00005  * except in compliance with the License. You may obtain a copy of         *
00006  * the License at http://www.mozilla.org/MPL/                              *
00007  *                                                                         *
00008  * Software distributed under the License is distributed on an "AS         *
00009  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or              *
00010  * implied. See the License for the specific language governing            *
00011  * rights and limitations under the License.                               *
00012  *                                                                         *
00013  * The Original Code is Game Network Framework (GaNeF).                    *
00014  *                                                                         *
00015  * The Initial Developers of the Original Code are                         *
00016  * Lars Langer and Emanuel Greisen                                         *
00017  * Copyright (C) 2005. Lars Langer & Emanuel Greisen                       *
00018  * All Rights Reserved.                                                    *
00019  *                                                                         *
00020  * Contributor(s):                                                         *
00021  *   none yet....                                                          *
00022  *                                                                         *
00023  ***************************************************************************/
00024 #include "testserversnakebite.h"
00025 
00026 #include <cmath>
00027 
00028 
00029 TestServerSnakeBite::TestServerSnakeBite(
00030       ServerFramework * f,
00031       int x,
00032       int y,
00033       double angle,
00034       TestServerSnakeBite * nextbite,
00035       bool is_head,
00036       Client * owner) :
00037    ServerData(f, 15, false),
00038    m_x(x),
00039    m_y(y),
00040    m_angle(angle),
00041    m_nextbite(nextbite),
00042    m_is_head(is_head),
00043    m_owner(owner)
00044 {
00045    if(m_is_head)
00046    {
00047       m_x_dest = m_x + 50;
00048       m_y_dest = m_y + 50;
00049    }
00050 }
00051 
00052 
00053 TestServerSnakeBite::~TestServerSnakeBite()
00054 {
00055    // Tell everyone that I have left the building
00056    std::cout << "Sending DestroyObject("<<getFrameworkId()<<" to all\n";
00057    sendDestroyToAll();
00058 }
00059 
00060 void TestServerSnakeBite::fillUpdatePacket( ServerPacket * packet, unsigned char type ) const
00061 {
00062    switch(type)
00063    {
00064       case 10:
00065          packet->writeInt32((int)m_x);
00066          packet->writeInt32((int)m_y);
00067          packet->writeDouble64(m_angle);
00068          break;
00069       case 20:
00070          packet->writePtr(m_nextbite);
00071          break;
00072       case 30:
00073       case 40:
00074          // Do nothing, this is just to let one player know, that this is his snake-bite or not any more
00075          break;
00076       default:
00077          std::cout << "damn unknown update-type:" << type << std::endl;
00078          break;
00079    }
00080 }
00081 
00082 void TestServerSnakeBite::fillCreateObjectPacket( ServerPacket * packet ) const
00083 {
00084    packet->writeInt32((int)m_x);
00085    packet->writeInt32((int)m_y);
00086    packet->writeDouble64(m_angle);
00087    packet->writePtr(m_nextbite);
00088    packet->writeBool8(m_is_head);
00089 }
00090 
00091 void TestServerSnakeBite::clientPacket( Client * client, unsigned char type, ServerPacket * packet )
00092 {
00093    //std::cout << "MyData received a packet from client:" << client << std::endl;
00094    switch(type)
00095    {
00096       case 10:
00097          m_x_dest = packet->readInt32();
00098          m_y_dest = packet->readInt32();
00099          break;
00100    }
00101 }
00102 
00103 void TestServerSnakeBite::updateMovement( )
00104 {
00105    // First update angle
00106    if(m_nextbite)
00107    {
00108       double dx,dy;
00109       dx = m_x - m_nextbite->m_x;
00110       dy = m_y - m_nextbite->m_y;
00111       double dist = std::sqrt(dx * dx + dy * dy);
00112       if(dist > 5.0)
00113       {
00114          // Then move
00115          m_x -= dx * 0.3;
00116          m_y -= dy * 0.3;
00117       }
00118    }
00119    else
00120    {
00121       m_x = m_x_dest;
00122       m_y = m_y_dest;
00123    }
00124 
00125 }
00126 
00127 double TestServerSnakeBite::dist( const TestServerSnakeBite & bite ) const
00128 {
00129    double dx = m_x - bite.m_x;
00130    double dy = m_y - bite.m_y;
00131    return std::sqrt(dx * dx + dy * dy);
00132 }
00133 
00134 /// @endcond
00135 

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