본문 바로가기
게임프로젝트/슈팅게임

팀프로젝트 (1) App , Game Main스크립트 작성

by pudding81 2024. 3. 21.
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class App : MonoBehaviour
{
    private GameMain gameMain;

    private void Awake()
    {
        DontDestroyOnLoad(this.gameObject);
    }

    private void Start()
    {
        this.AddEventHandlers();

        //비동기 씬로드 
        AsyncOperation oper = SceneManager.LoadSceneAsync("Game");
        oper.completed += (obj) => {

            this.gameMain = GameObject.FindAnyObjectByType<GameMain>();
            Debug.LogFormat("gameMain: {0}", gameMain);
            //gameMain.onReadyToStart = () => {
            //    Debug.Log("게임 준비가 되었음 게임을 시작 합니다.");
            //    gameMain.StartGame();
            //};
            gameMain.Init(100);
        };
    }

    private void AddEventHandlers()
    {
        EventDispatcher.instance.AddEventHandler((short)GameEnums.eGameEvent.READY_TO_START, (eventType) => {

            Debug.Log("게임 준비가 되었음 게임을 시작 합니다.");

        });

        EventDispatcher.instance.AddEventHandler((short)GameEnums.eGameEvent.GAME_OVER, (eventType) => {

            Debug.Log("게임이 종료 되었습니다. 게임정보를 저장 합니다.");

        });
    }
}

 

 

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

public class GameMain : MonoBehaviour
{
    [SerializeField] private GameObject playerPrefab;
    private PlayerController playerController;

    //public System.Action onReadyToStart;

    public void Init(int damage)
    {
        this.CreatePlayer(damage);


        Debug.Log("게임을 준비중....");

        //이벤트를 발생 
        EventDispatcher.instance.SendEvent((short)GameEnums.eGameEvent.READY_TO_START);

        //this.onReadyToStart();
    }

    private void CreatePlayer(int damage)
    {
        Debug.Log("플레이어를 생성합니다.");
        var go = Instantiate<GameObject>(this.playerPrefab);
        this.playerController = go.GetComponent<PlayerController>();
        this.playerController.Init(damage);
    }

    public void StartGame()
    {
        Debug.Log("게임을 시작 합니다.");
    }
}