Exemple #1
0
 private void CellOnStackCountChange(IBagCell cell)
 {
     if (cell.Item is IStack stack)
     {
         if (stack.Count == 0)
         {
             _cells.Remove(cell);
             cell.OnStackCountChange -= CellOnStackCountChange;
             OnCellRemoved?.Invoke(this, cell);
         }
     }
 }
Exemple #2
0
        public IReadOnlyCollection <IThing> Pull(IStack stack)
        {
            if (stack == null)
            {
                throw new ArgumentNullException(nameof(stack));
            }
            try
            {
                if (stack.Count == 0)
                {
                    return(new IThing[0]);
                }

                var stackCells = _cells.Where(c => c.Item is IStack s && s.Prototype == stack.Prototype).ToArray();
                if (stackCells.Any())
                {
                    if (stackCells.Sum(c => ((IStack)c.Item).Count) < stack.Count)
                    {
                        throw new Exception("Недостаточно предметов " + stack.Prototype.Name);
                    }

                    // TODO: тут надо бы с учётом того что предметы могут быть размазаны по разным стаков
                    stackCells.First().Remove(stack);

                    return(new IThing[] { new Stack(stack.Prototype, stack.Count) });
                }

                var cells = _cells.Where(c => c.Item.Prototype == stack.Prototype).Take(stack.Count).ToArray();
                if (cells.Length < stack.Count)
                {
                    throw new Exception("Недостаточно предметов " + stack.Prototype.Name);
                }

                var result = new Collection <IThing>();

                foreach (var cell in cells)
                {
                    result.Add(cell.Item);
                    cell.OnStackCountChange -= CellOnStackCountChange;
                    _cells.Remove(cell);
                    OnCellRemoved?.Invoke(this, cell);
                }

                return(result);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
Exemple #3
0
        public IThing Pull(IThing thing)
        {
            if (thing == null)
            {
                throw new ArgumentNullException(nameof(thing));
            }

            var cell = _cells.First(c => c.Item == thing);

            cell.OnStackCountChange -= CellOnStackCountChange;
            _cells.Remove(cell);
            OnCellRemoved?.Invoke(this, cell);

            return(thing);
        }