코딩메모

팀 스파르타 2주차 코딩메모

WMG1 2022. 9. 23. 02:34
반응형

gameManager.cs

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; // UI로 텍스트를 받기
using UnityEngine.SceneManagement; // Scene 새로고침을 위해 해당 기능을 불러옴

public class gameManager : MonoBehaviour
{
    public GameObject Square; // 외부에 게임 오브젝트인 "Square" 요청
    public GameObject endPanel; // 외부에 게임 오브젝트인 "endPanel" 요청
    public Text timeTxt; // 외부에 텍스트 타입인 "timeTxt" 요청
    public Text thisScoreTxt; // 외부에 텍스트 타입인 "thisScoreTxt" 요청
    public Text maxScoreTxt; // 외부에 텍스트 타입인 "maxScoreTxt" 요청
    public Animator anim; // 외부에 애니매이터인 "anim" 요청
    float alive = 0f; // 소수점 값인 alive 추가 버티는 시간이므로 시작값 0f
    bool isRunning = true; // 참/거짓값 둘중하나만 있는 isRunning는 기본적으로 참 값이다
    public static gameManager I; // 여러곳에서 부를 수 있게 만듬 싱글톤 gameManager(I) 를 
    

    void Awake() //누가 나 찾으면
    {
        I = this;
    }
    // Start is called before the first frame update
    void Start()
    {
        Time.timeScale = 1f; // 게임 오버되고 재시작 시 경우 타임스케일을 다시 1로 되돌려 재시작하게 만들어 줌
        InvokeRepeating("makeSquare", 0.0f, 0.5f); // 이함수를 지속해서 생성시켜라 몇초마다 (makeSquare를 0.0f~0.05f 간격으로)
    }

    // Update is called once per frame
    void Update()
    {
        alive += Time.deltaTime; // alive 값에 time.deltatime 을 더해준다
        timeTxt.text = alive.ToString("N2"); // timeTxt로 지정된 text는 이것이 된다 - alive.(문자값을 숫자값으로 변환)==("소수점 두번째까지 표기함")
        if(isRunning) // 만약 게임 진행중이라면 
        {
            alive += Time.deltaTime; // 얼라이브 값을 업데이트 해서
            timeTxt.text = alive.ToString("N2"); // 타임 텍스트를 계속 수정하라

        }
    }

    void makeSquare()  // 위의 makeSquare 함수 선언
    {
        Instantiate(Square);  // 찍어내라(Square)를
    }

    public void gameOver()  // public(여러곳에서 가져다 쓰는) gameOver함수 선언
    {
        isRunning = false; // update의 if값에 의해-- gameOver 함수가 불려올 경우 alive는 업데이트 되지 않는다
        anim.SetBool("isDie", true); // anim 에게 참거짓값을 설정 시킨다 (isDie 파라미터를 참으로) 
        Invoke("timeStop", 0.5f); //있다가 실행시켜라 timestop함수를 0.5f 후에    //Time.timeScale = 0f; // 시간이 흐르지않게 설정
        endPanel.SetActive(true); // endPanel 의 활성화 상태를 참으로 만들어 줌
        thisScoreTxt.text = alive.ToString("N2"); // thisScoreTXt에 문자값alive를 넣어달라 (소수점 둘째자리까지 표기) == 여기 헷갈림**
        if (PlayerPrefs.HasKey("bestscore") == false ) // 만약에 PlayerPrefs 가 bestscore를 가지고 있지 않다면
        {
            PlayerPrefs.SetFloat("bestscore", alive); // PlayerPrefs에게 기억시킨다 지금기록(alive)이 최고기록 이라고
        }
        else //혹은 (PlayerPrefs의 기본값은 true 이므로 참일경우=최고기록이 있는경우)
        {
            if (alive > PlayerPrefs.GetFloat("bestscore")) // 만약 alive가 PlayerPrefs가 가지고 있는 값보다 크다면
            {
                PlayerPrefs.SetFloat("bestscore", alive); // 지금기록이 최고기록이다
            }
            float maxScore = PlayerPrefs.GetFloat("bestscore"); // 최고기록은 PlayerPrefs에서 가져온 bestscore값이다
            maxScoreTxt.text = maxScore.ToString("N2"); // maxScore텍스트는 문자값인 maxScore이다(소수점2자리까지의) == 여기 헷갈림**
        }

    }
    public void retry() // retry 함수선언
    {
        SceneManager.LoadScene("MainScene");  // SceneManager 에게 메인씬을 리로드 요청
    }
    void timeStop() // 함수선언
    {
        Time.timeScale = 0f; // 시간을 0으로 만든다 (흐르지않게 설정)
    }
}

 


Square.cs(적 오브젝트)

 

 

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

public class Square : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
       float x = Random.Range(-3f, 3f); //소수점 포함인 x 값 = 랜덤 범위(-3~3사이의 범위)
       float y = Random.Range(3f, 5f); // 소수점 포함인 y값 = 랜덤 범위(3~5사이의 범위)
        transform.position = new Vector3(x, y, 0); // 위치값 입력

        float size = Random.Range(0.5f, 1.5f); // 소수점 포함인 사이즈값 = 랜덤 범위(0.5~1.5사이의 사이즈)
        transform.localScale = new Vector3(size, size, 0); // 크기값 입력
    }

    // Update is called once per frame
    void Update()
    {
        if( -5.0f > transform.position.y) // 만약 현재 위치의 y값이 -5.0f 보다 작다면
        {
            Destroy(gameObject); // 게임 오브젝트를 파괴
        }
    }
    void OnCollisionEnter2D(Collision2D coll) // 콜라이더 끼리 만났을 때 부르는 함수
    {
        if (coll.gameObject.tag == "balloon") // 만약 만난 콜라이더 태그가 balloon 이라면
        {
            gameManager.I.gameOver(); // gameManager를 호출시켜 gameOver 함수를 불러와라
        }
    }

}

 

 


 

shield.cs(플레이어)

 

 

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

public class shield : 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); // 마우스의 3가지 값 = 마우스 위치를 화면으로 보내는 코딩
        transform.position = new Vector3(mousePos.x, mousePos.y, 0); //rigidbody를 이용한 이동을 제외한 모든 이동은 transform.position을 이용 마우스위치 xyz값 입력
    }
}

반응형