Example #1
0
    public EntityConstructor NewEntity(Dictionary <Type, IIndexed <IComponent> > compWrite, Dictionary <Type, IIndexed <IComponent> > compAddable)
    {
        Entity newEntity;

        if (deletedEntities.Count > 0)
        {
            newEntity = deletedEntities.Dequeue();
            newEntity.generation++;
        }
        else
        {
            newEntity = new Entity(0, ++currentEntity);
        }
        entities.Insert(currentEntity, newEntity);
        return(new EntityConstructor(currentEntity, compWrite, compAddable));
    }
Example #2
0
        /// <summary>
        /// Method for inserting a value
        /// </summary>
        /// <param name="value">Represents value to be added</param>
        /// <param name="LeftIndex">Represents left index</param>
        /// <param name="RightIndex">Represents right index</param>
        public void Insert(int value, Index RightIndex)
        {
            //Initial Values
            int i;

            Items.Add(value);

            //Position values to the smallest place it
            //can go
            for (i = Items.Count - 1; (i > 0 && value < Items[i - 1]); i--)
            {
                Items[i] = Items[i - 1];
            }

            //Insert the value to the selected position
            Items[i] = value;

            //Set Index Reference
            IndexList.Insert(i, RightIndex);
        }
        /// <summary>
        /// constructor
        /// </summary>
        /// <param name="source">source collection</param>
        /// <param name="filter">filter function</param>
        public FilteredReadOnlyObservableCollection(TCollection source, Func <TElement, bool> filter)
        {
            Source = source;
            Filter = filter;

            Initialize();

            {
                // propertychanged
                CollectionUtilities.ObserveElementPropertyChanged <TCollection, TElement>(source)
                .Subscribe(x =>
                {
                    var index         = source.IndexOf(x.Sender);
                    var filteredIndex = IndexList[index];
                    var isTarget      = Filter(x.Sender);
                    if (isTarget && filteredIndex == null)
                    {
                        // add
                        AppearNewItem(index);
                    }
                    else if (!isTarget && filteredIndex.HasValue)
                    {
                        // remove
                        DisappearItem(index);
                        IndexList[index] = null;
                        OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, x.Sender, filteredIndex.Value));
                    }
                })
                .AddTo(Subscription);
            }
            {
                // collection changed(support single changed only)
                source.CollectionChangedAsObservable()
                .Subscribe(x =>
                {
                    switch (x.Action)
                    {
                    case NotifyCollectionChangedAction.Add:
                        // appear
                        IndexList.Insert(x.NewStartingIndex, null);
                        if (Filter(x.NewItems.Cast <TElement>().Single()))
                        {
                            AppearNewItem(x.NewStartingIndex);
                        }
                        break;

                    case NotifyCollectionChangedAction.Move:
                        throw new NotSupportedException("Move is not supported");

                    case NotifyCollectionChangedAction.Remove:
                        var removedIndex = IndexList[x.OldStartingIndex];
                        if (removedIndex.HasValue)
                        {
                            DisappearItem(x.OldStartingIndex);
                            IndexList.RemoveAt(x.OldStartingIndex);
                            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove,
                                                                                     x.OldItems.Cast <TElement>().Single(), removedIndex.Value));
                        }
                        else
                        {
                            IndexList.RemoveAt(x.OldStartingIndex);
                        }
                        break;

                    case NotifyCollectionChangedAction.Replace:
                        var index    = IndexList[x.NewStartingIndex];
                        var isTarget = Filter(x.NewItems.Cast <TElement>().Single());
                        if (index == null && isTarget)
                        {
                            // add
                            AppearNewItem(x.NewStartingIndex);
                        }
                        else if (index.HasValue && isTarget)
                        {
                            // replace
                            InnerCollection[index.Value] = x.NewItems.Cast <TElement>().Single();
                            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace,
                                                                                     x.NewItems.Cast <TElement>().Single(), x.OldItems.Cast <TElement>().Single(), index.Value));
                        }
                        else if (index.HasValue && !isTarget)
                        {
                            // remove
                            DisappearItem(x.NewStartingIndex);
                            IndexList[x.NewStartingIndex] = null;
                            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove,
                                                                                     x.OldItems.Cast <TElement>().Single(), index.Value));
                        }

                        break;

                    case NotifyCollectionChangedAction.Reset:
                        Initialize();
                        OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
                        break;

                    default:
                        throw new InvalidOperationException();
                    }
                })
                .AddTo(Subscription);
            }
        }