Пример #1
0
        private void AddFace(ref BlockWorldPos location, PlayerDiggingFace face, bool inverse = false)
        {
            switch (face)
            {
            case PlayerDiggingFace.Bottom:
                location.Y--;
                break;

            case PlayerDiggingFace.Top:
                location.Y++;
                break;

            case PlayerDiggingFace.North:
                location.Z--;
                break;

            case PlayerDiggingFace.South:
                location.Z++;
                break;

            case PlayerDiggingFace.West:
                location.X--;
                break;

            case PlayerDiggingFace.East:
                location.X++;
                break;

            case PlayerDiggingFace.Special:
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(face));
            }
        }
Пример #2
0
        public async Task PlaceBlock(BlockWorldPos location, EntityInteractHand hand, PlayerDiggingFace face, Vector3 cursorPosition)
        {
            if (face != PlayerDiggingFace.Special)
            {
                var world      = AttachedObject.GetWorld();
                var blockState = await world.GetBlockState(GrainFactory, location);

                var blockHandler = BlockHandler.Create((BlockId)blockState.Id);
                if (blockHandler.IsUsable)
                {
                    await blockHandler.UseBy(AttachedObject, GrainFactory, world, location, cursorPosition);
                }
                else
                {
                    var heldItem = await AttachedObject.GetComponent <HeldItemComponent>().GetHeldItem();

                    if (!heldItem.slot.IsEmpty)
                    {
                        var itemHandler = ItemHandler.Create((uint)heldItem.slot.BlockId);
                        if (itemHandler.IsPlaceable)
                        {
                            var inventory = AttachedObject.GetComponent <InventoryComponent>().GetInventoryWindow();
                            await itemHandler.PlaceBy(AttachedObject, GrainFactory, world, location, inventory, heldItem.index, face, cursorPosition);
                        }
                    }
                }
            }
        }
Пример #3
0
        public virtual async Task <bool> PlaceBy(IEntity entity, IGrainFactory grainFactory, IWorld world, BlockWorldPos position, Slot heldItem, PlayerDiggingFace face, Vector3 cursorPosition)
        {
            if (IsPlaceable)
            {
                AddFace(ref position, face);
                var blockState = await world.GetBlockState(grainFactory, position);

                if ((BlockId)blockState.Id == BlockId.Air)
                {
                    if (!heldItem.IsEmpty)
                    {
                        var newState = await ConvertToBlock(entity, grainFactory, world, position, heldItem);

                        var blockHandler = BlockHandler.Create((BlockId)newState.Id);
                        if (await blockHandler.CanBeAt(position, grainFactory, world))
                        {
                            await world.SetBlockState(grainFactory, position, newState);

                            heldItem.ItemCount--;
                            heldItem.MakeEmptyIfZero();
                            await entity.Tell(new SetHeldItem { Slot = heldItem });

                            await blockHandler.OnPlaced(entity, grainFactory, world, position, newState);

                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
Пример #4
0
        public virtual async Task <bool> PlaceBy(IPlayer player, IGrainFactory grainFactory, IWorld world, BlockWorldPos position, IInventoryWindow inventoryWindow, int slotIndex, PlayerDiggingFace face, Vector3 cursorPosition)
        {
            if (IsPlaceable)
            {
                AddFace(ref position, face);
                var blockState = await world.GetBlockState(grainFactory, position);

                if ((BlockId)blockState.Id == BlockId.Air)
                {
                    var slot = await inventoryWindow.GetSlot(player, slotIndex);

                    if (!slot.IsEmpty)
                    {
                        var newState = await ConvertToBlock(player, grainFactory, world, position, slot);

                        var blockHandler = BlockHandler.Create((BlockId)newState.Id);
                        if (await blockHandler.CanBeAt(position, grainFactory, world))
                        {
                            await world.SetBlockState(grainFactory, position, newState);

                            slot.ItemCount--;
                            slot.MakeEmptyIfZero();
                            await inventoryWindow.SetSlot(player, slotIndex, slot);

                            await blockHandler.OnPlaced(player, grainFactory, world, position, newState);

                            return(true);
                        }
                    }
                }
            }

            return(false);
        }