STIKA

D+25일 TIL < Invoke > 본문

프로그래밍/Unity + C#

D+25일 TIL < Invoke >

STIKA 2024. 1. 29. 13:40

24.01.29 Mon

D+25

 

❤️ TIL

Today I Learned

❤️하루에 1%씩만 나아가도 4개월 동안 쌓이면 꽤 덩치가 커진답니다 :)


✏️ 작업 내용

✔️ Obstacle


✔️ Obstacle

public class ObjectTrigger : MonoBehaviour
{
    private GameObject myObject;
    private Obstacle myObstacle;

    public Transform min;
    public Transform max;

    public float InstanseSpeed; 


    private void Start()
    {
        myObstacle = GetComponent<Obstacle>();
        myObject = myObstacle.choiceObject;
    }

    private void OnTriggerEnter2D(Collider2D coll)
    {
        if(coll.gameObject.tag == "Player")
        {
            if (myObstacle.choice == ModeChoice.Mode.Hide)
            {
                if (myObstacle.choiceObject.activeSelf == false)
                    myObstacle.choiceObject.SetActive(true);
                Destroy(myObstacle.triggerObject);
            }

            if (myObstacle.choice == ModeChoice.Mode.Drop ||
                myObstacle.choice == ModeChoice.Mode.Fly)
            {
                if (myObstacle.choiceObject.activeSelf == false)
                    Invoke("WaitDrop", myObstacle.wait);

                myObstacle.triggerObject.SetActive(false);
            }

            if (myObstacle.choice == ModeChoice.Mode.RandomRain)
            {
                if (myObstacle.choiceObject.activeSelf == false)
                    myObstacle.choiceObject.SetActive(true);

                InvokeRepeating("MakeRandomRain", 0, InstanseSpeed);

                myObstacle.triggerObject.SetActive(false);
            }

            if (myObstacle.choice == ModeChoice.Mode.Rain)
            {
                if (myObstacle.choiceObject.activeSelf == false)
                    myObstacle.choiceObject.SetActive(true);

                if (myObstacle.oneTime == false)
                    InvokeRepeating("MakeRain", myObstacle.wait, InstanseSpeed);
                if (myObstacle.oneTime == true)
                    Invoke("MakeRain", myObstacle.wait);


                myObstacle.triggerObject.SetActive(false);
                //test
            }
        }

        
    }

    public void MakeRandomRain()
    {
        float x = Random.Range(min.position.x,max.position.x);// 값조절
        myObstacle.choiceObject.transform.position = new Vector3(x, min.position.y, 0);
        Instantiate(myObstacle.choiceObject);
    }

    public void MakeRain()
    {
        myObstacle.choiceObject.transform.position = new Vector3(min.position.x, min.position.y, 0);
        Instantiate(myObstacle.choiceObject);
    }

    public void WaitDrop()
    {
        gameObject.GetComponent<Obstacle>().choiceObject.SetActive(true);
        Destroy(myObstacle.triggerObject);
    }
}
public class ModeChoice : MonoBehaviour
{
    public Mode choice;
    public GameObject choiceObject;
    public GameObject triggerObject;
    public float gravity;
    public float wait = 0f;
    private Rigidbody2D _rigidbody2D;
    public bool oneTime = false;


    public enum Mode
    {
        Fix = 0,
        Drop,
        RandomRain,
        Rain,
        Fly,
        Hide
    }

    protected virtual void Start()
    {
        switch (choice)
        {
            case Mode.Fix:
                FixMode();
                triggerObject = null;
                break;
            case Mode.Drop:
                DropMode();
                break;
            case Mode.RandomRain:
                RandomRainMode();
                break;
            case Mode.Rain:
                RainMode();
                break;
            case Mode.Fly:
                FlyMode();
                break;
            case Mode.Hide:
                HideMode();
                break;
        }
    }

    public void FixMode()
    {
        choiceObject.SetActive(true);

        _rigidbody2D = choiceObject.GetComponent<Rigidbody2D>();
        _rigidbody2D.bodyType = RigidbodyType2D.Static;
    }

    public void DropMode()
    {
        choiceObject.SetActive(false);

        _rigidbody2D = choiceObject.GetComponent<Rigidbody2D>();
        _rigidbody2D.bodyType = RigidbodyType2D.Dynamic;
        _rigidbody2D.gravityScale = gravity;
        _rigidbody2D.constraints = RigidbodyConstraints2D.FreezeRotation;
        _rigidbody2D.constraints = RigidbodyConstraints2D.FreezePositionX;        
    }

