/// <summary>
        /// 检测并执行<see cref="IRecyclable"/>接口的逻辑
        /// </summary>
        /// <typeparam name="TEntity">实体基类</typeparam>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="entity">要检测的实体信息</param>
        /// <param name="operation">回收站操作类型</param>
        /// <returns></returns>
        public static TEntity CheckIRecycle <TEntity, TKey>(this TEntity entity, RecycleOperation operation)
            where TEntity : IEntity <TKey>
            where TKey : IEquatable <TKey>
        {
            if (!(entity is IRecyclable))
            {
                return(entity);
            }
            IRecyclable entity1 = entity as IRecyclable;

            switch (operation)
            {
            case RecycleOperation.LogicDelete:
                entity1.IsDeleted = true;
                break;

            case RecycleOperation.Restore:
                entity1.IsDeleted = false;
                break;

            case RecycleOperation.PhysicalDelete:
                if (!entity1.IsDeleted)
                {
                    throw new InvalidOperationException("数据不处于回收(IsDeleted==true)状态,不能永久删除。");
                }
                break;
            }
            return((TEntity)entity1);
        }
Beispiel #2
0
        public IEnumerable <LogMessage> GetErrors(IMobileSpaceObject executor, IRecyclable target)
        {
            if (target == null)
            {
                yield return(new GenericLogMessage("A scrap order was issued for a nonexistent target."));

                yield break;
            }
            if (target.IsDisposed)
            {
                yield return(target.CreateLogMessage($"{target} cannot be scrapped because it is already destroyed."));
            }
            if (target.RecycleContainer != executor)
            {
                yield return(target.CreateLogMessage(target + " cannot be scrapped by " + executor + " because " + target + " does not belong to " + executor + "."));
            }
            if ((target is Ship || target is Base) && !executor.Sector.SpaceObjects.Any(sobj => sobj.Owner == executor.Owner && sobj.HasAbility("Space Yard")))
            {
                yield return(target.CreateLogMessage(target + " cannot be scrapped at " + executor.Sector + " because there is no space yard present in that sector."));
            }
            if ((target is IUnit) && !executor.Sector.SpaceObjects.Any(sobj => sobj.Owner == executor.Owner && (sobj is Planet || sobj.HasAbility("Space Yard"))))
            {
                yield return(target.CreateLogMessage(target + " cannot be scrapped at " + executor.Sector + " because there is no space yard or colony present in that sector."));
            }
        }
		public void Put(IRecyclable x) {
			if (x.GetType() != m_Type)
				throw new ArgumentException();

			lock (m_Stack)
				if (!m_Stack.Contains(x))
					m_Stack.Push(x);
		}
Beispiel #4
0
        /// <summary>
        ///   Calls the Recycle() method on an objects if it implements
        ///   the IRecyclable interface
        /// </summary>
        /// <param name="item">
        ///   Object whose Recycle() method will be called if supported by the object
        /// </param>
        private static void callRecycleIfSupported(ItemType item)
        {
            IRecyclable recycleable = item as IRecyclable;

            if (recycleable != null)
            {
                recycleable.Recycle();
            }
        }
Beispiel #5
0
        private RecyclableTreeNode add_subnode(IRecyclable child_info)
        {
            var subnodeObj = Instantiate(gameObject, subnodes);
            var subnode    = subnodeObj.GetComponent <RecyclableTreeNode>();

            subnode.ui = ui;
            subnode.subnodesToggle.group = null;
            subnode.subnodesToggle.SetIsOnAndColorWithoutNotify(false);
            subnode.SetRecyclableInfo(child_info);
            children[child_info.ID] = subnodeObj;
            subnodeObj.SetActive(true);
            return(subnode);
        }
 public void AddRoot(IRecyclable rootInfo)
 {
     if (!rootNodes.ContainsKey(rootInfo.ID))
     {
         var newNodeObj = Instantiate(recyclableTreeNodePrefab, rootsContainer);
         var newNode    = newNodeObj.GetComponent <RecyclableTreeNode>();
         newNode.ui = this;
         newNode.SetRecyclableInfo(rootInfo);
         newNode.subnodesToggle.group = rootToggles;
         newNodeObj.SetActive(true);
         rootNodes.Add(rootInfo.ID, newNode);
     }
 }
Beispiel #7
0
        public void Put(IRecyclable x)
        {
            if (x.GetType() != m_Type)
            {
                throw new ArgumentException();
            }

            lock (m_Stack)
                if (!m_Stack.Contains(x))
                {
                    m_Stack.Push(x);
                }
        }
