예제 #1
0
        public virtual TItem Add(TItem item)
        {
            Ensure.Any.IsNotNull(item, nameof(item));

            var key = item.Slot;

            Ensure.Bool.IsTrue(
                ContainsKey(item.Slot),
                nameof(item),
                opt => opt.WithMessage($"Unknown slot: '{key}'."));

            var slot = Slots[item.Slot];

            Ensure.Bool.IsTrue(
                slot.AllowedFor(item) && item.AllowedFor(this),
                nameof(item),
                opt => opt.WithMessage($"'{item}' is not allowed in this container: '{this}'."));

            var replacing = this[key];

            var node = item as Node;

            if (node != null)
            {
                ItemsParent.AddChild(node);
            }

            Cache[key] = item;

            _onAdd.OnNext(item);

            return(replacing);
        }
예제 #2
0
        public virtual void Remove(TItem item)
        {
            Ensure.Any.IsNotNull(item, nameof(item));

            var key = this.Where(t => t.Value == item).Select(t => t.Key).FirstOrDefault();

            if (key == null)
            {
                return;
            }

            ItemsParent.RemoveChild(item);

            _onRemove.OnNext(item);
        }
예제 #3
0
        private HandUpItem GetItem(string userName)
        {
            var items = ItemsParent.GetChildList();
            int count = items.Count;

            for (int i = 0; i < count; i++)
            {
                var item = items[i].GetComponent <HandUpItem>();
                if (userName == item.NickName)
                {
                    return(item);
                }
            }
            return(null);
        }
예제 #4
0
        private HandUpItem GetItem(int id)
        {
            var items = ItemsParent.GetChildList();
            int count = items.Count;

            for (int i = 0; i < count; i++)
            {
                var item = items[i].GetComponent <HandUpItem>();
                if (id == item.Id)
                {
                    return(item);
                }
            }
            return(null);
        }
예제 #5
0
        public virtual void Add(TItem item)
        {
            Ensure.Any.IsNotNull(item, nameof(item));

            Ensure.Bool.IsTrue(
                AllowedFor(item) &&
                item.AllowedFor(this) &&
                Slots.TryGetValue(item.Slot, out var slot) &&
                slot.AllowedFor(item),
                nameof(item),
                opt => opt.WithMessage($"'{item}' is not allowed in this container: '{this}'."));

            ItemsParent.AddChild(item);

            Cache[item.Slot] = item;

            _onAdd.OnNext(item);
        }
예제 #6
0
        public virtual void Remove(TItem item)
        {
            Ensure.Any.IsNotNull(item, nameof(item));

            if (!Cache.Remove(item.Key))
            {
                return;
            }

            var node = item as Node;

            if (node != null)
            {
                ItemsParent.RemoveChild(node);
            }

            _onRemove.OnNext(item);
        }