스네이크가 음식을 먹어서 얻은 점수의 총합이 일정 수치를 넘으면
게임 클리어 판정이 되도록 코드를 추가하였다.
게임 클리어가 되었을 경우,
게임 오버와 마찬가지로 게임을 재시작하거나 메인 메뉴로 돌아갈 수 있도록 구성하였다.
이전에 제작한 게임 오버 오브젝트에서
텍스트만 수정하는 방식으로 진행하였다.
(+게임 클리어 또는 오버 상태에서 "일시정지" 버튼이 클릭되는 현상이 발생하여 수정함)
추가적으로 조금 더 체계적인 게임 시스템을 위하여
"Snake" 스크립트에서 점수를 계산하던 것을
"GameManager" 스크립트에서 수행하도록 변경함으로써
"Snake" 스크립트는 스네이크 관련 모션을,
"GameManager" 스크립트는 전반적인 게임 관리를 담당하도록 해주었다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
// for Setting
public GameObject inSetting;
public GameObject mainButton;
// Snake Var
public GameObject snake;
// Score Var
public Text scoreText;
public int score = 0;
// Pause Var
bool isPause = false;
public Text pauseText;
public GameObject pauseButton;
// for Game Over & Clear
public GameObject gameEnd;
public Text endText;
void Update()
{
}
// Start Button
public void GameStart()
{
// Change Scene
SceneManager.LoadScene(1);
}
// Setting Button
public void GameSetting()
{
// On or Off the Object
inSetting.SetActive(true);
mainButton.SetActive(false);
}
// Back Button in Setting
public void SettingBack()
{
// On or Off the Object
inSetting.SetActive(false);
mainButton.SetActive(true);
}
// Add Score
public void AddScore(int point)
{
score += point;
scoreText.text = string.Format("{0:n0}", score); // {0:n0}: 세자리마다 쉼표로 나눠주는 숫자 양식
// Clear Check
if (score >= 100)
{
GameClear();
}
}
// Pause Game
public void TimePause()
{
if (isPause)
{
// Resume
Time.timeScale = 1;
pauseText.text = "일시정지";
isPause = false;
}
else
{
// Pause
Time.timeScale = 0;
pauseText.text = "다시시작";
isPause = true;
}
}
// Game Over
public void GameOver()
{
Time.timeScale = 0;
endText.text = "GAME OVER";
gameEnd.SetActive(true);
pauseButton.SetActive(false);
}
// Game Retry
public void GameRetry()
{
SceneManager.LoadScene(1);
Time.timeScale = 1;
}
// to Main Menu
public void GameToMain()
{
// Change Scene
SceneManager.LoadScene(0);
Time.timeScale = 1;
}
// Game Clear
public void GameClear()
{
Time.timeScale = 0;
endText.text = "GAME CLEAR";
gameEnd.SetActive(true);
pauseButton.SetActive(false);
}
}
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class Snake : MonoBehaviour
{
// Game Manager
public GameManager manager;
// Snake Movement Var
Vector2 dir = 1 * Vector2.right;
Vector2 dirTemp = 1 * Vector2.right;
private float size = 1;
// Snake Direction Var
float h;
float v;
bool isHorizonMove;
// Mobile Key Var
int up_Value;
int down_Value;
int left_Value;
int right_Value;
bool up_Down;
bool down_Down;
bool left_Down;
bool right_Down;
bool up_Up;
bool down_Up;
bool left_Up;
bool right_Up;
// Snake Tail Var & Prefab
List<Transform> tail = new List<Transform>();
public GameObject tailPrefab;
// for Check that Snake Eat
bool ate = false;
void Start()
{
// Reset Score
manager.score = 0;
InvokeRepeating("Move", 1f, 0.05f);
}
void Update()
{
// Move Value (PC + Mobile)
h = Input.GetAxisRaw("Horizontal") + (right_Value + left_Value);
v = Input.GetAxisRaw("Vertical") + (up_Value + down_Value);
// Check Button Down & Up (PC || Mobile)
bool hDown = Input.GetButtonDown("Horizontal") || right_Down || left_Down;
bool vDown = Input.GetButtonDown("Vertical") || up_Down || down_Down;
bool hUp = Input.GetButtonUp("Horizontal") || right_Up || left_Up;
bool vUp = Input.GetButtonUp("Vertical") || up_Up || down_Up;
// Check Horizontal Move
if (hDown)
isHorizonMove = true;
else if (vDown)
isHorizonMove = false;
else if (hUp || vUp)
isHorizonMove = (h != 0);
// Determine Direction
if (h != 0 || v != 0)
{
dirTemp = dir;
dir = (isHorizonMove ? new Vector2(h, 0) : new Vector2(0, v));
// Check Snake Direction (Reverse)
if (dir == -dirTemp)
{
dir = dirTemp;
}
}
// Mobile Var Init
up_Down = false;
down_Down = false;
left_Down = false;
right_Down = false;
up_Up = false;
down_Up = false;
left_Up = false;
right_Up = false;
}
void Move()
{
// Store Present Position
Vector2 v = transform.position;
// Move
transform.Translate(size * dir);
// Check whether Snake Eats Food or Not
if (ate)
{
// Copy the Tail in Present Position
GameObject g = (GameObject)Instantiate(tailPrefab, v, Quaternion.identity);
// Store the Tail
tail.Insert(0, g.transform);
ate = false;
}
// Move the Tail to the Head
else if (tail.Count > 0)
{
tail.Last().position = v;
tail.Insert(0, tail.Last());
tail.RemoveAt(tail.Count - 1);
}
}
// Button Down (Mobile)
public void ButtonDown(string type)
{
switch (type)
{
case "U":
up_Value = 1;
up_Down = true;
break;
case "D":
down_Value = -1;
down_Down = true;
break;
case "L":
left_Value = -1;
left_Down = true;
break;
case "R":
right_Value = 1;
right_Down = true;
break;
}
}
// Button Up (Mobile)
public void ButtonUp(string type)
{
switch (type)
{
case "U":
up_Value = 0;
up_Up = true;
break;
case "D":
down_Value = 0;
down_Up = true;
break;
case "L":
left_Value = 0;
left_Up = true;
break;
case "R":
right_Value = 0;
right_Up = true;
break;
}
}
void OnTriggerEnter2D(Collider2D col)
{
// Eat Food
if (col.gameObject.tag == "Food")
{
ate = true;
Destroy(col.gameObject);
// Add Score
Food foodLogic = col.gameObject.GetComponent<Food>();
manager.AddScore(foodLogic.point);
}
else
{
}
// Touch Border or Tail
if (col.gameObject.tag == "Border" || col.gameObject.tag == "Snake")
{
manager.GameOver();
}
}
}
궁금하신 점은 밑에 댓글 바랍니다
'메모장 > 스네이크 게임' 카테고리의 다른 글
스네이크 게임 - 빌드 및 오브젝트 앵커 변경 (0) | 2021.01.25 |
---|---|
스네이크 게임 - 해상도 변경 (0) | 2021.01.21 |
스네이크 게임 - 게임 오버 판정 (0) | 2021.01.19 |
스네이크 게임 - 메인 메뉴와 일시 정지 (0) | 2021.01.18 |
스네이크 게임 - 점수 카운트 (0) | 2021.01.17 |