示例#1
0
    bool CanAddBlock(Vector3Int pos)
    {
        NBTBlock generator = NBTGeneratorManager.GetMeshGenerator(InventorySystem.items[ItemSelectPanel.curIndex].id);

        // 手上的
        if (generator == null)
        {
            return(false);
        }

        byte     type            = NBTHelper.GetBlockByte(pos);
        NBTBlock targetGenerator = NBTGeneratorManager.GetMeshGenerator(type);

        if (generator is NBTPlant)
        {
            if (generator == targetGenerator)
            {
                return(false);
            }

            byte belowType = NBTHelper.GetBlockByte(pos + Vector3Int.down);

            //如果手上拿的是植物,则判断下方是否是否是实体
            NBTBlock targetBelowGenerator = NBTGeneratorManager.GetMeshGenerator(belowType);
            return(targetBelowGenerator != null && !(targetBelowGenerator is NBTPlant));
        }
        else
        {
            //如果手上拿的不是植物,则判断碰撞盒是否与玩家相交
            return(!cc.bounds.Intersects(new Bounds(pos, Vector3.one)));
        }
    }
示例#2
0
    //input is local position
    public byte GetBlockByte(int xInChunk, int worldY, int zInChunk)
    {
        if (xInChunk < 0 || xInChunk > 15 || zInChunk < 0 || zInChunk > 15)
        {
            //if (xInChunk < 0)
            //{
            //    NBTChunk chunk = NBTHelper.GetChunk(x - 1, z);
            //    return chunk.GetBlockByte(xInChunk + 16, worldY, zInChunk);
            //}
            //else if (xInChunk > 15)
            //{
            //    NBTChunk chunk = NBTHelper.GetChunk(x + 1, z);
            //    return chunk.GetBlockByte(xInChunk - 16, worldY, zInChunk);
            //}
            //else if (zInChunk < 0)
            //{
            //    NBTChunk chunk = NBTHelper.GetChunk(x, z - 1);
            //    return chunk.GetBlockByte(xInChunk, worldY, zInChunk + 16);
            //}
            //else if (zInChunk > 15)
            //{
            //    NBTChunk chunk = NBTHelper.GetChunk(x, z + 1);
            //    return chunk.GetBlockByte(xInChunk, worldY, zInChunk - 16);
            //}

            return(NBTHelper.GetBlockByte(xInChunk + 16 * x, worldY, zInChunk + 16 * z));
        }

        int sectionIndex = Mathf.FloorToInt(worldY / 16f);

        if (sectionIndex >= 0 && sectionIndex < Sections.Count)
        {
            TagNodeCompound  section = Sections[sectionIndex] as TagNodeCompound;
            TagNodeByteArray blocks  = section["Blocks"] as TagNodeByteArray;

            int yInSection = worldY - sectionIndex * 16;
            int blockPos   = yInSection * 16 * 16 + zInChunk * 16 + xInChunk;

            if (blockPos >= 0 && blockPos < 4096)
            {
                return(blocks.Data[blockPos]);
            }
        }
        return(0);
    }
示例#3
0
    public override void OnAddBlock(RaycastHit hit)
    {
        Vector3Int pos        = WireFrameHelper.pos + Vector3Int.RoundToInt(hit.normal);
        byte       targetType = NBTHelper.GetBlockByte(pos);

        bool       canAdd   = false;
        byte       type     = NBTGeneratorManager.id2type[id];
        byte       data     = (byte)InventorySystem.items[ItemSelectPanel.curIndex].damage;
        Vector3Int finalPos = pos;

        if (targetType == 126)
        {
            canAdd = true;
            type   = 125;
        }

        if (WireFrameHelper.generator is NBTWoodenSlab &&
            (WireFrameHelper.data < 8 && hit.normal == Vector3.up) ||
            (WireFrameHelper.data >= 8 && hit.normal == Vector3.down))
        {
            canAdd   = true;
            type     = 125;
            finalPos = WireFrameHelper.pos;
        }

        if (targetType == 0 && CanAddBlock(pos))
        {
            canAdd = true;
            if (WireFrameHelper.hitPos.y - pos.y > 0)
            {
                data += 8;
            }
        }

        if (canAdd)
        {
            PlayerController.instance.PlayHandAnimation();

            NBTHelper.SetBlockData(finalPos, type, data);

            InventorySystem.DecrementCurrent();
            ItemSelectPanel.instance.RefreshUI();
        }
    }
示例#4
0
    protected bool CanAddBlock(Vector3Int pos)
    {
        byte     type            = NBTHelper.GetBlockByte(pos);
        NBTBlock targetGenerator = NBTGeneratorManager.GetMeshGenerator(type);

        if (this is NBTPlant)
        {
            if (this == targetGenerator)
            {
                return(false);
            }

            byte belowType = NBTHelper.GetBlockByte(pos + Vector3Int.down);

            //如果手上拿的是植物,则判断下方是否是否是实体
            NBTBlock targetBelowGenerator = NBTGeneratorManager.GetMeshGenerator(belowType);
            return(targetBelowGenerator != null && !(targetBelowGenerator is NBTPlant));
        }
        else
        {
            //如果手上拿的不是植物,则判断碰撞盒是否与玩家相交
            return(!PlayerController.instance.cc.bounds.Intersects(new Bounds(pos, Vector3.one)));
        }
    }
