protected override void UseForPlayer(Player player, ItemsBean itemData, ItemUseTypeEnum useType) { //检测玩家前方是否有方块 if (player.playerRay.RayToChunkBlock(out RaycastHit hit, out Vector3Int targetBlockPosition)) { ChunkComponent chunkForHit = hit.collider.GetComponentInParent <ChunkComponent>(); if (chunkForHit != null) { //获取位置和方向 player.playerRay.GetHitPositionAndDirection(hit, out Vector3Int targetPosition, out Vector3Int closePosition, out BlockDirectionEnum direction); if (useType == ItemUseTypeEnum.Left) { TargetBreak(itemData, targetPosition); } else { Vector3Int localPosition = targetPosition - chunkForHit.chunk.chunkData.positionForWorld; //获取原位置方块 Block tagetBlock = chunkForHit.chunk.chunkData.GetBlockForLocal(localPosition); //如果不能锄地 if (tagetBlock.blockInfo.plough_state == 0) { return; } //获取上方方块 Block upBlock = chunkForHit.chunk.chunkData.GetBlockForLocal(localPosition + Vector3Int.up); //如果上方有方块 则无法使用锄头 if (upBlock != null && upBlock.blockType != BlockTypeEnum.None) { return; } //扣除道具耐久 if (this is ItemBaseTool itemTool) { ItemsDetailsToolBean itemsDetailsTool = itemData.GetMetaData <ItemsDetailsToolBean>(); //如果没有耐久 不能锄地 if (itemsDetailsTool.life <= 0) { return; } itemsDetailsTool.AddLife(-1); //保存数据 itemData.SetMetaData(itemsDetailsTool); //回调 EventHandler.Instance.TriggerEvent(EventsInfo.ItemsBean_MetaChange, itemData); } BlockTypeEnum ploughBlockType = (BlockTypeEnum)tagetBlock.blockInfo.remark_int; //替换为耕地方块 chunkForHit.chunk.SetBlockForLocal(localPosition, ploughBlockType, direction); //播放粒子特效 BlockCptBreak.PlayBlockCptBreakEffect(ploughBlockType, targetPosition + new Vector3(0.5f, 0.5f, 0.5f)); } } } }
/// <summary> /// 破坏方块 /// </summary> /// <returns></returns> public BlockCptBreak BreakBlock(Vector3Int worldPosition, Block block, int damage) { if (dicBreakBlock.TryGetValue(worldPosition, out BlockCptBreak value)) { value.Break(damage); return(value); } else { BlockCptBreak BlockCptBreak; if (listBreakBlockIdle.Count > 0) { BlockCptBreak = listBreakBlockIdle.Dequeue(); BlockCptBreak.SetData(block, worldPosition); BlockCptBreak.ShowObj(true); dicBreakBlock.Add(worldPosition, BlockCptBreak); } else { //创建破碎效果 GameObject objBlockBreak = Instantiate(gameObject, manager.blockBreakModel); BlockCptBreak = objBlockBreak.GetComponent <BlockCptBreak>(); BlockCptBreak.SetData(block, worldPosition); dicBreakBlock.Add(worldPosition, BlockCptBreak); } BlockCptBreak.Break(damage); return(BlockCptBreak); } }
/// <summary> /// 破碎目标 /// </summary> public virtual void TargetBreak(ItemsBean itemsData, Vector3Int targetPosition) { //获取原位置方块 WorldCreateHandler.Instance.manager.GetBlockForWorldPosition(targetPosition, out Block oldBlock, out BlockDirectionEnum oldBlockDirection, out Chunk targetChunk); if (targetChunk == null) { return; } //如果原位置是空则不做处理 if (oldBlock == null || oldBlock.blockType == BlockTypeEnum.None) { return; } //获取破坏值 int breakDamage = GetBreakDamage(itemsData, oldBlock); //扣除道具耐久 if (breakDamage > 0 && this is ItemBaseTool itemTool) { ItemsDetailsToolBean itemsDetailsTool = itemsData.GetMetaData <ItemsDetailsToolBean>(); //如果已经没有耐久了 则不造成伤害 if (itemsDetailsTool.life <= 0) { breakDamage = 0; } itemsDetailsTool.AddLife(-1); //保存数据 itemsData.SetMetaData(itemsDetailsTool); //回调 EventHandler.Instance.TriggerEvent(EventsInfo.ItemsBean_MetaChange, itemsData); } BlockCptBreak BlockCptBreak = BlockHandler.Instance.BreakBlock(targetPosition, oldBlock, breakDamage); if (BlockCptBreak.blockLife <= 0) { //移除破碎效果 BlockHandler.Instance.DestroyBreakBlock(targetPosition); //创建掉落 ItemsHandler.Instance.CreateItemCptDrop(oldBlock, targetChunk, targetPosition); //移除该方块 targetChunk.RemoveBlockForWorld(targetPosition); } }