魔鬼牧师游戏--简单工厂的改进

code

Basecode.cs
using UnityEngine;
using System.Collections;
using Com.Mygame;

namespace Com.Mygame {

    public enum WinOrLose {Win,Lose,Gaming};

    public interface ImyAction {
        void DevilGoOnBoat();
        void DevilGoOffBoat ();
        void PriestGoOnBoat();
        void PriestGoOffBoat();
        void BoatGo ();
        bool IQueryGameStatus ();
    }

    public class GameSceneController:System.Object, ImyAction {
        private static GameSceneController _instance;
        private BaseCode _base_code;
        private GenGameObjects _gen_game_object;

        public WinOrLose wl = WinOrLose.Gaming;

        public static GameSceneController GetInstance(){
            if (null == _instance) {
                _instance = new GameSceneController ();
            }
            return _instance;
        }

        public BaseCode getBaseCode(){
            return _base_code;
        }

        internal void setBaseCode(BaseCode bc){
            if (null == _base_code) {
                _base_code = bc;
            }
        }

        public GenGameObjects getGenGameObject(){
            return _gen_game_object;
        }

        internal void setGenGameObject(GenGameObjects g){
            if (_gen_game_object == null){
                _gen_game_object = g;
            }
        }

        public void DevilGoOnBoat(){
            _gen_game_object.DevilGoOnBoat ();
        }

        public void DevilGoOffBoat (){
            _gen_game_object.DevilGoOffBoat ();
        }

        public void PriestGoOnBoat(){
            _gen_game_object.PriestGoOnBoat ();
        }

        public void PriestGoOffBoat(){  
            _gen_game_object.PriestGoOffBoat ();
        }

        public void BoatGo (){
            _gen_game_object.BoatGo ();
        }

        public bool IQueryGameStatus (){
            return _gen_game_object.IQueryGameStatus ();
        }
    }

    // action factory
    public class ActionManager :System.Object {
        private static ActionManager _instance;

        public static ActionManager GetInstance(){
            if (_instance == null) {
                _instance = new ActionManager();
            }
            return _instance;
        }

        public U3dAction ApplyMoveGameObjectTo(GameObject obj,Vector3 target,int speed,IU3dActionCompleted com) {
            MoveGameObjectTo ac = obj.AddComponent<MoveGameObjectTo> ();
            ac.setting (obj,target,speed,com);
            return ac;
        }

        public U3dAction ApplyMoveToLocal(GameObject obj,Vector3 targetlocal,int speed,IU3dActionCompleted com){
            MoveToLocalPosition ac = obj.AddComponent<MoveToLocalPosition> ();
            ac.setting (obj,targetlocal,speed,com);
            return ac;
        }
    }
    //?
    public class U3dActionException : System.Exception {}

    public interface IU3dActionCompleted {
        void OnActionCompleted (U3dAction action);
    }

    public class U3dAction : MonoBehaviour {
        public void Free(){}
    }

    public class U3dActionman : U3dAction {}

    public class MoveGameObjectTo : U3dActionman {
        public GameObject obj;
        public Vector3 target;
        public int speed;

        private IU3dActionCompleted monitor = null;
        public void setting(GameObject obj,Vector3 target,int speed,IU3dActionCompleted monitor){
            this.obj = obj;
            this.target = target;
            this.speed = speed;
            this.monitor = monitor;
        }

        void Update(){
            float step = speed * Time.deltaTime;
            transform.position = Vector3.MoveTowards (transform.position,target,step);
            if(transform.position == target){
                if (monitor != null) {
                    monitor.OnActionCompleted (this);
                }
                Destroy (this); // destory when action done!
            }

        }

    }

    public class MoveToLocalPosition : U3dActionman {
        public GameObject obj;
        public Vector3 targetlocal;
        public int speed;
        private IU3dActionCompleted monitor = null;

        public void setting(GameObject obj,Vector3 targetlocal,int speed,IU3dActionCompleted monitor){
            this.obj = obj;
            this.targetlocal = targetlocal;
            this.speed = speed;
            this.monitor = monitor;
        }

        void Update(){
            float step = speed * Time.deltaTime;
            transform.localPosition = Vector3.MoveTowards (transform.localPosition,targetlocal,step);
            if(transform.localPosition == targetlocal){
                if(monitor != null){
                    monitor.OnActionCompleted (this);
                }
                Destroy (this);
            }
        }
    }

}

