Esempio n. 1
0
    // Update is called once per frame
    void Update()
    {
        Vector2 mouse_position;

        block_root.unprojectMousePosition(out mouse_position, Input.mousePosition);


        if (this.vanish_timer >= 0.0f)
        {
            this.vanish_timer -= Time.deltaTime;
            if (this.vanish_timer < 0.0f)
            {
                if (this.step != Block.STEP.slide)
                {
                    this.vanish_timer = -1.0f;
                    this.next_step    = Block.STEP.vacant;
                }
                else
                {
                    this.vanish_timer = 0.0f;
                }
            }
        }

        this.step_timer += Time.deltaTime;
        float slide_time = 0.2f;

        if (this.next_step == Block.STEP.NONE)
        {
            switch (this.step)
            {
            case Block.STEP.slide:
                if (this.step_timer >= slide_time)
                {
                    // 슬라이드 중인 블록이 소멸되면 vacant 상태로 이행
                    if (this.vanish_timer == 0.0f)
                    {
                        this.next_step = Block.STEP.vacant;
                        // vanish_timer가 0 이 아니면 idle 상태로 이행
                    }
                    else
                    {
                        this.next_step = Block.STEP.idle;
                    }
                }
                break;

            case Block.STEP.idle:
                this.GetComponent <Renderer>().enabled = true;
                break;

            case Block.STEP.fall:
                if (this.position_offset.y <= 0.0f)
                {
                    this.next_step         = Block.STEP.idle;
                    this.position_offset.y = 0.0f;
                }
                break;
            }
        }

        while (this.next_step != Block.STEP.NONE)
        {
            this.step      = this.next_step;
            this.next_step = Block.STEP.NONE;

            switch (this.step)
            {
            case Block.STEP.idle:
                this.position_offset      = Vector3.zero;
                this.transform.localScale = Vector3.one * 0.7f;
                break;

            case Block.STEP.grabbed:
                this.transform.localScale = Vector3.one * 1.0f;
                break;

            case Block.STEP.released:
                this.position_offset      = Vector3.zero;
                this.transform.localScale = Vector3.one * 0.7f;
                break;

            case Block.STEP.vacant:
                this.position_offset = Vector3.zero;
                this.setVisible(false);
                break;

            case Block.STEP.respawn:
                // 색을 랜덤하게 선택하여 블록을 선택한 색으로 설정.
                int color_index = Random.Range(0, SizeManager.colorNum);
                this.SetColor((Block.COLOR)color_index);
                this.next_step = Block.STEP.idle;
                break;

            case Block.STEP.fall:
                this.setVisible(true);     // 블록을 표시.
                this.fall.velocity = 0.0f; // 낙하 속도를 리셋.
                break;
            }
            this.step_timer = 0.0f;
        }

        switch (this.step)
        {
        case Block.STEP.grabbed:
            // 잡힌 상태일 때는 항상 슬라이드 방향을 체크
            this.slide_dir = this.calcSlideDir(mouse_position);
            break;

        case Block.STEP.slide:
            // 블록을 서서히 이동하는 처리
            float rate = this.step_timer / slide_time;
            rate = Mathf.Min(rate, 1.0f);
            rate = Mathf.Sin(rate * Mathf.PI / 2.0f);
            this.position_offset = Vector3.Lerp(this.position_offset_initial, Vector3.zero, rate);
            break;

        case Block.STEP.fall:
            // 속도에 중력의 영향을 준다.
            this.fall.velocity += Physics.gravity.y * Time.deltaTime * 1.5f;
            // 세로 방향 위치를 계산.
            this.position_offset.y += this.fall.velocity * Time.deltaTime;
            if (this.position_offset.y < 0.0f)
            {                                  // 다 내려왔다면.
                this.position_offset.y = 0.0f; // 그 자리에 머문다.
            }
            break;
        }

        // 그리드 좌표를 실제 좌표로 변환하고 position_offset을 추가한다
        Vector3 position = BlockRoot.calcBlockPosition(this.i_pos) + this.position_offset;

        // 실제 위치를 새로운 위치로 변경한다
        this.transform.position = position;

        this.SetColor(this.color);

        if (this.vanish_timer >= 0.0f)
        {
            Renderer rd     = this.GetComponent <Renderer>();
            Color    color0 = Color.Lerp(rd.material.color, Color.white, 0.5f);
            Color    color1 = Color.Lerp(rd.material.color, Color.black, 0.5f);

            // 불붙는 연출 시간이 절반을 지났다면,
            if (this.vanish_timer < Block.VANISH_TIME / 2.0f)
            {
                // 투명도(a)를 설정
                color0.a = this.vanish_timer / (Block.VANISH_TIME / 2.0f);
                color1.a = color0.a;

                rd.material = this.transparent_material;
            }
            // vanish_timer가 줄어들수록 1에 가까워진다
            float rate = 1.0f - this.vanish_timer / Block.VANISH_TIME;
            //서서히 색을 바꾼다
            rd.material.color = Color.Lerp(color0, color1, rate);
        }
    }