Esempio n. 1
0
 public void Kill()
 {
     if (!_pool.Despawn(this))
     {
         Object.Destroy(this.gameObject);
     }
     else
     {
         //TODO - need a cleaner way of doing this
         using (var lst = TempCollection.GetList <Rigidbody>())
         {
             this.transform.GetComponentsInChildren <Rigidbody>(lst);
             var e = lst.GetEnumerator();
             while (e.MoveNext())
             {
                 e.Current.velocity        = Vector3.zero;
                 e.Current.angularVelocity = Vector3.zero;
             }
         }
         using (var lst = TempCollection.GetList <Rigidbody2D>())
         {
             this.transform.GetComponentsInChildren <Rigidbody2D>(lst);
             var e = lst.GetEnumerator();
             while (e.MoveNext())
             {
                 e.Current.velocity        = Vector2.zero;
                 e.Current.angularVelocity = 0f;
             }
         }
     }
     if (this.OnKilled != null)
     {
         this.OnKilled(this, System.EventArgs.Empty);
     }
 }
        public static ButtonDelegate CreateAxleButtonDelegate <TInputId>(this IEnumerable <IInputProfile <TInputId> > profiles,
                                                                         TInputId axis, AxleValueConsideration consideration = AxleValueConsideration.Positive,
                                                                         Joystick joystick        = Joystick.All, Comparison <IInputProfile <TInputId> > comparison = null,
                                                                         float axleButtonDeadZone = InputUtil.DEFAULT_AXLEBTNDEADZONE)
            where TInputId : struct, System.IConvertible
        {
            using (var lst = TempCollection.GetList <IInputProfile <TInputId> >(from p in profiles where p != null select p))
            {
                if (comparison != null)
                {
                    lst.Sort(comparison);
                }

                if (lst.Count == 0)
                {
                    return(null);
                }
                if (lst.Count == 1)
                {
                    return(CreateAxleButtonDelegate(lst[0].GetMapping(axis).CreateAxisDelegate(joystick), consideration, axleButtonDeadZone));
                }
                if (lst.Count == 2)
                {
                    return(CreateAxleButtonDelegate(lst[0].GetMapping(axis).CreateAxisDelegate(joystick), consideration, axleButtonDeadZone).Merge(
                               CreateAxleButtonDelegate(lst[1].GetMapping(axis).CreateAxisDelegate(joystick), consideration, axleButtonDeadZone)));
                }

                var arr = (from p in lst select CreateAxleButtonDelegate(p.GetMapping(axis).CreateAxisDelegate(joystick), consideration, axleButtonDeadZone)).ToArray();
                return(Merge(arr));
            }
        }
        void IPersistantUnityObject.OnDeserialize(SerializationInfo info, StreamingContext context, IAssetBundle assetBundle)
        {
            this.transform.position   = (Vector3)info.GetValue("pos", typeof(Vector3));
            this.transform.rotation   = (Quaternion)info.GetValue("rot", typeof(Quaternion));
            this.transform.localScale = (Vector3)info.GetValue("scale", typeof(Vector3));

            int cnt = info.GetInt32("count");

            if (cnt > 0)
            {
                using (var lst = TempCollection.GetList <IPersistantUnityObject>())
                {
                    this.GetComponentsInChildren <IPersistantUnityObject>(true, lst);
                    for (int i = 0; i < cnt; i++)
                    {
                        ChildObjectData data = (ChildObjectData)info.GetValue(i.ToString(), typeof(ChildObjectData));
                        if (data != null && data.ComponentType != null)
                        {
                            IPersistantUnityObject pobj = (from o in lst where o.Uid == data.Uid select o).FirstOrDefault();
                            if (pobj != null)
                            {
                                pobj.OnDeserialize(data.DeserializeInfo, data.DeserializeContext, assetBundle);
                            }
                        }
                    }
                }
            }
        }