public class BaseCode : MonoBehaviour {

    public string gameName;
    public string gameRule;


    // Use this for initialization
    void Start () {
        this.transform.position = new Vector3 (0.0f,5.0f,-12.0f);
        GameSceneController mygame = GameSceneController.GetInstance ();
        mygame.setBaseCode (this);
    }

    // Update is called once per frame
    void Update () {

    }
}

GenGameObject.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Com.Mygame;

public class GenGameObjects : MonoBehaviour , IU3dActionCompleted{

    GameSceneController mygame;
    //boat
    public int moving = 0;
    int state = 0;
    GameObject Boat;
    Vector3 boatscale = new Vector3(4.0f,0.3f,2.0f);  //boat's size;
    Vector3 boatStartpos = new Vector3(-6.0f,0.0f,0.0f);
    Vector3 boatEndpos = new Vector3(6.0f,0.0f,0.0f);
    // sits on the boat
    Vector3 boatsit1 = new Vector3 (-0.25f,3.45f,0.0f); //  localposition is care with the scale
    Vector3 boatsit2 = new Vector3 (0.25f,3.45f,0.0f);
    // others about boats
    int boatspeed = 4;
    int boatnumber = 0;
    int Ifboatsit1have = 0;
    int Ifboatsit2have = 0;
    //river


    //Devils
    List<GameObject> devils = new List<GameObject>();
    Vector3[] devilStartpos = new Vector3[3]{ 
        new Vector3(-10f,1.15f,-3f),
        new Vector3(-10f,1.15f,-2f),
        new Vector3(-10f,1.15f,-1f)
    };
    Vector3[] devilEndpos = new Vector3[3]{ 
        new Vector3(10f,1.15f,-3f),
        new Vector3(10f,1.15f,-2f),
        new Vector3(10f,1.15f,-1f)
    };
    Vector3[] devilScale = new Vector3[3]{ 
        new Vector3(),
        new Vector3(),
        new Vector3()
    };
    int[] DevilsState = new int[3]{0,0,0}; // 0-startside 1-endside 2-onboat
    int[] Devilssite = new int[3]{0,0,0}; //0表示 nosite 1-1site 2-2site

    int devilStartside = 3;
    int devilEndside = 0;

    //Priest
    List<GameObject> priests = new List<GameObject>();
    Vector3[] priestStartpos = new Vector3[3]{ 
        new Vector3(-10f,1.15f,1f),
        new Vector3(-10f,1.15f,2f),
        new Vector3(-10f,1.15f,3f)
    };
    Vector3[] prisetEndpos = new Vector3[3]{ 
        new Vector3(10f,1.15f,1f),
        new Vector3(10f,1.15f,2f),
        new Vector3(10f,1.15f,3f)
    };
    Vector3[] priestScale = new Vector3[3]{ 
        new Vector3(),
        new Vector3(),
        new Vector3()
    };

    int[] PriestState = new int[3]{0,0,0}; // 0-startside 1-endside 2-onboat
    int[] Priestsite = new int[3]{0,0,0};

    int priestStartside = 3;
    int priestEndside = 0;

    // startside
    GameObject startside;
    Vector3 startsidepos = new Vector3 (-10.0f,0.0f,0.0f);
    Vector3 startsidescale =  new Vector3(4f,0.3f,8f);
    // endside
    GameObject endside;
    Vector3 endsidepos = new Vector3 (10.0f,0.0f,0.0f);
    Vector3 endsidescale = new Vector3(4f,0.3f,8f);

    // Use this for initialization
    void Start () {
        mygame = GameSceneController.GetInstance ();
        mygame.setGenGameObject (this);
        InsAll ();
    }

    // Update is called once per frame
    void Update () {
        float step = boatspeed * Time.deltaTime;
        if(state == 2){
            //Boat.transform.position = Vector3.MoveTowards (Boat.transform.position,boatEndpos,boatspeed);
            if(Boat.transform.position == boatEndpos) state = 1;
        } else if (state == 3){
            //Boat.transform.position = Vector3.MoveTowards (Boat.transform.position,boatStartpos,boatspeed);
            if(Boat.transform.position == boatStartpos) state = 0;
        }
    }



