Gamemanager.cs(全局控制)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameManager : MonoBehaviour {
private static GameManager _instance;
public static GameManager Instance//将Gamemanager实例化,
{
get
{
return _instance;
}
}
public GameObject Pacman;
public GameObject Blinky;
public GameObject Clyde;
public GameObject Inky;
public GameObject Pinky;
public GameObject Win;
private GameObject RestartClick;
public GameObject gameover;
public AudioClip StartClip;
public int score,remain;
public Text scoretext;
public Text remaintext;
public GameObject startCountDownPrefab;
public bool Wined=false ,died=false ;
public bool isSuperPacman = false ;
public List<int > rawIndex = new List<int > {0 ,1 ,2 ,3 };
public List<int > usingIndex = new List<int > ();
private List<GameObject> pacdotGos = new List<GameObject>();
private void Start (){
score = 0 ;
RestartClick = GameObject.Find ("RestartButton" );
AudioSource.PlayClipAtPoint (StartClip,new Vector3(23 f,16 f,-10 f));
StopGameState ();
StartCoroutine (PlayStartCountDown());
Invoke ("Continue" ,4 f);
Invoke ("CreateSuperPacdot" ,10 f);
}
private void Update (){
remain = pacdotGos.Count;
remaintext.text = " " + remain;
if (score >= 0 && score <= 9 )
scoretext.text = " " + score;
if (score >= 10 && score <= 999 )
scoretext.text = " " + score;
if (score >= 1000 && score <= 99999 )
scoretext.text = " " + score;
if (score >= 100000 && score <= 9999999 )
scoretext.text = "" + score;
if (pacdotGos.Count==0 ) {
Wined = true ;
Win.GetComponent<SpriteRenderer> ().color = new Color (255 f,255 f,255 f,255 f);
}
if (Wined) {
StopGameState ();
}
if (died || Wined) {
RestartClick.SetActive (true );
}
else {
RestartClick.SetActive (false );
}
}
IEnumerator PlayStartCountDown(){
GameObject go = Instantiate (startCountDownPrefab);
yield return new WaitForSeconds (4 f);
Destroy (go);
GetComponent<AudioSource> ().Play();
}
private void Awake (){
_instance = this ;
int tempCount = rawIndex.Count;
for (int i=0 ;i<tempCount;i++){
int tempIndex = Random.Range (0 ,rawIndex.Count);
usingIndex.Add (rawIndex [tempIndex]);
rawIndex.RemoveAt (tempIndex);
}
foreach (Transform child in GameObject.Find("Maze" ).transform){
pacdotGos.Add (child.gameObject);
}
}
private void ResetDots (){
pacdotGos.Clear ();
foreach (Transform child in GameObject.Find("Maze" ).transform){
child.gameObject.SetActive (true );
pacdotGos.Add (child.gameObject);
}
}
public void EatPacdot (GameObject dot){
pacdotGos.Remove (dot);
}
public void EatSuperPacdot (){
FreezeEnemy ();
Invoke ("CreateSuperPacdot" , 8 f);
isSuperPacman = true ;
StartCoroutine (RecoveryEnemy());
}
private void FreezeEnemy (){
Blinky.GetComponent<GhostMove> ().enabled = false ;
Clyde.GetComponent<GhostMove> ().enabled = false ;
Pinky.GetComponent<GhostMove> ().enabled = false ;
Inky.GetComponent<GhostMove> ().enabled = false ;
Blinky.GetComponent<SpriteRenderer> ().color = new Color (0.7 f, 0.7 f, 0.7 f, 0.7 f);
Clyde.GetComponent<SpriteRenderer> ().color = new Color (0.7 f, 0.7 f, 0.7 f, 0.7 f);
Inky.GetComponent<SpriteRenderer> ().color = new Color (0.7 f, 0.7 f, 0.7 f, 0.7 f);
Pinky.GetComponent<SpriteRenderer> ().color = new Color (0.7 f, 0.7 f, 0.7 f, 0.7 f);
}
private void DisFreezeEnemy (){
Blinky.GetComponent<GhostMove> ().enabled = true ;
Clyde.GetComponent<GhostMove> ().enabled = true ;
Pinky.GetComponent<GhostMove> ().enabled = true ;
Inky.GetComponent<GhostMove> ().enabled = true ;
Blinky.GetComponent<SpriteRenderer> ().color = new Color (1 f, 1 f, 1 f, 1 f);
Clyde.GetComponent<SpriteRenderer> ().color = new Color (1 f, 1 f, 1 f, 1 f);
Inky.GetComponent<SpriteRenderer> ().color = new Color (1 f, 1 f, 1 f, 1 f);
Pinky.GetComponent<SpriteRenderer> ().color = new Color (1 f, 1 f, 1 f, 1 f);
}
IEnumerator RecoveryEnemy(){
yield return new WaitForSeconds (3 f);
DisFreezeEnemy ();
isSuperPacman = false ;
}
public void GotoPlayScene (){
SceneManager.UnloadSceneAsync ("StartPanel" );
SceneManager.LoadScene ("Main" );
}
public void GotoStartPanel (){
SceneManager.UnloadSceneAsync ("Main" );
SceneManager.LoadScene ("StartPanel" );
}
public void Exit (){
Application.Quit();
}
private void CreateSuperPacdot (){
if (pacdotGos.Count<8 ) {
return ;
}
int tempindex = Random.Range (0 ,pacdotGos.Count);
pacdotGos [tempindex].transform.localScale = new Vector3 (3 ,3 ,3 );
pacdotGos [tempindex].GetComponent<Pacdot> ().isSuperPacDot = true ;
}
private void StopGameState (){
Blinky.GetComponent<GhostMove> ().enabled = false ;
Clyde.GetComponent<GhostMove> ().enabled = false ;
Pinky.GetComponent<GhostMove> ().enabled = false ;
Inky.GetComponent<GhostMove> ().enabled = false ;
Pacman.GetComponent<PacmanMove> ().enabled = false ;
}
public void Continue (){
Blinky.GetComponent<GhostMove> ().enabled = true ;
Clyde.GetComponent<GhostMove> ().enabled = true ;
Pinky.GetComponent<GhostMove> ().enabled = true ;
Inky.GetComponent<GhostMove> ().enabled = true ;
Pacman.GetComponent<PacmanMove> ().enabled = true ;
}
public void RestartGame (){
StopGameState ();
score = 0 ;
ResetDots ();
gameover.GetComponent<SpriteRenderer> ().color = new Color (0 ,0 ,0 ,0 );
Win.GetComponent<SpriteRenderer> ().color = new Color (0 ,0 ,0 ,0 );
Pacman.SetActive(true );
StartCoroutine (PlayStartCountDown());
Invoke ("Continue" ,4 f);
Blinky.GetComponent<GhostMove> ().Restart();
Clyde.GetComponent<GhostMove> ().Restart();
Pinky.GetComponent<GhostMove> ().Restart();
Inky.GetComponent<GhostMove> ().Restart();
Pacman.GetComponent<PacmanMove> ().Restart();
Pacman.GetComponent<CircleCollider2D> ().enabled = true ;
died = false ;
Wined = false ;
}
}
Ghostmove.cs(敌人相关功能)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GhostMove : MonoBehaviour {
private int index=0 ;
public GameObject[] wayPointsGos;
private List<Vector3> wayPoints = new List <Vector3> ();
public float speed=0.15 f;
public GameObject gameover;
private Vector3 StartPos;
private void Start (){
StartPos=transform.position + new Vector3 (0 ,3 ,0 );
LoadAPath (wayPointsGos[GameManager.Instance.usingIndex[GetComponent<SpriteRenderer>().sortingOrder-2 ]]);
}
private void FixedUpdate () //目前还不知道FixedUpdate和Update的区别
{
if (transform.position != wayPoints [index])
{
Vector2 temp = Vector2.MoveTowards (transform.position, wayPoints [index], speed);
GetComponent<Rigidbody2D> ().MovePosition (temp);
}
else
{
index +=1 ;
if (index >= wayPoints.Count) {
index = 0 ;
}
}
Vector2 dir = wayPoints [index] - transform.position;
GetComponent<Animator> ().SetFloat ("DirX" ,dir.x);
GetComponent<Animator> ().SetFloat ("DirY" ,dir.y);
}
private void LoadAPath (GameObject go){
wayPoints.Clear ();
foreach (Transform t in go.transform) {
wayPoints.Add (t.position);
}
wayPoints.Insert (0 ,StartPos);
wayPoints.Add (StartPos);
}
public void Restart (){
gameObject.transform.position = StartPos- new Vector3(0 f,3 f,0 f);
index = 0 ;
}
public void deleteGameoverImage (){
gameover.GetComponent<SpriteRenderer> ().color = new Color (255 ,255 ,255 ,255 );
}
private void OnTriggerEnter2D (Collider2D collision)//碰撞函数
{
if (collision.gameObject.name == "Pacman" )
{
if (GameManager.Instance.isSuperPacman) {
transform.position = StartPos - new Vector3 (0 ,3 ,0 );
index = 0 ;
GameManager.Instance.score += 500 ;
}
else {
gameover.GetComponent<SpriteRenderer> ().color = new Color (255 ,255 ,255 ,255 );
GameManager.Instance.died = true ;
collision.gameObject.SetActive (false );
}
}
}
}
Pacmanmove.cs(主角相关功能)
using UnityEngine;
public class PacmanMove : MonoBehaviour
{
public float speed = 0.35 f;
private Vector2 dest = Vector2.zero,StartPos;
private void Start ()
{
dest = transform.position;
StartPos = transform.position;
}
private void FixedUpdate ()
{
Vector2 temp = Vector2.MoveTowards(transform.position, dest, speed);
GetComponent<Rigidbody2D>().MovePosition(temp);
if ((Vector2)transform.position == dest)
{
if ((Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W)) && Valid(Vector2.up))
{
dest = (Vector2)transform.position + Vector2.up;
}
if ((Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S)) && Valid(Vector2.down))
{
dest = (Vector2)transform.position + Vector2.down;
}
if ((Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A)) && Valid(Vector2.left))
{
dest = (Vector2)transform.position + Vector2.left;
}
if ((Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D)) && Valid(Vector2.right))
{
dest = (Vector2)transform.position + Vector2.right;
}
Vector2 dir = dest - (Vector2)transform.position;
GetComponent<Animator>().SetFloat("DirX" , dir.x);
GetComponent<Animator>().SetFloat("DirY" , dir.y);
}
}
public void Restart (){
gameObject.transform.position = StartPos;
dest = transform.position;
}
private bool Valid (Vector2 dir)
{
Vector2 pos = transform.position;
RaycastHit2D hit = Physics2D.Linecast(pos + dir, pos);
return (hit.collider == GetComponent<Collider2D>());
}
}
Pacdot.cs(豆子相关功能)
using UnityEngine;
public class Pacdot : MonoBehaviour
{
public bool isSuperPacDot = false ;
public GameObject scoretext;
public GameObject remaindots;
private void OnTriggerEnter2D (Collider2D collision){
if (collision.gameObject.name == "Pacman" ){
if (isSuperPacDot) {
GameManager.Instance.EatPacdot (gameObject);
GameManager.Instance.EatSuperPacdot ();
gameObject.SetActive (false );
isSuperPacDot = false ;
gameObject.transform.localScale = new Vector3 (1 ,1 ,1 );
GameManager.Instance.score += 100 ;
}
else {
GameManager.Instance.EatPacdot (gameObject);
gameObject.SetActive (false );
GameManager.Instance.score += 10 ;
}
}
}
}
ScreenWindowSetting.cs(游戏运行窗口的设置)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScreenWindowSetting : MonoBehaviour {
void Start () {
Screen.SetResolution (1024 ,768 ,false );
}
void Update () {
}
}