Ejemplo n.º 1
0
    void OnGUI()
    {
        GUILayout.Label("Ping:" + m_rtt + ",Pos:" + m_game.m_ball.position + ",Vel:" + m_game.m_ball.rigidbody.velocity);
        GUILayout.Label(m_state.ToString());
        if (m_state == State.ST_Disconnected)
        {
            if (GUILayout.Button("Connect"))
            {
                //Security.PrefetchSocketPolicy("127.0.0.1", 5001);
                m_client.Connect("127.0.0.1", 5001);
            }
        }
        else
        {
            if (GUILayout.Button("Disconnect"))
            {
                m_client.Close();
                m_state = State.ST_Disconnected;
            }
            switch (m_state)
            {
            case State.ST_Connected:
                if (GUILayout.Button("Automatch"))
                {
                    //send automatch
                    CmdPacket packet = new CmdPacket();
                    packet.WriteUShort(Proto.C_AutoMatch);
                    send(packet);
                    m_state = State.ST_WaitingToMatch;
                }
                break;

            case State.ST_WaitingToMatch:
                if (GUILayout.Button("Cancel Match"))
                {
                    CmdPacket packet = new CmdPacket();
                    packet.WriteUShort(Proto.C_UAutoMatch);
                    send(packet);
                    m_state = State.ST_Connected;
                }
                break;

            case State.ST_GameInit:
            {
                if (GUILayout.Button("Ready"))
                {
                    CmdPacket packet = new CmdPacket();
                    packet.WriteUShort(Proto.C_GameReady);
                    send(packet);
                    m_state = State.ST_WaitingToStart;
                }
                break;
            }

            case State.ST_WaitingToStart:
            case State.ST_GameRunning:
                break;
            }
        }
    }
Ejemplo n.º 2
0
    public void sendInput(float x, float y)
    {
        CmdPacket packet = new CmdPacket();

        packet.WriteUShort(Proto.C_GameShot);
        packet.WriteFloat(x);
        packet.WriteFloat(y);
        send(packet);
    }
Ejemplo n.º 3
0
 void Update()
 {
     if (m_state == State.ST_Start)
     {
         m_state           = State.ST_Running;
         m_time            = 0;
         m_input.m_enabled = true;
         if (m_bottom)
         {
             m_other.position = m_tranUp.position;
             m_other.rotation = m_tranUp.rotation;
             Camera.main.transform.position = m_tranBottom.position;
             Camera.main.transform.rotation = m_tranBottom.rotation;
         }
         else
         {
             m_other.position = m_tranBottom.position;
             m_other.rotation = m_tranBottom.rotation;
             Camera.main.transform.position = m_tranUp.position;
             Camera.main.transform.rotation = m_tranUp.rotation;
         }
         m_ball.position = m_tranBall.position;
         m_ball.rotation = m_tranBall.rotation;
     }
     if (m_state == State.ST_Running)
     {
         if (m_time > m_gameTime)
         {
             m_state = State.ST_End;
             CmdPacket packet = new CmdPacket();
             //packet.WriteUShort(Proto.C_AutoMatch);
             //m_client.m_client.Send(packet);
         }
         m_time += Time.deltaTime;
         //同步位置和速度
         if (m_bSer)
         {
             //if (Time.realtimeSinceStartup - m_lstSyncTime > Time.deltaTime)
             {
                 CmdPacket packet = new CmdPacket();
                 packet.WriteUShort(Proto.Synch_Pos);
                 packet.WriteUInt64(TimeMgr.getTimeStampMicro());
                 packet.WriteFloat(m_ball.position.x);
                 packet.WriteFloat(m_ball.position.z);
                 packet.WriteFloat(m_ball.rigidbody.velocity.x);
                 packet.WriteFloat(m_ball.rigidbody.velocity.z);
                 m_client.send(packet);
                 m_lstSyncTime = Time.realtimeSinceStartup;
             }
         }
     }
 }
Ejemplo n.º 4
0
 void FixedUpdate()
 {
     if (m_state != State.ST_Disconnected)
     {
         if (Sys_GetTime() - m_lstSendTime > 10 * 1000)
         {
             CmdPacket packet = new CmdPacket();
             packet.WriteUShort(Proto.Keep_Alive);
             packet.WriteUInt(Sys_GetTime());
             send(packet);
         }
         if (Sys_GetTime() - m_lstReadTime > 30 * 1000)
         {
             //dead
         }
     }
 }