    // ins all gameobjects
    void InsAll(){
        //sides
        startside = GameObject.CreatePrimitive(PrimitiveType.Cube);
        startside.transform.localScale = startsidescale;
        startside.transform.position = startsidepos;
        startside.GetComponent<Renderer> ().material.color = Color.green;
        endside = GameObject.CreatePrimitive (PrimitiveType.Cube);
        endside.transform.localScale = endsidescale;
        endside.transform.position = endsidepos;
        endside.GetComponent<Renderer> ().material.color = Color.green;
        //boat
        Boat = GameObject.CreatePrimitive(PrimitiveType.Cube);
        Boat.transform.position = boatStartpos;
        Boat.transform.localScale = boatscale;
        Boat.GetComponent<Renderer> ().material.color = Color.grey;
        //devils and Priests
        for(int i = 0;i<3;i++){
            GameObject devil = GameObject.CreatePrimitive (PrimitiveType.Cylinder);
            devil.GetComponent<Renderer> ().material.color = Color.red;
            devil.transform.position = devilStartpos [i];
            devils.Add (devil);
            GameObject priest = GameObject.CreatePrimitive (PrimitiveType.Cylinder);
            priest.transform.position = priestStartpos [i];
            priests.Add (priest);

        }

        //light !!cool
        GameObject light = new GameObject("The light");
        Light lightComp = light.AddComponent<Light> ();
        lightComp.type = LightType.Directional;
        light.transform.position = new Vector3 ();
        Quaternion Q;
        Q = Quaternion.Euler (20.0f,0.0f,0.0f);
        light.transform.rotation = Q;

    }

    public void DevilGoOnBoat(){ // must write by public ,or the interface can not use it

        if (!IQueryGameStatus ())
            return;

        if (state == 0) { //startside
            if (boatnumber != 2) {
                for (int i = 0; i < 3; i++) {
                    if (DevilsState [i] == 0) {

                        DevilsState [i] = 2;//mean now this devil's state is on boat

                        devils [i].transform.parent = Boat.transform;

                        if (Ifboatsit1have == 0 && Ifboatsit2have == 0) {
                            ActionManager.GetInstance ().ApplyMoveToLocal (devils[i],boatsit1,4,this);
                            Devilssite [i] = 1;
                            Ifboatsit1have = 1;
                        } else if (Ifboatsit1have == 1 && Ifboatsit2have == 0) {
                            ActionManager.GetInstance ().ApplyMoveToLocal (devils[i],boatsit2,4,this);
                            Devilssite [i] = 2;
                            Ifboatsit2have = 1;
                        } else if (Ifboatsit1have == 0 && Ifboatsit2have == 1) {
                            ActionManager.GetInstance ().ApplyMoveToLocal (devils[i],boatsit1,4,this);
                            Devilssite [i] = 1;
                            Ifboatsit1have = 1;
                        }

                        devilStartside--;
                        boatnumber++;
                        break;
                    }
                }
            }
        } else if (state == 1) { // endside
            if (boatnumber != 2) {
                for (int i = 0; i < 3; i++) {
                    if (DevilsState [i] == 1) {

                        DevilsState [i] = 2;//mean now this devil's state is on boat
                        devils [i].transform.parent = Boat.transform;

                        if (Ifboatsit1have == 0 && Ifboatsit2have == 0) {
                            ActionManager.GetInstance ().ApplyMoveToLocal (devils[i],boatsit1,4,this);
                            Devilssite [i] = 1;
                            Ifboatsit1have = 1;
                        } else if (Ifboatsit1have == 1 && Ifboatsit2have == 0) {
                            ActionManager.GetInstance ().ApplyMoveToLocal (devils[i],boatsit2,4,this);
                            Devilssite [i] = 2;
                            Ifboatsit2have = 1;
                        } else if (Ifboatsit1have == 0 && Ifboatsit2have == 1) {
                            ActionManager.GetInstance ().ApplyMoveToLocal (devils[i],boatsit1,4,this);
                            Devilssite [i] = 1;
                            Ifboatsit1have = 1;
                        }

                        devilEndside--;
                        boatnumber++;
                        break;
                    }
                }
            }
        }
    }

