public void Use(Simulation simulation, SimulationEntity entity, IItem item)
        {
            if (entity.ControlState == null || entity.Inventory == null || !(item.Configuration.ItemType == ItemType.Block || item.Configuration.ItemType == ItemType.Wall))
            {
                return;
            }

            int mouseX = (int)entity.ControlState.Mouse.X;
            int mouseY = (int)entity.ControlState.Mouse.Y;

            Tuple <int, int, int, int> chunkInformation = simulation.World.TranslatePixelPosition(mouseX, mouseY);

            //If we are inside map
            if (chunkInformation == null)
            {
                return;
            }

            Chunk targetChunk = simulation.World.ChunkFromChunkLocation(chunkInformation.Item1, chunkInformation.Item2);

            int xSelection = chunkInformation.Item3;
            int ySelection = chunkInformation.Item4;

            //If the chunk exist
            if (targetChunk == null)
            {
                return;
            }

            //we cant place a wall or block if its covered
            if (targetChunk.Block[xSelection, ySelection] != 0)
            {
                return;
            }

            //only place if no block exist at location
            if (item.Configuration.ItemType == ItemType.Block)
            {//place block
                targetChunk.ApplyBlockUpdate(new TileUpdate
                {
                    X         = (byte)xSelection,
                    Y         = (byte)ySelection,
                    Mode      = true,
                    TileValue = (ushort)item.Configuration.TileKey
                });
            }
            else if (targetChunk.Wall[xSelection, ySelection] == 0)
            {//place wall
                targetChunk.ApplyBlockUpdate(new TileUpdate
                {
                    X         = (byte)xSelection,
                    Y         = (byte)ySelection,
                    Mode      = false,
                    TileValue = (ushort)item.Configuration.TileKey
                });
            }
            else
            {
                return;
            }

            if (item.DecrementStack())
            {
                return;
            }

            byte slotId = entity.Inventory.GetActiveSlotId();

            entity.Inventory.Remove(slotId);
        }
Exemple #2
0
        public override void Init(SimulationEntity entity)
        {
            if (entity.Socket == null || entity.Inventory == null)
            {
                return;
            }

            if (entity.Meta == null)
            {
                entity.Meta = new EntityMeta();
            }

            //right click select
            entity.Socket.On("/inventory/select", (m) =>
            {
                lock (_lock)
                {
                    InventoryActionMessage action = (InventoryActionMessage)m;

                    if (action.SlotId == null)
                    {
                        if (_swapItem == null)
                        {
                            return;
                        }

                        _dropItems.Add(_swapItem);
                        _swapItem             = null;
                        entity.Meta.MouseSlot = null;
                        return;
                    }

                    if (action.LeftClick)
                    {
                        _swapItem =
                            _swapItem?.ItemConfigKey == entity.Inventory.GetSlot((byte)action.SlotId)?.ItemConfigKey
                                ? entity.Inventory.Merge((byte)action.SlotId, _swapItem)
                                : entity.Inventory.Swap((byte)action.SlotId, _swapItem);
                    }
                    else if (action.RightClick)
                    {
                        if (_swapItem == null)
                        {
                            _swapItem = entity.Inventory.Split((byte)action.SlotId);
                        }
                        else
                        {
                            if (!entity.Inventory.IsSlotFull((byte)action.SlotId) &&
                                (_swapItem?.ItemConfigKey == entity.Inventory.GetSlot((byte)action.SlotId)?.ItemConfigKey ||
                                 entity.Inventory.GetSlot((byte)action.SlotId) == null))
                            {
                                IItem item = _swapItem.Clone();
                                item.SetStackSize(1);

                                if (!_swapItem.DecrementStack())
                                {
                                    _swapItem = null;
                                }

                                //place single item in new slot or merge left over back into original stack
                                entity.Inventory.Merge((byte)action.SlotId, item);
                            }
                        }
                    }

                    entity.Meta.MouseSlot = _swapItem?.GetInventorySlotItem();
                }
            });


            entity.Socket.On("/inventory/throwHolding", (m) =>
            {
                if (_swapItem == null)
                {
                    return;
                }

                if (m.Message == "one")
                {
                    //Throw 1 item
                    IItem dropItem = _swapItem?.Clone();
                    dropItem?.SetStackSize(1);

                    if (_swapItem?.DecrementStack() == false)
                    {
                        _swapItem             = null;
                        entity.Meta.MouseSlot = null;
                    }

                    _dropItems.Add(dropItem);
                }
                else
                {//Throw whole stack
                    _dropItems.Add(_swapItem);
                    _swapItem             = null;
                    entity.Meta.MouseSlot = null;
                }
            });
        }