Ejemplo n.º 5
0
    void RecvData(object sender, NetEventArgs e)
    {
        Sys_Log(getString(e.Client.RecvPacket.GetReadData()));
        m_lstReadTime = Sys_GetTime();
        UInt16    cmd    = 0;
        CmdPacket packet = e.Client.RecvPacket;

        packet.ReadUShort(out cmd);
        switch (cmd)
        {
        case Proto.Keep_Alive:
        {
            UInt32 time = 0;
            if (packet.ReadUInt(out time))
            {
                CmdPacket tpacket = new CmdPacket();
                tpacket.WriteUShort(Proto.Keep_Alive_Ack);
                tpacket.WriteUInt(time);
                send(tpacket);
            }
            break;
        }

        case Proto.Keep_Alive_Ack:
        {
            UInt32 time = 0;
            if (packet.ReadUInt(out time))
            {
                m_rtt = Sys_GetTime() - time;
            }
            break;
        }

        case Proto.Synch_Pos:
        {
            //同步位置和速度
            if (!m_game.m_bSer)
            {
                UInt64 time;
                packet.ReadUInt64(out time);
                float x, z, vx, vz;
                packet.ReadFloat(out x);
                packet.ReadFloat(out z);
                packet.ReadFloat(out vx);
                packet.ReadFloat(out vz);
                m_other.change(time, x, z, vx, vz);
            }
            break;
        }

        case Proto.C_GameShot:
        {
            //同步操作
            float vx, vz;
            packet.ReadFloat(out vx);
            packet.ReadFloat(out vz);
            //if (m_game.m_bSer)
            m_other.shot(vx, vz);
            break;
        }

        case Proto.S_GameInit:
        {
            m_state = State.ST_GameInit;
            break;
        }

        case Proto.S_GameStart:
        {
            m_state = State.ST_GameRunning;
            char bSer;
            if (packet.ReadByte(out bSer))
            {
                m_game.m_bSer = (bSer != 0);
            }
            char bottom;
            if (packet.ReadByte(out bottom))
            {
                m_game.m_bottom = (bottom != 0);
            }
            m_game.gameStart();
            break;
        }
        }
    }
Ejemplo n.º 6
0
    void Update()
    {
        Vector3 vec = ball.rigidbody.velocity;

        if (vec.x > 50)
        {
            vec.x = 50;
        }
        if (vec.x < -50)
        {
            vec.x = -50;
        }
        if (vec.z > 50)
        {
            vec.z = 50;
        }
        if (vec.z < -50)
        {
            vec.z = -50;
        }
        ball.rigidbody.velocity = vec;
        Vector3 cameraVec = Camera.main.transform.position;

        if (cameraVec.x < -3)
        {
            cameraVec.x = -3;
        }
        if (cameraVec.x > 3)
        {
            cameraVec.x = 3;
        }
        Camera.main.transform.position = cameraVec;
        if (m_game.m_state != Game.State.ST_Running)
        {
            return;
        }
        if (m_game.m_bottom && ball.position.z <= 0 || !m_game.m_bottom && ball.position.z >= 0)
        {
            bool click = false;
            if (m_automatic)
            {
                if (Time.realtimeSinceStartup - m_lstClickTime > 0.11f)
                {
                    click          = true;
                    m_lstClickTime = Time.realtimeSinceStartup;
                }
            }
            else if (Input.GetMouseButtonUp(0))
            {
                click = true;
            }
            if (click)
            {
                Vector3 direction = ball.position - selfGun.position;
                direction.y = 0;
                Debug.Log("self:" + direction);
                ball.rigidbody.velocity = direction * rate;
                //ball.rigidbody.AddForce(direction * rate, ForceMode.Impulse);

                CmdPacket packet = new CmdPacket();
                packet.WriteUShort(Proto.C_GameShot);
                packet.WriteFloat(ball.rigidbody.velocity.x);
                packet.WriteFloat(ball.rigidbody.velocity.z);
                m_client.send(packet);
            }
        }
        if (Input.GetKey(KeyCode.A))
        {
            cameraVec.x += -1 * Time.deltaTime * cameraMoveSpeed;
        }
        if (Input.GetKey(KeyCode.D))
        {
            cameraVec.x += 1 * Time.deltaTime * cameraMoveSpeed;
        }
    }