ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 팀 스파르타 3주차 코딩메모
    코딩메모 2022. 9. 27. 00:00
    반응형

    cat.cs

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class cat : MonoBehaviour
    {
        float full = 5f; // full 선언
        float energy = 0f; // energy 선언
        bool isFull = false; // isFull의 값이 기본적으로 거짓이다 (평소엔 고양이가 배가 부르지 않다)
        public int type; // 타입이라는 정수 퍼블릭 선언
        // Start is called before the first frame update
        void Start()
        {
            float x = Random.Range(-8.5f, 8.5f); // x 값은 -8.5에서 8.5 사이의 랜덤 범위
            float y = 30f; // y값은 30고정

            if(type == 1) // 타입이 1이라면
            {
                full = 10f; // full (체력)이 10이다
            }
            transform.position = new Vector3(x, y, 0); // 위치값 설정
        }

        // Update is called once per frame
        void Update()
        {
            if (energy < full) // 만약 energy 가 full 보다 작다면
            {
                if(type == 0) // 타입이 0이라면 
                {
                    transform.position += new Vector3(0, -0.05f, 0); // 기존 위치값에 더한다== 새로운 백터 값 y-0.05f (아래로 내려간다)
                }
                
                else if(type == 1) // 타입이 1이라면
                {
                    transform.position += new Vector3(0, -0.03f, 0); // 기존 위치값에 더한다== 새로운 백터 값 y-0.03f (아래로 내려간다)
                }

                else if(type == 2) // 타입이 2라면
                {
                    transform.position += new Vector3(0, -0.1f, 0); // 기존 위치값에 더한다== 새로운 백터 값 y-0.1f (아래로 내려간다)
                }
                if(transform.position.y < -16f) // 만약 위치값 y가 -16f이하일때
                {
                    gameManager.I.GameOver(); // 게임 매니저에게 GameOver 함수 호출요청
                }
            }

            else //그 외의 경우 (full 보다 크거나 같다면)
            {
                if(transform.position.x > 0) // 만약 현재 위치값x 가 0보다 크다면
                {
                    transform.position += new Vector3(0.05f, 0, 0); //x오른쪽으로 0.05씩 이동
                }
                else //그 외의 경우
                {
                    transform.position += new Vector3(-0.05f, 0, 0); //x왼쪽으로 0.05씩 이동
                }
                Destroy(gameObject, 3f); // 게임 오브젝트 파괴 , 3초 뒤에
            }
        }

        private void OnTriggerEnter2D(Collider2D coll) // 콜라이더끼리 만나면 함수호출
        {
            if (coll.gameObject.tag == "food") // 만약 게임 오브젝트 태그 food를 가진 콜라이더와 만난다면
                {
                if (energy < full) // 만약 energy가 full보다 작다면
                {
                    energy += 1f; // energy에 1을 더해준다
                    Destroy(coll.gameObject); // 그리고 부순다 함수를 coll 한 오브젝트를
                    gameObject.transform.Find("hungry/Canvas/front").transform.localScale = new Vector3(energy / full, 1.0f);
                    // 게임오브젝트에서 찾아라 프론트를 . 그리고 그것의 로컬스케일을 에너지와/풀 비율로 바꿔준다
                }
                else // 이외의 경우에는
                {
                    if (isFull == false) // 만약 지금 isFull 값이 거짓이면
                    {


                        gameManager.I.addCat();
                        gameObject.transform.Find("hungry").gameObject.SetActive(false);
                        // 게임오브젝트에게 트랜스폼 요청해서 트랜스폼에게 hungry 요청 후 그것의 setActive를 거짓으로 바꿈
                        gameObject.transform.Find("full").gameObject.SetActive(true);
                        // 게임오브젝트에게 트랜스폼 요청해서 트랜스폼에게 full 요청 후 그것의 setActive를 참으로 바꿈
                        isFull = (true); // 위를 실행 후 isFull값을 참으로 바꿔줘서 함수 호출을 멈춤
                    }
                }
            }
        }
    }

     


    dog.cs

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class dog : MonoBehaviour
    {
        // Start is called before the first frame update
        void Start()
        {
            
        }

        // Update is called once per frame
        void Update()
        {
            Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); // 마우스 현재 위치 잡아오는 코드
            float x = mousePos.x; // x값 따로 저장
            if(x > 8.5f)  // 만약 x가 8.5f보다 크다면
            {
                x = 8.5f; // x는 8.5 고정
            }
            if(x < -8.5f) // 만약 x가 -8.5보다 작다면
            {
                x = -8.5f; // x는 -8.5 고정
            }


            transform.position = new Vector3(x, transform.position.y, 0); // dog의 포지션은 따로 저장한 float x와 원래 좌표y, 0z 로 설정한다
        }
    }

     


     

     

    retryBtn.cs

     

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;

    public class retryBtn : MonoBehaviour
    {
        // Start is called before the first frame update
        void Start()
        {
            
        }

        // Update is called once per frame
        void Update()
        {
            
        }
        public void ReGame() // ReGame 이라는 외부요청 가능한 함수선언
        {
            SceneManager.LoadScene("MainScene"); // 씬 불러오기 (메인씬) //(이것만 입력 후 alt + enter 누르면 자동으로 문제 해결해줌
        }
    }

     

     


     

     

     

    gameManager.cs

     

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;

    public class gameManager : MonoBehaviour
    {
        public GameObject food; // 외부에서 게임 오브젝트 푸드 요청
        public GameObject dog; // 외부에서 게임 오브젝트 독 요청 (위치를 받아야함)
        public GameObject nomalCat; // 외부에서 게임 오브젝트 노말캣 요청
        public GameObject fatCat; // 외부에서 게임 오브젝트 팻캣 요청 
        public GameObject pirateCat; // 외부에서 게임 오브젝트 피래이트캣 요청 
        public Text levelText; // 외부에서 텍스트 타입의 레벨텍스트 요청
        public GameObject levelFront; // 외부에서 게임 오브젝트 레벨프론트 요청
        public static gameManager I; // 게임매니저를 싱글톤 화 함수 여기부터**
        public GameObject retryBtn; // 외부에서 게임 오브젝트 리트라이 버튼 요청

        int level = 0; // 정수 level 선언
        int cat = 0; // 정수 cat 선언
        void Awake()  // 켜자마자 적용
        {
            I = this; // 싱글톤 함수 여기까지**
        }
        // Start is called before the first frame update
        void Start()
        {
            Time.timeScale = 1f; // 게임 재시작 시 시간이 다시 흐르도록 1로 설정
            InvokeRepeating("makefood", 0.0f, 0.05f); // 계속 반복해서 해라 makefood 함수 실행을 0.05초 마다
            InvokeRepeating("makeCat", 0.0f, 1f); // 계속 반복해서 해라 makeCat 함수 실행을 1초마다
        }

        // Update is called once per frame
        void Update()
        {
            
        }
        void makefood()  // InvokeRepeating에 사용할 makefood 함수선언 
        {
            float x = dog.transform.position.x; // 소수점인 좌표 x값은 == dog 오브젝트의 포지션 값 x이다.
            float y = dog.transform.position.y + 2.0f; // 소수점인 좌표 y값은 == dog 오브젝트의 포지션 값 y이며 거기에 2.0를 더해준다
            Instantiate(food, new Vector3(x, y, 0), Quaternion.identity); // 필요할때마다 실시간으로 만든다 food를 포지션 x,y,0에서  // , 뒤는 회전 관련함수
        }
        void makeCat() // nvokeRepeating에 사용할 makeCat 함수선언 
        {
            Instantiate(nomalCat); // 복제해서 생성한다 (nomalCat 을)
            if(level == 1) // 만약 레벨이 1이라면
            {
                float p = Random.Range(0, 10);//랜던값 설정 0~10
                if(p < 2) Instantiate(nomalCat); // 만약 p가 2보다 작으면 복제해서 생성한다 (이러면 두개만듬)

            }
            
            else if (level == 2) // 만약 레벨이 2면
            {
                float p = Random.Range(0, 10); // 랜덤값을 설정 0~10
                if (p < 5) Instantiate(nomalCat); // 만약 p가 5보다 작으면 복제해서 생성한다 (이러면 두개만듬)
                }
            else if (level == 3) // 만약 레벨이 3이면
            {
                float p = Random.Range(0, 10); // 랜덤값을 설정 0~10
                if (p < 6) Instantiate(nomalCat); // 만약 p가 6보다 작으면 복제해서 생성한다 (이러면 두개만듬)
                Instantiate(fatCat); // 복제해서 fatCat을 소환
            }

            else if(level >=4) // 만약 레벨이 4이상이면
            {
                float p = Random.Range(0, 10); // 랜덤값을 설정 0~10
                if (p < 7) Instantiate(nomalCat); // 만약 p가 7보다 작으면 복제해서 생성한다 (이러면 두개만듬)
                Instantiate(fatCat); // 복제해서 fatCat을 소환
                Instantiate(pirateCat); // 복제해서 pirateCat을 소환
            }
        }
        public void GameOver() // 게임오버 함수선언 퍼블릭으로
        {
            retryBtn.SetActive(true); // 함수 호출 시 리트라이 버튼 setactive 상태 참으로 변경
            Time.timeScale = 0f; // 게임이 멈추도록 timeScale 을 0으로 설정
        }

        public void addCat() // addCat 함수선언
        {
            cat += 1; // cat은 1이다
            level = cat / 5; // 레벨 은 잡은 고양이 가 5가 되면 1이다 ( 정수 정수 (int) 소수점은 0처리 임)

            levelText.text = level.ToString(); // 레벨 텍스트는 레벨을 투스트링 해준다
            levelFront.transform.localScale = new Vector3((cat - level * 5) / 5.0f, 1.0f, 1.0f); 
            //레벨프론트의 변경값은 (레벨 계산식) // (레벨 계산 시 수는 float 값이여야 한다) 
        }

    }

     


     

     

    food.cs

     

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class food : MonoBehaviour
    {
        // Start is called before the first frame update
        void Start()
        {
            
        }

        // Update is called once per frame
        void Update()
        {
            transform.position += new Vector3(0, 0.5f, 0); // 위치 이동 더해줌 y축 0.5f 만큼

            if (transform.position.y > 26.0f) // 만약 현재 y위치값이 26보다 크다면
            {
                Destroy(gameObject); // 오브젝트 파괴
            }
        }
    }

     


     

     

    startBtn.cs

     

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement; // 스크립트에 씬 매니지먼트 추가해서 SceneManager를 사용할 수 있도록 함

    public class startBtn : MonoBehaviour
    {
        // Start is called before the first frame update
        void Start()
        {
            
        }

        // Update is called once per frame
        void Update()
        {
            
        }
        
        public void GameStart() // GameStart 라는 퍼블릭(외부에서 가져오는) 함수 선언
        {
            SceneManager.LoadScene("MainScene"); // 씬 매니저 에게 씬 불러오기 요청 (씬 이름)  // 씬 변경코딩
        }
    }

    반응형
Designed by Tistory.