コード例 #1
0
    //获得用户列表
    void OnList(string msgArgs)
    {
        Debug.Log("OnList:" + msgArgs);
        string[] split = msgArgs.Split(',');
        int      count = (split.Length - 1) / 6;

        for (int i = 0; i < count; i++)
        {
            string desc = split[i * 6 + 0];
            float  x    = float.Parse(split[i * 6 + 1]);
            float  y    = float.Parse(split[i * 6 + 2]);
            float  z    = float.Parse(split[i * 6 + 3]);
            float  eulY = float.Parse(split[i * 6 + 4]);
            int    hp   = int.Parse(split[i * 6 + 5]);
            //是自己
            if (desc == SimpleNetManager.GetDesc())
            {
                continue;
            }
            //添加一个角色
            GameObject obj = (GameObject)Instantiate(humanPrefab);
            obj.transform.position    = new Vector3(x, y, z);
            obj.transform.eulerAngles = new Vector3(0, eulY, 0);
            BaseHuman h = obj.AddComponent <SyncHuman>();
            h.desc = desc;
            otherHumans.Add(desc, h);
        }
    }
コード例 #2
0
    //收到玩家进入消息
    void OnEnter(string msgArgs)
    {
        Debug.Log("OnEnter:" + msgArgs);
        //解析参数
        string[] split = msgArgs.Split(',');
        string   desc  = split[0];
        float    x     = float.Parse(split[1]);
        float    y     = float.Parse(split[2]);
        float    z     = float.Parse(split[3]);
        float    eulY  = float.Parse(split[4]);

        //是自己
        if (desc == SimpleNetManager.GetDesc())
        {
            return;
        }
        //添加一个角色
        GameObject obj = (GameObject)Instantiate(humanPrefab);

        obj.transform.position    = new Vector3(x, y, z);
        obj.transform.eulerAngles = new Vector3(0, eulY, 0);
        BaseHuman h = obj.AddComponent <SyncHuman>();

        h.desc = desc;
        otherHumans.Add(desc, h);
    }
コード例 #3
0
    private void InitHumanSelf()
    {
        //添加一个角色
        GameObject obj = (GameObject)Instantiate(humanPrefab);
        float      x   = Random.Range(-5, 5);
        float      z   = Random.Range(-5, 5);

        obj.transform.position = new Vector3(x, 0, z);
        myHuman      = obj.AddComponent <CtrlHuman>();
        myHuman.desc = SimpleNetManager.GetDesc();

        //发送协议
        Vector3 pos     = myHuman.transform.position;
        Vector3 eul     = myHuman.transform.eulerAngles;
        string  sendStr = "Enter|";

        sendStr += SimpleNetManager.GetDesc() + ",";
        sendStr += pos.x + ",";
        sendStr += pos.y + ",";
        sendStr += pos.z + ",";
        sendStr += eul.y;
        SimpleNetManager.Send(sendStr);
    }
コード例 #4
0
ファイル: CtrlHuman.cs プロジェクト: jiahaodev/Network
    private void CtrlHumanMove()
    {
        //移动
        if (Input.GetMouseButtonDown(0))  //鼠标左键
        {
            Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            Physics.Raycast(ray, out hit);
            if (hit.collider.tag == "Terrain")
            {
                MoveTo(hit.point);
                //发送协议
                string sendStr = "Move|";
                sendStr += SimpleNetManager.GetDesc() + ",";
                sendStr += hit.point.x + ",";
                sendStr += hit.point.y + ",";
                sendStr += hit.point.z + ",";
                SimpleNetManager.Send(sendStr);
            }
        }
        //攻击
        if (Input.GetMouseButtonDown(1))  //鼠标右键
        {
            if (isAttacking)
            {
                return;
            }
            if (isMoving)
            {
                return;
            }

            Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            Physics.Raycast(ray, out hit);

            transform.LookAt(hit.point);
            Attack();

            //发送协议(攻击)
            string sendStr = "Attack|";
            sendStr += SimpleNetManager.GetDesc() + ",";
            sendStr += transform.eulerAngles.y + ",";
            SimpleNetManager.Send(sendStr);

            //攻击判定
            Vector3 lineEnd   = transform.position + 0.5f * Vector3.up;
            Vector3 lineStart = lineEnd + 20f * transform.forward;
            if (Physics.Linecast(lineStart, lineEnd, out hit))
            {
                GameObject hitObj = hit.collider.gameObject;
                if (hitObj == gameObject)
                {
                    return;
                }
                SyncHuman h = hitObj.GetComponent <SyncHuman>();
                if (h == null)
                {
                    return;
                }
                sendStr  = "Hit|";
                sendStr += SimpleNetManager.GetDesc() + ",";
                sendStr += h.desc + ",";
                SimpleNetManager.Send(sendStr);
            }
        }
    }