728x90
반응형
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
// 싱글턴 => 공유하는 변수
public static GameManager instance;
// 스코어 관리, 게임오버 관리 => 플레이어 사망
// 재시작 관리 => 마우스 클릭 => 재시작
[SerializeField] GameObject gameover;
[SerializeField] Text scoreText;
public Text hpText;
public int hp = 3;
public bool isGameover = false;
int score = 0;
private void Awake()
{
if (instance == null)
instance = this;
}
private void Start()
{
hpText.text = hp.ToString();
}
void Update()
{
if (isGameover && Input.GetMouseButton(0))
SceneManager.LoadScene(SceneManager.GetActiveScene().name); //SceneManager.LoadScene("Main");
}
public void EndGame() // 플레이어 사망시 호출 => 게임오버
{
gameover.SetActive(true);
isGameover = true;
}
public void AddScore() // 발판에 닿으면 점수증가
{
score++;
scoreText.text = "score : " + score;
}
public void HeartControl()
{
if(!isGameover)
{
hp -= 1;
hpText.text = hp.ToString();
}
}
public void Sibal()
{
if(!isGameover)
{
hp = 0;
hpText.text = hp.ToString();
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMove : MonoBehaviour
{
// 변수 => 이단점프 => 애니메이션, 사망
[SerializeField] float jumpForce;
[SerializeField] AudioClip jumpClip;
[SerializeField] AudioClip deadClip;
int jumpCount = 0;
bool isGrounded = false;
Rigidbody2D rb;
Animator anim;
AudioSource source;
void Start()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
source = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
if (GameManager.instance.isGameover == true)
return;
/*
if(Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
if(touch.phase == TouchPhase.Began && jumpCount < 2)
{
source.clip = jumpClip;
source.Play();
jumpCount++;
rb.velocity = Vector2.zero;
rb.AddForce(Vector3.up * jumpForce); // ( 방향 * 힘 )
}
else if(touch.phase == TouchPhase.Ended && rb.velocity.y > 0)
{
rb.velocity = rb.velocity * 0.5f;
}
}*/
// 마우스 왼쪽 버튼을 클릭하면 점프
if (Input.GetMouseButtonDown(0) && jumpCount < 2) // 0 = 왼쪽 클릭, 1 = 오른쪽 클릭, 2 = 휠 클릭
{
source.clip = jumpClip;
source.Play();
jumpCount++;
rb.velocity = Vector2.zero;
rb.AddForce(Vector3.up * jumpForce); // ( 방향 * 힘 )
}
else if(Input.GetMouseButtonUp(0)&&rb.velocity.y > 0) // y가 상승중일때 마우스에서 손을 떼면
{
rb.velocity = rb.velocity * 0.5f;
}
anim.SetBool("Grounded", isGrounded);
}
// 땅에 닿으면 점프카운트를 리셋
private void OnCollisionEnter2D(Collision2D collision)
{
jumpCount = 0;
isGrounded = true;
}
private void OnCollisionExit2D(Collision2D collision)
{
isGrounded = false;
}
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.tag == "Dead" && !GameManager.instance.isGameover)
{
GameManager.instance.HeartControl();
}
if(collision.tag == "Dead" && GameManager.instance.hp <= 0)
{
// 사망 => 애니메이션, 소리, 속도제로
anim.SetTrigger("Die");
source.PlayOneShot(deadClip);
rb.velocity = Vector2.zero;
rb.gravityScale = 0;
// 게임오버 메소드 호출
GameManager gm = FindObjectOfType<GameManager>();
gm.EndGame();
// GameManager.instance.EndGame(); 도 가능
}
if(collision.tag == "DeadZone")
{
GameManager.instance.Sibal();
GameManager.instance.EndGame();
}
}
}
728x90
반응형
'유니티 > 유니런(2D 러닝 게임)' 카테고리의 다른 글
유니런(7) - 씬 전환 (0) | 2021.09.01 |
---|---|
유니런(6) - 발판 반복 생성과 게임 완성 (0) | 2021.08.28 |
유니런(5) - 게임 매니저 (1) | 2021.08.09 |
유니런(4) - UI (0) | 2021.08.08 |
유니런(3) - 배경 스크롤링 (0) | 2021.08.08 |