Example #1
0
    /// <summary>
    /// 破碎方块
    /// </summary>
    /// <param name="damage"></param>
    public void Break(int damage, bool isPlayEffect = true)
    {
        //重置刷新时间
        timeForUpdate = 0;

        //生命值扣除
        blockLife -= damage;
        if (blockLife < 0)
        {
            blockLife = 0;
        }
        else if (blockLife > block.blockInfo.life)
        {
            blockLife = block.blockInfo.life;
        }

        float breakPro;

        if (block.blockInfo.life != 0)
        {
            breakPro = 1 - ((float)blockLife / block.blockInfo.life);
        }
        else
        {
            breakPro = 1;
        }
        SetBreakPro(breakPro);

        //播放粒子特效
        if (isPlayEffect)
        {
            PlayBlockCptBreakEffect(block, worldPosition);
        }

        WorldCreateHandler.Instance.manager.GetBlockForWorldPosition(worldPosition, out Block targetBlock, out BlockDirectionEnum targetDirection, out Chunk targetChunk);
        Vector3Int localPosition = worldPosition - targetChunk.chunkData.positionForWorld;
        //设置破坏的形状
        Mesh newMeshData = block.blockShape.GetCompleteMeshData(targetChunk, localPosition, targetDirection);

        Vector2[] newUVS = new Vector2[newMeshData.vertices.Length];
        for (int i = 0; i < newMeshData.vertices.Length; i++)
        {
            int indexUV = i % 4;
            newUVS[i] = uvList[indexUV];
        }
        newMeshData.SetUVs(0, newUVS);
        mfBlockBreak.mesh = newMeshData;

        //设置方向
        tfCenter.eulerAngles = targetBlock.GetRotateAngles(targetDirection);
        tfCenter.localScale  = new Vector3(1.001f, 1.001f, 1.001f);

        //如果是link类型,
        if (targetBlock.blockInfo.GetBlockShape() == BlockShapeEnum.CustomLink)
        {
            BlockBean         blockData         = targetChunk.GetBlockData(localPosition.x, localPosition.y, localPosition.z);
            BlockMetaBaseLink blockMetaBaseLink = Block.FromMetaData <BlockMetaBaseLink>(blockData.meta);
            transform.position = blockMetaBaseLink.GetBasePosition();
        }
    }
    public void Show(Vector3Int worldPosition, Block block, bool isInteractive)
    {
        gameObject.SetActive(true);
        //展示文本互动提示
        objInteractive.ShowObj(isInteractive);

        //如果和上一个方块处于同一个位置
        if (lastWorldPosition == worldPosition)
        {
            lastWorldPosition = worldPosition;
            return;
        }
        lastWorldPosition = worldPosition;

        //设置方向
        WorldCreateHandler.Instance.manager.GetBlockForWorldPosition(worldPosition, out Block targetBlock, out BlockDirectionEnum targetDirection, out Chunk targetChunk);
        objTargetCenterBlock.transform.eulerAngles = targetBlock.GetRotateAngles(targetDirection);
        Vector3Int localPosition = worldPosition - targetChunk.chunkData.positionForWorld;
        //如果和上一个时同一个
        Mesh newMeshData = block.blockShape.GetCompleteMeshData(targetChunk, localPosition, targetDirection);

        //设置形状
        if (block.blockShape is BlockShapeCustom blockShapeCustom)
        {
        }
        else
        {
            Vector2[] newUVS = new Vector2[newMeshData.vertices.Length];
            for (int i = 0; i < newMeshData.vertices.Length; i++)
            {
                newUVS[i] = Vector2.zero;
            }
            newMeshData.SetUVs(0, newUVS);
        }
        meshFilter.mesh    = newMeshData;
        transform.position = worldPosition;
        //如果是link类型,
        if (targetBlock.blockInfo.GetBlockShape() == BlockShapeEnum.CustomLink)
        {
            BlockBean         blockData         = targetChunk.GetBlockData(localPosition.x, localPosition.y, localPosition.z);
            BlockMetaBaseLink blockMetaBaseLink = Block.FromMetaData <BlockMetaBaseLink>(blockData.meta);
            transform.position = blockMetaBaseLink.GetBasePosition();
        }
    }