Beispiel #8
0
        public void Recycle(T obj)
        {
            IRecyclable recyclable = obj as IRecyclable;

            recyclable?.Clear();
            lock (mObjects)
            {
                if (mObjects.Contains(obj))
                {
                    throw new Exception("The object has been released.");
                }
                mObjects.Enqueue(obj);
            }
        }
        private RecyclableTreeNode add_subnode(IRecyclable child_info)
        {
            if (children.TryGetValue(child_info.ID, out var node))
            {
                return(node);
            }
            var subnodeObj = Instantiate(prefab, subnodes);
            var subnode    = subnodeObj.GetComponent <RecyclableTreeNode>();

            subnode.ui = ui;
            subnode.subnodesToggle.group = null;
            subnode.subnodesToggle.SetIsOnAndColorWithoutNotify(false);
            subnode.SetRecyclableInfo(child_info);
            children[child_info.ID] = subnode;
            subnodeObj.SetActive(true);
            return(subnode);
        }
Beispiel #10
0
 public T Acquire()
 {
     lock (mObjects)
     {
         T obj;
         if (mObjects.Count > 0)
         {
             obj = mObjects.Dequeue();
         }
         else
         {
             obj = new T();
         }
         IRecyclable recyclable = obj as IRecyclable;
         recyclable?.Reset();
         return(obj);
     }
 }
Beispiel #11
0
        public void Execute(IRecyclable target, bool didRecycle = false)
        {
            // don't scrap stuff that's already been scrapped due to it being in cargo of something else being scrapped!
            if (!target.IsDisposed)
            {
                var val = target.ScrapValue;
                if (target.Owner != null)                 // if not, it's already scrapped?
                {
                    target.Owner.StoredResources += val;
                    target.Owner.Log.Add(target.CreateLogMessage("We have scrapped " + target + " and reclaimed " + val + "."));
                }
                target.Dispose();

                if (!didRecycle)
                {
                    target.Recycle(this, true);
                }
            }
        }
    private void LaunchProjectile(Vector3 direction)
    {
        if (ObjectPoolManager.Instance.PoolCount(projectileType) <= 0)
        {
            return;
        }

        IRecyclable projectile     = ObjectPoolManager.Instance.DequeueObject(projectileType);
        Vector3     resultVelocity = (projectileSpeed + Vector3.Dot(transform.forward, rb.velocity)) * direction;

        int finalDamage = damage;

        finalDamage = (int)(finalDamage * Random.Range(0.9f, 1.1f));

        if (isPlayer)
        {
            finalDamage = (int)(finalDamage * resultVelocity.magnitude / projectileSpeed);
        }

        projectile.SetInitialProjectileValues(firePoint.position, resultVelocity, finalDamage, targetLayers, gameObject);
    }
Beispiel #13
0
        public void Trash(IRecyclable recyclable)
        {
            if (recyclable.isTrashed)
            {
                throw new ArgumentException("Trying to add an element to the Recycler more than once");
            }
            Type type = recyclable.GetType();
            Stack <IRecyclable> stack;

            if (!this.m_ReusableStacks.TryGetValue(type, out stack))
            {
                stack = new Stack <IRecyclable>();
                this.m_ReusableStacks.Add(type, stack);
            }
            recyclable.isTrashed = true;
            recyclable.OnTrash();
            if (stack.Count < 500)
            {
                stack.Push(recyclable);
            }
        }
        private static IEnumerator <YieldInstruction> find_children(
            IRecyclable recyclable,
            string named_like,
            HashSet <uint> show_subnode_ids
            )
        {
            var show = recyclable.Name.ToLowerInvariant().Contains(named_like);

            foreach (var child in recyclable.GetChildren())
            {
                var child_coro = find_children(child, named_like, show_subnode_ids);
                while (child_coro.MoveNext())
                {
                    yield return(child_coro.Current);
                }
                show |= show_subnode_ids.Contains(child.ID);
            }
            if (show)
            {
                show_subnode_ids.Add(recyclable.ID);
            }
            yield return(null);
        }