    public void DevilGoOffBoat(){

        if (!IQueryGameStatus ())
            return;

        if (state == 0) { // startside
            for (int i = 0; i < 3; i++) {
                if(DevilsState[i] == 2){
                    devils [i].transform.parent = null;
                    //devils[i].transform.position = devilStartpos[i];
                    ActionManager.GetInstance ().ApplyMoveGameObjectTo (devils[i],devilStartpos[i],6,this);
                    DevilsState[i] = 0;

                    if (Devilssite [i] == 1)
                        Ifboatsit1have = 0;
                    else if (Devilssite [i] == 2)
                        Ifboatsit2have = 0;
                    Devilssite [i] = 0;

                    devilStartside++;
                    boatnumber--;
                    break;
                }
            }
        } else if (state == 1) { // endside
            for (int i = 0; i < 3; i++) {
                if(DevilsState[i] == 2){
                    devils [i].transform.parent = null;
                    ActionManager.GetInstance ().ApplyMoveGameObjectTo (devils[i],devilEndpos[i],6,this);
                    DevilsState[i] = 1;

                    if (Devilssite [i] == 1)
                        Ifboatsit1have = 0;
                    else if (Devilssite [i] == 2)
                        Ifboatsit2have = 0;
                    Devilssite [i] = 0;

                    devilEndside++;
                    boatnumber--;
                    break;
                }
            }
        }
    }

    public void PriestGoOnBoat(){

        if (!IQueryGameStatus ())
            return;

        if (state == 0) { //startside
            if (boatnumber != 2) {
                for (int i = 0; i < 3; i++) {
                    if (PriestState [i] == 0) {

                        PriestState [i] = 2;//mean now this devil's state is on boat
                        priests [i].transform.parent = Boat.transform;

                        if (Ifboatsit1have == 0 && Ifboatsit2have == 0) {
                            ActionManager.GetInstance ().ApplyMoveToLocal (priests[i],boatsit1,4,this);
                            Priestsite [i] = 1;
                            Ifboatsit1have = 1;
                        } else if (Ifboatsit1have == 1 && Ifboatsit2have == 0) {
                            ActionManager.GetInstance ().ApplyMoveToLocal (priests[i],boatsit2,4,this);
                            Priestsite [i] = 2;
                            Ifboatsit2have = 1;
                        } else if (Ifboatsit1have == 0 && Ifboatsit2have == 1) {
                            ActionManager.GetInstance ().ApplyMoveToLocal (priests[i],boatsit1,4,this);
                            Priestsite [i] = 1;
                            Ifboatsit1have = 1;
                        }

                        priestStartside--;
                        boatnumber++;
                        break;
                    }
                }
            }
        } else if (state == 1) { // endside
            if (boatnumber != 2) {
                for (int i = 0; i < 3; i++) {
                    if (PriestState [i] == 1) {

                        PriestState [i] = 2;//mean now this devil's state is on boat
                        priests [i].transform.parent = Boat.transform;

                        if (Ifboatsit1have == 0 && Ifboatsit2have == 0) {
                            ActionManager.GetInstance ().ApplyMoveToLocal (priests[i],boatsit1,4,this);
                            Priestsite [i] = 1;
                            Ifboatsit1have = 1;
                        } else if (Ifboatsit1have == 1 && Ifboatsit2have == 0) {
                            ActionManager.GetInstance ().ApplyMoveToLocal (priests[i],boatsit2,4,this);
                            Priestsite [i] = 2;
                            Ifboatsit2have = 1;
                        } else if (Ifboatsit1have == 0 && Ifboatsit2have == 1) {
                            ActionManager.GetInstance ().ApplyMoveToLocal (priests[i],boatsit1,4,this);
                            Priestsite [i] = 1;
                            Ifboatsit1have = 1;
                        }

                        priestEndside--;
                        boatnumber++;
                        break;
                    }
                }
            }
        }
    }

    public void PriestGoOffBoat(){

        if (!IQueryGameStatus ())
            return;

        if (state == 0) { // startside
            for (int i = 0; i < 3; i++) {
                if(PriestState[i] == 2){
                    priests [i].transform.parent = null;
                    //priests[i].transform.position = priestStartpos[i];
                    ActionManager.GetInstance ().ApplyMoveGameObjectTo (priests[i],priestStartpos[i],6,this);
                    PriestState[i] = 0;
                    if (Priestsite [i] == 1)
                        Ifboatsit1have = 0;
                    else if (Priestsite [i] == 2)
                        Ifboatsit2have = 0;
                    Priestsite [i] = 0;
                    priestStartside++;
                    boatnumber--;
                    break;
                }
            }
        } else if (state == 1) { // endside
            for (int i = 0; i < 3; i++) {
                if(PriestState[i] == 2){
                    priests [i].transform.parent = null;
                    ActionManager.GetInstance ().ApplyMoveGameObjectTo (priests[i],prisetEndpos[i],6,this);
                    PriestState[i] = 1;

                    if (Priestsite [i] == 1)
                        Ifboatsit1have = 0;
                    else if (Priestsite [i] == 2)
                        Ifboatsit2have = 0;
                    Priestsite [i] = 0;
                    priestEndside++;
                    boatnumber--;
                    break;
                }
            }
        }
    }

