本文共 13341 字,大约阅读时间需要 44 分钟。
链接: https://pan.baidu.com/s/1DEeuIeuJ_0AtOuErshtcFA 提取码: byid
/**************************************************** 文件:GameStart.cs 作者:唐孝辉 邮箱: 1351105506@qq.com 日期:2019/9/1 17:52:48 功能:游戏入口*****************************************************/using SUIFW;using UnityEngine;public class GameStart : MonoBehaviour { private void Awake() { UIManager.GetInstance().ShowUIForms("StartGameForm"); } void Start() { //启动MVC框架 new ApplicationFacade(); }}
/**************************************************** 文件:ApplicationFacade.cs 作者:唐孝辉 邮箱: 1351105506@qq.com 日期:2019/9/1 17:52:31 功能:全局管理类*****************************************************/using PureMVC.Patterns;using UnityEngine;public class ApplicationFacade : Facade{ public ApplicationFacade() { //注册命令 RegisterCommand("startGame",typeof(StartGameComand)); RegisterCommand("endGame", typeof(EndGameComand)); AddScript(); } //添加脚本 private void AddScript() { GameObject.FindGameObjectWithTag("Player").AddComponent(); GameObject.FindGameObjectWithTag("GameController").transform.Find("GameLanding").gameObject .AddComponent (); GameObject.FindGameObjectWithTag("GameController").transform.Find("GamePipes").gameObject .AddComponent (); GameObject.FindGameObjectWithTag("GameController").AddComponent (); }}
/**************************************************** 文件:StartGameComand.cs 作者:唐孝辉 邮箱: 1351105506@qq.com 日期:2019/9/2 14:53:34 功能:开始游戏*****************************************************/using PureMVC.Patterns;using UnityEngine;public class StartGameComand : MacroCommand{ protected override void InitializeMacroCommand() { //注册模型和视图 AddSubCommand(typeof(RegisterModelAndVieComand)); //注册重新开始 AddSubCommand(typeof(RestartGameComand)); }}
/**************************************************** 文件:RestartGameComand.cs 作者:唐孝辉 邮箱: 1351105506@qq.com 日期:2019/9/2 17:53:28 功能:重新开始*****************************************************/using PureMVC.Interfaces;using PureMVC.Patterns;using UnityEngine;public class RestartGameComand : SimpleCommand { //重新开始 public override void Execute(INotification notification) { GameObject.FindGameObjectWithTag("Player").GetComponent().GameStart(); GameObject.FindGameObjectWithTag("GameController").transform.Find("GamePipes").gameObject .GetComponent ().StarGame(); GameObject.FindGameObjectWithTag("GameController").GetComponent ().StartGame(); }}
/**************************************************** 文件:RegisterModelAndVieComand.cs 作者:唐孝辉 邮箱: 1351105506@qq.com 日期:2019/9/2 17:58:58 功能:注册*****************************************************/using PureMVC.Interfaces;using PureMVC.Patterns;using UnityEngine;public class RegisterModelAndVieComand : SimpleCommand{ //注册模型和视图 public override void Execute(INotification notification) { Facade.RegisterProxy(new GameDateProxy()); Facade.RegisterMediator(new GamePlayinMediator()); }}
/**************************************************** 文件:GameDate.cs 作者:唐孝辉 邮箱: 1351105506@qq.com 日期:2019/9/2 13:46:35 功能:游戏数据*****************************************************/public class GameDate{ public int Socre=0; public int MaxScore=0; public int Time = 0;}
/**************************************************** 文件:GameDateProxy.cs 作者:唐孝辉 邮箱: 1351105506@qq.com 日期:2019/9/2 13:46:49 功能:数据代理类*****************************************************/using PureMVC.Patterns;using UnityEngine;public class GameDateProxy :Proxy{ public new const string NAME = "GameDateProxy"; private GameDate date=null; public GameDateProxy():base(NAME) { this.date = new GameDate(); date.MaxScore = PlayerPrefs.GetInt("MaxScore"); } public void StopTime() { date.Time=0; //给视图层发送消息 SendNotification("DisPlayGame", date); } //增加时间 public void AddTime() { ++date.Time; //给视图层发送消息 SendNotification("DisPlayGameInfo",date); } //增加分数 public void AddScore() { ++date.Socre; //更新最高分数 GetMaxScore(); //给视图层发送消息 SendNotification("DisPlayGameInfo", date); } //获取最大分数 public int GetMaxScore() { if (date.Socre>date.MaxScore) { date.MaxScore = date.Socre; } return date.MaxScore; } //保存最高分数 游戏结束时候comand调用 public void SaveMaxScore() { if (date.MaxScore> PlayerPrefs.GetInt("MaxScore")) { PlayerPrefs.SetInt("MaxScore", date.MaxScore); } }}
/**************************************************** 文件:EndGameComand.cs 作者:唐孝辉 邮箱: 1351105506@qq.com 日期:2019/9/2 17:53:3 功能:游戏结束*****************************************************/using PureMVC.Interfaces;using PureMVC.Patterns;using SUIFW;using UnityEngine;public class EndGameComand : SimpleCommand{ //停止游戏 public override void Execute(INotification notification) { GameObject.FindGameObjectWithTag("Player").AddComponent().GameStop(); GameObject.FindGameObjectWithTag("GameController").transform.Find("GamePipes").gameObject .GetComponent ().StopGame(); UIManager.GetInstance().CloseUIForms("GamePlayForm"); GameObject.FindGameObjectWithTag("GameController").GetComponent ().StopGame(); //保存最高分 GameDateProxy proxy= PureMVC.Patterns.Facade.Instance.RetrieveProxy(GameDateProxy.NAME) as GameDateProxy; proxy.SaveMaxScore(); }}
/**************************************************** 文件:GamePlayinMediator.cs 作者:唐孝辉 邮箱: 1351105506@qq.com 日期:2019/9/2 17:50:50 功能:视图层游戏运行中*****************************************************/using System.Collections.Generic;using PureMVC.Interfaces;using PureMVC.Patterns;using UnityEngine;using UnityEngine.UI;public class GamePlayinMediator : Mediator { public new const string NAME = "GamePlayinMediator"; private Text time; private Text score; private Text maxSocre; public GamePlayinMediator():base(NAME) { Transform canvas = GameObject.FindGameObjectWithTag("_TagCanvas").transform; Transform gamePlayForm = canvas.transform.Find("Normal/GamePlayForm(Clone)").transform; time=gamePlayForm.Find("Time").GetComponent(); maxSocre = gamePlayForm.Find("Max").GetComponent (); score=gamePlayForm.Find("Socre").GetComponent (); } public override IList ListNotificationInterests() { IList list=new List (); list.Add("DisPlayGameInfo"); list.Add("DisPlayGame"); return list; } public override void HandleNotification(INotification notification) { switch (notification.Name) { case "DisPlayGameInfo": GameDate date= notification.Body as GameDate; time.text = date.Time.ToString(); score.text = date.Socre.ToString(); maxSocre.text = date.MaxScore.ToString(); break; case "DisPlayGame": time.text = 0.ToString(); break; } }}
/**************************************************** 文件:BirdController.cs 作者:唐孝辉 邮箱: 1351105506@qq.com 日期:2019/9/1 22:35:39 功能:控制小鸟飞行*****************************************************/using System.Collections;using UnityEngine;[RequireComponent(typeof(Rigidbody2D))]public class BirdController : MonoBehaviour{ public static BirdController instance; private Rigidbody2D rigidbody2D; private float upPower = 3f; private Vector2 postion; public bool gameStart = false; public void GameStart() { gameStart = true; rigidbody2D.isKinematic = false; this.transform.position = postion; } public void GameStop() { gameStart = false; this.transform.position = postion; UnEnable(); } void Start() { instance = this; rigidbody2D = this.GetComponent(); postion = this.transform.position; UnEnable(); } private void UnEnable() { //去重力效果 物体停止 this.GetComponent ().isKinematic = true; } void Update() { if (gameStart) { if (Input.GetButton("Fire1")) { rigidbody2D.velocity=Vector2.up*upPower; } if (Input.GetButton("Horizontal")) { rigidbody2D.velocity = Vector2.right * upPower; } } }}
/**************************************************** 文件:LandMove.cs 作者:唐孝辉 邮箱: 1351105506@qq.com 日期:2019/9/2 15:51:9 功能:地面移动*****************************************************/using UnityEngine;public class LandMove : MonoBehaviour{ private Vector3 postion; private float speed = 2.0f; void Start() { postion = this.transform.position; } void Update() { if (this.transform.position.x<-8f) { this.transform.position = postion; } this.transform.Translate(Vector3.left*Time.deltaTime*speed); }}
/**************************************************** 文件:Pipes.cs 作者:唐孝辉 邮箱: 1351105506@qq.com 日期:2019/9/2 19:7:24 功能:碰撞检测*****************************************************/using PureMVC.Patterns;using UnityEngine;public class Pipes : MonoBehaviour { private void OnCollisionEnter2D(Collision2D collision) { if (collision.collider.tag=="Player") { Facade.Instance.SendNotification("endGame"); } }}
/**************************************************** 文件:PipesMove.cs 作者:唐孝辉 邮箱: 1351105506@qq.com 日期:2019/9/2 18:43:20 功能:管道移动*****************************************************/using UnityEngine;public class PipesMove : MonoBehaviour { private Vector3 postion; private float speed = 1.7f; private bool starGame = false; void Start() { postion = this.transform.position; } public void StarGame() { starGame = true; } public void StopGame() { starGame = false; this.transform.position = postion; } void Update() { if (starGame) { if (this.transform.position.x < -20f) { this.transform.position = postion; } this.transform.Translate(Vector3.left * Time.deltaTime * speed); } }}
/**************************************************** 文件:GetSocre.cs 作者:唐孝辉 邮箱: 1351105506@qq.com 日期:2019/9/2 22:49:40 功能:触发器*****************************************************/using PureMVC.Patterns;using UnityEngine;public class GetSocre : MonoBehaviour { private GameDateProxy proxy = null; private void OnTriggerEnter2D(Collider2D collision) { if (collision.tag == "Player") { proxy = Facade.Instance.RetrieveProxy(GameDateProxy.NAME) as GameDateProxy; proxy.AddScore(); } }}
/**************************************************** 文件:GetTime.cs 作者:唐孝辉 邮箱: 1351105506@qq.com 日期:2019/9/2 22:18:43 功能:得到时间*****************************************************/using System.Collections;using PureMVC.Patterns;using UnityEngine;public class GetTime : MonoBehaviour{ private bool isStart = false; private GameDateProxy proxy = null; public void StartGame() { proxy=Facade.Instance.RetrieveProxy(GameDateProxy.NAME) as GameDateProxy; isStart = true; StartCoroutine(AddTime()); } public void StopGame() { proxy = Facade.Instance.RetrieveProxy(GameDateProxy.NAME) as GameDateProxy; isStart = false; proxy.StopTime(); } IEnumerator AddTime() { yield return new WaitForSeconds(0); //发送时间 while (true) { yield return new WaitForSeconds(1f); if (isStart&&proxy!=null) { proxy.AddTime(); } } }}
/**************************************************** 文件:StartGameForm.cs 作者:唐孝辉 邮箱: 1351105506@qq.com 日期:2019/9/1 19:32:44 功能:开始游戏*****************************************************/using SUIFW;using UnityEngine;public class StartGameForm : BaseUIForm{ void Awake() { RigisterButtonObjectEvent("bgImage", (GameObject go) => { OpenUIForm("GameGuideForm");}); }}
/**************************************************** 文件:GameGuideForm.cs 作者:唐孝辉 邮箱: 1351105506@qq.com 日期:2019/9/1 19:35:42 功能:游戏介绍*****************************************************/using PureMVC.Patterns;using SUIFW;using UnityEngine;public class GameGuideForm : BaseUIForm{ void Awake() { CurrentUIType.UIForms_ShowMode = UIFormShowMode.HideOther; RigisterButtonObjectEvent("GuideBtn", (GameObject go) => { OpenUIForm("GamePlayForm"); //发送游戏开始事件 Facade.Instance.SendNotification("startGame"); }); }}
/**************************************************** 文件:GamePlayForm.cs 作者:唐孝辉 邮箱: 1351105506@qq.com 日期:2019/9/1 19:38:42 功能:游戏ing*****************************************************/using SUIFW;using UnityEngine;public class GamePlayForm :BaseUIForm{ void Awake() { CurrentUIType.UIForms_ShowMode = UIFormShowMode.HideOther; }}
转载地址:http://dzrxo.baihongyu.com/