[개발일지] 나폴리탄 스파게티 20230612 ~ 20230617

2023. 6. 23. 14:422학년 1학기 프로젝트

전체적인 스크립트 수정 및 오류 수정

 

EventManager

public class EventManager : MonoBehaviour
{
    // 이벤트 진행 오브젝트를 받아옴
    public GameObject timeManagerObj;

    public GameObject paintEventManagerObj;
    public GameObject cctvEventObj;
    public GameObject waterSoundEventObj;
    public GameObject windowEventObj;

    // 이벤트 진행 스크립트를 받아옴
    TimeManager time;
    
    PaintEventManager paintEventManager;
    CctvEvent cctvEvent;
    WaterSoundEvent waterSoundEvent;
    WindowEvent windowEvent;

    // 랜덤 시스템 관련 변수
    List<int> TypeEventList = new List<int>(); // 현재 진행중인 이벤트 종류 정보를 담을 리스트
    int nowEvent = 0; // 현재 진행중인 이벤트 개수를 담을 변수
    int maxEvent = 1; // 최대로 진행할 이벤트 개수를 담은 변수
    
    public Text eventFailText;

    void Start()
    {
        // 이벤트 진행 스크립트 불러오기
        time = timeManagerObj.GetComponent<TimeManager>();
        
        paintEventManager = paintEventManagerObj.GetComponent<PaintEventManager>();
        cctvEvent = cctvEventObj.GetComponent<CctvEvent>();
        waterSoundEvent = waterSoundEventObj.GetComponent<WaterSoundEvent>();
        windowEvent = windowEventObj.GetComponent<WindowEvent>();

        // 진행 이벤트 결정
        RandomEvent();

        // 이벤트 실패시 뜰 문구 시작시 뜨지 않도록
        eventFailText.color = new Color(eventFailText.color.r, eventFailText.color.g, eventFailText.color.b, 0);
    }

    void Update()
    {
        // 최대 이벤트 진행 개수 설정 (maxEvent)
        if(time.timeHour == 0 && time.timeMinute == 0) // 00:00 ~ 02:00
        {
            maxEvent = 2;
            Debug.Log("maxEvent = " + maxEvent);
        }
        else if(time.timeHour == 2 && time.timeMinute == 0) // 02:00 ~ 04:00
        {
            maxEvent = 6;
            Debug.Log("maxEvent = " + maxEvent);
        }
        else if(time.timeHour == 4 && time.timeMinute == 0) // 04:00 ~ 06:00
        {
            maxEvent = 8;
            Debug.Log("maxEvent = " + maxEvent);
        }
        // 05:00 ~ 06:00 +1

    }

    // 이벤트 진행 여부 및 종류 결정 메서드
    void RandomEvent()
    {
        int isEvent = Random.Range(0, 2); // 이벤트를 진행할지 추첨

        // 이벤트 진행시 이벤트의 종류 추첨
        if(isEvent == 1)
        {
            Debug.Log("진행여부 추첨: "+ isEvent + " 진행"); 
            int eventNum = Random.Range(1, 8);               // 1번~7번 이벤트 중 추첨
            bool isThere = TypeEventList.Contains(eventNum); // 현재 진행중인 이벤트 중 중복된 이벤트가 있는지 확인
            
            // 중복된 이벤트가 있을 때
            if(isThere == true)
            {
                RandomEvent(); // 재추첨
            }
            // 중복된 이벤트가 있을 때
            else                            
            {
                nowEvent++;                  // 현재 진행중인 이벤트 개수 추가
                GoEvent(eventNum);           // 이벤트 진행 확정
                TypeEventList.Add(eventNum); // 진행 이벤트 정보 저장
            }

            // 진행중인 이벤트 Debug.Log (확인용)
            foreach(int a in TypeEventList)
            {
                Debug.Log(a);
            }
        }
        // (확인용) 확인 끝나면 지울 것
        else
        {
            Debug.Log("진행여부 추첨: "+ isEvent + " 진행하지 않음");
        }

        // 최대 진행 이벤트 개수보다 적을 때 계속해서 추첨 진행
        if(nowEvent < maxEvent)
        {
            Invoke("RandomEvent", 2);
        }
    }

