siki学院愤怒的小鸟脚本

GameManager.csusing System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.SceneManagement;public class GameManager : MonoBehaviour { public List<Bird>...
摘要由CSDN通过智能技术生成

GameManager.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour {
    public List<Bird> birds;//场景中的小鸟作为元素的List,实际上是小鸟物体的Bird组件作为元素
    public List<Break> pigs;//场景中的猪作为元素的List,实际上是猪的Break组件作为元素
    private bool Win = false,Lose = false;//赢和输状态波尔值
    public Vector3 BirdsBornPos;//下一只小鸟出生传送点
    private GameObject LoseResult;//输了之后要显示的界面
    private GameObject WinResult;//赢了之后要显示的界面
    private GameObject BackAppear;//输或赢都显示的背景暗淡,附带有一段Color动画
    public GameObject[] Stars;//评分用的三个星星的List
    private GameObject pausePanel;//暂停界面
    private GameObject pauseButton;//暂停按钮
    public static GameManager _instance;//对GameManager的单例化
    private int CurrentStars;//临时存储当前关卡过关后获得的星星数
    private int sum;//临时使用的整型统计变量
    private GameObject LevelResult;//一个Canvas物体
    private Camera UIcamera;//专用以渲染UI类物体的摄像机
    private void Awake()
    {

        _instance = this;//单例语句
        if (birds.Count > 0) BirdsBornPos = birds[0].gameObject.transform.position;//给小鸟出生瞬移点一个初始值
        LevelResult = GameObject.Find("LevelResult");
        UIcamera = GameObject.Find("UICamera").GetComponent<Camera>();//一些赋值
        BackAppear = LevelResult.transform.Find("BackAppear").gameObject;
        //查找active gameobject的hidden child gameobject
        WinResult = LevelResult.transform.Find("Win").gameObject;
        LoseResult = LevelResult.transform.Find("Lose").gameObject;
        BackAppear.transform.SetSiblingIndex(0);
        //设置backappear为子物体列表里第一个渲染的(最后面)/通过调整Hierarchy的层级排序也可以确定渲染的顺序,在上的先渲染

        pauseButton = GameObject.Find("Pause").transform.Find("pauseButton").gameObject;
        pausePanel = GameObject.Find("Pause").transform.Find("pausePanel").gameObject;
        //指定暂停按钮和面板物体

        Stars[0] = GameObject.Find("LevelResult").transform.Find("Win").transform.Find("Stars").transform.Find("left").gameObject;
        Stars[1] = GameObject.Find("LevelResult").transform.Find("Win").transform.Find("Stars").transform.Find("middle").gameObject;
        Stars[2] = GameObject.Find("LevelResult").transform.Find("Win").transform.Find("Stars").transform.Find("right").gameObject;
        //指定左中右三个胜利星星

        Canvas canvas = LevelResult.GetComponent<Canvas>();//先设定rendermode再设定相机
         canvas.renderMode= RenderMode.ScreenSpaceCamera;
        canvas.worldCamera = UIcamera;
        //设置canvas的渲染相机
    }
    private void Start()
    {
        Initialize();
        //if (BackAppear) Debug.Log("Found");
        //查找失败会返回一个空指针,否则是有值的
        //else Debug.Log("Missing");
    }
    public void Initialize()//重新对小鸟们的属性控制
    {
        for(int i = 0; i < birds.Count; i++)
        {
            if (i == 0)
            {
                birds[i].gameObject.transform.position = BirdsBornPos;
                birds[i].enabled = true;//对于component的可用性使用enabled
                birds[i].sp.enabled = true;//启用弹簧
            }
            else
            {
                birds[i].enabled = false;
                birds[i].sp.enabled = false;
            }
        }
    }
    public void JudgeLevelResult()
    {
        if (Win == false&&pigs.Count == 0)
        {
            Win = true;

            WinResult.SetActive(true);//对于gameobject的可用性使用setactive
            BackAppear.SetActive(true);
            StartCoroutine("ShowStars");//协程显示星星
        }
        if (Lose == false&&pigs.Count > 0 && birds.Count == 0)
        {
            Lose = true;
            LoseResult.SetActive(true);
            BackAppear.SetActive(true);//背景逐渐变黑,实质上是BackAppear物体的激活,关于它的动画是自动播放的

        }
    }

    IEnumerator  ShowStars()
    {

        for(CurrentStars = 0; CurrentStars < birds.Count&&CurrentStars<3; CurrentStars++)//显示星星不超过3个并且显示星星数是剩余小鸟的数量加一
        {
            yield return new WaitForSeconds(0.5f);//每个星星显示的间隔

            Stars[CurrentStars].SetActive(true);
        }

        SaveData();//显示完星星存储一下关卡星星的数据
    }

    private void SaveData()
    {

        if (CurrentStars > PlayerPrefs.GetInt(PlayerPrefs.GetString("CurrentTheme")+ PlayerPrefs.GetString("CurrentLevel")))//只有刷新过往记录才执行存储
        {
            PlayerPrefs.SetInt(PlayerPrefs.GetString("CurrentTheme")+PlayerPrefs.GetString("CurrentLevel"), CurrentStars);//向对应的“目前地图+目前关卡”键值存整型值
        }
        switch (PlayerPrefs.GetString("CurrentTheme"))//分类讨论
        {
            case "T1":
                sum = 0;
                for(int i = 1; i <= 12; i++)
                {

                    sum += PlayerPrefs.GetInt("T1"+"level"+i);
                }
                break;
            case "T2":
                sum = 0;
                for (int i = 1; i <= 12; i++)
                {

                    sum += PlayerPrefs.GetInt("T1" + "level" + i);
                }
                for (int i = 1; i <= 8; i++)
                {
                    sum += PlayerPrefs.GetInt("T2"+"level" + i);
                }
                break;
            case "T3":
                sum = 0;
                for (int i = 1; i <= 12; i++)
                {

                    sum += PlayerPrefs.GetInt("T1" + "level" + i);
                }
                for (int i = 1; i <= 8; i++)
                {
                    sum += PlayerPrefs.GetInt("T2" + "level" + i);
                }
                for (int i = 1; i <= 1; i++)
                {
                    sum += PlayerPrefs.GetInt("T3"+"level" + i);
                }
                break;//遍历已解锁的地图的所有关卡的星星数
        }
        PlayerPrefs.SetInt("CollectedNum", sum);//保存已收集星星总数,赋值计算好的sum

    }
    public void Pause()
    {
        Time.timeScale
  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值