    public void RandomRainMode()
    {
        _rigidbody2D = choiceObject.GetComponent<Rigidbody2D>();
        _rigidbody2D.bodyType = RigidbodyType2D.Dynamic;
        _rigidbody2D.gravityScale = gravity;
        _rigidbody2D.constraints = RigidbodyConstraints2D.FreezeRotation;
        _rigidbody2D.constraints = RigidbodyConstraints2D.FreezePositionX;
    }

    public void RainMode()
    {
        _rigidbody2D = choiceObject.GetComponentInChildren<Rigidbody2D>();
        _rigidbody2D.bodyType = RigidbodyType2D.Dynamic;
        _rigidbody2D.gravityScale = gravity;
        _rigidbody2D.constraints = RigidbodyConstraints2D.FreezeRotation;
        _rigidbody2D.constraints = RigidbodyConstraints2D.FreezePositionX;
    }

    public void FlyMode()
    {
        choiceObject.SetActive(false);

        _rigidbody2D = choiceObject.GetComponent<Rigidbody2D>();
        _rigidbody2D.bodyType = RigidbodyType2D.Dynamic;
        _rigidbody2D.gravityScale = gravity;
        _rigidbody2D.constraints = RigidbodyConstraints2D.FreezeRotation;
        _rigidbody2D.constraints = RigidbodyConstraints2D.FreezePositionX;
    }

    public void HideMode()
    {
        choiceObject.SetActive(false);

        _rigidbody2D = choiceObject.GetComponent<Rigidbody2D>();
        _rigidbody2D.bodyType = RigidbodyType2D.Static;
    }
}
public class DropCollision : MonoBehaviour
{
    UIManager uiManager;

    public Mode choice;

    private string bedStr = "폭신한 침대의 유혹에 지고 말았다.\n시계를 보니 밤 12시가 지났다....\n더 무서운건 퇴실버튼을 못 눌러서 매니저님이 찾아왔다..";
    private string nintendoStr = "몰래 게임을 하다가 걸려서 한효승 매니저님한테 진실의 방으로 끌려갔다...";
    private string clearStr = "  퇴  실  성  공  ! ! !\n\n모든 유혹을 뿌리치고 무사히 오늘의 공부와 TIL작성을 끝내고 퇴실에 성공했다!!!";
    private string mapOut = "공부를 때려치고 놀고싶은 욕망과\n주변의 많은 유혹들을 이겨내지 못하고 탈주를 해버렸다...";

    public enum Mode
    {
        Fix = 0,
        Drop,
        RandomRain,
        Rain,
        Fly,
        Hide
    }

    private void Start()
    {
        uiManager = GameObject.FindGameObjectWithTag("UI").GetComponent<UIManager>();
    }

    private void OnCollisionEnter2D(Collision2D coll)
    {
        if (coll.gameObject.tag == "MapOut")
            Destroy(gameObject);

        if (coll.gameObject.tag == "Nag")
            Destroy(coll.gameObject);

        if (choice == Mode.Fly && coll.gameObject.tag == "Ground")//wallTile
            Destroy(gameObject);

        if (coll.gameObject.tag == "Player")
        {
            coll.gameObject.GetComponentInChildren<Animator>().enabled = false;
            coll.gameObject.GetComponent<PlayerInput>().enabled = false;

            if (this.gameObject.tag == "Clear")
                uiManager.endResult(clearStr);
            if (this.gameObject.tag == "Bed")
                uiManager.endResult(bedStr);
            if (this.gameObject.tag == "Nintendo")
                uiManager.endResult(nintendoStr);

            Debug.Log($"{gameObject.tag} drop or fly랑 Player부딪힘");
        }
    }
}

 


 

✏️ 스스로 어제보다 한 발 더 나아갔다는 것을 자각하기

✏️ TIL을 쓰기 위해서라도, 오늘 반드시 단 하나라도 배우기

✏️ 꾸준히 기록을 남기는 습관 가지기

 

📕 오늘의 학습 키워드

📕  공부한 내용 본인의 언어로 정리하기

📕  오늘의 회고

📕  12시간 중 얼마나 몰입했는지

📕  더 나은 미래를 위해 내일 어떤 공부를 진행할 것인지