游戏开发之Unity学习(七)——自动巡逻兵(订阅与发布模式)

游戏设计要求:

  • 创建一个地图和若干巡逻兵(使用动画);
  • 每个巡逻兵走一个3~5个边的凸多边型,位置数据是相对地址。即每次确定下一个目标位置用自己当前位置为原点计算;
  • 巡逻兵碰撞到障碍物,则会自动选下一个点为目标;
  • 巡逻兵在设定范围内感知到玩家,会自动追击玩家;
  • 失去玩家目标后,继续巡逻;
  • 计分:玩家每次甩掉一个巡逻兵计一分,与巡逻兵碰撞游戏结束;

游戏设计UML图

这里写图片描述
沿用牧师与魔鬼动作管理器的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<Animator>();
            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<GameObject> patrols = new List<GameObject>();
    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<PatrolFactory>();
        pf = Singleton<PatrolFactory>.Instance;
        this.gameObject.AddComponent<UserGUI>();
        director.currentSceneController.GenGameObjects();
        this.gameObject.AddComponent<CCActionManager>();
    }
    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<GameObject>(Resources.Load<GameObject>("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<GameObject>(Resources.Load<GameObject>("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<Rigidbody>())
                    {
                        player.GetComponent<Rigidbody>().freezeRotation = true;
                    }
                }
                else
                {
                    GameObject trigger = Instantiate<GameObject>(Resources.Load<GameObject>("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<CCMoveToAction> seq = new List<CCMoveToAction>();
    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<Animator>().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<Animator>().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<UserAction>();
        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<Animator>().SetBool("running", true);
            }
            else
            {
                gameObject.GetComponent<Animator>().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
这里写图片描述


更多更详细的代码请戳传送门

猜你喜欢

转载自blog.csdn.net/JC2474223242/article/details/80279995
今日推荐