智能巡逻兵
玩家通过上下左右方向箭头来控制玩家移动,地图每个隔间都有一个巡逻兵,靠近巡逻兵的时候,巡逻兵会追击玩家,甩掉巡逻兵,分数加1,若被巡逻兵追到,则游戏结束
public class PropFactory {
public static PropFactory PF = new PropFactory();
private Dictionary used = new Dictionary();
private PropFactory(){}
int[] pos_x = { -7, 1, 7 };
int[] pos_z = { 8, 2, -8 };
public Dictionary GetProp()
{
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
GameObject newProp = GameObject.Instantiate(Resources.Load("Prefabs/Prop"));
newProp.AddComponent();
newProp.transform.position = new Vector3(pos_x[j], 0, pos_z[i]);
newProp.GetComponent().block = i * 3 + j;
newProp.SetActive(true);
used.Add(i*3+j, newProp);
}
}
return used;
}
public void StopPatrol()
{
for (int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
used[i * 3 + j].transform.position = new Vector3(pos_x[j], 0, pos_z[i]);
}
}
}
}
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 allProp = null;
CCActionManager CCManager = null;
void Awake()
{
SSDirector director = SSDirector.getInstance();
director.currentScenceController = this;
PF = PropFactory.PF;
if(CCManager == null) CCManager = gameObject.AddComponent();
if (player == null && allProp == null)
{
Instantiate(Resources.Load("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())
{
player.GetComponent().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().Play("New State");
PF.StopPatrol();
gameState = true;
score = 0;
player.transform.position = new Vector3(0, 0, 0);
allProp[PlayerArea].GetComponent().follow_player = true;
CCManager.Tracert(allProp[PlayerArea], player);
foreach (GameObject x in allProp.Values)
{
if (!x.GetComponent().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().SetBool("run", true);
}
else
{
player.GetComponent().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);
}
}
public class GameEventManager
{
public static GameEventManager Instance = new GameEventManager();
public delegate void ScoreEvent();
public static event ScoreEvent ScoreChange;
public delegate void GameoverEvent();
public static event GameoverEvent GameoverChange;
private GameEventManager() { }
public void PlayerEscape()
{
if (ScoreChange != null)
{
ScoreChange();
}
}
public void PlayerGameover()
{
if (GameoverChange != null)
{
GameoverChange();
}
}
}
有对象进入该区域的时候,判断是否为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();
}
}
}
public class PlayerCollide : MonoBehaviour
{
void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Player")
{
GameEventManager.Instance.PlayerGameover();
}
}
}
实现了巡逻兵的追踪Tracert与巡逻GoAround动作方法,通过回调函数来执行巡逻或在追踪结束进行巡逻
追踪动作:传入了追踪者和被追踪的对象也就是玩家对象。创建了追踪事件,在追上玩家或者追踪标志follow_player
被置为false
前一直追着玩家,发生碰撞事件,追踪者的标志会被场记设置为false)。
巡逻动作:选取在合理范围的位置,朝该位置移动,到达后调用回调函数。
public class CCActionManager : SSActionManager, SSActionCallback
{
public SSActionEventType Complete = SSActionEventType.Completed;
Dictionary actionList = new Dictionary();
public void Tracert(GameObject p,GameObject player)
{
if (actionList.ContainsKey(p.GetComponent().block)) actionList[p.GetComponent().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().block,0.6f,GetNewTarget(p));
actionList.Add(p.GetComponent().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().block)) actionList.Remove(source.gameObject.GetComponent().block);
GoAround(source.gameObject);
}
}
演示视频
十分感谢学长的参考博客