#region License /* Copyright (c) 2010-2014 Danko Kozar Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #endregion License using System; using UnityEngine; using Debug = UnityEngine.Debug; namespace eDriven.Core.Tasks { /// /// Async task that could be synced via TaskQueue /// public abstract class TaskBase : ITask { /// /// Debug mode on /// public static bool DebugMode; #region Properties public DateTime StartTime { get { return _startTime; } } private TaskQueue.Callback _callback; public TaskQueue.Callback Callback { get { return _callback; } set { _callback = value; } } private GameObject _parent; /// /// The parent game object /// public GameObject Parent { get { return _parent; } set { _parent = value; } } private string _description; /// /// The job description (will be displayed in progress bars) /// public string Description { get { return _description; } set { _description = value; } } private Token _token = new Token(); public virtual Token Token { get { return _token; } internal set { _token = value; } } /// /// Status /// public abstract bool IsDone { get; } private bool _excluded; /// /// A flag indicating that this job is included in progress /// public bool Excluded { get { return _excluded; } } #endregion #region Members private DateTime _startTime; #endregion #region Methods /// /// Heartbeat /// public abstract void Tick(); /// /// Runs a job /// public virtual void Run() { _startTime = DateTime.Now; #if DEBUG if (DebugMode) //Debug.Log(string.Format(@"######### STARTING job [{0}] at {1} #########", this, _startTime.ToShortTimeString())); Debug.Log(string.Format(@"######### STARTING job [{0}] #########", this)); #endif } public override string ToString() { return string.Format(GetType().Name); } #endregion #region Chaining (fluent interface) /// /// Sets a job description (displayed in progress bar) /// /// /// public TaskBase SetDescription(string description) { _description = description; return this; } /// /// Sets a parent GameObject /// /// /// public TaskBase SetParent(GameObject parent) { Parent = parent; return this; } /// /// Sets the callback /// /// /// public TaskBase SetCallback(TaskQueue.Callback callback) { Callback = callback; return this; } /// /// Excludes a job from the list to show in progress bar /// /// public TaskBase Exclude() { _excluded = true; return this; } #endregion } }