public JsonResult EditAction(mAction mAction)
        {
            ResSubmit submit = new ResSubmit(true, "Chỉnh sửa thành công");

            try
            {
                TbAction actionUpdate = db.TbAction.FirstOrDefault(ctr => ctr.Id == mAction.id);
                if (submit.success && actionUpdate == null)
                {
                    submit = new ResSubmit(false, "Chỉnh sửa thất bại");
                }

                if (submit.success)
                {
                    actionUpdate.Name        = mAction.name;
                    actionUpdate.Display     = mAction.display;
                    actionUpdate.Description = mAction.description;
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                var settingEx = ViewBag.SettingEx as Dictionary <string, string>;
                Utils.writeLog(ex);
                submit = new ResSubmit(false, settingEx[ex.GetType().FullName]);
            }
            return(Json(submit));
        }
    //終止Action
    public async void StopAction(mAction _old)
    {
        Debug.Log("終止" + _old.description);

        process_cts.Cancel();
        actionQueue.Remove(_old);
        actionQueue.Sort((a, b) => b.priority.CompareTo(a.priority));

        await Task.Yield();
    }
    private void OnBecameInvisible()
    {
        if (stopWhenInvisiable)
        {
            this.StopAllCoroutines();
            currentAction = null;
            actionQueue.Clear();

            isVisiable = false;
        }
    }
        private EventFunc GetExecuteDelegate(MethodInfo methodInfo, object instance, bool isFilter)
        {
            if (methodInfo.ReturnType != typeof(bool) && methodInfo.ReturnType != typeof(void))
            {
                throw new Exception("Not suport that return type not bool or void");
            }
            ParameterExpression paramsExp = Expression.Parameter(typeof(object[]), "params");

            ConstantExpression instanceExp = methodInfo.IsStatic? null : Expression.Constant(instance, instance.GetType());

            ParameterInfo[]   paramInfos           = methodInfo.GetParameters();
            List <Expression> parameterExpressions = new List <Expression>();

            if (isFilter)
            {
                if (paramInfos.Length != 1 || paramInfos[0].ParameterType != typeof(object[]))
                {
                    throw new Exception("The filter must only one param whith object[]");
                }
                parameterExpressions.Add(paramsExp);
            }
            else
            {
                for (int i = 0; i < paramInfos.Length; i++)
                {
                    parameterExpressions.Add(Expression.Convert(Expression.ArrayIndex(paramsExp, Expression.Constant(i, typeof(int))), paramInfos[i].ParameterType));
                }
            }

            MethodCallExpression methodCall = Expression.Call(instanceExp, methodInfo, parameterExpressions);

            if (methodCall.Type == typeof(bool))
            {
                Expression <EventFunc> lambda = Expression.Lambda <EventFunc>(methodCall, paramsExp);
                return(lambda.Compile());
            }
            else
            {
                Expression <mAction> lambda =
                    Expression.Lambda <mAction>(methodCall, paramsExp);

                mAction execute = lambda.Compile();

                return((object[] args) => { execute(args); return true; });
            }
        }
    //檢查用

    /*
     * private void FixedUpdate()
     * {
     *
     *  if (!isVisiable) { return; }
     *  if (cDoProcess == null && actionQueue.Count > 0)
     *  {
     *      cDoProcess = StartCoroutine(DoProcess());
     *  }
     *
     *  if (actionQueue.Count == 0)
     *  {
     *      //Debug.Log(gameObject.name + "all clear");
     *      if (eActionQueueCleared != null)
     *          eActionQueueCleared();
     *  }
     * }
     *
     */
    public void AddAction(mAction _newAct)
    {
        if (!allowDuplicate)
        {
            //if (_newAct == null || _newAct.is_in_gap_time_lock || actionQueue.Contains(_newAct))
            if (_newAct == null || _newAct.CheckCoolTime() || actionQueue.Contains(_newAct))
            {
                //Debug.Log(_newAct.description + " 還在冷卻");
                return;
            }//還在冷卻中
        }


        Debug.Log("Add Action" + _newAct.description);
        _newAct.time_out_counter = _newAct.timeOut;

        //檢查是否可斷
        if (currentAction != null && cDoProcess != null &&
            _newAct.priority > currentAction.priority &&
            _newAct.force)
        {
            Debug.Log(_newAct.description + " 斷 " + currentAction.description);
            //做Callback:
            currentAction.callbackEvent?.Invoke();
            //先終止,加入後再重啟
            StopCoroutine(cDoProcess);
            currentAction.is_in_gap_time_lock = false;
            currentAction = null;
            cDoProcess    = null;
        }

        if (actionQueue.Count > maxActionCount)
        {
            return;
        }

        actionQueue.Add(_newAct);

        if (cDoProcess == null)
        {
            Debug.Log("啟動");
            cDoProcess    = StartCoroutine(DoProcess());
            cTimeOutCheck = StartCoroutine(CheckTimeOut());
        }
    }
    //新增動作
    public async void AddAction(mAction _newAct)
    {
        //if (actionQueue.Count > 0)
        if (isProcessig)
        {
            Debug.Log("加入" + _newAct.description);
            AddActionToQueue(_newAct);
        }
        else
        {
            Debug.Log("啟動並加入" + _newAct.description);

            AddActionToQueue(_newAct);

            //processQueueingTask = Task.Run(() => ProcessActionQueue(process_cts.Token));
            StartProcess();
        }
        await Task.Yield();
    }
    async void AddActionToQueue(mAction _newAction)
    {
        //檢查優先度是否比正在執行的action高? 且可斷目前action
        if (actionQueue.Count > 0 &&
            _newAction.priority > actionQueue[0].priority &&
            _newAction.force)
        {
            //中斷目前action,執行新action
            actionQueue.Add(_newAction);
            StopAction(actionQueue[0]);
            await Task.Yield();

            return;
        }

        //加入queue
        actionQueue.Add(_newAction);
        actionQueue.Sort((a, b) => b.priority.CompareTo(a.priority));

        await Task.Yield();
    }
    //------------TEST----------------
    private async void Start()
    {
        await Task.Yield();

        Debug.Log("staty開始");

        _act_idle = new ActionController.mAction(
            Idle,
            "Idle",
            0,
            false,
            1,
            10
            );
        _act_walk = new ActionController.mAction(
            Move,
            "Move",
            1,
            false,
            1,
            10
            );

        _act_dash = new ActionController.mAction(
            Dash,
            "Dash",
            5,
            true,
            1,
            10
            );



        Debug.Log("start結束");
    }
    IEnumerator DoProcess()
    {
        //執行排隊中的方法
        while (actionQueue.Count > 0)
        {
            int next_index = !actionQueue.Any() ? -1 :
                             actionQueue
                             .Select((value, index) => new { Value = value.priority, Index = index })
                             .Aggregate((a, b) => (a.Value > b.Value) ? a : b)
                             .Index;
            Debug.Log("next" + next_index);

            /*
             * //刪掉還在冷卻的
             * if (actionQueue[next_index].is_in_gap_time_lock && actionQueue.Count > 1)
             * {
             *  actionQueue.RemoveAt(next_index);
             *  yield return WaitForFixedUpdate;
             *  //continue;
             * }*/

            //提取第一個執行
            currentAction = actionQueue[next_index];

            Debug.Log("Do action " + currentAction.description);
            //actionQueue.RemoveAt(0);
            actionQueue.RemoveAt(next_index);
            currentAction.is_in_gap_time_lock = true;

            //非一次性方法
            if (currentAction.isLoop)
            {
                float time_counter = 0;
                while (currentAction != null &&
                       currentAction.isLoop &&
                       time_counter < currentAction.duration)
                {
                    Debug.Log("Doing " + currentAction.description);
                    //currentAction.action();
                    if (currentAction.action != null)
                    {
                        currentAction.action.Invoke();
                    }

                    time_counter += Time.fixedDeltaTime;
                    yield return(WaitForFixedUpdate);
                }
            }

            //一次性方法
            else
            {
                Debug.Log("Doing " + currentAction.description + " once");
                //currentAction.action();
                if (currentAction.action != null)
                {
                    currentAction.action.Invoke();
                }
                yield return(new WaitForSeconds(currentAction.duration));
            }

            /*
             * //各自進行冷卻
             * Debug.Log("計算冷卻 " + currentAction.description);
             * StartCoroutine(currentAction.ResetLock());
             */

            //執行完
            Debug.Log("Action Done: " + currentAction.description);
            currentAction.callbackEvent?.Invoke();

            //currentAction = null;


            yield return(WaitForFixedUpdate);
        }

        //清空時
        if (eActionQueueCleared != null)
        {
            eActionQueueCleared();
        }
        cDoProcess = null;
    }
 public void ClearCurrent()
 {
     currentAction = null;
 }
Example #11
0
    public void AddAction(mAction _newAct)
    {
        if (!allowDuplicate)
        {
            if (_newAct == null || _newAct.is_in_gap_time_lock || actionQueue.Contains(_newAct))
            {
                //Debug.Log(_newAct.description + " 還在冷卻");
                return;
            }//還在冷卻中
        }


        Debug.Log("Add Action" + _newAct.description);
        _newAct.time_out_counter = _newAct.timeOut;

        //檢查是否可斷
        if (currentAction != null && cDoProcess != null &&
            _newAct.priority > currentAction.priority &&
            _newAct.force)
        {
            Debug.Log(_newAct.description + " 斷 " + currentAction.description);
            //先終止,加入後再重啟
            StopCoroutine(cDoProcess);
            currentAction.is_in_gap_time_lock = false;
            currentAction = null;
            cDoProcess    = null;
        }
        else if (currentAction == null && cDoProcess != null)
        {
            StopCoroutine(cDoProcess);
            currentAction = null;
            cDoProcess    = null;
        }


        if (actionQueue.Count > maxActionCount)
        {
            return;
        }

        /*
         * //與queue第一項比較排序
         * if (actionQueue.Count > 1 && actionQueue[0].priority < _newAct.priority)
         * {
         *  //mAction _temp=actionQueue[0];
         *  //交換
         *  actionQueue.Add(actionQueue[0]);
         *  actionQueue[0] = _newAct;
         * }*/

        actionQueue.Add(_newAct);
        //actionQueue.Sort((a, b) => b.priority.CompareTo(a.priority));
        //actionQueue.Sort();

        if (cDoProcess == null)
        {
            Debug.Log("啟動");
            cDoProcess    = StartCoroutine(DoProcess());
            cTimeOutCheck = StartCoroutine(CheckTimeOut());
        }
    }