Пример #1
0
    //-----------------------------------fuction methods---------------------------//
    private void PlayerPlaceBlockAtPointingDir(BlockInfo blockInfo)
    {
        //then ray cast on the block
        Vector2      playerPointingDir = GetPlayerPointingDir();
        RaycastHit2D raycastHit2D      = GetRayCastHitAtPlayerLooking();

        if (raycastHit2D != new RaycastHit2D())
        {
            //we will use the block grid later
            BlockGridController blockGridController = BlockGridController.Instance;
            //calculate
            Vector2 hittedBlockPos      = raycastHit2D.transform.position;
            Vector2 DiffOfBlockAndPoint = hittedBlockPos - raycastHit2D.point;
            //then get the position of the block's position
            Vector2 colliderPos = raycastHit2D.collider.transform.position;
            //then check the length from the player to the block
            Vector2 diffFromPlayerToPointedBlock = hittedBlockPos - (Vector2)transform.position;
            //check if the player's pos to the block's pos's length
            if (diffFromPlayerToPointedBlock.magnitude > transform.localScale.magnitude * 0.75f)
            {
                bool placedBlock = false;
                //check for placed place
                //left
                if (DiffOfBlockAndPoint.x >= 0.5f)
                {
                    blockGridController.SetBlock(blockGridController.FindGamePosWithSceneVector(colliderPos - Vector2.right), blockInfo);
                    placedBlock = true;
                    if (isDebugging)
                    {
                        Debug.Log("left");
                    }
                }
                //right
                else if (DiffOfBlockAndPoint.x <= -0.5f)
                {
                    blockGridController.SetBlock(blockGridController.FindGamePosWithSceneVector(colliderPos + Vector2.right), blockInfo);
                    placedBlock = true;
                    if (isDebugging)
                    {
                        Debug.Log("Right");
                    }
                }
                //dpwn
                else if (DiffOfBlockAndPoint.y >= 0.5f)
                {
                    blockGridController.SetBlock(blockGridController.FindGamePosWithSceneVector(colliderPos - Vector2.up), blockInfo);
                    placedBlock = true;
                    if (isDebugging)
                    {
                        Debug.Log("Down");
                    }
                }
                //up
                else if (DiffOfBlockAndPoint.y <= 0.5f)
                {
                    blockGridController.SetBlock(blockGridController.FindGamePosWithSceneVector(colliderPos + Vector2.up), blockInfo);
                    placedBlock = true;
                    if (isDebugging)
                    {
                        Debug.Log("Up");
                    }
                }
                //check for if the player placed block
                if (placedBlock)
                {
                    //delete the block on ur current hand
                    PlayerHotBarSlotController currentSelectedHotBarSlotController = PlayerInventoryController.Instance.GetCurrentSelectedHotBarSlotController();
                    SlotInfo selectedHotBarSlotInfo = currentSelectedHotBarSlotController.slotInfoContainer;
                    selectedHotBarSlotInfo.Add(-1);
                    currentSelectedHotBarSlotController.UpdateAllDisplay();
                    //play sound check if there is sounds
                    if (blockInfo.blockBreakSoundInfo.Length != 0)
                    {
                        //play sound
                        int randomIndex = Random.Range(0, blockInfo.blockBreakSoundInfo.Length);
                        SoundManager.Instance.audioSource.volume = blockLoudNess;
                        SoundManager.Instance.ChangeWithSoundInfoAndPlay(blockInfo.blockBreakSoundInfo[randomIndex]);
                    }
                }
            }
        }
    }