unity3d开发巡逻兵游戏

阅读: 评论:0

unity3d开发巡逻兵游戏

unity3d开发巡逻兵游戏

游戏规则:
* 创建一个地图和若干巡逻兵(使用动画);
* 每个巡逻兵走一个3~5个边的凸多边型,位置数据是相对地址。即每次确定下一个目标位置,用自己当前位置为原点计算;
* 巡逻兵碰撞到障碍物,则会自动选下一个点为目标;
* 巡逻兵在设定范围内感知到玩家,会自动追击玩家;
* 失去玩家目标后,继续巡逻;
程序设计要求:
* 必须使用订阅与发布模式传消息
* 工厂模式生产巡逻兵

UML图

游戏截图

代码实现
巡逻兵创建
  1. 给巡逻兵添加两个预制体, Box Collider和Capsule Collider, BoxCollider使用trigger检测player进入巡逻兵的范围,Capsule Collider用来检测player与巡逻兵的碰撞
  2. PatrolData:巡逻兵数据
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public enum Dir { down, right, up, left}
public class PatrolData : MonoBehaviour {// Use this for initialization//巡逻兵的路线为一个正方形public Vector3 startPostion; //巡逻的起始位置public Vector3 endPostion;//正方形对角线位置public int sideLength; //正方形边长public int dir = 0;public GameObject player; //玩家public int sign; //巡逻兵在哪一块区域
}

PatrolFactory创建巡逻兵的工厂

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class PatrolFactory : MonoBehaviour {private List<GameObject> patrols = new List<GameObject>();// Use this for initializationpublic List<GameObject> GetPatrols(){int rectLength; //正方形边长int[] pos_x = { 5, -2, -9 };int[] pos_z = { 5, -2, -9 };int index = 1;for (int i = 0; i < 3; ++i){for (int j = 0; j < 3; ++j){Vector3 startPostion = new Vector3(pos_x[i], 0, pos_z[j]);rectLength = UnityEngine.Random.Range(3, 5);Vector3 endPostion = new Vector3(pos_x[i] + rectLength, 0, pos_z[j] + rectLength);GameObject monster =  Instantiate(Resources.Load<GameObject>("Prefabs/Monster"));ansform.position = startPostion;PatrolData data =  monster.AddComponent<PatrolData>();data.startPostion = dPostion = endPostion;data.sideLength = rectLength;data.sign = index;patrols.Add(monster);++index;}}return patrols;}
}

巡逻兵追捕与巡逻动作
PatrolGoAction

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class PatrolGoAction : SSAction {private Vector3 start; //巡逻兵的起始位置private Vector3 end;//巡逻兵起始位置的正方形对角线位置private Vector3 next;private int dir; //方向:0 x+, 1:z+, 2:x-,3:z-// Use this for initializationpublic static SSAction getPatrolGoAction(){PatrolGoAction action = ScriptableObject.CreateInstance<PatrolGoAction>();return action;}public override void Start () {PatrolData data = gameObject.GetComponent<PatrolData>();dir = 0;start = data.startPostion;end = dPostion;// transform.localEulerAngles =new  Vector3(0, 90, 0);next = new Vector3(end.x,0,start.z);transform.LookAt(next);able = true;this.distroy = false;}// Update is called once per framepublic override void Update () {//防止碰撞发生后的旋转if (transform.localEulerAngles.x != 0 || transform.localEulerAngles.z != 0){transform.localEulerAngles = new Vector3(0, transform.localEulerAngles.y, 0);}if (transform.position.y != 0){transform.position = new Vector3(transform.position.x, 0, transform.position.z);}//移动transform.position = Vector3.MoveTowards(transform.position, next, Time.deltaTime * 2f);gameObject.GetComponent<Animator>().SetBool("walk", true);GameObject player = gameObject.GetComponent<PatrolData>().player;if (player !=null && player.GetComponent<PlayerData>().area == gameObject.GetComponent<PatrolData>().sign ){this.callback.SSActionEvent(this, 0, this.gameObject);}if (Vector3.Distance(transform.position, next) < 0.1){dir = (dir + 1) % 4;if (dir == 1){next = new Vector3(end.x, 0, end.z);}else if (dir == 2){next = new Vector3(start.x, 0, end.z);}else if (dir == 3){next = new Vector3(start.x, 0, start.z);}else{next = new Vector3(end.x, 0, start.z);}transform.LookAt(next);}}
}

PatrolFollowAction

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class PatrolFollowAction : SSAction {public GameObject player;// Use this for initializationpublic override void Start () {this.distroy = able = true;}// Update is called once per framepublic override void Update () {//防止碰撞发生后的旋转if (transform.localEulerAngles.x != 0 || transform.localEulerAngles.z != 0){transform.localEulerAngles = new Vector3(0, transform.localEulerAngles.y, 0);}if (transform.position.y != 0){transform.position = new Vector3(transform.position.x, 0, transform.position.z);}transform.ansform.position);transform.position = Vector3.MoveTowards(transform.position, ansform.position, Time.deltaTime * 1.5f);gameObject.GetComponent<Animator>().SetBool("walk", true);player = gameObject.GetComponent<PatrolData>().player;if(player == null || player.GetComponent<PlayerData>().area != gameObject.GetComponent<PatrolData>().sign){this.callback.SSActionEvent(this, 1, this.gameObject);}}public static SSAction GetPatrolFollowAction(GameObject player){PatrolFollowAction action = ScriptableObject.CreateInstance<PatrolFollowAction>();action.player = player;return action;}
}
玩家数据

