学习目标:愤怒的小鸟核心玩法
游戏画面
项目结构目录
部分核心代码
using System.Collections;
using System.Collections.Generic;
using birds;
using utils;
using UnityEngine;public class GameManager : MonoBehaviour
{public static GameManager sInstance;private readonly List<Bird> birds = new List<Bird>(); //存放场景中的小鸟public List<Pig> pigs = new List<Pig>(); //存放场景中的小猪private float allScore; //总成绩[HideInInspector] public bool gameIsOver; //比赛是否结束private GameObject winUi; //胜利的UIprivate GameObject loseUi; //失败的UIprivate GameObject stars; //存放星星的物体public Vector3 originPos; //小鸟的初始位置public AudioClip birdWinAudio; //小鸟胜利的音乐private void Awake(){sInstance = this; //单例模式GameObject canvas = GameObject.Find("Canvas");winUi = canvas.transform.Find("WinPanel").gameObject;loseUi = canvas.transform.Find("LosePanel").gameObject;stars = winUi.transform.Find("bg").Find("stars").gameObject;}/// <summary>/// 这个时候去绑定小鸟们/// </summary>private void Start(){Transform birdsTransform = transform.parent.Find("birds"); //找到存放小鸟的游戏物体Vector2 springPos = new Vector2();for (int i = 0; i < birdsTransform.childCount; i++){if (i == 0){originPos = birdsTransform.GetChild(i).position; //设置一下位置springPos = birdsTransform.GetChild(i).GetComponent<SpringJoint2D>().connectedAnchor;}else{birdsTransform.GetChild(i).GetComponent<SpringJoint2D>().connectedAnchor = springPos;}birds.Add(birdsTransform.GetChild(i).GetComponent<Bird>()); //将小鸟添加进来allScore += birds[i].addScore;}Transform pigTransform = transform.parent.Find("pigs"); //找到存放小猪的游戏物体for (int i = 0; i < pigTransform.childCount; i++){pigs.Add(pigTransform.GetChild(i).GetComponent<Pig>()); //将小猪添加进去allScore += pigs[i].addScore; //所用的成绩加加}Transform blockTransform = transform.parent.Find("blocks"); //找到存放小猪的游戏物体for (int i = 0; i < blockTransform.childCount; i++){if (blockTransform.GetChild(i).GetComponent<Block>() != null)allScore += blockTransform.GetChild(i).GetComponent<Block>().addScore; //所有的成绩加加}print("游戏场景的所有成绩 allScore " + allScore);Initialized(); //初始化一下}private void Initialized(){for (int i = 0; i < birds.Count; i++){if (i == 0){birds[i].TriggerCollider.enabled = true; //触发碰撞器启用birds[i].Ready();birds[i].springJoint2D.enabled = true; //启用birds[i].enabled = true; //可用}else{birds[i].TriggerCollider.enabled = false; //触发碰撞器禁用birds[i].springJoint2D.enabled = false; //启用birds[i].enabled = false; //可用}}}/// <summary>/// 下一只小鸟/// </summary>public void NextBird(){if (gameIsOver) //游戏结束就返回return;if (birds.Count > 0){birds.RemoveAt(0); //移除第一只小鸟}if (pigs.Count > 0){if (birds.Count > 0) //还有小鸟,替换下一只小鸟{Initialized(); //替换小鸟}else{gameIsOver = true; //游戏结束loseUi.SetActive(true); //失败UI可见}}else{Win(); //游戏胜利}}/// <summary>/// 判断是否胜利/// </summary>public void IsSuccess(){if (!gameIsOver) //没有游戏结束{if (pigs.Count <= 0){if (birds.Count > 0 && birds[0].IsIdle()){Win();}}}}/// <summary>/// 胜利的效果/// </summary>private void Win(){gameIsOver = true; //游戏结束AudioUtil.sInstance.AudioPlay(birdWinAudio); //播放小鸟胜利音乐StartCoroutine("ShowBird"); //开启协程}/// <summary>/// 展示小鸟/// </summary>/// <returns></returns>private IEnumerator ShowBird(){if (birds.Count > 0){birds[0].enabled = true; //可用for (int i = 0; i < birds.Count; i++){yield return new WaitForSeconds(1f);birds[i].Win(); //播放胜利特效}}yield return new WaitForSeconds(1f); //休息1秒winUi.SetActive(true); //胜利了}/// <summary>/// 展示星星/// </summary>public void ShowStart(){int counts = 1; //用户得到的星星数if (Score.sInstance.getNowScore() / allScore > 3 / 4f)counts = 3; //三颗星else if (Score.sInstance.getNowScore() / allScore > 0.5f)counts = 2; //三颗星if (counts >= 2)StartCoroutine("Show", 3);else if (counts == 1)StartCoroutine("Show", 2);elseStartCoroutine("Show", 1);}/// <summary>/// 显示星星/// </summary>/// <param name="num">星星数目</param>/// <returns></returns>private IEnumerator Show(int num){saveStar(num); //存储一下for (int i = 0; i < num; i++){stars.transform.GetChild(i).gameObject.SetActive(true); //激活星星yield return new WaitForSeconds(0.3f);}}/// <summary>/// 将星星存储一下/// </summary>private void saveStar(int num){int starNum = PlayerPrefUtil.GetNowMapLevelStarNum(); //得到星星数if (num > starNum){PlayerPrefUtil.addTotalStar(num - starNum); //添加总星星PlayerPrefUtil.addNowMapTotalStar(num - starNum); //添加当前地图星星PlayerPrefUtil.SetNowMapLevelStarNum(num); //设置星星数}}
}
下载链接:AngerBird-master: 愤怒的小鸟