美文网首页
Unity3D开发技术研究构建框架PureMVC项目之炸弹人(无

Unity3D开发技术研究构建框架PureMVC项目之炸弹人(无

作者: TonyWan_AR | 来源:发表于2021-06-15 00:17 被阅读0次

    一、案例说明

    本篇为PureMVC的进阶项目应用,由于项目源码还存在bug,完善后再分享出来。
    

    二、框架视图

    三、关键代码

    Ctrl_EnemyMoving

    /***
     * 
     *    Title: "炸弹人" 项目
     *           主题: 控制层--> 敌人移动
     *    Description: 
     *           功能:
     *                  
     *    Date: 2021.6.2
     *    Version: 0.1版本
     *    Modify Recoder: 
     *    
     *   
     */
    using System.Collections;
    using System.Collections.Generic;
    using SUIFW;
    using UnityEngine;
    
    namespace PureMVCDemo
    {
        public class Ctrl_EnemyMoving : MonoBehaviour
        {
            private float speed = 0.04f;
            private Rigidbody2D rig;
            private SpriteRenderer spriteRenderer;
            private Color color;
            /// <summary>
            /// 方向:0上 1下 2左 3右
            /// </summary>
            private int dirId = 0;
            private Vector2 dirVector;
            private float rayDistance = 0.7f;
            /// <summary>
            /// 是否可以移动
            /// </summary>
            private bool canMove = true;
    
            private void Awake()
            {
                spriteRenderer = GetComponent<SpriteRenderer>();
                color = spriteRenderer.color;
                rig = GetComponent<Rigidbody2D>();
            }
            /// <summary>
            /// 初始化方法
            /// </summary>
            public void Init()
            {
                color.a = 1;
                spriteRenderer.color = color;
                canMove = true;
                InitDir(Random.Range(0, 4));
            }
            private void InitDir(int dir)
            {
                dirId = dir;
                switch (dirId)
                {
                    case 0:
                        dirVector = Vector2.up;
                        break;
                    case 1:
                        dirVector = Vector2.down;
                        break;
                    case 2:
                        dirVector = Vector2.left;
                        break;
                    case 3:
                        dirVector = Vector2.right;
                        break;
                    default:
                        break;
                }
            }
            private void Update()
            {
                if (canMove)
                    rig.MovePosition((Vector2)transform.position + dirVector * speed);
                else
                {
                    ChangeDir();
                }
            }
            private void OnTriggerEnter2D(Collider2D collision)
            {
                //消灭敌人
                if (collision.CompareTag(Tags.BombEffect) && gameObject.activeSelf)
                {
                    GameController.Instance.enemyCount--;
                    ObjectPool.Instance.Add(ObjectType.Enemy, gameObject);
                }
                if (collision.CompareTag(Tags.Enemy))
                {
                    color.a = 0.3f;
                    spriteRenderer.color = color;
                }
                if (canMove)
                    if (collision.CompareTag(Tags.SuperWall) || collision.CompareTag(Tags.Wall))
                    {
                        //复位
                        transform.position = new Vector2(Mathf.RoundToInt(transform.position.x),
                            Mathf.RoundToInt(transform.position.y));
                        ChangeDir();
                    }
            }
            private void OnTriggerStay2D(Collider2D collision)
            {
                if (collision.CompareTag(Tags.Enemy))
                {
                    color.a = 0.3f;
                    spriteRenderer.color = color;
                }
            }
            private void OnTriggerExit2D(Collider2D collision)
            {
                if (collision.CompareTag(Tags.Enemy))
                {
                    color.a = 1;
                    spriteRenderer.color = color;
                }
            }
            private void ChangeDir()
            {
                List<int> dirList = new List<int>();
                if (Physics2D.Raycast(transform.position, Vector2.up, rayDistance, 1 << 8) == false)
                {
                    dirList.Add(0);
                }
                if (Physics2D.Raycast(transform.position, Vector2.down, rayDistance, 1 << 8) == false)
                {
                    dirList.Add(1);
                }
                if (Physics2D.Raycast(transform.position, Vector2.left, rayDistance, 1 << 8) == false)
                {
                    dirList.Add(2);
                }
                if (Physics2D.Raycast(transform.position, Vector2.right, rayDistance, 1 << 8) == false)
                {
                    dirList.Add(3);
                }
                if (dirList.Count > 0)
                {
                    canMove = true;
                    int index = Random.Range(0, dirList.Count);
                    InitDir(dirList[index]);
                }
                else canMove = false;
            }
            private void OnDrawGizmos()
            {
                Gizmos.color = Color.red;
                Gizmos.DrawLine(transform.position, transform.position + new Vector3(0, rayDistance, 0));
                Gizmos.color = Color.blue;
                Gizmos.DrawLine(transform.position, transform.position + new Vector3(0, -rayDistance, 0));
                Gizmos.DrawLine(transform.position, transform.position + new Vector3(-rayDistance, 0, 0));
                Gizmos.DrawLine(transform.position, transform.position + new Vector3(rayDistance, 0, 0));
            }
        }
    }
    

    Ctrl_Prop

    /***
     * 
     *    Title: "炸弹人" 项目
     *           主题: 控制层--> 道具
     *    Description: 
     *           功能:
     *                  
     *    Date: 2021.6.2
     *    Version: 0.1版本
     *    Modify Recoder: 
     *    
     *   
     */
    using System.Collections;
    using System.Collections.Generic;
    using PureMVC.Patterns;
    using UnityEngine;
    
    namespace PureMVCDemo
    {
        public class Ctrl_Prop : MonoBehaviour {
            //模型代理
            private Model_GameDataProxy _proxyObj;  
            //是否开始
            private bool _IsStartGame = false;
    
    
    
            /// <summary>
            /// 开始游戏
            /// </summary>
            public void StartGame()
            {
                _proxyObj = Facade.Instance.RetrieveProxy(Model_GameDataProxy.NAME) as Model_GameDataProxy;
                //开始游戏
                _IsStartGame = true;
            }
    
            /// <summary>
            /// 结束游戏
            /// </summary>
            public void StopGame()
            {
                _IsStartGame = false;
            }
    
            /// <summary>
            /// 触发检测
            /// </summary>
            /// <param name="collision"></param>
            public void OnTriggerEnter2D(Collider2D collision)
            {
                if (_IsStartGame)
                {
                    if (collision.gameObject.tag==ProjectConsts.Player)
                    {
                        _proxyObj.AddScores();
                    }
    
                }
            }
        }
    }
    

    Ctrl_GetTime

    /***
     * 
     *    Title: "炸弹人" 项目
     *           主题: 控制层--> 得到时间
     *    Description: 
     *           功能:
     *                  
     *    Date: 2021.6.2
     *    Version: 0.1版本
     *    Modify Recoder: 
     *    
     *   
     */
    using System.Collections;
    using System.Collections.Generic;
    using PureMVC.Patterns;
    using UnityEngine;
    
    namespace PureMVCDemo
    {
        public class Ctrl_GetTime : MonoBehaviour {
            //模型代理
            public Model_GameDataProxy dataProxy = null;
            //游戏是否开始
            private bool IsStartGame = false;
    
    
            /// <summary>
            /// 游戏开始
            /// </summary>
            public void StartGame()
            {
                //得到模型层类的对象实例
                //dataProxy = Facade.Instance.RetrieveProxy(Model_GameDataProxy.NAME) as Model_GameDataProxy;
                IsStartGame = true;
            }
    
            /// <summary>
            /// 游戏结束
            /// </summary>
            public void StopGame()
            {
                IsStartGame = false;
            }
    
    
            void Start () {
                //得到模型层类的对象实例
                dataProxy = Facade.Instance.RetrieveProxy(Model_GameDataProxy.NAME) as Model_GameDataProxy;
                //启动协程,每隔一秒种,往PureMVC 模型层发送一条消息。
                StartCoroutine("GetTime");
            }
    
            /// <summary>
            /// 协程,得到时间
            /// </summary>
            /// <returns></returns>
            private IEnumerator GetTime()
            {
                yield return new WaitForEndOfFrame();
                while (true)
                {
                    yield return new WaitForSeconds(1F);
                    if (dataProxy != null && IsStartGame)
                    {
                        //调用(Mediator)模型层方法
                        dataProxy.AddGameTime();
                    }
                }
    
            }
    
    
    
        }
    }
    

    四、效果展示

    相关文章

      网友评论

          本文标题:Unity3D开发技术研究构建框架PureMVC项目之炸弹人(无

          本文链接:https://www.haomeiwen.com/subject/iqpaeltx.html