PlayerData

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class PlayerData : MonoBehaviour {public int area = 1; //玩家位于地图那一块区域
}
SSActionManager与PatrolActionManager

SSActionManager

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class SSActionManager : MonoBehaviour,ISSActionCallback {protected Dictionary<int, SSAction> actions = new Dictionary<int, SSAction>();protected List<SSAction> waitingAdd = new List<SSAction>();protected List<int> waitingDelete = new List<int>();protected void Update(){foreach (SSAction ac in waitingAdd){actions[ac.GetInstanceID()] = ac;}waitingAdd.Clear();foreach (KeyValuePair<int, SSAction> kv in actions){SSAction ac = kv.Value;if (ac.distroy){waitingDelete.Add(ac.GetInstanceID());}else if (ac.enable){ac.Update();}}foreach (int key in waitingDelete){SSAction ac = actions[key];actions.Remove(key);DestroyObject(ac);}waitingDelete.Clear();}public void DestroyAllAction(){foreach (KeyValuePair<int, SSAction> kv in actions){SSAction ac = kv.Value;ac.gameObject.GetComponent<Animator>().SetBool("walk", false);ac.distroy = true;}}public void RunAction(GameObject gameobject, SSAction action, ISSActionCallback manager){action.gameObject = ansform = ansform;action.callback = manager;waitingAdd.Add(action);action.Start();}protected void Start(){}public void SSActionEvent(SSAction source, int intParam = 0, GameObject objectParam = null){if (intParam == 0){//跟踪玩家source.distroy = true;GameObject player = objectParam.GetComponent<PatrolData>().player;SSAction follow= PatrolFollowAction.GetPatrolFollowAction(player);this.RunAction(objectParam,follow, this);}else if(intParam == 1) {//继续巡逻source.distroy = true;objectParam.GetComponent<PatrolData>().player = null;SSAction go = PatrolGoAction();this.RunAction(objectParam, go, this);}}
}

PatrolActionManager

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class PatrolActionManager : SSActionManager {// Use this for initializationpublic void StartGoAction(GameObject gameObject){SSAction action = PatrolGoAction();this.RunAction(gameObject, action, this);}}
事件发布器EventPublisher
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class EventPublisher : MonoBehaviour {//分数实践public delegate void ScoreEvent();public static event ScoreEvent ScoreAddEvent;public delegate void GameEvent();public static event GameEvent GameOverEvent;// Use this for initializationpublic void ScoreAdd(){if(ScoreAddEvent!=null){ScoreAddEvent();}}public void GameOver(){if(GameOverEvent!=null){GameOverEvent();}}
}
基本代码

FirstSceneController

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class FirstSceneController : MonoBehaviour, SceneController,IUserAction {private PatrolActionManager actionManger;public bool game_over;private ScoreRecorder recorder;private UserGUI userGUI;public EventPublisher publisher;public GameObject player;public GameObject map;private List<GameObject> patrolsList;public int area;public void loadResources(){map = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/map"));player = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/Player"));player.AddComponent<PlayerData>();actionManger = gameObject.AddComponent<PatrolActionManager>();recorder = gameObject.AddComponent<ScoreRecorder>();publisher = gameObject.AddComponent<EventPublisher>();userGUI = gameObject.AddComponent<UserGUI>();Instance().currentSceneController = this;PatrolFactory patrols = gameObject.AddComponent<PatrolFactory>();patrolsList = patrols.GetPatrols();}private void Awake(){loadResources();}public void StartGame(){for (int i = 0; i < patrolsList.Count; ++i){actionManger.StartGoAction(patrolsList[i]);}}void Start () {}void Update () {player.GetComponent<PlayerData>().area = area;}private void OnEnable(){EventPublisher.ScoreAddEvent += AddScore;EventPublisher.GameOverEvent += GameOver;}public void MovePlayer(){if(!game_over){float horizontal = Input.GetAxis("Horizontal"); //A D 左右float vertical = Input.GetAxis("Vertical"); //W S 上 下ansform.Translate(Vector3.forward * vertical * 3 * Time.deltaTime);//W S 上 下if (vertical != 0){player.GetComponent<Animator>().SetBool("run", true);}else{player.GetComponent<Animator>().SetBool("run", false);}ansform.Rotate(0, horizontal * 50 * Time.deltaTime, 0);}}void AddScore(){recorder.addRecord();}void GameOver(){game_over = true;actionManger.DestroyAllAction();}public int GetScore(){return recorder.score;}
}

本文发布于:2024-01-27 18:56:21,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/17063529832016.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:巡逻兵   游戏   unity3d
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23