    // 이벤트가 완료될 경우 list에 담긴 이벤트 정보 삭제
    public void CompleteEvent(int completeEvent)
    {
        int indexCompleteEvent = TypeEventList.IndexOf(completeEvent); // List에 저장된 이벤트 종류가 몇번에 저장되어있는지 찾아줌
        TypeEventList.RemoveAt(indexCompleteEvent); // 완료된 이벤트 정보 제거
        Debug.Log(completeEvent + "완료 및 제거");
    }
    
    // 이벤트가 진행되도록 해당 이벤트 관련 스크립트 변수 변경
    void GoEvent(int goEventNum)
    {
        switch(goEventNum)
        {
            case 1:
                Debug.Log("01번 이벤트 진행");
                break;
            case 2:
                Debug.Log("02번 이벤트 진행");
                paintEventManager.EventPaintPick(2);
                break;
            case 3:
                Debug.Log("03번 이벤트 진행");
                paintEventManager.EventPaintPick(3);
                break;
            case 4:
                Debug.Log("04번 이벤트 진행");
                break;
            case 5:
                Debug.Log("05번 이벤트 진행");
                cctvEvent.Event();
                break;
            case 6: 
                waterSoundEvent.Event();
                break;
            case 7:
                Debug.Log("07번 이벤트 진행");
                break;
            case 8:
                Debug.Log("08번 이벤트 진행");
                break;
            case 9:
                windowEvent.Event9();
                Debug.Log("09번 이벤트 진행");
                break;
            case 10:
                Debug.Log("10번 이벤트 진행");
                break;
        }
    }

    // 이벤트를 실패했을 때
    public void EventFail()
    {
        eventFailText.color = new Color(eventFailText.color.r, eventFailText.color.g, eventFailText.color.b, 1); // 실패 텍스트 띄우기
        Invoke("EventFailTextOff", 5);
        Debug.Log("생명 깎기 함수 호출하기");
    }

    void EventFailTextOff()
    {
        eventFailText.color = new Color(eventFailText.color.r, eventFailText.color.g, eventFailText.color.b, 0); // 실패 텍스트 끄기
    }
}

 

PaintEventManager

public class PaintEventManager : MonoBehaviour
{
    // 이벤트 진행 관련 변수
    public bool event2 = false;
    public bool event3 = false;

    // 게임 오브젝트를 담을 변수
    public GameObject[] paints; // 그림 오브젝트
    public GameObject bloodPaintEventObj; // 검붉은 액체 그림 이벤트 진행 오브젝트를 담을 변수
    public GameObject twistedPaintEventObj; // 뒤틀린 그림 이벤트 진행 오브젝트를 담을 변수

    BloodPaintEvent bloodPaintEvent;
    TwistedPaintEvent twistedPaintEvent;

    // 그림들이 생성될 위치
    Vector3[] paintsPositions = 
    {
        new Vector3(0, 2, 1),   // 0
        new Vector3(10, 2, 1),  // 1
        new Vector3(20, 2, 1),  // 2
        new Vector3(30, 2, 1),  // 3
    };

    int goEventPaint; // 이벤트를 진행할 그림
    int goEventType; // 진행될 이벤트 타입
    List<int> nowPaintEvent = new List<int>(); // 이벤트를 진행중인 그림 정보를 저장할 리스트 

    void Start()
    {
        bloodPaintEvent = bloodPaintEventObj.GetComponent<BloodPaintEvent>();
        twistedPaintEvent = twistedPaintEventObj.GetComponent<TwistedPaintEvent>();

        // 지정된 위치에 그림 프리펩 생성
        for(int i = 0; i < paints.Length; i++)
        {
            SpawnPaints(paints[i], paintsPositions[i]); // 지정된 위치에 그림을 생성함
            paints[i].SetActive(true);
        }
    }

    // 그림을 생성 메서드
    void SpawnPaints(GameObject paintObject, Vector3 spawnPoint)
    {
        GameObject paint = Instantiate(paintObject);
        paint.transform.position = spawnPoint;
    }

    // 이벤트를 진행할 그림을 추첨
    public void EventPaintPick(int eventType)
    {
        goEventType = eventType;
        int paintPick = Random.Range(0, paints.Length); // 여러 그림중 하나를 뽑음
        Check(paintPick);
    }

