오디오, 게임매니저 등 수 많은 버그;;
하지만 노력하시는 팀원들 덕에 점점 완성되어가는 코드들...
//gameManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Linq;
using UnityEngine.SceneManagement;
using Unity.Profiling;
using UnityEngine.Events;
public class GameManager : MonoBehaviour
{
public static GameManager I;
public GameObject card;
public GameObject firstCard;
public GameObject secondCard;
public Text timeTxt;
public GameObject endTxt;
public Text addTxt;
public Text maxScoreText;
public Text thisScoreText;
public GameObject endPanel;
private RectTransform transaddtxt;
byte c;
public int check;
float time;
public float maxTime;
bool isRunning = true;
public AudioManager audioManager;
public AudioClip match;
public AudioClip failed;
public AudioClip bestscore;
public AudioClip lowscore;
public AudioSource audioSource;
public Sprite[] sprites; // sprite를 Inspector창에서 받기 위한 선언
List<GameObject> cardList; // card들을 담을 cardList, 현재는 card를 섞는데 사용
public List<GameObject> namelist;
//enum TeamName { 팀원1, 팀원2, 팀원3, 팀원4, 팀원5, 팀원6, }
private void Awake()
{
I = this;
}
void Start()
{
transaddtxt = addTxt.GetComponent<RectTransform>();
Time.timeScale = 1.0f;
cardList = new List<GameObject>();
//namelist = new List<GameObject>();
Sprite tempSprite = sprites[0];
int tempSpriteNum = 0;
// 12개의 카드 생성
// 카드 sprite를 순차적으로 넣어줌
for (int i = 0; i < 12; ++i)
{
// 카드는 12개 생성되어야 하는데 sprite는 6개
// 2개의 카드는 같은 카드여야 하므로
if (i % 2 == 0) // 0, 2, 4, 6, 8, 10 일때만 sprite가 바뀜
{
tempSprite = sprites[i / 2];
tempSpriteNum = i / 2;
}
GameObject newCard = Instantiate(card);
newCard.transform.parent = GameObject.Find("Cards").transform;
float x = (i / 3) * 1.4f - 2.1f;
float y = (i % 3) * 1.4f - 3.0f;
newCard.transform.position = new Vector3(x, y, 0);
newCard.transform.Find("Front").GetComponent<SpriteRenderer>().sprite = tempSprite;
newCard.GetComponent<Card>().spriteNum = tempSpriteNum; // card에 spriteNum 넣어주기
cardList.Add(newCard); // List에 생성한 카드 넣어주기
}
// 카드 섞기
for(int i = cardList.Count- 1; i> 0; --i)
{
int randomNum = Random.Range(0, i);
// swap
Vector3 tempPosition = cardList[i].transform.position;
cardList[i].transform.position = cardList[randomNum].transform.position;
cardList[randomNum].transform.position = tempPosition;
}
}
void Update()
{
float addy = transaddtxt.anchoredPosition.y; // addtxt 위치
addy += 0.5f; // addtxt y값 상승
transaddtxt.anchoredPosition = new Vector2(0, addy); // addtxt y값 상승
c -= 1; // 글자 색상 투명하게
addTxt.color = new Color32(255, 0, 0, c); // 글자 색상 투명하게
time += Time.deltaTime;
if (time > maxTime)
Invoke("GameEnd",0f);
timeTxt.text = time.ToString("N2");
}
public void IsMatched()
{
int firstCardSpriteNum = firstCard.GetComponent<Card>().spriteNum;
int secondCardSpriteNum = secondCard.GetComponent<Card>().spriteNum;
if(firstCardSpriteNum == secondCardSpriteNum)
{
audioSource.PlayOneShot(match);
string info = firstCard.GetComponentInChildren<SpriteRenderer>().sprite.name; // sprite의 이름 rtanx info에 저장
check = int.Parse(info.Substring(info.Length - 1)) -1; // rtanx 의 x부분 자르기, int 로 변형
// 배열은 0부터 시작하므로 -1
//check = firstCard.GetComponent<Card>().spriteNum;
namelist[check].SetActive(true); // Active True
Invoke("nActiveFalse", 1.0f); // 1초 후 false
firstCard.GetComponent<Card>().DestrotyCard();
secondCard.GetComponent<Card>().DestrotyCard();
int cardsLeft = GameObject.Find("Cards").transform.childCount;
if (cardsLeft == 2)
{
Invoke("GameEnd", 1f);
}
}
else
{
audioSource.PlayOneShot(failed);
firstCard.GetComponent<Card>().CloseCard();
secondCard.GetComponent<Card>().CloseCard();
// 시간 추가 기능
time += 5;
addTxt.color = new Color32(255, 0, 0, 255); // 글자색 RED
c = 0; // 투명도 초기화
transaddtxt.anchoredPosition = new Vector2(0, 450); // 글자 위치 초기화 (진행시간 위)
addTxt.gameObject.SetActive(true); // addTXT 활성화
Invoke("ActiveFalse", 1.0f); // 1초 후 ActiveFalse 실행
}
firstCard = null;
secondCard = null;
}
void ActiveFalse()
{
addTxt.gameObject.SetActive(false); // addtxt 비활성화 하기
}
void nActiveFalse()
{
namelist[check].SetActive(false);
}
void GameEnd()
{
isRunning = false;
endPanel.SetActive(true);
Time.timeScale = 0f;
thisScoreText.text = time.ToString("N2");
//endTxt.SetActive(true);
if (PlayerPrefs.HasKey("bestscore") == false)
{
// 게임종료시 베스트 스코어면 나오는 노래
audioSource.PlayOneShot(bestscore);
PlayerPrefs.SetFloat("bestscore", time);
}
else if (time < PlayerPrefs.GetFloat("bestscore"))
{
// 게임종료시 베스트 스코어보다 낮으면 나오는 노래
audioSource.PlayOneShot(bestscore);
PlayerPrefs.SetFloat("bestscore", time);
}
else
{
// 게임종료시 베스트 스코어보다 낮으면 나오는 노래
audioSource.PlayOneShot(lowscore);
}
float maxScore = PlayerPrefs.GetFloat("bestscore");
maxScoreText.text = maxScore.ToString("N2");
EndGameBgmStop();
}
void EndGameBgmStop()
{
if (audioManager != null && audioManager.audioSource != null)
{
audioManager.audioSource.Stop();
}
}
public void RetryGame()
{
SceneManager.LoadScene("MainScene");
}
public void GoHomeBtn()
{
SceneManager.LoadScene("StartScene");
}
}
마지막으로 오늘 배운 git 용어 정리...........
git = vcs = Version Control System
(형상 관리)
협업 프로그램
git , git Hub 등
캡쳐 Commit (Init)
정보수정 Amend
되돌리기 Undo
개별수정 Discard
치워두기 Stash
받아오기(원격 -> 로컬) pull
올리기(로컬 -> 원격) push
줄기 - main
가지 - branch
합치기 - merge
'Today I Learned' 카테고리의 다른 글
[내일배움캠프] C# 문법 종합반 - 1주차 숙제풀이 (1) | 2023.11.06 |
---|---|
<TIL> [내일배움캠프] 본과정 5일차 (미니프로젝트 마무리) (0) | 2023.11.03 |
<TIL> [내일배움캠프] 본과정 4일차 (0) | 2023.11.02 |
<TIL> [내일배움캠프] 본과정 3일차 (오늘 일기) (0) | 2023.11.01 |
<TIL> [내일배움캠프] 본과정 1일차 (2) | 2023.10.30 |