Пример #1
0
    public void MakeActionDuration(WorkerAction action, Action onActionFinished)
    {
        float     duration = GetRandomDurationForAction(action);
        TickTimer tt       = TimeTickSystem.CreateTickTimer((int)Math.Round(duration));

        tt.AddTimerFinishedListener(onActionFinished);
        tt.StartTimer();
    }
Пример #2
0
 private Worker(MigrationOptions options, WorkerAction action)
 {
     this.threads = Enumerable.Range(0, options.MaxThreads).Select(_ => new Thread(DoWork)
     {
         IsBackground = true
     }).ToArray();
     this.action  = action;
     this.options = options;
 }
    public WorkerAction GetActionByStateHash(int hash)
    {
        WorkerAction action = default;

        if (_actionsWithHashes.ContainsKey(hash))
        {
            action = _actionsWithHashes[hash];
        }
        else if (_finalActionWithHash.Item1 == hash)
        {
            action = _finalActionWithHash.Item2;
        }
        return(action);
    }
Пример #4
0
    public Quaternion GetPathTargetRotation(WorkerAction action)
    {
        Quaternion rot = new Quaternion();

        foreach (ActionPath path in _actionsPaths)
        {
            if (path.Action == action && path.Positions.Count > 0)
            {
                rot = path.Positions[path.Positions.Count - 1].rotation;
                break;
            }
        }
        return(rot);
    }
Пример #5
0
    public List <Vector3> GetPathFromPositionToActionPosition(Vector3 start, WorkerAction action)
    {
        List <Vector3> result = new List <Vector3>();

        if (!_paths.ContainsKey(action))
        {
            return(result);
        }
        List <Vector3> path = new List <Vector3>(_paths[action]);

        if (path.Count == 0)
        {
            return(result);
        }
        if (path.Count > 0 && (start - path[0]).sqrMagnitude < _sqrBias)
        {
            return(path);
        }
        if (path.Count > 0 && (start - path[path.Count - 1]).sqrMagnitude < _sqrBias)
        {
            return(path.GetRange(path.Count - 1, 1));
        }
        foreach (KeyValuePair <WorkerAction, List <Vector3> > p in _paths)
        {
            if (p.Key == action || p.Value.Count <= 0 ||
                (p.Value[p.Value.Count - 1] - start).sqrMagnitude >= _sqrBias)
            {
                continue;
            }
            result = new List <Vector3>(p.Value);
            result.Reverse();
            result.AddRange(path);
            return(result);
        }
        int   minIdx    = 0;
        float minSqrMag = (path[0] - start).sqrMagnitude;

        for (int i = 0; i < path.Count; i++)
        {
            float sqrMag = (path[i] - start).sqrMagnitude;
            if (sqrMag < minSqrMag)
            {
                minSqrMag = sqrMag;
                minIdx    = i;
            }
        }
        return(path.GetRange(minIdx, path.Count - minIdx));
    }
Пример #6
0
    public float GetRandomDurationForAction(WorkerAction action)
    {
        float duration = 0f;

        foreach (DurationParameter param in _durationParameters)
        {
            if (param.Action == action)
            {
                duration = Random.Range(param.MinDuration, param.MaxDuration);
                break;
            }
        }
        if (duration == 0f)
        {
            duration = Random.Range(_generalParameters.workerActionDurationMin, _generalParameters.workerActionDurationMax);
        }
        return(duration);
    }
Пример #7
0
 private void Initialize(int pathHash)
 {
     if (workerActions == null)
     {
         workerActions = _animator.GetComponent <WorkerActionsComponent>();
     }
     if (_action == WorkerAction.None && workerActions != null)
     {
         _action = workerActions.GetActionByStateHash(pathHash);
     }
     if (workerPathfinder == null)
     {
         workerPathfinder = _animator.GetComponent <WorkerActionsPaths>();
     }
     if (workerDurations == null)
     {
         workerDurations = _animator.GetComponent <WorkerActionsDurations>();
     }
 }
Пример #8
0
		///<summary>
		///	Runs a <see cref = "WorkerAction" />.
		///</summary>
		public virtual void Run(WorkerAction workerAction)
		{
			if (workerAction == null)
			{
				// null action means do nothing
				return;
			}

			workerAction.SynchronizationContext = SynchronizationContext;
			Start();
			if (SynchronizationContext == null)
			{
				// no sync context means run on current thread
				RunHelper(workerAction);
			}
			else
			{
				// we have a sync context so schedule on a worker thread
				ThreadPool.QueueUserWorkItem(RunHelper, workerAction);
			}
		}
