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); } } } } }
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); }
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); }
public async Task <bool> FinishedDigging(IEntity entity, IGrainFactory grainFactory, IWorld world, BlockWorldPos position, BlockState blockState, long usedTick, GameMode gameMode) { if (!blockState.IsSameId(BlockStates.Bedrock())) { var newState = BlockStates.Air(); await world.SetBlockState(grainFactory, position, newState); // 产生 Pickup if (gameMode.ModeClass != GameMode.Class.Creative) { var chunk = position.ToChunkWorldPos(); var finder = grainFactory.GetGrain <ICollectableFinder>(world.MakeAddressByPartitionKey(chunk)); var blockHandler = BlockHandler.Create((BlockId)blockState.Id); var droppedSlot = blockHandler.DropBlock(ItemId, blockState); if (!droppedSlot.IsEmpty) { await finder.SpawnPickup(position + new Vector3(0.5f, 0.5f, 0.5f), new[] { droppedSlot }.AsImmutable()); } } return(true); } return(false); }
private BuildResult BuildMesh(ChunkSectionCompactStorage section, NeighborSections neighbor) { // 计算面总数 int planeCount = 0; for (int z = 0; z < ChunkConstants.BlockEdgeWidthInSection; z++) { for (int y = 0; y < ChunkConstants.BlockEdgeWidthInSection; y++) { for (int x = 0; x < ChunkConstants.BlockEdgeWidthInSection; x++) { var blockHandler = BlockHandler.Create((BlockId)section.Data[x, y, z].Id, ServiceProvider); planeCount += blockHandler.CalculatePlanesCount(new Vector3Int(x, y, z), section, neighbor); } } } if (planeCount == 0) { return(null); } // 填充 Mesh var vertices = new Vector3[planeCount * 4]; var normals = new Vector3[planeCount * 4]; var uvs = new Vector2[planeCount * 4]; var triangles = new int[planeCount * 6]; int planeIndex = 0; for (int z = 0; z < ChunkConstants.BlockEdgeWidthInSection; z++) { for (int y = 0; y < ChunkConstants.BlockEdgeWidthInSection; y++) { for (int x = 0; x < ChunkConstants.BlockEdgeWidthInSection; x++) { var blockHandler = BlockHandler.Create((BlockId)section.Data[x, y, z].Id, ServiceProvider); blockHandler.CreateMesh(vertices, normals, uvs, triangles, ref planeIndex, new Vector3Int(x, y, z), section, neighbor); } } } return(new BuildResult { Vertices = vertices, Normals = normals, UVs = uvs, Triangles = triangles }); }