본문 바로가기
카테고리 없음

[포트폴리오] 노트판정을 위한 Note.cs 스크립트 작성하기

by 불타는홍당무 2017. 6. 13.


노트판정을 위해 판정 컬라이더를 만들고 다음 정책에 의거하여 Note.cs 스크립트를 작성하였다.




빨간선이 판정 기준선이다. 플레이어의 노란선이 빨간선과 겹쳐질 때 플레이어의 액션이 발생해야 한다.




Note.cs

using UnityEngine;
using System.Collections;

public class Note : MonoBehaviour
{
    //Sound for Android
    public string AudioFileName;
    private int _fileID;
    private int _soundID;
    //Sound for StandAlone
    public AudioClip _audioClip;

    private Transform _transform;
    private BoxCollider2D _boxCollider2D;
    public string InputType;
    private float _notePosX;
    private float _playerPosX;

    public DifficultyLevel Difficulty;
    public enum DifficultyLevel
    {
        Easy,   //0
        Normal, //1
        Hard    //2
    }

    void Start()
    {
        _boxCollider2D = GetComponent();
        _playerPosX = -3.0f;
        _transform = transform;
        _notePosX = _transform.position.x;

        if (Application.platform == RuntimePlatform.Android)
        {
            AudioFileName = "Android Native Audio/" + AudioFileName.Trim();
            _fileID = AndroidNativeAudio.load(AudioFileName);
        }
    }

    public void PlayNote(string type)
    {
        if (SaveData.SFXOnOff == "OFF")
            return;

        if (InputType.Equals(type))
        {
            //점수 판정
            Evaluate();

            if (Application.platform == RuntimePlatform.Android)
            {
                _soundID = AndroidNativeAudio.play(_fileID); //Android
            }
            else
            {
                SoundManager.Instance.PlayNote(_audioClip);
            }
        }
    }

    public void Evaluate()
    {
        int resultIdx = 0;
        float points = 0.0f;
        bool isBeforeNote = _transform.position.x >= _playerPosX ? true : false;
        float distance = Mathf.Abs(_transform.position.x - _playerPosX);

        if (isBeforeNote) //판정 기준선 앞
        {
           if (distance > 2.56f)
            {
                return;
            }
            else if (distance > 1.28f)  //GOOD
            {
                resultIdx = 2;
                points = 100.0f;
            }
            else if (distance > 0.512f) //GREAT
            {
                resultIdx = 1;
                points = 200.0f;
            }
            else                        //PERFECT
            {
                resultIdx = 0;
                points = 300.0f;
            }
        }
        else          //판정 기준선 뒤
        {
            if (distance > 1.28f)
            {
                return;
            }
            else if (distance > 0.64f)  //GOOD
            {
                resultIdx = 2;
                points = 100.0f;
            }
            else if (distance > 0.256f) //GREAT
            {
                resultIdx = 1;
                points = 200.0f;
            }
            else                       //PERFECT
            {
                resultIdx = 0;
                points = 300.0f;
            }
        }

        LevelManager.Instance.HandleEvaluationResult(resultIdx, points);
    }

    void OnApplicationPause(bool pause)
    {
        if (pause)
        {
            AndroidNativeAudio.stop(_soundID);
        }
    }

    void OnApplicationQuit()
    {
        AndroidNativeAudio.unload(_fileID);
    }
}