상세 컨텐츠

본문 제목

유니티 공부(13)

유니티

by MJ_119 2023. 10. 25. 17:15

본문

출처 : 골드메탈 유니티 뱀서라이크 13

https://www.youtube.com/watch?v=Lt2Q9glJlw0&list=PLO-mt5Iu5TeZF8xMHqtT_DhAPKmjF6i3x&index=17

 

게임 시작과 종료

 

1. 게임시작

 

 

2. 플레이어 피격

 - childCount : 자식 오브젝트의 개수

 

3. 게임 오버

 

 

4. 게임 승리

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

public class GameManager : MonoBehaviour
{
    public static GameManager instance;
    [Header("# Game Control")]
    public bool isLive; // 시간 정지 여부를 알려주는 bool 변수 선언
    public float gameTime; // 실제 게임 시간
    public float maxGameTime = 20f; // 최대 게임 시간
    [Header("# Player Info")]
    public float health;
    public float maxHealth = 100;
    public int level; // 레벨
    public int kill; // 킬수
    public int exp; // 경험치
    public int[] nextExp = { 3, 5, 10, 100, 150, 210, 280, 360, 450, 600 }; // 현재레벨에 필요한 경험치
    [Header("# Game Object")]
    public PoolManager pool;
    public Player player;
    public LevelUp uiLevelUp;
    public Result uiResult;
    public GameObject EnemyCleaner;

    void Awake()
    {
        instance = this;
    }

    public void GameStart()
    {
        health = maxHealth;
        uiLevelUp.Select(0); // 임시 스크립트 ( 첫번째 캐릭터 선택)
        Resume();
    }

    public void GameOver()
    {
        StartCoroutine(GameOverRoutine());
    }

    IEnumerator GameOverRoutine()
    {
        isLive = false;

        yield return new WaitForSeconds(0.5f); // 사망 애니메이션 기다리는 시간

        uiResult.gameObject.SetActive(true);
        uiResult.Lose();


        Stop();
    }

    public void GameVictory()
    {
        StartCoroutine(GameVictoryRoutine());
    }

    IEnumerator GameVictoryRoutine()
    {
        isLive = false;
        EnemyCleaner.SetActive(true);

        yield return new WaitForSeconds(0.5f); // 모든 적이 없어지기를 기다리는 시간

        uiResult.gameObject.SetActive(true);
        uiResult.Win();


        Stop();
    }

    public void GameRetry()
    {
        SceneManager.LoadScene(0);
    }

    void Update()
    {
        if (!isLive) // isLive가 false일때 리턴
            return;

        gameTime += Time.deltaTime;

        if (gameTime > maxGameTime)
        {
            gameTime = maxGameTime;
            GameVictory();

        }
    }

    public void GetExp()
    {
        if (!isLive)
        {
            return;
        }
        exp++;
        
        // 레벨업 코드
        if (exp == nextExp[Mathf.Min(level, nextExp.Length -1 )]) // 현재레벨에 필요한 경험치까지 100%차면 레벨업
        {
            level++; // 레벨업하고 경험치 초기화
            exp = 0;
            uiLevelUp.Show(); // 레벨업하면 UI 활성화. UI 비활성화는 LevelUp -> Panel -> ItemGroup -> Item 0~4에서 담당
        }

    }

    public void Stop() // 시간 정지
    {
        isLive = false;
        Time.timeScale = 0; // 유니티의 시간 속도(배율)
    }
    public void Resume() // 정지 해제
    {
        isLive = true;
        Time.timeScale = 1; // 유니티의 시간 속도(배율)
    }
}

 

 

'유니티' 카테고리의 다른 글

유니티 공부(15)  (0) 2023.10.26
유니티 공부(14)  (0) 2023.10.25
유니티 공부(12)  (0) 2023.10.20
유니티 공부(11)  (0) 2023.10.19
유니티 공부(10)  (0) 2023.09.18

관련글 더보기