Ejemplo n.º 1
0
 private void Start()
 {
     //网络模块
     SimpleNetManager.AddListener("Enter", OnEnter);
     SimpleNetManager.AddListener("List", OnList);
     SimpleNetManager.AddListener("Move", OnMove);
     SimpleNetManager.AddListener("Attack", OnAttack);
     SimpleNetManager.AddListener("Die", OnDie);
     SimpleNetManager.AddListener("Leave", OnLeave);
     SimpleNetManager.Connect("127.0.0.1", 8888);
     //添加玩家自身
     InitHumanSelf();
     Thread.Sleep(500);
     //请求玩家列表
     SimpleNetManager.Send("List|");
 }
Ejemplo n.º 2
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);
    }
Ejemplo n.º 3
0
    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);
            }
        }
    }