上次弄了一个牧师与恶魔小游戏的MVC基础版(博客),这次,在原来代码的基础上进行修改,增加动作管理器。动作管理部分的类图如下:
由于上课时间有限,老师对这个的讲解并不是很多,自己一开始直到作业要求的时候,也是有点懵,搞不懂究竟要怎样把动作分离出来,就索性先把老师课件上的代码打一遍,一边打代码一边理解,也大概理解了这个所谓的动作分离是怎么一个原理,简单地说,就是把所有游戏对象的动作都抽取出来,通过这个动作管理器统一安排动作的执行。由于把动作都挂在各自的对象上分开执行的时候,会显得杂乱且无序,而且,当需要增加新的动作的时候,会挺麻烦的。把所有动作统一交给动作管理器,可以使得程序更能适应需求的变化,更加易于维护。
这个小游戏的游戏对象的动作很少,所以,动作分离器的效果还没有很明显,而且,有些作用也还没用到,但是,这次也只是先对动作分离这种设计模式的一个适应,以后当游戏对象的动作多起来的时候,效果就会明显了。
下面来看一下动作分离器的代码
对于动作管理具体实现的理解,均在注释中了
首先是SSAction类,所有动作的基类:
public class SSAction : ScriptableObject {
public bool enable = true;//动作是否可以执行
public bool destory = false;//动作是否可以销毁
//动作的基本属性,动作对象等
public GameObject gameobject { get; set; }
public Transform transform { get; set; }
public ISSActionCallback callback { get; set; }
protected SSAction() { }
public virtual void Start()
{
throw new System.NotImplementedException();
}
public virtual void Update()
{
throw new System.NotImplementedException();
}
}
接下来是CCMoveToAction类,这里实现最简单的动作——对象的移动。
public class CCMoveToAction : SSAction {
public Vector3 target;
public float speed;
public static CCMoveToAction GetSSAction(Vector3 target, float speed)
{
CCMoveToAction action = ScriptableObject.CreateInstance<CCMoveToAction>();
//Debug.Log(target);
action.target = target;
action.speed = speed;
return action;
}
public override void Update()
{
this.transform.position = Vector3.MoveTowards(this.transform.position, target, speed * Time.deltaTime);
// Debug.Log("po:" + this.transform.position);
if(this.transform.position == target)
{
this.destory = true;
this.callback.SSActionEvent(this);
}
}
public override void Start()
{
}
}
接下来是组合动作的实现(在这次游戏中并没有用到,人物的移动本来可以放在这里的,考虑到移动一次也只有两个动作,就不放进来了)
public class CCSequenceAction : SSAction, ISSActionCallback {
public List<SSAction> sequence;
public int repeat = -1;
public int start = 0;
public static CCSequenceAction GetSSAction(int repeat, int start, List<SSAction> sequence)
{
CCSequenceAction action = ScriptableObject.CreateInstance<CCSequenceAction>();
action.repeat = repeat;
action.sequence = sequence;
action.start = start;
return action;
}
public override void Update()
{
if (sequence.Count == 0) return;
if(start < sequence.Count)
{
sequence[start].Update();
}
}
public void SSActionEvent(SSAction source,
SSActionEventType events = SSActionEventType.Competeted,
int intParam = 0,
string strParam = null,
Object objectParam = null)
{
source.destory = false;
this.start++;
if(this.start >= sequence.Count)
{
this.start = 0;
if (repeat > 0) repeat--;
if(repeat == 0)
{
this.destory = true;
this.callback.SSActionEvent(this);
}
}
}
public override void Start()
{
foreach(SSAction action in sequence)
{
action.gameobject = this.gameobject;
action.transform = this.transform;
action.callback = this;
action.Start();
}
}
public void OnDestroy()
{
}
}
然后是动作事件接口的定义:
public enum SSActionEventType:int { Started, Competeted }
public interface ISSActionCallback
{
//还没理解怎么用的。先从PPT上抄下来再说
void SSActionEvent(SSAction source,
SSActionEventType events = SSActionEventType.Competeted,
int intParam = 0,
string strParam = null,
Object objectParam = null);
}
动作管理的基类:
public class SSActionManager : MonoBehaviour {
//物体的运动等待队列及等待序号,就绪队列
private Dictionary<int, SSAction> actions = new Dictionary<int, SSAction>();
//动作等待队列
private List<SSAction> waitingAdd = new List<SSAction>();
//已完成的动作的等待删除队列
private 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.destory)
{
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 RunAction(GameObject gameobject, SSAction action, ISSActionCallback manager)
{
action.gameobject = gameobject;
action.transform = gameobject.transform;
action.callback = manager;
waitingAdd.Add(action);
action.Start();
//Debug.Log(gameobject.ToString());
}
protected void Start()
{
}
}
最后,是实战动作管理类的实现。
public class CCActionManager:SSActionManager, ISSActionCallback
{
private FirstController sceneController;
protected new void Start()
{
sceneController = (FirstController)Director.getInstance().currentSceneController;
sceneController.actionManager = this;
}
void ISSActionCallback.SSActionEvent(SSAction source,
SSActionEventType events = SSActionEventType.Competeted,
int intParam = 0,
string strParam = null,
Object objectParam = null)
{
//不知道要实现些啥
}
public void moveBoat(BoatController boat)
{
CCMoveToAction action = CCMoveToAction.GetSSAction(boat.getDestination(), boat.Speed);
// Debug.Log(boat.getDestination());
this.RunAction(boat.getGameobj(), action, this);
}
public void moveCharacter(ChaController characterCtrl, Vector3 destination)
{
Vector3 halfDest = destination;
Vector3 currentPos = characterCtrl.getPosition();
if (destination.y > currentPos.y)
{
halfDest.x = currentPos.x;
}
//上船时转折处理
else
{
halfDest.y = currentPos.y;
}
SSAction action1 = CCMoveToAction.GetSSAction(halfDest, characterCtrl.Speed);
SSAction action2 = CCMoveToAction.GetSSAction(destination, characterCtrl.Speed);
SSAction seqAction = CCSequenceAction.GetSSAction(1, 0, new List<SSAction> { action1, action2 });
this.RunAction(characterCtrl.getCharacter(), seqAction, this);
}
public new void Update()
{
base.Update();
}
}
至此,动作管理这部分就算是完成了,接下来,就是更改之前的代码,将所有的动作都由动作管理器来管理和实现。那么,之前用来辅助游戏对象移动的MoveState类,也就不再需要了,剩余部分的代码如下:
public class UserGUI : MonoBehaviour {
private UserAction action;
//用于记录游戏的状态(1 - 输了,2 - 赢了, 0 - 还在玩)
private int status = 0;
GUIStyle style;
GUIStyle buttonStyle;
//属性封装
public int Status
{
set
{
status = value;
}
get
{
return status;
}
}
private void Start()
{
action = Director.getInstance().currentSceneController as UserAction;
//label的样式
style = new GUIStyle();
style.fontSize = 50;
style.normal.textColor = Color.red;
//button的样式
buttonStyle = new GUIStyle("button");
buttonStyle.fontSize = 30;
buttonStyle.normal.textColor = Color.blue;
}
private void OnGUI()
{
if (status == 1)//输了
{
GUI.Label(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 85, 150, 50), "Game Over!", style);
if (GUI.Button(new Rect(Screen.width / 2 - 70, Screen.height / 2, 140, 70), "Restart", buttonStyle))
{
//重新开始游戏
status = 0;
action.restart();
}
}
else if (status == 2)//赢了
{
GUI.Label(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 85, 150, 50), "You win!", style);
if (GUI.Button(new Rect(Screen.width / 2 - 70, Screen.height / 2, 140, 70), "Restart", buttonStyle))
{
status = 0;
action.restart();
}
}
}
}
//用于检测用户的活动
public interface UserAction
{
void moveBoat();
void isClickCha(ChaController chaController);
void restart();
}
public class FirstController : MonoBehaviour, SceneController, UserAction {
UserGUI userGUI;
public LandController fromLand;
public LandController toLand;
public BoatController boat;
private ChaController[] people;
public CCActionManager actionManager;
private Transform background;
//加点声音
public AudioClip audio;
private AudioSource audioSource;
//初始化游戏场景和配置。
void Awake()
{
Director.getInstance().currentSceneController = this;
userGUI = gameObject.AddComponent<UserGUI>() as UserGUI;
people = new ChaController[6];
actionManager = new CCActionManager();
audioSource = this.gameObject.GetComponent<AudioSource>();
audioSource.loop = false;
audioSource.volume = 1.0f;
audioSource.clip = audio;
LoadResources();
}
//创建对象的辅助函数
private GameObject createObject(string name, Vector3 position)
{
return (GameObject)Object.Instantiate(Resources.Load("Prefabs/" + name), position, Quaternion.identity);
}
//导入各种预制体资源
public void LoadResources()
{
//背景设置(其实就是一个方块加点贴图。能力限制,就这么弄了)
background = Instantiate<Transform>(Resources.Load<Transform>("Prefabs/backGround"), new Vector3(0, 6, 3), Quaternion.identity);
background.name = "background";
background.localScale += new Vector3(35, 20, 2);
background.Rotate(new Vector3(10, 0, 180));
//导入陆地、河流和船
GameObject river = createObject("River", new Vector3(0, 0, -2));
river.name = "river";
GameObject leftLand = createObject("Land", new Vector3(-10, 0.5f, -2));
leftLand.name = "leftLand";
GameObject rightLand = createObject("Land", new Vector3(10, 0.5f, -2));
rightLand.name = "rightLand";
GameObject t_boat = createObject("Boat", new Vector3(5, 1.15f, -2.5f));
t_boat.name = "boat";
//设置控制器
fromLand = new LandController(rightLand, 1);
toLand = new LandController(leftLand, -1);
boat = new BoatController(t_boat);
//导入游戏人物对象并设置控制器
for (int i = 0; i < 3; i++)
{
GameObject temp = createObject("devil", Vector3.zero);
ChaController cha = new ChaController(temp, 1);
cha.setName("devil" + i);
cha.setPosition(fromLand.getEmptyPosition());
cha.getOnLand(fromLand);
fromLand.getOnLand(cha);
people[i] = cha;
}
for (int i = 0; i < 3; i++)
{
GameObject temp = createObject("Priests", Vector3.zero);
ChaController cha = new ChaController(temp, 0);
cha.setName("priest" + i);
cha.setPosition(fromLand.getEmptyPosition());
cha.getOnLand(fromLand);
fromLand.getOnLand(cha);
people[i + 3] = cha;
}
}
//船的移动,当船上有人时,船才可以移动。。
public void moveBoat()
{
if (!boat.isEmpty())
// boat.Move();
actionManager.moveBoat(boat);
userGUI.Status = isOver();
}
/// <summary>
/// 如果点了某个人物,判断其是在船上还是陆地上
/// 如果在船上,则上岸;否则,上船
/// </summary>
/// <param name="chac">某个人</param>
public void isClickCha(ChaController chac)
{
//上岸
if(chac.isOnBoat())
{
LandController whichLand;
if (boat.getOnWhere() == -1)
whichLand = toLand;
else
whichLand = fromLand;
boat.getOffBoat(chac.getChaName());
//chac.movePosition(whichLand.getEmptyPosition());
// chac.setDestination(whichLand.getEmptyPosition());
actionManager.moveCharacter(chac, whichLand.getEmptyPosition());//动作执行
chac.getOnLand(whichLand);
whichLand.getOnLand(chac);
}
//上船
else
{
LandController whichLand = chac.getLandCont();
if (boat.getEmptyIndex() == -1) return;
if (whichLand.getSide() != boat.getOnWhere()) return;
whichLand.getOffLand(chac.getChaName());
//chac.movePosition(boat.getEmptyPos());
// chac.setDestination(boat.getEmptyPos());
actionManager.moveCharacter(chac, boat.getEmptyPos());//动作执行
chac.getOnBoat(boat);
boat.getOnBoat(chac);
}
userGUI.Status = isOver();//判断游戏是否已经达到了结束的条件
}
/// <summary>
/// 判断游戏是否结束
/// </summary>
/// <returns>2 - 赢了; 1 - 输了; 0 - 还没结束</returns>
public int isOver()
{
int fromP = 0;//右边牧师人数
int fromD = 0;//右边魔鬼人数
int toP = 0;//左边牧师人数
int toD = 0;//左边魔鬼人数
//获取右边对应的人数
int[] fromCount = fromLand.getChaNum();
fromP += fromCount[0];
fromD += fromCount[1];
//获取左边对应的人数
int[] toCount = toLand.getChaNum();
toP += toCount[0];
toD += toCount[1];
//如果左边有六个人,证明全部安全过河,你赢了
if (toP + toD == 6) return 2;
//将船上人的数目也加上去
int[] boatCount = boat.getChaNum();
if(boat.getOnWhere() == -1)
{
toP += boatCount[0];
toD += boatCount[1];
}
else
{
fromP += boatCount[0];
fromD += boatCount[1];
}
//如果任意一边牧师人数少于魔鬼,你输了
if (fromP < fromD && fromP > 0 || toP < toD && toP > 0) return 1;
return 0;
}
//重置游戏
public void restart()
{
audioSource.Play();
boat.reset();
fromLand.reset();
toLand.reset();
foreach (ChaController chac in people) chac.Reset();
}
}
public class ClickGUI : MonoBehaviour {
UserAction action;
ChaController chac;
//设置点击的人物
public void setController(ChaController chaController)
{
chac = chaController;
}
private void Start()
{
action = Director.getInstance().currentSceneController as UserAction;
}
//鼠标点击事件
private void OnMouseUp()
{
try
{
action.isClickCha(chac);
}
catch(Exception e)
{
Debug.Log(e.ToString());
}
finally
{
Debug.Log("Clicking:" + gameObject.name);
}
}
private void OnGUI()
{
//开船事件。由于用鼠标点击的时候,总是点不中,就直接用button来了
if ((Director.getInstance().currentSceneController as FirstController).isOver() == 0)
{
GUIStyle buttonStyle = new GUIStyle("button");
buttonStyle.fontSize = 30;
buttonStyle.normal.textColor = Color.blue;
if (GUI.Button(new Rect(Screen.width / 2 - 60, Screen.height / 2 - 25, 120, 50), "set sail", buttonStyle))
{
action.moveBoat();
}
}
//加点儿游戏的文字说明
GUIStyle gUIStyle = new GUIStyle();
gUIStyle.fontSize = 40;
gUIStyle.normal.textColor = Color.green;
GUI.Label(new Rect(Screen.width / 2 - 150, 10, 300, 40), "Priests And Devils", gUIStyle);
GUI.Label(new Rect(10, 200, 300, 300), "Priests and Devils is a " +
"game in which you will help the Priests and Devils to cross the river." +
" There are 3 priests and 3 devils at one side of the river." +
" They all want to get to the other side of this river, " +
"but there is only one boat and this boat can only carry two persons each time." +
" And there must be one person steering the boat from one side to the other side." +
" In this game, you can click on them to move them and click the button to move the boat to the other direction. " +
"If the priests are out numbered by the devils on either side of the river, they get killed and the game is over." +
" You can try it in many ways. Keep all priests alive! Good luck!");
}
}
public class ChaController {
GameObject character;//游戏人物控制器
// public MoveStatus moveable;//人物动作
ClickGUI clickGUI;//点击事件
private int whatCharacter;//人物的角色(牧师还是魔鬼)
private bool onBoat;//人数是否在船上
LandController landController;
private float speed = 20;
//public Vector3 destination;
//public Vector3 halfDest;
public float Speed
{
set
{
speed = value;
}
get
{
return speed;
}
}
//人物控制器的初始化
public ChaController(GameObject chac, int status)
{
character = chac;
whatCharacter = status;
// moveable = character.AddComponent(typeof(MoveStatus)) as MoveStatus;
clickGUI = character.AddComponent(typeof(ClickGUI)) as ClickGUI;
clickGUI.setController(this);
}
public GameObject getCharacter()
{
return character;
}
public void setName(string name)
{
character.name = name;
}
//设置人物的位置
public void setPosition(Vector3 position)
{
character.transform.position = position;
}
//人物移动事件
//public void setDestination(Vector3 dest)
//{
// // moveable.setDestination(dest);
// destination = dest;
// if (dest.y > character.transform.position.y)
// {
// halfDest.x = character.transform.position.x;
// halfDest.y = dest.y;
// }
// //上船时转折处理
// else
// {
// halfDest.y = character.transform.position.y;
// halfDest.x = dest.x;
// }
//}
public Vector3 getPosition()
{
return character.transform.position;
}
public int getChaType()
{
return whatCharacter;
}
public string getChaName()
{
return character.name;
}
//人物上船。设置船为人物的父对象
public void getOnBoat(BoatController boat)
{
landController = null;
character.transform.parent = boat.getGameobj().transform;
onBoat = true;
}
//人物下船
public void getOnLand(LandController land)
{
landController = land;
character.transform.parent = null;
onBoat = false;
}
public bool isOnBoat()
{
return onBoat;
}
public LandController getLandCont()
{
return landController;
}
//重置
public void Reset()
{
// moveable.Reset();
landController = (Director.getInstance().currentSceneController as FirstController).fromLand;
getOnLand(landController);
setPosition(landController.getEmptyPosition());
landController.getOnLand(this);
}
}
public class BoatController {
GameObject boat;//对象
// MoveStatus moveable;//移动
Vector3[] from_pos;//船上的能坐人的位置,右边
Vector3[] to_pos;//船上能坐人的位置,左边
int onWhere;//标志位,船在左边还是右边(1 - 右边, -1 - 左边)
ChaController[] people = new ChaController[2];//船上的人物
private float speed = 15;//船的移动速度
public float Speed
{
get
{
return speed;
}
set
{
speed = value;
}
}
public BoatController(GameObject t_boat)
{
onWhere = 1;//初始在右边
from_pos = new Vector3[] { new Vector3(4.5f, 1.65f, -2), new Vector3(5.5f, 1.65f, -2) };
to_pos = new Vector3[] { new Vector3(-5.5f, 1.65f, -2), new Vector3(-4.5f, 1.65f, -2) };
boat = t_boat;
// moveable = boat.AddComponent(typeof(MoveStatus)) as MoveStatus;
boat.AddComponent(typeof(ClickGUI));
}
/// <summary>
/// 船的移动。每移动一次,改变标志位
/// </summary>
//public void Move()
//{
// if (onWhere == -1)
// {
// moveable.setDestination(new Vector3(5, 1.15f, -2.5f));
// onWhere = 1;
// }
// else
// {
// moveable.setDestination(new Vector3(-5, 1.15f, -2.5f));
// onWhere = -1;
// }
//}
//获取船将要去的位置
public Vector3 getDestination()
{
// Debug.Log("on boat:" + onWhere);
if(onWhere == -1)
{
onWhere = 1;
return new Vector3(5, 1.15f, -2.5f);
}
else
{
onWhere = -1;
return new Vector3(-5, 1.15f, -2.5f);
}
}
//获取船上的空位
public int getEmptyIndex()
{
for(int i = 0; i < people.Length; i++)
{
if (people[i] == null) return i;
}
return -1;
}
//判断船是否没人
public bool isEmpty()
{
foreach(ChaController chac in people)
{
if (chac != null) return false;
}
return true;
}
//获取没人的位置的坐标
public Vector3 getEmptyPos()
{
int index = getEmptyIndex();
if(onWhere == -1)
{
return to_pos[index];
}
else
{
return from_pos[index];
}
}
//人物对象上船
public void getOnBoat(ChaController chac)
{
int index = getEmptyIndex();
people[index] = chac;
}
/// <summary>
/// 人物对象下船
/// </summary>
/// <param name="name">下船的人的名字</param>
/// <returns>返回这个对象的控制器</returns>
public ChaController getOffBoat(string name)
{
for(int i = 0; i < people.Length; i++)
{
if(people[i] != null && people[i].getChaName() == name)
{
ChaController chac = people[i];
people[i] = null;
return chac;
}
}
return null;
}
public GameObject getGameobj()
{
return boat;
}
public int getOnWhere()
{
return onWhere;
}
//获取船上各身分人物的数量
public int[] getChaNum()
{
int[] count = { 0, 0 };
foreach (ChaController chac in people)
{
if (chac != null)
{
int add = chac.getChaType() == 0 ? 0 : 1;
count[add]++;
}
}
return count;
}
//重置
public void reset()
{
// moveable.Reset();
if (onWhere == -1)
{
onWhere = 1;
boat.transform.position = new Vector3(5, 1.15f, -2.5f);
//moveable.setDestination(new Vector3(5, 1.15f, -2.5f));
}
people = new ChaController[2];
}
}
//导演类。不多说了
public class Director : System.Object {
private static Director _instance;
public SceneController currentSceneController { set; get; }
public static Director getInstance()
{
if(_instance == null)
{
_instance = new Director();
}
return _instance;
}
}
public class LandController {
GameObject land;//对象
Vector3[] pos;//用于放置人物对象的位置
int side;//标志位,1 表示这是右边的陆地,-1表示这是左边的位置
ChaController[] people;//陆地上的任务对象
//初始化
public LandController(GameObject t_land, int t_status)
{
pos = new Vector3[] {new Vector3(6.5f,2.5f,-2), new Vector3(7.5f,2.5f,-2), new Vector3(8.5f,2.5f,-2),
new Vector3(9.5f,2.5f,-2), new Vector3(10.5f,2.5f,-2), new Vector3(11.5f,2.5f,-2)};
people = new ChaController[6];
land = t_land;
side = t_status;
}
//获取人物位置数组中,没有人物的位置下标
public int getEmptyIndex()
{
for(int i = 0; i < people.Length; i++)
{
if (people[i] == null) return i;
}
return -1;
}
//获取人物位置数组中,没有人物的位置坐标
public Vector3 getEmptyPosition()
{
Vector3 position = pos[getEmptyIndex()];
position.x *= side;
return position;
}
//人物上岸,找个没人的位置给他
public void getOnLand(ChaController cha)
{
int index = getEmptyIndex();
people[index] = cha;
}
//人物上船
public ChaController getOffLand(string person)
{
for(int i = 0; i < people.Length; i++)
{
if(people[i] != null && people[i].getChaName() == person)
{
ChaController chaCon = people[i];
people[i] = null;
return chaCon;
}
}
return null;
}
//获取当前陆地的标识(左边还是右边)
public int getSide()
{
return side;
}
//获取陆地上牧师的数目和魔鬼的数目
public int[] getChaNum()
{
int[] count = {0,0};
foreach(ChaController chac in people)
{
if(chac != null)
{
int add = chac.getChaType() == 0 ? 0 : 1;
count[add]++;
}
}
return count;
}
//重置
public void reset()
{
people = new ChaController[6];
}
}
//游戏场景控制器
public interface SceneController {
void LoadResources();
}
至此,整个游戏的实现就结束了。