public void CollectCommandInput(ICommandInput input)
        {
            CharacterStateComponent property_CharacterState = GetParent <Unit>().GetComponent <CharacterStateComponent>();

            if (property_CharacterState.Get(SpecialStateType.CantDoAction) || property_CharacterState.Get(SpecialStateType.NotInControl))
            {
                return;
            }
            collectedNewInput = true;
            Command command = CommandGCHelper.GetCommand();

            command.commandInput          = input;
            currCommands[input.GetType()] = command;
        }
Exemple #2
0
        public ICommandResult Simulate(ICommandInput commandInput, Unit unit)
        {
            CommandInput_Move input_Move        = commandInput as CommandInput_Move;
            UnitPathComponent unitPathComponent = unit.GetComponent <UnitPathComponent>();

            PathfindingComponent pathfindingComponent = Game.Scene.GetComponent <PathfindingComponent>();

            unitPathComponent.ABPath = ComponentFactory.Create <ABPathWrap, Vector3, Vector3>(unit.Position, input_Move.clickPos);
            pathfindingComponent.Search(unitPathComponent.ABPath);

            CommandResult_Move result_Move = CommandGCHelper.GetCommandResult <CommandResult_Move>();

            result_Move.Path = unitPathComponent.ABPath.Result;
            // result_Move.dir = input_Move.moveDir;// 暂时就以输入的方向作为角色的方向
            return(result_Move);
        }
        public void GetCommandResult(UnitStateDelta unitStateDelta)
        {
            //延迟太高了,迟迟收不到服务器发过来的确认消息
            if (simulateFrame - unitStateDelta.frame > maxDiffFrame)
            {
                Log.Debug("拉扯回来");
                simulateFrame  = unitStateDelta.frame;
                preActualFrame = simulateFrame;
                //拉扯回来

                switch (unitStateDelta.commandResult)
                {
                case CommandResult_Move result:
                    //设置当前位置
                    unit.Position = result.Path[0];

                    //再预测这一帧的结果
                    unit.GetComponent <CharacterMoveComponent>().MoveAsync(result.Path).Coroutine();
                    break;
                }

                return;
            }
            if (preActualFrame != unitStateDelta.frame)
            {
                //移除上一次的确定帧和接收到的这一帧之间的缓存指令
                for (int i = preActualFrame; i < unitStateDelta.frame; i++)
                {
                    if (cacheCommands.ContainsKey(i))
                    {
                        foreach (var v in cacheCommands[i])
                        {
                            CommandGCHelper.Recycle(v);
                        }
                        cacheCommands.Remove(i);
                    }
                }
            }
            bool needRecal = false;

            try
            {
                if (cacheCommands.ContainsKey(unitStateDelta.frame))
                {
                    switch (unitStateDelta.commandResult)
                    {
                    case CommandResult_Move actualMove:
                        foreach (var v in cacheCommands[unitStateDelta.frame])
                        {
                            if (v.commandResult is CommandResult_Move simulateMove)
                            {
                                Log.Debug("frame : " + unitStateDelta.frame + "  收到服务器发过来的路径: " + actualMove.Path.ListToString <Vector3>());
                                if (simulateMove.Path.Count == 0 || actualMove.Path.Count == 0)
                                {
                                    break;
                                }
                                if (Vector3.Distance(simulateMove.Path[0], actualMove.Path[0]) < 0.05f &&
                                    Vector3.Distance(simulateMove.Path[simulateMove.Path.Count - 1], actualMove.Path[actualMove.Path.Count - 1]) < 0.05f)
                                {
                                    break;
                                }
                                else
                                {
                                    needRecal     = true;
                                    unit.Position = actualMove.Path[0];
                                    break;
                                }
                            }
                        }

                        break;
                    }
                }
            }
            catch (Exception e)
            {
                Log.Error(e.ToString());
            }

            preActualFrame = unitStateDelta.frame;
            if (needRecal)
            {
                ReCal(unitStateDelta.frame);
            }
        }