赞
踩
工厂模式(Factory Design Pattern)也是游戏开发中比较常用的创建型模式,一般情况下,工厂模式分为三种更加细分的类型:简单工厂、工厂方法和抽象工厂。
将实例化对象的代码提取出来,放到一个类中统一管理和维护,达到和主项目的依赖关系的解耦。从而提高项目的扩展和维护性。
使用WSAD或方向键上下左右移动player,进入巡逻兵的追捕后逃脱可积累一分,若与巡逻兵碰撞则游戏结束,收集完地图上的所有水晶即可获胜。
使用订阅与发布模式传消息
工厂模式生产巡逻兵
在网站Unity3D_模型_资源_商店(unity asset store)_免费下载 - 爱给网 (aigei.com)下载
工厂模式:PatrolFactory.cs
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class PatorlFactory : MonoBehaviour {
-
- //工厂模式就负责生产,其他的不负责,所以可以看到这个类只和角色有关系和其他所有代码都没有关系
- private GameObject player = null; //玩家
- private GameObject patrol = null; //巡逻兵
- private List<GameObject> patrolList = new List<GameObject>(); //正在被使用的巡逻兵
- private Vector3[] vec = new Vector3[9]; //保存每个巡逻兵的初始位置
-
- public GameObject LoadPlayer()
- {
- player = Instantiate(Resources.Load("Prefabs/Player"), new Vector3(0, 9, 0), Quaternion.identity) as GameObject;
- return player;
- }
-
- public List<GameObject> LoadPatrol()
- {
- int[] pos_x = { -6, 4, 13 };
- int[] pos_z = { -4, 6, -13 };
- int index = 0;
-
- for(int i=0;i < 3;i++)
- {
- for(int j=0;j < 3;j++)
- {
- vec[index] = new Vector3(pos_x[i], 0, pos_z[j]);
- index++;
- }
- }
- for(int i=0; i < 9; i++)
- {
- patrol = Instantiate(Resources.Load<GameObject>("Prefabs/Patrol1"));
- patrol.transform.position = vec[i];
- patrol.GetComponent<PatrolData>().sign = i + 1;
- patrol.GetComponent<PatrolData>().start_position = vec[i];
- patrolList.Add(patrol);
- }
- return patrolList;
- }
-
- //游戏结束时,暂停所有动作
- public void StopPatrol()
- {
- for (int i = 0; i < patrolList.Count; i++)
- {
- patrolList[i].gameObject.GetComponent<Animator>().SetBool("run", false);
- }
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
巡逻动作:PatrolAction.cs
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class GoPatrolAction : SSAction
- {
- private enum Dirction { E, N, W, S };
- private float pos_x, pos_z;
- private float move_length;
- private float move_speed = 1.2f;
- private bool move_sign = true;
- private Dirction dirction = Dirction.E;
- private PatrolData data;
-
- private GoPatrolAction() { }
- public static GoPatrolAction GetSSAction(Vector3 location)
- {
- GoPatrolAction action = CreateInstance<GoPatrolAction>();
- action.pos_x = location.x;
- action.pos_z = location.z;
- action.move_length = Random.Range(5, 8);
- return action;
- }
- public 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);
- Gopatrol();
- if (data.follow_player && data.wall_sign == data.sign)
- {
- this.destroy = true;
- this.callback.SSActionEvent(this,0,this.gameobject);
- }
- }
- public override void Start(){data = this.gameobject.GetComponent<PatrolData>();}
- void Gopatrol()
- {
- if (move_sign)
- {
- switch (dirction)
- {
- case Dirction.E:
- pos_x -= move_length;
-
- break;
- case Dirction.N:
- pos_z += move_length;
-
- break;
- case Dirction.W:
- pos_x += move_length;
-
- break;
- case Dirction.S:
- pos_z -= move_length;
-
- break;
- }
- move_sign = false;
- }
- this.transform.LookAt(new Vector3(pos_x, 0, pos_z));
- float distance = Vector3.Distance(transform.position, new Vector3(pos_x, 0, pos_z));
- if (distance > 0.9){transform.position = Vector3.MoveTowards(this.transform.position, new Vector3(pos_x, 0, pos_z), move_speed * Time.deltaTime);
- }else
- {
- dirction = dirction + 1;
- if(dirction > Dirction.S)dirction = Dirction.E;
- move_sign = true;
- }
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
追踪动作:PatrolFollowAction.cs
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class PatrolFollowAction : SSAction
- {
- private float speed = 2f;
- private GameObject player;
- private PatrolData data;
- private PatrolFollowAction() { }
- public static PatrolFollowAction GetSSAction(GameObject player)
- {
- PatrolFollowAction action = CreateInstance<PatrolFollowAction>();
- action.player = player;
- return action;
- }
- public 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);
- Follow();
- if (!data.follow_player || data.wall_sign != data.sign)
- {
- this.destroy = true;
- this.callback.SSActionEvent(this,1,this.gameobject);
- }
- }
- public override void Start(){data = this.gameobject.GetComponent<PatrolData>();}
- void Follow()
- {
- transform.position = Vector3.MoveTowards(this.transform.position, player.transform.position, speed * Time.deltaTime);
- this.transform.LookAt(player.transform.position);
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
动作管理:PatrolActionManager.cs
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class PatrolActionManager : SSActionManager, ISSActionCallback
- {
- private GoPatrolAction go_patrol;
- public void GoPatrol(GameObject patrol)
- {
- go_patrol = GoPatrolAction.GetSSAction(patrol.transform.position);
- this.RunAction(patrol, go_patrol, this);
- }
- public void DestroyAllAction(){DestroyAll();}
- public void SSActionEvent(SSAction source, int intParam = 0, GameObject objectParam = null)
- {
- if (intParam == 0)
- {
- PatrolFollowAction follow = PatrolFollowAction.GetSSAction(objectParam.gameObject.GetComponent<PatrolData>().player);
- this.RunAction(objectParam, follow, this);
- }
- else
- {
- GoPatrolAction move = GoPatrolAction.GetSSAction(objectParam.gameObject.GetComponent<PatrolData>().start_position);
- this.RunAction(objectParam, move, this);
- Singleton<GameEventManager>.Instance.PlayerEscape();
- }
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。