public Vector2 GetPositionInInventory(InventoryContainerItem item)
        {
            Vector2 result = new Vector2(item.InventoryX * SlotSize.X, item.InventoryY * SlotSize.Y);

            result.X += item.Width * SlotSize.X * 0.5f;
            result.Y += item.Height * SlotSize.Y * 0.5f;
            result.X -= Size.X * 0.5f;
            result.Y -= Size.Y * 0.5f;
            result   += this.Position;
            return(result);
        }
        public void GetInventoryPosition(InventoryContainerItem item, ref int x, ref int y)
        {
            //determine the inventory position;
            Vector2 delta = item.Position - this.Position;

            delta.X -= item.Width * SlotSize.X * 0.5f;
            delta.Y -= item.Height * SlotSize.Y * 0.5f;
            delta.X += Size.X * 0.5f;
            delta.Y += Size.Y * 0.5f;
            x        = (int)Math.Round(delta.X / SlotSize.X);
            y        = (int)Math.Round(delta.Y / SlotSize.Y);
        }
        internal void UpdateMouse(Vector2 mousePosition)
        {
            int x = -1;
            int y = -1;

            GetInventoryPosition(mousePosition, ref x, ref y);
            if (x < 0 || y < 0 || x >= Width || y >= Height)
            {
                Hovering = null;
            }
            else
            {
                Hovering = Slots[x, y];
            }
        }
 protected override void OnComponentRemoved(Core.Component component)
 {
     base.OnComponentRemoved(component);
     if (component is InventoryContainerItem)
     {
         InventoryContainerItem item = (InventoryContainerItem)component;
         if (item.InventoryX >= 0 && item.InventoryY >= 0)
         {
             for (int y = item.InventoryY; y < item.InventoryY + item.Height; y++)
             {
                 for (int x = item.InventoryX; x < item.InventoryX + item.Width; x++)
                 {
                     Slots[x, y] = null;
                 }
             }
         }
     }
 }
        public override bool CanAccept(ContainerItem content)
        {
            if (!this.Enabled)
            {
                return(false);
            }
            if (!(content is InventoryContainerItem))
            {
                return(false);
            }
            InventoryContainerItem item = (InventoryContainerItem)content;

            int invX = -1;
            int invY = -1;

            GetInventoryPosition(item, ref invX, ref invY);

            if (invX < 0 || invX + item.Width > this.Width || invY < 0 || invY + item.Height > this.Height)
            {
                return(false);
            }

            int empty = 0;

            for (int y = invY; y < invY + item.Height; y++)
            {
                for (int x = invX; x < invX + item.Width; x++)
                {
                    if (Slots[x, y] == null)
                    {
                        empty++;
                    }
                }
            }

            if (empty < item.Width * item.Height)
            {
                return(false);
            }

            return(true);
        }
        public bool FindEmptySpotFor(InventoryContainerItem item)
        {
            if (!this.Enabled)
            {
                return(false);
            }

            for (int invY = 0; invY < this.Height - item.Height + 1; invY++)
            {
                for (int invX = 0; invX < this.Height - item.Height + 1; invX++)
                {
                    int empty = 0;
                    for (int y = invY; y < invY + item.Height; y++)
                    {
                        for (int x = invX; x < invX + item.Width; x++)
                        {
                            if (Slots[x, y] == null)
                            {
                                empty++;
                            }
                        }
                    }

                    if (empty == item.Width * item.Height)
                    {
                        item.InventoryX = invX;
                        item.InventoryY = invY;
                        return(true);
                    }
                }
            }



            return(false);
        }
Example #7
0
        private void DockAtLocation(InventoryContainer inv)
        {
            if (!CanDockAt(inv) || !inv.CanAccept(this))
            {
                if (LastContainer != null)
                {
                    MoveTo(LastContainer);
                }
                else
                {
                    MoveTo(LastPosition);
                }
                return;
            }
            inv.GetInventoryPosition(this, ref InventoryX, ref InventoryY);
            InventoryContainerItem other = inv.Slots[InventoryX, InventoryY];

            if (other != null)
            {
                //its not empty, check if it is the same and then try to stack
                if (this.StackSize > 1 && this.Name == other.Name)
                {
                    int s = other.Count + this.Count;
                    if (s <= other.StackSize)
                    {
                        //this stacks fits with the other stack
                        this.Destroyed = true;
                        other.Count    = s;
                    }
                    else
                    {
                        //return any left-overs
                        other.Count = other.StackSize;
                        this.Count  = s - other.StackSize;
                        if (LastContainer != null)
                        {
                            MoveTo(LastContainer);
                        }
                        else
                        {
                            MoveTo(LastPosition);
                        }
                    }
                    return;
                }
            }

            if (Parent != null)
            {
                this.Parent.RemoveComponent(this);
            }
            inv.AddComponent(this);
            this.Container = inv;
            this.Inventory = inv;
            State          = UIContentState.Docked;
            for (int y = InventoryY; y < InventoryY + Height; y++)
            {
                for (int x = InventoryX; x < InventoryX + Width; x++)
                {
                    inv.Slots[x, y] = this;
                }
            }
            this.Position.X = inv.Position.X + (-0.5f * inv.Width + 0.5f * this.Width + this.InventoryX) * inv.SlotSize.X;
            this.Position.Y = inv.Position.Y + (-0.5f * inv.Height + 0.5f * this.Height + this.InventoryY) * inv.SlotSize.Y;
        }