沿用牧师与魔鬼动作管理器的UML图,适当修改几个类。加上一个工厂和碰撞动作即可。
发布订阅(pub/sub)是一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收消息。
Redis 客户端可以订阅任意数量的频道。
下图展示了频道 channel1 , 以及订阅这个频道的三个客户端 —— client2 、 client5 和 client1 之间的关系:
当有新消息通过 PUBLISH 命令发送给频道 channel1 时, 这个消息就会被发送给订阅它的三个客户端:
自动巡逻兵中因为要用到动画,所以先设置动画控制器,这是player的动画部分
下面是patrol的动画部分
触发器设计,作为预制放入资源中
巡逻兵设计,作为预制放入资源中
将下面的脚本挂载到触发器和巡逻兵上,实现玩家碰撞时的通知。
//ColiAction.cs
//该脚本挂载在触发器和巡逻兵上
using UnityEngine;
public class ColiAction : MonoBehaviour {
FirstSceneController sceneController;
GameObject myobject = null;
GameObject player;
public delegate void AddScore();
public static event AddScore myAddScore;
public delegate void GameOver();
public static event GameOver myGameOver;
private void Start()
{
sceneController = SSDirector.getInstance().currentSceneController as FirstSceneController;
player = sceneController.player;
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject == player)
{
int k = this.name[this.name.Length - 1] - '0';
myobject = sceneController.patrols[k];
foreach (var i in sceneController.actionManager.seq)
{
if (i.gameObject == myobject)
{
i.enable = false;
Vector3 a = new Vector3(myobject.transform.position.x, 0, myobject.transform.position.z);
Vector3 b = new Vector3(player.transform.position.x, 0, player.transform.position.z);
Quaternion rotation = Quaternion.LookRotation(b - a, Vector3.up);
myobject.transform.rotation = rotation;
}
}
}
}
private void OnTriggerExit(Collider other)
{
if (other.gameObject == player)
{
int k = this.name[this.name.Length - 1] - '0';
foreach (var i in sceneController.actionManager.seq)
{
if (i.gameObject == myobject)
{
i.enable = true;
Vector3 a = new Vector3(myobject.transform.position.x, 0, myobject.transform.position.z);
Vector3 b = new Vector3(i.target.x, 0, i.target.z);
Quaternion rotation = Quaternion.LookRotation(b - a, Vector3.up);
myobject.transform.rotation = rotation;
}
}
myobject = null;
myAddScore();
}
}
private void OnCollisionEnter(Collision collision)
{
if(this.tag == "patrol" && collision.gameObject == player)
{
myGameOver();
var k = collision.gameObject.GetComponent();
k.SetBool("death",true);
}
}
private void Update()
{
if (myobject != null && sceneController.flag == 0)
{
myobject.transform.position = Vector3.MoveTowards(myobject.transform.position, player.transform.position, 3 * Time.deltaTime);
}
}
}
将脚本挂载好之后,我们使用空对象和场景控制器载入场景。为了实现巡逻兵和触发器的一一对应,给触发器名字加上后缀编号,使用列表保存创建好的巡逻兵,这样,一个触发器就能恰好对应到相应的巡逻兵了。
//FirstSceneController.cs
//此脚本挂载到空对象上,在这个脚本里订阅ColiAction
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class FirstSceneController : MonoBehaviour, IUserAction, ISceneController{
public CCActionManager actionManager;
public GameObject player;
public List patrols = new List();
public PatrolFactory pf;
public int flag = 0;
public int score = 0;
private void Awake()
{
SSDirector director = SSDirector.getInstance();
director.setFPS(60);
director.currentSceneController = this;
this.gameObject.AddComponent();
pf = Singleton.Instance;
this.gameObject.AddComponent();
director.currentSceneController.GenGameObjects();
this.gameObject.AddComponent();
}
private void OnEnable()
{
ColiAction.myAddScore += AddScore;
ColiAction.myGameOver += GameOver;
}
private void OnDisable()
{
ColiAction.myAddScore -= AddScore;
ColiAction.myGameOver -= GameOver;
}
private void GameOver()
{
Pause();
flag = 1;
}
private void Start()
{
}
public void GenGameObjects ()
{
int count = 0;
GameObject plane = Instantiate(Resources.Load("Prefabs/Plane"));
plane.transform.parent = this.transform;
for(int i=0;i<3;++i)
{
for(int j=0;j<3;++j)
{
if (i == 0 && j == 2)
{
player = Instantiate(Resources.Load("Prefabs/player"));
player.transform.position = new Vector3(plane.transform.position.x + 9 * (i - 1), 0, plane.transform.position.z + 9 * (j - 1));
if (player.GetComponent())
{
player.GetComponent().freezeRotation = true;
}
}
else
{
GameObject trigger = Instantiate(Resources.Load("Prefabs/Trigger"));
trigger.transform.parent = plane.transform;
trigger.transform.position = new Vector3(plane.transform.position.x + 9 * (i - 1), 0, plane.transform.position.z + 10 * (j - 1));
trigger.name = "trigger" + count;
count++;
GameObject patrol = pf.GetPatrol();
patrol.transform.position = trigger.transform.position;
patrols.Add(patrol);
}
}
}
}
public void Restart()
{
SceneManager.LoadScene("1");
}
public void Pause ()
{
actionManager.Pause();
}
private void AddScore()
{
score++;
}
}
使用CCActionManager管理每个巡逻兵的巡逻动作和用户的输入
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CCActionManager : SSActionManager, ISSActionCallback {
public FirstSceneController sceneController;
public List seq = new List();
public UserAction userAction;
public PatrolFactory pf;
protected new void Start()
{
sceneController = (FirstSceneController)SSDirector.getInstance().currentSceneController;
userAction = UserAction.GetSSAction(5);
sceneController.actionManager = this;
pf = sceneController.pf;
RunAction(sceneController.player, userAction, this);
foreach (var i in sceneController.patrols)
{
float x = Random.Range(-3.0f, 3.0f);
int z = Random.Range(0, 4);
Vector3 target = new Vector3(z % 2 == 0 ? (z - 1) * 3 : x, 0, z % 2 != 0 ? (z - 2) * 3 : x);
CCMoveToAction k = CCMoveToAction.GetSSAction(target+i.transform.position,100,i.transform.position);
seq.Add(k);
Quaternion rotation = Quaternion.LookRotation(target, Vector3.up);
i.transform.rotation = rotation;
RunAction(i, k, this);
}
}
protected new void Update()
{
base.Update();
}
public void SSActionEvent(SSAction source, SSActionEventType events = SSActionEventType.Completed, int intParam = 0, string strParam = null, Object objParam = null)
{
if(source != userAction)
{
CCMoveToAction cCMoveTo = source as CCMoveToAction;
float x = Random.Range(-3.0f, 3.0f);
int z = Random.Range(0, 4);
Vector3 target = new Vector3(z % 2 == 0 ? (z - 1) * 3.0f : x, 0, z % 2 == 0 ? x : (z - 2) * 3.0f);
CCMoveToAction k = CCMoveToAction.GetSSAction(target + cCMoveTo.initPosition, 1.5f, cCMoveTo.initPosition);
seq.Remove(cCMoveTo);
source.destory = true;
seq.Add(k);
Quaternion rotation = Quaternion.LookRotation(target + cCMoveTo.initPosition - source.transform.position, Vector3.up);
source.transform.rotation = rotation;
RunAction(source.gameObject, k, this);
}
}
public void CheckEvent(SSAction source, SSActionEventType events = SSActionEventType.Completed, int intParam = 0, string strParam = null, Object objParam = null)
{
}
public void Pause()
{
if(sceneController.flag == 0)
{
foreach (var k in seq)
{
k.enable = false;
k.gameObject.GetComponent().SetBool("running", false);
}
userAction.enable = false;
sceneController.flag = 2;
}
else if(sceneController.flag == 2)
{
foreach (var k in seq)
{
k.enable = true;
k.gameObject.GetComponent().SetBool("running", true);
}
userAction.enable = true;
sceneController.flag = 0;
}
}
}
巡逻兵工厂和之前的几个博客写的基本相同,大概改改就可以了,详细代码这里略过,想看可以去文末的传送门看看。
用户输入动作脚本用来处理player的移动和动画:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UserAction : SSAction {
// Use this for initialization
private float speed;
private float rspeed = 90;
public static UserAction GetSSAction(float speed)
{
UserAction action = CreateInstance();
action.speed = speed;
return action;
}
public override void Start()
{
}
public override void Update()
{
if(enable)
{
float translationX = Input.GetAxis("Horizontal");
float translationZ = Input.GetAxis("Vertical");
if (translationX != 0 || translationZ != 0)
{
gameObject.GetComponent().SetBool("running", true);
}
else
{
gameObject.GetComponent().SetBool("running", false);
}
gameObject.transform.Translate(translationX * speed * Time.deltaTime * 0.1f, 0,translationZ*speed*Time.deltaTime);
gameObject.transform.Rotate(0, translationX * rspeed * Time.deltaTime, 0);
if (gameObject.transform.localEulerAngles.x != 0 || gameObject.transform.localEulerAngles.z != 0)
{
gameObject.transform.localEulerAngles = new Vector3(0, gameObject.transform.localEulerAngles.y, 0);
}
if (gameObject.transform.position.y != 0)
{
gameObject.transform.position = new Vector3(gameObject.transform.position.x, 0, gameObject.transform.position.z);
}
}
}
}
其他内容基本和之前的几篇博客相同,在这里就不复述了。
游戏演示视频地址:http://v.youku.com/v_show/id_XMzU5OTA0NzEyMA==.html?spm=a2hzp.8244740.0.0
更多更详细的代码请戳传送门