# UnityGame-DungeonEscape程式碼 ## 目錄 [TOC] ## Player.cs ``` c#= using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityStandardAssets.CrossPlatformInput; public class Player : MonoBehaviour { private Rigidbody2D rb; private SpriteRenderer sprite; public LayerMask groundlayer; public float speed; public float jumpForce; public static int diamonds = 0; public int amount; bool resetJumpNeeded; bool grounded = false; PlayerAnimation playerAnimation; public class PlayerStats { public int Health = 100; } // Start is called before the first frame update void Start() { rb = this.GetComponent<Rigidbody2D>(); sprite = GetComponentInChildren<SpriteRenderer>(); playerAnimation = GetComponent<PlayerAnimation>(); } // Update is called once per frame void Update() { float move = CrossPlatformInputManager.VirtualAxisReference("Horizontal").GetValue; float movePos = CrossPlatformInputManager.GetAxis("Horizontal"); grounded = IsGrounded(); if(movePos > 0) //往左 { sprite.flipX = false; playerAnimation.Move(move); } else if(movePos < 0) //往右 { sprite.flipX = true; playerAnimation.Move(move); } if ((Input.GetKeyDown(KeyCode.Space) || CrossPlatformInputManager.GetButtonDown("Jump_Button")) && IsGrounded()) { // 跳躍 rb.velocity = new Vector2(rb.velocity.x, jumpForce); StartCoroutine(ResetJumpNeededRoutine()); Audio.instance.Playsingle(Audio.instance.playerjumpsound); playerAnimation.Jump(true); } rb.velocity = new Vector2(move * speed, rb.velocity.y); } public void GetDiamonds(int amount) { diamonds += amount; Audio.instance.Playsingle(Audio.instance.playergetdiamonds); UIManager.instance.UpdateDiamondsText(diamonds); } bool IsGrounded() //是否踩地 { RaycastHit2D hitinfo = Physics2D.Raycast(this.transform.position , Vector2.down , 0.8f , groundlayer); Debug.Log(hitinfo.collider); if(hitinfo.collider != null) { if (resetJumpNeeded == false) { playerAnimation.Jump(false); return true; } } return false; } IEnumerator ResetJumpNeededRoutine() //防止連續跳 { resetJumpNeeded = true; yield return new WaitForSeconds(1); resetJumpNeeded = false; } } ``` ## PlayerAnimation.cs ```c#= using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerAnimation : MonoBehaviour { Animator anim; // Start is called before the first frame update void Start() { anim = GetComponentInChildren<Animator>(); } public void Move(float value) { anim.SetFloat("Move", Mathf.Abs(value)); } public void Jump(bool value) { anim.SetBool("Jump", value); anim.SetBool("Grounded", false); } } ``` ## CameraFollow.cs ```C#= using System.Collections; using System.Collections.Generic; using UnityEngine; public class CameraFollow : MonoBehaviour { public LayerMask groundlayer; public Transform target; public Vector3 offsets; public float yPosBoundary; // Update is called once per frame void Update() { if(target == null) { return; } Vector3 newPos = target.position; newPos = new Vector3(newPos.x, Mathf.Clamp(newPos.y, yPosBoundary, Mathf.Infinity), newPos.z); transform.position = newPos + offsets; } } ``` ## UIManager ```C#= using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; public class UIManager : MonoBehaviour { public Text DiamondAmount; public GameObject completedpanel; public GameObject gameover; private static UIManager _instance; public static UIManager instance { get { if(_instance == null) { Debug.LogError("UIManger is NULL"); } return _instance; } } private void Awake() { _instance = this; } public void UpdateDiamondsText(int count) { DiamondAmount.text = count.ToString(); } public void OpenCompletedPanel() { completedpanel.SetActive(true); } public void RetryButton() { Player.diamonds = 0; SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex); } public void HomeButton() { Player.diamonds = 0; SceneManager.LoadScene(0); } public void GameOver() { gameover.SetActive(true); } } ``` ## MainMenu.cs ```c#= using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class MainMenu : MonoBehaviour { public void StartButton() { SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1); } public void QuitButton() { Application.Quit(); } } ``` ## GameMaster.cs ```C#= using System.Collections; using System.Collections.Generic; using UnityEngine; public class GameMaster : MonoBehaviour { public static GameMaster gm; void Start() { if( gm == null) { gm = GameObject.FindGameObjectWithTag("GM").GetComponent<GameMaster>(); } } public static void KillPlayer(Player player) { Destroy(player.gameObject); UIManager.instance.GameOver(); AudioSource source = GameObject.FindGameObjectWithTag("BackGroundMusic").GetComponent<AudioSource>(); Audio.instance.Playsingle(Audio.instance.playerdeathsound); source.Stop(); } } ``` ## Audio.cs ```C#= using System.Collections; using System.Collections.Generic; using UnityEngine; public class Audio : MonoBehaviour { public static Audio instance = null; AudioSource audiosource; public AudioClip playerdeathsound; public AudioClip playerjumpsound; public AudioClip playergetdiamonds; public AudioClip gamecompletesounds; private void Awake() { if(instance == null) { instance = this; } else if (instance != this) { Destroy(gameObject); } DontDestroyOnLoad(gameObject); } public void Playsingle(AudioClip clip) { audiosource.clip = clip; audiosource.Play(); } private void Start() { audiosource = GetComponent<AudioSource>(); } } ``` ## DeathWall.cs ```C#= using System.Collections; using System.Collections.Generic; using UnityEngine; public class DeathWall : MonoBehaviour { private Rigidbody2D rb; private SpriteRenderer sprite; public static DeathWall deathwall; public float speed; private void Awake() { deathwall = GameObject.FindGameObjectWithTag("DeathWall").GetComponent<DeathWall>(); } // Start is called before the first frame update void Start() { rb = GetComponent<Rigidbody2D>(); sprite = GetComponentInChildren<SpriteRenderer>(); } // Update is called once per frame void Update() { rb.velocity = new Vector2(speed, 0); } private void OnTriggerEnter2D(Collider2D other) { if(other.tag == "Player") { Player player = other.GetComponent<Player>(); if (player != null) { Debug.Log("player touched"); GameMaster.KillPlayer(player); this.gameObject.SetActive(false); } } } } ``` ## Boundary.cs ```C#= using System.Collections; using System.Collections.Generic; using UnityEngine; public class Boundry : MonoBehaviour { private void OnTriggerEnter2D(Collider2D collider) { if (collider.tag == "Player") { Player player = collider.GetComponent<Player>(); if (player != null) { GameMaster.KillPlayer(player); DeathWall.deathwall.gameObject.SetActive(false); } } } } ``` ## Diamond.cs ```C#= using System.Collections; using System.Collections.Generic; using UnityEngine; public class Diamond : MonoBehaviour { public int amount; private void OnTriggerEnter2D(Collider2D collision) { if(collision.tag == "Player") { Player player = collision.GetComponent<Player>(); if(player != null) { player.GetDiamonds(amount); Destroy(this.gameObject); } } } } ``` ## Chest.cs ```C#= using System.Collections; using System.Collections.Generic; using UnityEngine; public class Chest : MonoBehaviour { [SerializeField] SpriteRenderer sprite_open; private void Update() { if (Player.diamonds >= 10) { sprite_open.gameObject.SetActive(true); } } private void OnTriggerEnter2D(Collider2D collision) { if (Player.diamonds >= 10) { if (collision.tag == "Player") { UIManager.instance.OpenCompletedPanel(); Audio.instance.Playsingle(Audio.instance.gamecompletesounds); DeathWall.deathwall.gameObject.SetActive(false); } } } } ```