Esempio n. 4
0
        public static int SelectFromMultiple(ISpawner spawnPoint, int optionCount, bool ignoreSelectionModifiers = false)
        {
            if (optionCount <= 0)
            {
                return(-1);
            }
            if (optionCount == 1)
            {
                return(0);
            }

            if (!ignoreSelectionModifiers && spawnPoint != null && spawnPoint.component != null)
            {
                using (var lst = TempCollection.GetList <ISpawnPointSelector>())
                {
                    spawnPoint.component.GetComponents <ISpawnPointSelector>(lst);
                    for (int i = 0; i < lst.Count; i++)
                    {
                        if (lst[i].enabled)
                        {
                            int index = lst[i].Select(spawnPoint, optionCount);
                            if (index >= 0)
                            {
                                return(index);
                            }
                        }
                    }
                }
            }

            return(RandomUtil.Standard.Range(optionCount));
        }
        void IPersistantUnityObject.OnSerialize(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("pos", this.transform.position);
            info.AddValue("rot", this.transform.rotation);
            info.AddValue("scale", this.transform.localScale);

            using (var lst = TempCollection.GetList <IPersistantUnityObject>())
            {
                this.GetComponentsInChildren <IPersistantUnityObject>(true, lst);
                if (lst.Count > 0)
                {
                    var data = new ChildObjectData();
                    int cnt  = 0;

                    for (int i = 0; i < lst.Count; i++)
                    {
                        if (object.ReferenceEquals(this, lst[i]))
                        {
                            continue;
                        }

                        data.Uid           = lst[i].Uid;
                        data.ComponentType = lst[i].GetType();
                        data.Pobj          = lst[i];
                        info.AddValue(cnt.ToString(), data, typeof(ChildObjectData));
                        cnt++;
                    }
                    info.AddValue("count", cnt);
                }
            }
        }
        /// <summary>
        /// Destroys the GameObject and its children, if the GameObject contains a KillableEntity component that will handle the death first and foremost.
        /// </summary>
        /// <param name="obj"></param>
        public static void Kill(this GameObject obj)
        {
            if (obj.IsNullOrDestroyed())
            {
                return;
            }

            using (var lst = TempCollection.GetList <IKillableEntity>())
            {
                //this returns in the order from top down, we will loop backwards to kill bottom up
                obj.GetComponentsInChildren <IKillableEntity>(true, lst);
                if (lst.Count > 0)
                {
                    for (int i = lst.Count - 1; i > -1; i--)
                    {
                        lst[i].Kill();
                    }

                    if (lst[0].gameObject != obj)
                    {
                        ObjUtil.SmartDestroy(obj);
                    }
                }
                else
                {
                    ObjUtil.SmartDestroy(obj);
                }
            }
        }
        public static IEnumerable <Transform> GetAllChildrenAndSelf(this Transform t)
        {
            //yield return t;

            ////we do the first children first
            //foreach (Transform trans in t.transform)
            //{
            //    yield return trans;
            //}

            ////then grandchildren
            //foreach (Transform trans in t.transform)
            //{
            //    foreach (var child in GetAllChildren(trans))
            //    {
            //        yield return child;
            //    }
            //}

            using (var lst = TempCollection.GetList <Transform>())
            {
                GetAllChildrenAndSelf(t, lst);
                return(lst.ToArray());
            }
        }
        /// <summary>
        /// Destroys the entire entity, if the entity contains a KillableEntity component that will handle death first and foremost.
        /// </summary>
        /// <param name="obj"></param>
        public static void KillEntity(this GameObject obj)
        {
            if (obj.IsNullOrDestroyed())
            {
                return;
            }

            obj = obj.FindRoot();

            using (var lst = TempCollection.GetList <IKillableEntity>())
            {
                obj.GetComponentsInChildren <IKillableEntity>(true, lst);
                if (lst.Count > 0)
                {
                    var e = lst.GetEnumerator();
                    while (e.MoveNext())
                    {
                        e.Current.Kill();
                    }
                }
                else
                {
                    ObjUtil.SmartDestroy(obj);
                }
            }
        }
        public static IAspect Sense(this Sensor sensor, System.Func <IAspect, bool> predicate = null, System.Comparison <IAspect> precedence = null)
        {
            if (sensor == null)
            {
                throw new System.ArgumentNullException("sensor");
            }

            if (precedence == null)
            {
                return(sensor.Sense(predicate));
            }
            else
            {
                using (var lst = TempCollection.GetList <IAspect>())
                {
                    if (sensor.SenseAll(lst, predicate) > 0)
                    {
                        lst.Sort(precedence);
                        return(lst[0]);
                    }
                    else
                    {
                        return(null);
                    }
                }
            }
        }
Esempio n. 10
0
        private void DealWithEnable(MonoBehaviour component)
        {
            using (var todestroy = TempCollection.GetList <RadicalCoroutine>())
                using (var toresume = TempCollection.GetList <RadicalCoroutine>())
                {
                    var e = _routines.GetEnumerator();
                    while (e.MoveNext())
                    {
                        if (e.Current.Operator == component)
                        {
                            switch (e.Current.OperatingState)
                            {
                            case RadicalCoroutineOperatingState.Active:
                                //if the routine is currently active, that means the routine was already running. This could be because it
                                //was started in Awake, in an override of OnEnable, or the routine is in a mode that does not pause it OnDisable.
                                continue;

                            case RadicalCoroutineOperatingState.Inactive:
                            case RadicalCoroutineOperatingState.Paused:
                                if (e.Current.DisableMode.HasFlag(RadicalCoroutineDisableMode.Resumes))
                                {
                                    toresume.Add(e.Current);
                                }
                                else
                                {
                                    todestroy.Add(e.Current);
                                    Debug.LogWarning("A leaked RadicalCoroutine was found and cleaned up.", component);
                                }
                                break;

                            default:
                                //somehow a finished routine made its way into the collection... remove it
                                todestroy.Add(e.Current);
                                Debug.LogWarning("A leaked RadicalCoroutine was found and cleaned up.", component);
                                break;
                            }
                        }
                    }

                    if (todestroy.Count > 0)
                    {
                        for (int i = 0; i < todestroy.Count; i++)
                        {
                            _routines.Remove(todestroy[i]);
                        }
                    }

                    if (toresume.Count > 0)
                    {
                        for (int i = 0; i < toresume.Count; i++)
                        {
                            toresume[i].Resume(component);
                        }
                    }
                }
        }