using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DinoGameManager : MonoBehaviour
{
#region instance
public static DinoGameManager instance;
private void Awake()
{
if (instance != null)
{
Destroy(gameObject);
return;
}
instance = this;
}
#endregion
public float gameSpeed = 1;
public bool isPlay = false;
public delegate void OnPlay(bool isPlay);
public OnPlay onPlay;
[@SerializeField] private GameObject startText;
[@SerializeField] private GameObject retryUI;
[@SerializeField] private GameObject scoreUI;
private void Update()
{
if (!isPlay && Input.GetKeyDown(KeyCode.Space)) // spacebar 누르면
{
Play();
}
}
public void Play()
{
// 게임 시작 안내 문구 비활성화
if (startText.activeSelf)
startText.SetActive(false);
// 다시하기 UI 비활성화
if (retryUI.activeSelf)
retryUI.SetActive(false);
// 점수 UI 활성화
if (!scoreUI.activeSelf)
scoreUI.SetActive(true);
isPlay = true;
onPlay.Invoke(isPlay);
}
public void GameOver()
{
retryUI.SetActive(true);
isPlay = false;
onPlay.Invoke(isPlay);
}
}
변수
gameSpeed
: 게임의 속도를 제어하는데 사용isPlay
: 게임이 현재 실행 중인지 여부onPlay
: 게임 실행/정지 이벤트를 처리하기 위해 사용되는 델리게이트흐름
Awake()
Update()
Play()
GameOver()
< Transitions >
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DinoPlayerController : MonoBehaviour
{
private Animator animator;
private bool isJump = false;
private bool isTop = false;
public float jumpHeight = 0;
public float jumpSpeed = 0;
private Vector2 startPosition;
void Start()
{
startPosition = transform.position;
animator = GetComponent<Animator>();
}
void Update()
{
if (DinoGameManager.instance.isPlay)
{
animator.SetBool("die", false);
// 달리는 애니메이션으로 설정하기
animator.SetBool("run", true);
}
else
{
animator.SetBool("run", false);
}
// 점프 애니메이션으로 설정하기
if (Input.GetKeyDown(KeyCode.Space) && DinoGameManager.instance.isPlay)
{
animator.SetBool("run", false);
animator.SetBool("jump", true);
isJump = true;
}
else if (transform.position.y <= startPosition.y && isJump)
{
isJump = false;
isTop = false;
transform.position = startPosition;
// 땅에 착지했을 경우
if (transform.position.y == startPosition.y)
{
animator.SetBool("jump", false);
animator.SetBool("run", true);
}
}
if (isJump)
{
if (transform.position.y <= jumpHeight - 0.1f && !isTop)
{
transform.position = Vector2.Lerp(transform.position,
new Vector2(transform.position.x, jumpHeight), jumpSpeed * Time.deltaTime);
}
else
{
isTop = true;
}
if (transform.position.y > startPosition.y && isTop)
{
transform.position = Vector2.MoveTowards(transform.position,
startPosition, jumpSpeed * Time.deltaTime);
}
}
}
private void OnTriggerEnter2D(Collider2D col)
{
if (col.CompareTag("Mob"))
{
DinoGameManager.instance.GameOver();
animator.SetBool("die", true);
}
}
}
private void Start()
{
DinoGameManager.instance.onPlay += PlayGame;
}
void PlayGame(bool isPlay)
{
if (isPlay)
{
// 시작 전 장애물 비화성화
for (int i = 0; i < cactusPool.Count; i++)
{
if (cactusPool[i].activeSelf)
cactusPool[i].SetActive(false);
}
StartCoroutine(CreateCactus());
}
else
StopAllCoroutines();
}
변경된 부분
Start()
PlayGame()
공룡이 귀여워요