本文主要是介绍[3D游戏编程]Priests And Devils,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
游戏简介
游戏规则
将左岸上的priest和devil都移到右岸即为胜利。
如果出现某一边(如果船开往该边,还要算上船上的)的devil数量大于priest数量,且此时的priest数量不为0,则游戏失败。
游戏效果
课堂任务
1)用unity实现该游戏
2)通过LoadResources动态生成对象
3)严格按照MVC结构编写程序
4)注意细节,船未靠岸,牧师与魔鬼上下船运动中,均不能接受用户事件
游戏实现
游戏框架设计(UML图)
动作表
对象 | 动作 | 对应函数 |
---|---|---|
priest | 上下船 | priestsGetOn() priestsGetOff() |
devil | 上下船 | devilsGetOn() devilsGetOff() |
boat | 向两岸移动 | boatMove() |
代码实现
游戏严格按照课程所提出的MVC总体框架设计(保证看得懂),所以正如课程所说,分为五个类:FirstController、ISceneController、IUserAction、SSDirector、UserInterface
(各位学长大佬的代码真的看不懂_(:зゝ∠)_)
项目代码(https://github.com/Ivan53471/Priests-And-Devils.git)
SSDirector
这部分没啥好说的,按老师教的写就行,不影响游戏逻辑实现
public class SSDirector : System.Object
{// singlton instanceprivate static SSDirector _instance;public ISceneController currentSceneController { get; set; }// get instance anytime anywhare!public static SSDirector getInstance(){if (_instance == null){_instance = new SSDirector();}return _instance;}public int getFPS(){return Application.targetFrameRate;}public void setFPS(int fps){Application.targetFrameRate = fps;}public void NextScene(){Debug.Log("Waiting next Scene now...");
#if UNITY_EDITORUnityEditor.EditorApplication.isPlaying = false;//UnityEditor.EditorApplication.Exit(0);
#elseApplication.Quit();
#endif}
}
UserInterface
这部分主要实现的是MVC架构中的View,主要用于规定按键出现的位置,以及按下按键对应执行什么函数
void OnGUI(){//游戏正常进行if(mySceneController.GameOver() == 0){if (GUI.Button(new Rect(btnWidth / 2, 250, btnWidth, btnHeight), "Priests GetOn")){myActions.priestsGetOn();}if (GUI.Button(new Rect(btnWidth / 2 + btnWidth, 250, btnWidth, btnHeight), "Priests GetOff")){myActions.priestsGetOff();}if (GUI.Button(new Rect(btnWidth / 2 + 2 * btnWidth, 250, btnWidth, btnHeight), "Go!")){myActions.boatMove();}if (GUI.Button(new Rect(btnWidth / 2 + 3 * btnWidth, 250, btnWidth, btnHeight), "Devils GetOn")){myActions.devilsGetOn();}if (GUI.Button(new Rect(btnWidth / 2 + 4 * btnWidth, 250, btnWidth, btnHeight), "Devils GetOff")){myActions.devilsGetOff();}}//loseelse if(mySceneController.GameOver() == 1){GUI.Box(new Rect(2 * btnWidth, btnHeight, 2 * btnWidth, btnHeight), "\nYOU LOSE");if (GUI.Button(new Rect(2.5f * btnWidth, 2 * btnHeight, btnWidth, btnHeight), "Restart")){mySceneController.restart();}}//winelse{GUI.Box(new Rect(2 * btnWidth, btnHeight, 2 * btnWidth, btnHeight), "\nYOU WIN");if (GUI.Button(new Rect(2.5f * btnWidth, 2 * btnHeight, btnWidth, btnHeight), "Restart")){mySceneController.restart();}}}
IUserAction与ISceneController
这是为FirstController定义的两个接口,要弄明白这两个接口有什么用,其实可以从名字上看出来。
IUserAction,顾名思义,也就是和对象的动作有关。从之前的动作表可以得出,一共应该有
priestsGetOn()、priestsGetOff()、devilsGetOn()、devilsGetOff()、boatMove()五个函数
public interface IUserAction
{void boatMove();void priestsGetOn();void priestsGetOff();void devilsGetOn();void devilsGetOff();
}
ISceneController又是做什么的呢?本人在这里也疑惑了很久,既然都有FirstController这个东西了,为什么还要再加一个名字感觉差不多的东西呢?
先看一下这个接口里声明了什么函数:
public interface ISceneController
{void LoadResources();int GameOver();void restart();
}
可以看到,这些函数应该是每个场景都需要实现的功能,现在只有一个场景,确实可以省去ISceneController,但是如果有多个场景呢?这个时候总不能在SecondController、ThirdController又声明一次这些函数,那不就冗余了嘛。
同样的,上面的IUserAction也可以这么理解,同一个游戏不同场景之间应该是有关联的,那么对象所实现的动作应该也有所关联,IUserAction的存在同样也是为了减少代码冗余。
FirstController
这部分比较繁杂,需要分开解释。
MVC架构中的Model部分
为了记录每种对象的状态,创建用于记录状态的类(devil与priest相似,不展示)
// 对每种物体新建类记录当前状态public class PriestsStatus{public bool onBoatLeft, onBoatRight;public bool onBankLeft, onBankRight;public PriestsStatus(){this.onBoatLeft = false;this.onBoatRight = false;this.onBankLeft = true;this.onBankRight = false;}}public class BoatBehaviour{public bool isMoving;public bool atLeftSide;public bool leftPosEmpty, rightPosEmpty;public BoatBehaviour() {this.isMoving = false;this.atLeftSide = true;this.leftPosEmpty = true;this.rightPosEmpty = true;}}
然后声明了该场景需要用到的变量
// 记录物体样式、位置等等public List<GameObject> Priests, Devils;public GameObject boat, bankLeft, bankRight;// 物体状态public List<PriestsStatus> p_status;public List<DevilsStatus> d_status;public BoatBehaviour myBoatBehaviour;// 记录某个位置有多少个priest(devil)public int leftBankPriests, rightBankPriests;public int leftBankDevils, rightBankDevils;public int boatPriest, boatDevil;
MVC架构中的Controller部分
ISceneController.LoadResource
实现了为所有变量初始化,设置物体的样式、初始位置等等(只展示priest)
public void LoadResources(){// priestsPriests = new List<GameObject>();p_status = new List<PriestsStatus>();for (int i = 0; i < 3; i++){GameObject priests = Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/Priests"),Vector3.zero, Quaternion.identity);priests.name = "Priest " + (i + 1);//priests.tag = "Priest";Priests.Add(priests);}// 初始化restart();}
其中调用了ISceneController.restart函数
该函数用于初始化所有变量的值,其中leftBankDevils = leftBankPriests = 3是因为priest和devil初始设定都在左岸。
public void restart(){// priestsp_status = new List<PriestsStatus>();for (int i = 0; i < 3; i++){p_status.Add(new PriestsStatus());}Priests[0].transform.position = new Vector3(-8.6f, 3, 0);Priests[1].transform.position = new Vector3(-7.3f, 3, 0);Priests[2].transform.position = new Vector3(-6, 3, 0);//devilsd_status = new List<DevilsStatus>();for (int i = 0; i < 3; i++){d_status.Add(new DevilsStatus());}Devils[0].transform.position = new Vector3(-12.9f, 3, 0);Devils[1].transform.position = new Vector3(-11.6f, 3, 0);Devils[2].transform.position = new Vector3(-10.3f, 3, 0);//boatmyBoatBehaviour = new BoatBehaviour();boat.transform.position = new Vector3(-2.4f, 0.5f, 0);//bankbankLeft.transform.position = new Vector3(-8.5f, 1.5f, 0);bankRight.transform.position = new Vector3(8.5f, 1.5f, 0);// 初始化leftBankDevils = 3;leftBankPriests = 3;rightBankDevils = 0;rightBankPriests = 0;boatPriest = 0;boatDevil = 0;}
ISceneController.GameOver
返回值有0、1、2,分别代表游戏正常进行、游戏失败、游戏成功三种情况。
public int GameOver(){// 船靠岸分情况讨论if(myBoatBehaviour.atLeftSide){if ((leftBankPriests + boatPriest > 0&& leftBankDevils + boatDevil > leftBankPriests + boatPriest)|| (rightBankDevils > rightBankPriests && rightBankPriests > 0)){return 1;}}else{if ((rightBankPriests + boatPriest > 0&& rightBankDevils + boatDevil > rightBankPriests + boatPriest)|| (leftBankDevils > leftBankPriests && leftBankPriests > 0)){return 1;}if (rightBankDevils + boatDevil == 3 && rightBankPriests + boatPriest == 3){return 2;}}return 0;}
IUserAction.priestsGetOn、IUserAction.devilsGetOn
IUserAction.devilsGetOn逻辑相似,只展示IUserAction.priestsGetOn
priest上船可以分为:从左岸上船到船的左边、从左岸上船到船的右边、从右岸上船到船的左边、从右岸上船到船的右边,这里只展示第一种情况
public void priestsGetOn(){// 如果船正在动,那么不能响应用户事件if (myBoatBehaviour.isMoving)return;// 船在左边if (myBoatBehaviour.atLeftSide){for (int i = 0; i < Priests.Count; i++){// 左侧岸上有牧师if (p_status[i].onBankLeft){// 上船位置if (myBoatBehaviour.leftPosEmpty){// 更改岸上、船上状态p_status[i].onBankLeft = false;p_status[i].onBoatLeft = true;p_status[i].onBoatRight = false;// 把船上左边位置设为有人myBoatBehaviour.leftPosEmpty = false;// 将priest移动到船左边的位置Priests[i].transform.position = new Vector3(-3.3f, 1.5f, 0);// 修改船上、岸上人数boatPriest++;leftBankPriests--;break;}if (myBoatBehaviour.rightPosEmpty){}}}}else{}}
IUserAction.priestsGetOff、IUserAction.devilsGetOff
IUserAction.devilsGetOff逻辑相似,只展示IUserAction.priestsGetOff
priest下船可以分为:从船的左边到左岸、从船的右边到左岸、从船的左边到左岸、从船的右边到右岸,这里只展示第一种情况
public void priestsGetOff(){if (myBoatBehaviour.isMoving)return;// 船在左边if (myBoatBehaviour.atLeftSide){for (int i = 0; i < Priests.Count; i++){//船左侧有priestif (p_status[i].onBoatLeft){p_status[i].onBoatLeft = false;p_status[i].onBankLeft = true;p_status[i].onBankRight = false;myBoatBehaviour.leftPosEmpty = true;Priests[i].transform.position = new Vector3((-8.6f + i * 1.3f), 3, 0);boatPriest--;leftBankPriests++;break;}//船右侧有priestif (p_status[i].onBoatRight){}}}else{}}
IUserAction.boatMove
public void boatMove(){// 船在动或者船上没人时不能开船if(!myBoatBehaviour.isMoving && (boatPriest + boatDevil > 0)){myBoatBehaviour.isMoving = true;}}
亮点设计(自认为)
本项目亮点设计来了,上面也提到了船是要动起来的,而不是瞬移过去的,这个操作该怎么实现呢?
在网上如果直接搜如何实现运动,大多数都是推荐使用协程,每帧通过协程调用状态改变的函数,达到运动的效果。
而本项目巧妙地运用了MonoBehavior中的Update函数,该函数每帧都会被调用,实现了和使用协程一样的效果
// Update is called once per framevoid Update(){//give advice first//用于船和人的移动onBoatMoving();}
其中Update中调用的onBoatMoving函数,就是船的状态改变函数(具体实现参见详细代码)
详细代码
Github(https://github.com/Ivan53471/Priests-And-Devils.git)
总结
该项目用Unity实现了经典小游戏Priests And Devils,完成了老师所布置的任务
(预告:下期将优化代码,实现Priests And Devils动作分离版,敬请期待)
这篇关于[3D游戏编程]Priests And Devils的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!