一憔四、案例說明
本篇為PureMVC的進(jìn)階項(xiàng)目應(yīng)用牡肉,由于項(xiàng)目源碼還存在bug颗圣,完善后再分享出來利朵。
二待榔、框架視圖
三逞壁、關(guān)鍵代碼
Ctrl_EnemyMoving
/***
*
* Title: "炸彈人" 項(xiàng)目
* 主題: 控制層--> 敵人移動(dòng)
* 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>
/// 是否可以移動(dòng)
/// </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))
{
//復(fù)位
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: "炸彈人" 項(xiàng)目
* 主題: 控制層--> 道具
* 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>
/// 結(jié)束游戲
/// </summary>
public void StopGame()
{
_IsStartGame = false;
}
/// <summary>
/// 觸發(fā)檢測(cè)
/// </summary>
/// <param name="collision"></param>
public void OnTriggerEnter2D(Collider2D collision)
{
if (_IsStartGame)
{
if (collision.gameObject.tag==ProjectConsts.Player)
{
_proxyObj.AddScores();
}
}
}
}
}
Ctrl_GetTime
/***
*
* Title: "炸彈人" 項(xiàng)目
* 主題: 控制層--> 得到時(shí)間
* 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()
{
//得到模型層類的對(duì)象實(shí)例
//dataProxy = Facade.Instance.RetrieveProxy(Model_GameDataProxy.NAME) as Model_GameDataProxy;
IsStartGame = true;
}
/// <summary>
/// 游戲結(jié)束
/// </summary>
public void StopGame()
{
IsStartGame = false;
}
void Start () {
//得到模型層類的對(duì)象實(shí)例
dataProxy = Facade.Instance.RetrieveProxy(Model_GameDataProxy.NAME) as Model_GameDataProxy;
//啟動(dòng)協(xié)程流济,每隔一秒種,往PureMVC 模型層發(fā)送一條消息腌闯。
StartCoroutine("GetTime");
}
/// <summary>
/// 協(xié)程绳瘟,得到時(shí)間
/// </summary>
/// <returns></returns>
private IEnumerator GetTime()
{
yield return new WaitForEndOfFrame();
while (true)
{
yield return new WaitForSeconds(1F);
if (dataProxy != null && IsStartGame)
{
//調(diào)用(Mediator)模型層方法
dataProxy.AddGameTime();
}
}
}
}
}