Beispiel #1
0
    public void UpDateInput(float dt)
    {
        float x = Input.GetAxis("Horizontal");
        float y = Input.GetAxis("Vertical");

        if (x != 0 || y != 0)
        {
            Vector3 dir = new Vector3(x, 0, y);

            //移动的趋势
            Vector3 dest = transfrom.position + dir * dt * speed;

            RaycastHit hit1;
            RaycastHit hit2;
            Vector3    origin = transfrom.position + new Vector3(0, 0.2f, 0);

            Vector3 direction = (dest - transfrom.position).normalized;
            if (Physics.Raycast(origin, transfrom.forward, out hit1, 1f) && Physics.Raycast(origin, direction, out hit2, 1f))
            {
            }
            else
            {
                //方向
                transfrom.forward = direction;
                //坐标
                transfrom.position = dest;

                ReqMove req = new ReqMove();
                req.pos = Tools.ToVec_3(transfrom.position);
                req.rot = Tools.ToVec_3(transfrom.eulerAngles);
                NetClient.instance.Send <ReqMove>((int)MsgID.ReqMove, req);
            }
        }
        //开火
        if (Input.GetMouseButton(0))
        {
            if (Time.time - lastFireTime >= cd)
            {
                //发射
                Fire(transfrom.position, transfrom.eulerAngles);

                ReqFire req = new ReqFire();
                req.pos = Tools.ToVec_3(transfrom.position + new Vector3(0, 1, 0));
                req.rot = Tools.ToVec_3(transfrom.eulerAngles);

                NetClient.instance.Send <ReqFire>((int)MsgID.ReqFire, req);
                lastFireTime = Time.time;
            }
        }
    }
Beispiel #2
0
    private void OnReqMove(UserToken token, SocketModel model)
    {
        ReqMove req = SerializeUtil.Deserialize <ReqMove>(model.message);

        //保存坦克的实时坐标
        AccountData acc = CacheManager.instance.GetAccount(token.accountid);

        if (acc == null)
        {
            return;
        }

        Battle battle = CacheManager.instance.GetBattle(acc.battleid, acc.battleType);

        if (battle == null)
        {
            return;
        }

        Tank t = battle.GetTank(token.accountid);

        if (t != null && t.hp > 0)
        {
            ////校验两帧的移动距离
            //float deltaTime = Time.time * 0.001f - t.lastMoveTime;
            //float distance = deltaTime * 5;

            //float reqDistance = Luna3D.Vector3.Distance(Tools.ToLunaVec3(req.pos), t.lastPos);
            //if (reqDistance > distance + 0.2f) return;

            //t.lastMoveTime = Time.time * 0.001f;

            //修改缓存
            t.pos = new Vector3(req.pos.x, 0, req.pos.z);
            t.rot = Tools.ToLunaVec3(req.rot);

            t.lastPos = t.pos;
            //给别人发通知
            NotifyMove notify = new NotifyMove();
            notify.id  = token.accountid;
            notify.pos = Tools.ToVec_3(t.pos);
            notify.rot = req.rot;
            MsgSender.SendOther <NotifyMove>(battle.accounts, token, (int)MsgID.NotifyMove, notify);
        }
    }