智能巡逻兵
玩家通过上下左右方向箭头来控制玩家移动,地图每个隔间都有一个巡逻兵,靠近巡逻兵的时候,巡逻兵会追击玩家,甩掉巡逻兵,分数加1,若被巡逻兵追到,则游戏结束
使用动作分离,MVC模式和工厂模式,订阅与发表模式
玩家
巡逻兵
UserAction类中主要为GUI和场景控制器交互的的方法,SSActionCallback中为运动控制器的回调函数
namespace Interfaces { public interface ISceneController { void LoadResources(); } public interface UserAction { int GetScore(); void Restart(); bool GetGameState(); void MovePlayer(float translationX, float translationZ); } public enum SSActionEventType : int { Started, Completed } public interface SSActionCallback { void SSActionCallback(SSAction source); } }FirstSceneController类继承了接口ISceneController和UserAction,并且在其中实现了接口声明的函数。场景控制器作为订阅者,初始化时将自身相应事件处理函数提交给消息处理器,在相应事件发生时被自动调用
public class FirstSceneController : MonoBehaviour, ISceneController, UserAction { GameObject player = null; PropFactory PF; int score = 0; int PlayerArea = 4; bool gameState = false; Dictionary<int, GameObject> allProp = null; CCActionManager CCManager = null; void Awake() { SSDirector director = SSDirector.getInstance(); director.currentScenceController = this; PF = PropFactory.PF; if(CCManager == null) CCManager = gameObject.AddComponent<CCActionManager>(); if (player == null && allProp == null) { Instantiate(Resources.Load<GameObject>("Prefabs/Plane"), new Vector3(0, 0, 0), Quaternion.identity); player = Instantiate(Resources.Load("Prefabs/Player"), new Vector3(0, 0, 0), Quaternion.identity) as GameObject; allProp = PF.GetProp(); } if (player.GetComponent<Rigidbody>()) { player.GetComponent<Rigidbody>().freezeRotation = true; } } void Update () { if (player.transform.localEulerAngles.x != 0 || player.transform.localEulerAngles.z != 0) { player.transform.localEulerAngles = new Vector3(0, player.transform.localEulerAngles.y, 0); } if (player.transform.position.y <= 0) { player.transform.position = new Vector3(player.transform.position.x, 0, player.transform.position.z); } } void OnEnable() { GameEventManager.ScoreChange += AddScore; GameEventManager.GameoverChange += Gameover; } void OnDisable() { ··· } public void LoadResources() { ··· } public int GetScore() { return score; } public void Restart() { player.GetComponent<Animator>().Play("New State"); PF.StopPatrol(); gameState = true; score = 0; player.transform.position = new Vector3(0, 0, 0); allProp[PlayerArea].GetComponent<Prop>().follow_player = true; CCManager.Tracert(allProp[PlayerArea], player); foreach (GameObject x in allProp.Values) { if (!x.GetComponent<Prop>().follow_player) { CCManager.GoAround(x); } } } public bool GetGameState() { return gameState; } public void SetPlayerArea(int x) { ··· } void AddScore() { ··· } void Gameover() { ··· } public void MovePlayer(float translationX, float translationZ) { if (gameState&&player!=null) { if (translationX != 0 || translationZ != 0) { player.GetComponent<Animator>().SetBool("run", true); } else { player.GetComponent<Animator>().SetBool("run", false); } player.transform.Translate(0, 0, translationZ * 4f * Time.deltaTime); player.transform.Rotate(0, translationX * 50f * Time.deltaTime, 0); } } }实现分数和时间显示,游戏结束显示重新开始
public class InterfaceGUI : MonoBehaviour { UserAction UserActionController; ISceneController SceneController; public GameObject t; bool ss = false; float S; void Start () { UserActionController = SSDirector.getInstance().currentScenceController as UserAction; SceneController = SSDirector.getInstance().currentScenceController as ISceneController; S = Time.time; } private void OnGUI() { if(!ss) S = Time.time; GUI.Label(new Rect(Screen.width -160, 30, 150, 30),"Score: " + UserActionController.GetScore().ToString() + " Time: " + ((int)(Time.time - S)).ToString()); if (ss) { if (!UserActionController.GetGameState()) { ss = false; } } else { if (GUI.Button(new Rect(Screen.width / 2 - 30, Screen.height / 2 - 30, 100, 50), "Start")) { ss = true; SceneController.LoadResources(); S = Time.time; UserActionController.Restart(); } } } private void Update() { float translationX = Input.GetAxis("Horizontal"); float translationZ = Input.GetAxis("Vertical"); UserActionController.MovePlayer(translationX, translationZ); } }有对象进入该区域的时候,判断是否为player,若是,则调用事件管理器发布玩家进入新区域的事件,巡逻兵发生碰撞时候,判断对象是否为player,若是,则调用事件管理器发布游戏结束
public class AreaCollide : MonoBehaviour { public int sign = 0; FirstSceneController sceneController; private void Start() { sceneController = SSDirector.getInstance().currentScenceController as FirstSceneController; } void OnTriggerEnter(Collider collider) { if (collider.gameObject.tag == "Player") { sceneController.SetPlayerArea(sign); GameEventManager.Instance.PlayerEscape(); } } }实现了巡逻兵的追踪Tracert与巡逻GoAround动作方法,通过回调函数来执行巡逻或在追踪结束进行巡逻
追踪动作:传入了追踪者和被追踪的对象也就是玩家对象。创建了追踪事件,在追上玩家或者追踪标志follow_player被置为false前一直追着玩家,发生碰撞事件,追踪者的标志会被场记设置为false)。
巡逻动作:选取在合理范围的位置,朝该位置移动,到达后调用回调函数。
public class CCActionManager : SSActionManager, SSActionCallback { public SSActionEventType Complete = SSActionEventType.Completed; Dictionary<int,CCMoveToAction> actionList = new Dictionary<int, CCMoveToAction>(); public void Tracert(GameObject p,GameObject player) { if (actionList.ContainsKey(p.GetComponent<Prop>().block)) actionList[p.GetComponent<Prop>().block].destroy = true; CCTracertAction action = CCTracertAction.getAction(player, 0.8f); addAction(p.gameObject, action, this); } public void GoAround(GameObject p) { CCMoveToAction action = CCMoveToAction.getAction(p.GetComponent<Prop>().block,0.6f,GetNewTarget(p)); actionList.Add(p.GetComponent<Prop>().block, action); addAction(p.gameObject, action, this); } private Vector3 GetNewTarget(GameObject p) { ··· } public void StopAll() { ··· } public void SSActionCallback(SSAction source) { if(actionList.ContainsKey(source.gameObject.GetComponent<Prop>().block)) actionList.Remove(source.gameObject.GetComponent<Prop>().block); GoAround(source.gameObject); } }演示视频 十分感谢学长的参考博客