用Unity3D实现简单的牧师与魔鬼游戏(动作分离版)
生活随笔
收集整理的這篇文章主要介紹了
用Unity3D实现简单的牧师与魔鬼游戏(动作分离版)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
用Unity3D實(shí)現(xiàn)簡單的牧師與魔鬼游戲(動(dòng)作分離版)
項(xiàng)目地址
牧師與魔鬼游戲(動(dòng)作分離版)
完成效果圖
上次博客鏈接
牧師與魔鬼游戲
實(shí)現(xiàn)心得
這次作業(yè)是在上次作業(yè)的基礎(chǔ)上完成的,具體做出的改變是添加了一個(gè)裁判類和一個(gè)行為類,將其它控制類中涉及到判斷游戲狀態(tài)的代碼都封裝到了裁判類中,并在主控制類添加了一個(gè)裁判類的成員變量,在主控制器中使用裁判類的相關(guān)方法對(duì)游戲狀態(tài)和游戲?qū)ο鬆顟B(tài)進(jìn)行判斷:
/* 裁判控制類 */ public class JudgeController {private int gameState = (int)GameState.Playing; // 游戲狀態(tài),0表示游戲正在進(jìn)行,1表示游戲失敗,2表示游戲成功public void InitJudgeController() {gameState = (int)GameState.Playing;}/* 獲取游戲?qū)ο笫欠裨诖?*/public bool IsOnBoat(RoleController role) {return role.GetRoleModel().GetIsOnBoat();}/* 判斷船上是否沒有角色 */public bool IsNoRoleOnBoat(BoatController boat) {string [] roleOnBoat = boat.GetBoatModel().GetRoleOnBoat();if (roleOnBoat[0] != "" || roleOnBoat[1] != "") {return false;} else {return true;}}/* 檢查游戲狀態(tài) */public int CheckGameState(ShoreController leftShore, ShoreController rightShore, BoatController boat) {// 初始化左邊和右邊的牧師和魔鬼數(shù)量都為0int leftPriest = 0, rightPriest = 0;int leftDevil = 0, rightDevil = 0;// 獲取左岸和右岸以及船上的牧師和魔鬼的數(shù)量數(shù)組int [] leftShoreCount = leftShore.GetRoleCountOnShore();int [] rightShoreCount = rightShore.GetRoleCountOnShore();int [] boatCount = boat.GetRoleCountOnBoat();// 如果船在左邊,那么左邊的牧師和魔鬼的數(shù)量為左岸數(shù)量加上船里的數(shù)量if (boat.GetBoatSide() == (int)ShoreSide.Left) {leftPriest = leftShoreCount[0] + boatCount[0];leftDevil = leftShoreCount[1] + boatCount[1];rightPriest = rightShoreCount[0];rightDevil = rightShoreCount[1]; } else {// 如果船在右邊,那么右邊的牧師和魔鬼的數(shù)量為右岸數(shù)量加上船里的數(shù)量leftPriest = leftShoreCount[0];leftDevil = leftShoreCount[1];rightPriest = rightShoreCount[0] + boatCount[0];rightDevil = rightShoreCount[1] + boatCount[1]; }// 如果左邊牧師加魔鬼的數(shù)量等于6且魔鬼全部上岸,那么游戲成功if (leftPriest + leftDevil == 6 && leftShoreCount[1] == 3) {gameState = (int)GameState.Win;} else if (leftPriest < leftDevil && leftPriest > 0) {// 如果任意一邊牧師的數(shù)量小于魔鬼的數(shù)量且有牧師,那么游戲失敗gameState = (int)GameState.Lose;} else if (rightPriest < rightDevil && rightPriest > 0) {gameState = (int)GameState.Lose;} else {// 否則游戲正在進(jìn)行中g(shù)ameState = (int)GameState.Playing;}return gameState;}/* 返回是否正在進(jìn)行游戲 */public bool isPlayingGame() {return gameState == (int)GameState.Playing; } }而行為類則是將原先代碼中的移動(dòng)控制器MoveController和點(diǎn)擊控制器ClickController合并到了一起,其它控制類中出現(xiàn)這兩個(gè)類的地方也換成了行為類:
/* 行為控制器,一般會(huì)掛載到某個(gè)具體的游戲?qū)ο笊?*/ public class ActionController : MonoBehaviour {private ActionModel actionModel;private ActionView actionView;private RoleController clickRole; // 如果點(diǎn)擊的是角色,那么記錄點(diǎn)擊的對(duì)象的控制器void Start() {actionModel = new ActionModel();actionView = gameObject.AddComponent<ActionView>() as ActionView;}void Update() {int state = actionModel.GetState();Vector3 middlePos = actionModel.GetMiddlePos();Vector3 endPos = actionModel.GetEndPos();int speed = actionModel.GetSpeed();// 根據(jù)速度、中間位置、終點(diǎn)位置、移動(dòng)狀態(tài)來移動(dòng)物體Move(state, middlePos, endPos, speed);}/* 初始化控制器,設(shè)置物體不移動(dòng) */public void InitActionController() {// 如果對(duì)象是船,獲取船在哪邊int side = actionModel.GetSide();// 在左邊的話,船會(huì)回到右邊去if (side == (int)ShoreSide.Left) {MoveToOtherSide();}}/* 如果對(duì)象是船,設(shè)置船的岸邊邊側(cè) */public void SetBoatSide(int side) {actionModel.SetSide(side);}/* 獲取船的岸邊邊側(cè) */public int GetBoatSide() {return actionModel.GetSide();}/* 根據(jù)速度、中間位置、終點(diǎn)位置、移動(dòng)狀態(tài)來移動(dòng)物體 */public void Move(int state, Vector3 middlePos, Vector3 endPos, int speed) {switch (state) {case (int)MoveState.Not_Moving:break;case (int)MoveState.Moving_To_Middle:// 移動(dòng)物體到中間位置,移動(dòng)到后,設(shè)置物體的下一個(gè)狀態(tài)為移動(dòng)到終點(diǎn)actionView.MoveTo(middlePos, speed);if (transform.position == middlePos) {actionModel.SetState((int)MoveState.Moving_To_End);}break;case (int)MoveState.Moving_To_End:// 移動(dòng)物體到終點(diǎn)位置,移動(dòng)到后,設(shè)置物體的下一個(gè)狀態(tài)為不移動(dòng)actionView.MoveTo(endPos, speed);if (transform.position == endPos) {actionModel.SetState((int)MoveState.Not_Moving);}break;}}/* 設(shè)置物體要移動(dòng)到的位置 */public void SetMovePos(Vector3 pos) {// 設(shè)置物體開始往中間位置移動(dòng)actionModel.SetState((int)MoveState.Moving_To_Middle);// 設(shè)置最終移動(dòng)位置為posactionModel.SetEndPos(pos);// 如果要移動(dòng)到的位置的y坐標(biāo)等于物體所在位置的y坐標(biāo),說明是船在移動(dòng)if (pos.y == transform.position.y) {// 船沿直線運(yùn)動(dòng),所以中間位置也可設(shè)為最終位置actionModel.SetMiddlePos(pos);// 移動(dòng)狀態(tài)可以直接設(shè)為往終點(diǎn)移動(dòng)actionModel.SetState((int)MoveState.Moving_To_End);}// 如果要移動(dòng)到的位置的y坐標(biāo)小于物體所在位置的y坐標(biāo),說明是物體從岸上移動(dòng)到船上else if (pos.y < transform.position.y) {actionModel.SetMiddlePos(new Vector3(pos.x, transform.position.y, pos.z));}// 如果要移動(dòng)到的位置的y坐標(biāo)小于物體所在位置的y坐標(biāo),說明是物體從船上移動(dòng)到岸上else {actionModel.SetMiddlePos(new Vector3(transform.position.x, pos.y, pos.z));}}/* 將船移動(dòng)到岸的另一側(cè) */public void MoveToOtherSide() {// 先獲取船在哪邊int lastSide = actionModel.GetSide();// 移動(dòng)船到另一邊if (lastSide == (int)ShoreSide.Left) {SetMovePos(new Vector3(2.5F, -1, 0));actionModel.SetSide((int)ShoreSide.Right);} else {SetMovePos(new Vector3(-2.5F, -1, 0));actionModel.SetSide((int)ShoreSide.Left);}}/* 如果對(duì)象是角色,設(shè)置被點(diǎn)擊的對(duì)象為該角色 */public void SetClickRole(RoleController role) {clickRole = role;}void OnMouseDown() {// 如果鼠標(biāo)點(diǎn)擊的對(duì)象是船,那么移動(dòng)船只if (gameObject.name == "Boat") {SingleObject.getInstance().GetMainController().MoveBoat();} else {// 如果鼠標(biāo)點(diǎn)擊的對(duì)象是角色,那么移動(dòng)對(duì)象SingleObject.getInstance().GetMainController().MoveRole(clickRole);}} }這樣一來,在實(shí)現(xiàn)模塊分離和降低耦合性的同時(shí),也保證了游戲功能的一致性。游戲功能和效果和之前沒有任何區(qū)別。
游戲截圖:
正在進(jìn)行游戲:
游戲失敗:
游戲成功:
總結(jié)
以上是生活随笔為你收集整理的用Unity3D实现简单的牧师与魔鬼游戏(动作分离版)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用Unity3D实现简单的牧师与魔鬼游戏
- 下一篇: 软件测试作业1:正确理解原型方法对软件生