    public void BoatGo(){

        if (!IQueryGameStatus ())
            return;

        if(boatnumber != 0){
            check ();
            if (state == 0) {
                // from startpos to endpos,move
                ActionManager.GetInstance().ApplyMoveGameObjectTo(Boat,boatEndpos,boatspeed,this);
                state = 2;
            } else if (state == 1){
                ActionManager.GetInstance().ApplyMoveGameObjectTo(Boat,boatStartpos,boatspeed,this);
                state = 3;
                // form endpos to startpos,move
            }
        }
    }

    public bool IQueryGameStatus(){
        if (moving == 1) {
            return false;
        }
        else {
            moving = 1;
            return true;
        }
    }

    public void check(){
        //lose check
        print(devilStartside);
        print (devilEndside);
        print (priestStartside);
        print (priestEndside);
        if (state == 0) {
            if ((devilStartside > priestStartside && priestStartside!=0) || ((3 - devilStartside) > (3 - priestStartside) && (3-priestStartside)!=0)) {
                mygame.wl = WinOrLose.Lose;
                return;
            }
        } else if (state == 1) {
            if ((devilEndside > priestEndside && priestEndside!=0 ) || ((3 - devilEndside) > (3 - priestEndside) && (3-priestEndside)!=0)) {
                mygame.wl = WinOrLose.Lose;
                return;
            }
        }
        //win check
        if(devilStartside == 0 && priestStartside == 0) mygame.wl = WinOrLose.Win;

    }

    public void OnActionCompleted(U3dAction action){
            moving = 0;
    }

}


Actions.cs
using UnityEngine;
using System.Collections;
using Com.Mygame;

public class Actions : MonoBehaviour {
    GameSceneController mygame;
    ImyAction actions;
    float BottonW = Screen.width/4;
    float BottonH = Screen.height/16;
    float firstBx = Screen.width/4;
    float firstBy = Screen.height/8;
    float blank = 5;
    private string str = "点击重置!";
    // Use this for initialization
    void Start () {

        mygame = GameSceneController.GetInstance();
        actions = GameSceneController.GetInstance () as ImyAction;
    }

    void OnGUI(){

        if(GUI.Button(new Rect(firstBx,firstBy,BottonW,BottonH), "魔鬼上船")){
            actions.DevilGoOnBoat ();
        }
        if(GUI.Button(new Rect(firstBx+BottonW+blank,firstBy,BottonW,BottonH), "魔鬼下船")){
            actions.DevilGoOffBoat ();
        }
        if(GUI.Button(new Rect(firstBx,firstBy+BottonH+blank,BottonW,BottonH), "牧师上船")){
            actions.PriestGoOnBoat ();
        }
        if(GUI.Button(new Rect(firstBx+BottonW+blank,firstBy+BottonH+blank,BottonW,BottonH), "牧师下船")){
            actions.PriestGoOffBoat ();
        }
        if(GUI.Button(new Rect(firstBx,firstBy+(BottonH+blank)*2,BottonW*2+blank,BottonH*2), "开船!")){
            actions.BoatGo ();
            if (mygame.wl == WinOrLose.Gaming) str = "点击重置!";
            else if (mygame.wl == WinOrLose.Lose) str = "输了...重来!";
            else if (mygame.wl == WinOrLose.Win) str = "赢了!再来!";
        }

        if(GUI.Button(new Rect(Screen.width-BottonW/2,firstBy,BottonW/2,BottonH),str)){
            Application.LoadLevel (Application.loadedLevelName);
            mygame.wl = WinOrLose.Gaming;
        }

        //GUI.Label (new Rect (firstBx,firstBy-BottonH,BottonW*2,BottonH), str);

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值