예제 #1
0
        /// <summary>
        /// Retrieve a TimeSupplier by id or create a CustomTimeSupplier if it doesn't exist.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static T GetOrCreate <T>(string id) where T : class, ITimeSupplier
        {
            if (id == null)
            {
                return(null);
            }

            ITimeSupplier ts;

            if (_registeredTimeSuppliers.TryGetValue(id, out ts))
            {
                return(ts as T);
            }
            else
            {
                if (typeof(T).IsAssignableFrom(typeof(CustomTimeSupplier)))
                {
                    var ct = new CustomTimeSupplier(id);
                    _registeredTimeSuppliers[id] = ct;
                    if (_customTimeSuppliers.Count == 0)
                    {
                        GameLoop.RegisterInternalEarlyUpdate(SPTime.Update);
                    }
                    _customTimeSuppliers.Add(ct);
                    return(ct as T);
                }
                else
                {
                    throw new System.ArgumentException(string.Format("Supplied type '{0}' can not have a CustomTimeSupplier auto created for it.", typeof(T).FullName));
                }
            }
        }
예제 #2
0
 /// <summary>
 /// Removes a CustomTimeSupplier from the update pool by reference.
 /// </summary>
 /// <param name="time"></param>
 /// <returns></returns>
 public static bool RemoveCustomTime(CustomTimeSupplier time)
 {
     if (_customTimes != null)
     {
         if (_customTimes.ContainsKey(time.Id) && _customTimes[time.Id] == time)
         {
             return(_customTimes.Remove(time.Id));
         }
         else
         {
             return(false);
         }
     }
     else
     {
         return(false);
     }
 }
        public static CustomTimeSupplier CreateCustomTime(string id, float scale = 1f)
        {
            if (_customTimes == null)
            {
                _customTimes = new Dictionary<string, CustomTimeSupplier>();
                GameLoopEntry.RegisterInternalEarlyUpdate(SPTime.Update);
            }

            if(_customTimes.ContainsKey(id))
            {
                var ct = _customTimes[id];
                ct.Scale = scale;
                return ct;
            }
            else
            {
                var ct = new CustomTimeSupplier(id, scale);
                _customTimes[ct.Id] = ct;
                return ct;
            }
        }
예제 #4
0
 /// <summary>
 /// Retrieve a CustomTimeSupplier by name.
 /// </summary>
 /// <param name="id"></param>
 /// <param name="createIfNotExists"></param>
 /// <returns></returns>
 public static CustomTimeSupplier Custom(string id, bool createIfNotExists = false)
 {
     if (id == null)
     {
         return(null);
     }
     if (_customTimes == null)
     {
         if (createIfNotExists)
         {
             _customTimes = new Dictionary <string, CustomTimeSupplier>();
             GameLoopEntry.RegisterInternalEarlyUpdate(SPTime.Update);
             var ct = new CustomTimeSupplier(id);
             _customTimes[ct.Id] = ct;
             return(ct);
         }
         else
         {
             return(null);
         }
     }
     else
     {
         CustomTimeSupplier ct;
         if (_customTimes.TryGetValue(id, out ct))
         {
             return(ct);
         }
         else if (createIfNotExists)
         {
             ct = new CustomTimeSupplier(id);
             _customTimes[ct.Id] = ct;
             return(ct);
         }
         else
         {
             return(null);
         }
     }
 }
예제 #5
0
 /// <summary>
 /// Removes a CustomTimeSupplier from the update pool by reference.
 /// </summary>
 /// <param name="time"></param>
 /// <returns></returns>
 public static bool RemoveCustomTime(CustomTimeSupplier time)
 {
     if (_customTimes != null)
     {
         if (_customTimes.ContainsKey(time.Id) && _customTimes[time.Id] == time)
         {
             return _customTimes.Remove(time.Id);
         }
         else
         {
             return false;
         }
     }
     else
     {
         return false;
     }
 }
예제 #6
0
 /// <summary>
 /// Retrieve a CustomTimeSupplier by name.
 /// </summary>
 /// <param name="id"></param>
 /// <param name="createIfNotExists"></param>
 /// <returns></returns>
 public static CustomTimeSupplier Custom(string id, bool createIfNotExists = false)
 {
     if (id == null) return null;
     if (_customTimes == null)
     {
         if (createIfNotExists)
         {
             _customTimes = new Dictionary<string, CustomTimeSupplier>();
             GameLoopEntry.RegisterInternalEarlyUpdate(SPTime.Update);
             var ct = new CustomTimeSupplier(id);
             _customTimes[ct.Id] = ct;
             return ct;
         }
         else
         {
             return null;
         }
     }
     else
     {
         CustomTimeSupplier ct;
         if (_customTimes.TryGetValue(id, out ct))
         {
             return ct;
         }
         else if (createIfNotExists)
         {
             ct = new CustomTimeSupplier(id);
             _customTimes[ct.Id] = ct;
             return ct;
         }
         else
         {
             return null;
         }
     }
 }