Пример #9
0
    public int GetActionDuration(WorkerAction action)
    {
        float duration = GetRandomDurationForAction(action);

        return((int)Math.Round(duration));
    }
Пример #10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="WorkerActionInfo"/> class..
 /// </summary>
 /// <param name="action"></param>
 /// <param name="cell"></param>
 public WorkerActionInfo(WorkerAction action, GridCell cell)
 {
     this.action = action;
     this.cell   = cell;
 }
Пример #11
0
        public void ShowDialog(System.Windows.Forms.Control owner, string text, string caption, Utils.ProgressDialog.Animations animation = ProgressDialog.Animations.None)
        {
            Exception workerException = null;
            var       workerThread    = new System.Threading.Thread(() =>
            {
                try
                {
                    WorkerAction.Invoke();
                }
                catch (Exception e)
                {
                    workerException = e;
                }
            });

            workerThread.Name = "ProgressCounterDialog worker";

            var dialogUpdateEvent = new AutoResetEvent(false);
            var dialogCloseEvent  = new ManualResetEvent(false);

            using (var workerProgress = new ProgressCounter(workerThread, 1))
            {
                workerProgress.StatusChanged += (List <string> status) =>
                {
                    dialogUpdateEvent.Set();
                };
                workerProgress.ProgressChanged += (double percent) =>
                {
                    dialogUpdateEvent.Set();
                };

                workerThread.Start();

                var dialog = new Utils.ProgressDialog();
                dialog.Title         = caption;
                dialog.Line1         = text;
                dialog.Line2         = workerProgress.Status;
                dialog.Animation     = animation;
                dialog.CancelMessage = "Please wait while the operation is cancelled";
                dialog.Maximum       = 100;
                dialog.Value         = (uint)(workerProgress.GetProgress() * dialog.Maximum);
                dialog.Modal         = true;
                dialog.NoTime        = true;

                IntPtr handle = IntPtr.Zero;
                if (owner != null)
                {
                    if (owner.InvokeRequired)
                    {
                        owner.Invoke(new Action(delegate { handle = owner.Handle; }));
                    }
                    else
                    {
                        handle = owner.Handle;
                    }
                }

                dialog.ShowDialog(handle);

                while (workerThread.IsAlive)
                {
                    if (dialog.HasUserCancelled)
                    {
                        workerProgress.Abort();
                        Cancelled = true;
                        break;
                    }

                    int waitResult = WaitHandle.WaitAny(new WaitHandle[] { dialogUpdateEvent, dialogCloseEvent }, 50);
                    if (waitResult == WaitHandle.WaitTimeout)
                    {
                        continue;
                    }
                    if (waitResult == 1) // dialogCloseEvent
                    {
                        break;
                    }

                    var statusList = workerProgress.GetStatusList();
                    if (statusList.Count >= 1)
                    {
                        dialog.Line2 = statusList[0];
                        if (statusList.Count >= 2)
                        {
                            dialog.Line3 = statusList[1];
                        }
                        else
                        {
                            dialog.Line3 = "";
                        }
                    }
                    else
                    {
                        dialog.Line2 = "";
                    }
                    dialog.Value = (uint)(workerProgress.GetProgress() * dialog.Maximum);
                }

                dialog.CloseDialog();
                dialog = null;

                if (workerException != null)
                {
                    throw new Exception(workerException.Message, workerException);
                }

                if (owner != null)
                {
                    owner.Invoke(new Action(delegate
                    {
                        var form = owner as System.Windows.Forms.Form;
                        if (form != null)
                        {
                            form.Activate();
                        }
                        else
                        {
                            owner.Focus();
                        }
                    }));
                }
            }
        }
Пример #12
0
        public static void Process(MigrationOptions options, IEnumerable <MigrationItem> items, int totalCount, WorkerAction action, bool noRetry = false)
        {
            var worker = new Worker(options, action);

            worker.noRetry = noRetry;
            worker.Process(items, totalCount);
        }
Пример #13
0
 public static void Process(MigrationOptions options, IEnumerable <MigrationItem> items, WorkerAction action, bool noRetry = false)
 {
     Process(options, items, items.Count(), action, noRetry);
 }
 private static void Worker_DoWork(object sender, DoWorkEventArgs e)
 {
     WorkerAction?.Invoke();
     WorkerResetEvent.Set();
 }