    // 이벤트를 진행하기 위해 뽑은 그림이 이벤트 진행중인지 아닌지를 확인
    void Check(int checkEvent)
    {
        bool isThere = nowPaintEvent.Contains(checkEvent); // 중복된 값이 있는지 확인

        // 중복값이 있을 경우 재추첨
       if(isThere == true)
       {
            EventPaintPick(goEventType);
       }
       // 중복값 없을 경우 진행 확정
       else
        {
            goEventPaint = checkEvent; // 진행 확정
            nowPaintEvent.Add(goEventPaint); // 이벤트가 진행중인 그림 정보를 리스트에 담음
            
            // 이벤트 타입에 따라 이벤트 진행
            if(goEventType == 2)
            {
                bloodPaintEvent.paintNum = goEventPaint; // 이벤트 진행 그림 정보 저장
                bloodPaintEvent.BloodEvent(paintsPositions[goEventPaint]); // 해당 그림에 이벤트 진행
                Debug.Log("이벤트 2번 진행");
            }
            else if(goEventType == 3)
            {
                twistedPaintEvent.paintNum = goEventPaint;
                twistedPaintEvent.TwistedEvent(paintsPositions[goEventPaint]);
                Debug.Log("이벤트 3번 진행");
            }
        }
    }

    // 이벤트가 완료될 경우 list에 담긴 그림 정보 삭제
    public void CompleteEvent(int completeEvent)
    {
        int indexCompleteEvent = nowPaintEvent.IndexOf(completeEvent); // List에 저장된 이벤트 종류가 몇번에 저장되어있는지 찾아줌
        nowPaintEvent.RemoveAt(indexCompleteEvent); // 완료된 이벤트 정보 제거
        Debug.Log(completeEvent + "완료 및 제거");
    }
    
}

 

BloodPaintEvent

public class BloodPaintEvent : MonoBehaviour
{
    public GameObject bloodPrefab; // 검붉은 액체 프리펩을 받아옴
    public GameObject paintEventObj; // 페인트 이벤트 관련 오브젝트를 받아옴

    public int paintNum; // 현재 이벤트가 진행중인 그림 정보를 담을 변수

    PaintEventManager paintEventManager; 

    void Start()
    {
        paintEventManager = paintEventObj.GetComponent<PaintEventManager>();
    }

    // 검붉은 액체 생성 메서드
    public void BloodEvent(Vector3 paintPosition)
    {
        GameObject blood = Instantiate(bloodPrefab);
        blood.transform.position = paintPosition;
    }

    // 인벤토리 아이템 코드와 비교하여 일치 할 경우 지워지도록 함

    // 검붉은 액체 파괴 메서드 (이벤트 완료)
    void CompleteEvent2()
    {
        Destroy(gameObject); // 검붉은 액체가 제거됨
        paintEventManager.CompleteEvent(paintNum); // 이벤트 진행 완료 정보 전달 (리스트에 담긴 정보 삭제)
        paintEventManager.event2 = false; // 이벤트 진행 관련 변수 끄기
    }
}

 

TwistedPaintEvent

public class TwistedPaintEvent : MonoBehaviour
{
    public GameObject[] twistedPaintPrefab; // 각 그림의 뒤틀린 형태 프리펩
    GameObject twistedPaint; // 생성할 뒤틀린 그림
    public int paintNum; // 현재 이벤트가 진행중인 그림 정보를 담을 변수

    public GameObject paintEventManagerObj;
    PaintEventManager paintEventManager;
    
    void Start()
    {
        paintEventManager = paintEventManagerObj.GetComponent<PaintEventManager>();
    }

    void Update()
    {
        // 상호작용시 그림이 뒤집히도록
        if(Input.GetKeyDown("t"))
        {
            TurnPaint();
        }
    }

    // 원본 그림에 뒤틀린 그림 생성
    public void TwistedEvent(Vector3 originalPaintPosition)
    {
        twistedPaint = Instantiate(twistedPaintPrefab[paintNum]);
        twistedPaint.transform.position = originalPaintPosition;
    }
    
    // 그림 뒤집기
    void TurnPaint()
    {
        twistedPaint.transform.rotation = Quaternion.Euler(0, 180, 0);
        Invoke("Original", 25); // 25초 후에 원래 그림으로 돌아옴
    }
    
    // 이벤트 완료
    void CompleteEvent3()
    {
        Destroy(twistedPaint); // 원래 그림으로 돌아옴(뒤틀린 그림 제거)
        paintEventManager.CompleteEvent(paintNum); // 이벤트 진행 완료 정보 전달 (리스트에 담긴 정보 삭제)
        paintEventManager.event3 = false; // 이벤트 진행 관련 변수 끄기
    }
}