示例#5
0
    void ProcessMovement(float v, float h)
    {
        if (cc.isGrounded)
        {
            if (v != 0 || h != 0)
            {
                horizontalSpeed += transform.forward * v + transform.right * h;
            }
            else
            {
                horizontalSpeed *= attenuation;
            }
            if (isFlying)
            {
                isFlying = false;
                vcamWide.SetActive(false);
            }
        }
        else
        {
            if (isFlying)
            {
                if (v != 0 || h != 0)
                {
                    if (horizontalSpeed.magnitude > 1)
                    {
                        horizontalSpeed += (transform.forward * v + transform.right * h) * 0.1f;
                    }
                    else
                    {
                        horizontalSpeed += (transform.forward * v + transform.right * h);
                    }
                    horizontalSpeed = Vector3.ClampMagnitude(horizontalSpeed, flyHorizontalSpeed);
                }
                else
                {
                    horizontalSpeed *= 0.9f;
                }
            }
            else
            {
                horizontalSpeed = horizontalSpeed + (transform.forward * v + transform.right * h) * inAirSpeed;
            }
        }
        if (!isFlying)
        {
            horizontalSpeed = Vector3.ClampMagnitude(horizontalSpeed, 1);
        }
        Vector3 horizontalMovement = horizontalSpeed * Time.fixedDeltaTime;

        if (isFlying)
        {
            if (Input.GetKey(KeyCode.Space))
            {
                verticalSpeed += Vector3.up;
            }
            else if (Input.GetKey(KeyCode.LeftShift))
            {
                verticalSpeed -= Vector3.up;
            }
            else
            {
                verticalSpeed *= attenuation;
            }
            verticalSpeed.y = Mathf.Clamp(verticalSpeed.y, -flyVerticalSpeed, flyVerticalSpeed);
        }
        else
        {
            verticalSpeed += fallSpeed * Time.fixedDeltaTime;
        }
        Vector3 verticalMovement = verticalSpeed * Time.fixedDeltaTime;

        float precision = 0.001f;

        if (horizontalMovement != Vector3.zero)
        {
            if (!isMoving)
            {
                isMoving = true;
            }
        }
        else
        {
            if (isMoving)
            {
                isMoving = false;
            }
        }
        cc.Move(horizontalMovement * (isRun ? runSpeed : horizontalScale) + verticalMovement * verticalScale);

        position = transform.position;
        forward  = transform.forward;

        if (cc.isGrounded)
        {
            verticalSpeed = Vector3.zero;
            if ((verticalMovement.sqrMagnitude > precision) || (isMoving && horizontalSpeed.sqrMagnitude > 0.2f))
            {
                Vector3Int pos  = Vector3Int.RoundToInt(transform.position - Vector3.up / 10);
                byte       type = NBTHelper.GetBlockByte(pos.x, pos.y, pos.z);
                if (type != 0 && Time.realtimeSinceStartup - lastFootstepTime > footstepInterval)
                {
                    SoundManager.PlayFootstepSound(type, gameObject);
                    lastFootstepTime = Time.realtimeSinceStartup;
                }
            }
        }
    }
示例#6
0
    public override void OnAddBlock(RaycastHit hit)
    {
        if (hit.normal != Vector3.up)
        {
            return;
        }

        Vector3Int pos = WireFrameHelper.pos + Vector3Int.RoundToInt(hit.normal);

        byte upperType = NBTHelper.GetBlockByte(pos + Vector3Int.up);
        byte lowerType = NBTHelper.GetBlockByte(pos);

        if (upperType != 0 || lowerType != 0)
        {
            return;
        }

        PlayerController.instance.PlayHandAnimation();

        byte type      = NBTGeneratorManager.id2type[id];
        byte upperData = 0b1000;
        byte lowerData = 0b0000;

        Vector3 playerPos = PlayerController.instance.position;
        Vector2 dir       = (new Vector2(playerPos.x, playerPos.z) - new Vector2(pos.x, pos.z)).normalized;

        Vector3 diff = WireFrameHelper.hitPos - pos;

        if (dir.x > 0)
        {
            if (dir.y > 0)
            {
                if (dir.y > dir.x)
                {
                    // positive z
                    lowerData = 3;
                    if (diff.x > 0)
                    {
                        upperData |= 0b0001;
                    }
                }
                else
                {
                    // positive x
                    lowerData = 2;
                    if (diff.z < 0)
                    {
                        upperData |= 0b0001;
                    }
                }
            }
            else
            {
                if (-dir.y > dir.x)
                {
                    // negative z
                    lowerData = 1;
                    if (diff.x < 0)
                    {
                        upperData |= 0b0001;
                    }
                }
                else
                {
                    // positive x
                    lowerData = 2;
                    if (diff.z < 0)
                    {
                        upperData |= 0b0001;
                    }
                }
            }
        }
        else
        {
            if (dir.y > 0)
            {
                if (dir.y > -dir.x)
                {
                    // positive z
                    lowerData = 3;
                    if (diff.x > 0)
                    {
                        upperData |= 0b0001;
                    }
                }
                else
                {
                    // negative x
                    lowerData = 0;
                    if (diff.z > 0)
                    {
                        upperData |= 0b0001;
                    }
                }
            }
            else
            {
                if (-dir.y > -dir.x)
                {
                    // negative z
                    lowerData = 1;
                    if (diff.x < 0)
                    {
                        upperData |= 0b0001;
                    }
                }
                else
                {
                    // negative x
                    lowerData = 0;
                    if (diff.z > 0)
                    {
                        upperData |= 0b0001;
                    }
                }
            }
        }

        NBTHelper.SetBlockData(pos + Vector3Int.up, type, upperData);
        NBTHelper.SetBlockData(pos, type, lowerData);

        InventorySystem.DecrementCurrent();
        ItemSelectPanel.instance.RefreshUI();
    }