Example #1
0
    // Start is called before the first frame update
    IEnumerator Start()
    {
        boxHealthState = BoxHealthState.Healthy;
        if (boxOrigin == BoxOrigin.Instantiate)
        {
            boxOccupiedState = BoxOccupiedState.Unoccupied;
        }
        float heightToFallTo = BOX_Y;

        if (boxOrigin == BoxOrigin.Instantiate)
        {
            //Debug.Log("instantiated boxes");
            obstaclesInColumn = GridWorld.GetColumnObjects(transform.position);
            if (obstaclesInColumn.Count > 0)
            {
                //Debug.Log("obstacles count greater than 0! " + obstaclesInColumn[0].transform.name);
                Box boxObstacle = obstaclesInColumn[0].GetComponent <Box>();
                if (boxObstacle != null && boxObstacle.countDownS.Elapsed() < (0.9f * boxObstacle.HEALTH))
                {
                    heightToFallTo = BOX_Y + obstaclesInColumn[0].GetComponent <SpriteRenderer>().bounds.size.y;
                    //Debug.Log("!setting height of fall " + BOX_Y);
                }
            }

            Tween boxFallTween = transform.DOLocalMoveY(heightToFallTo, FALL_SPEED).SetSpeedBased().SetEase(Ease.InExpo);
            //yield return boxFallTween.WaitForPosition(0.8f);

            yield return(boxFallTween.WaitForCompletion());

            SoundManager.instance.PlaySingle(SoundManager.instance.crash, 0.2f);
            //OnComplete(()=>
            //{
            if (obstaclesInColumn.Count > 0)
            {
                //Debug.Log("obstacles count greater than 0!");
                Box boxObstacle = obstaclesInColumn[0].GetComponent <Box>();
                if (boxObstacle != null)
                {
                    Debug.Log("setting boxOnTop variable");
                    boxObstacle.boxOnTop = gameObject;
                }
            }

            if (boxOccupiedState == BoxOccupiedState.Unoccupied)
            {
                //only broadcast event if first box on ground
                if (heightToFallTo > -11.5f && heightToFallTo < -11f)
                {
                    OnBoxEmpty?.Invoke(gameObject);
                }
            }
        }

        CountDown();
        //StartCoroutine(CountDown());
        //});
        GridWorld.RegisterObstacle(transform, false);
        //Debug.Log("box LOC: " + GridWorld.GetSquare(transform.position)[0] + " : " + GridWorld.GetSquare(transform.position)[1]);
    }