Beispiel #15
0
    private IRecyclable GetEnemy()
    {
        int      enemyTypeIndex  = Random.Range(0, enemyTypes.Length);
        int      randomizedIndex = enemyTypeIndex;
        PoolType enemyType       = enemyTypes[enemyTypeIndex];

        do
        {
            if (ObjectPoolManager.Instance.PoolCount(enemyType) <= 0)
            {
                enemyTypeIndex++;
                enemyTypeIndex %= enemyTypes.Length;
                enemyType       = enemyTypes[enemyTypeIndex];
                continue;
            }

            IRecyclable enemy = ObjectPoolManager.Instance.DequeueObject(enemyType);
            enemy.SetInitialEnemyValues(GetSpawningPosition(), transform.position, areaRadius);

            return(enemy);
        } while (enemyTypeIndex != randomizedIndex);
        return(null);
    }
        /// <summary>
        /// 检测并执行<see cref="IRecyclable"/>接口的逻辑
        /// </summary>
        /// <param name="entity">要检测的实体信息</param>
        /// <param name="operation">回收站操作类型</param>
        public static TEntity CheckIRecycle <TEntity, TKey>(this TEntity entity, RecycleOperation operation)
            where TEntity : IEntity <TKey>
        {
            if (!(entity is IRecyclable))
            {
                return(entity);
            }
            IRecyclable entity1 = entity as IRecyclable;

            switch (operation)
            {
            case RecycleOperation.LogicDelete:
                entity1.IsDeleted = true;
                break;

            case RecycleOperation.Restore:
                entity1.IsDeleted = false;
                break;

            case RecycleOperation.PhysicalDelete:
                break;
            }
            return((TEntity)entity1);
        }
 /// <summary>
 ///
 /// </summary>
 private void Start()
 {
     recycle = GetComponent <IRecyclable>();
 }
Beispiel #18
0
 public static void Recycle(IRecyclable instance)
 {
     ResuableObjectPool <IRecyclable> .Instance.Recycle(instance);
 }
 public void EnqueueObject(IRecyclable recyclable, PoolType poolType)
 {
     objectPools[poolType].Enqueue(recyclable);
 }
 public void SetRecyclableInfo(IRecyclable recyclable_info)
 {
     hide_subnodes();
     info = recyclable_info;
     recyclable_info.SetDisplay(this);
 }
 /// <summary>
 /// Releases an instance of a recyclable object back to the pool.
 /// </summary>
 /// <param name="instance">The instance of IRecyclable to release.</param>
 void IRecycler.Release(IRecyclable instance)
 {
     this.Release(instance as T);
 }
 /// <summary>
 ///
 /// </summary>
 void Awake()
 {
     _rb2d   = GetComponent <Rigidbody2D>();
     recycle = GetComponent <IRecyclable>();
 }
Beispiel #23
0
 public void SetRecyclableInfo(IRecyclable recyclable_info)
 {
     show_subnodes(false);
     info = recyclable_info;
     recyclable_info.SetDisplay(this);
 }
Beispiel #24
0
    public void AddResToCache(int resId, GameObject go)
    {
        if (MaxCacheCount == 0)
        {
            if (go != null)
            {
                UnityEngine.Object.Destroy(go);
            }
            return;
        }

        LinkedList <CacheItem> ls;

        if (!_CacheMap.TryGetValue(resId, out ls))
        {
            ls = new LinkedList <CacheItem>();
            _CacheMap[resId] = ls;
            _CurCacheCount++;
        }
        else
        {
            if (_CurCacheCount >= MaxCacheCount)
            {
                var oldGo = ls.First.Value.Model;
                ls.RemoveFirst();

                if (oldGo != null)
                {
                    UnityEngine.Object.Destroy(oldGo);
                }
            }
            else
            {
                _CurCacheCount++;
            }
        }

        if (go != null)
        {
            go.transform.parent        = _ResCacheRoot;
            go.transform.localPosition = Vector3.zero;
            go.transform.forward       = Vector3.forward;
            go.transform.localScale    = Vector3.one;
            var entityComps = go.GetComponents <MonoBehaviour>();
            for (var i = 0; i < entityComps.Length; i++)
            {
                IRecyclable recyclable = entityComps[i] as IRecyclable;
                if (recyclable != null)
                {
                    recyclable.OnRecycle();
                }
            }

            var bocol = go.GetComponent <BodyPartCollector>();
            if (null != bocol)
            {
                bocol.Revert();
            }
        }

        CacheItem ent = new CacheItem();

        ent.Model     = go;
        ent.TimeStamp = Time.time;
        ls.AddLast(ent);
    }
 public RecycleFacilityOrCargoOrder(IRecycleBehavior behavior, IRecyclable target)
 {
     Behavior = behavior;
     Target   = target;
 }
Beispiel #26
0
 void IRecyclablePool <T> .RecycleObject(IRecyclable obj)
 {
     RecycleObject((T)obj);
 }