Unity3D学习(二)农夫与魔鬼过河

第二次实践是做一个农夫与魔鬼过河的小游戏,示例如下:
http://www.flash-game.net/game/2535/priests-and-devils.html
要求使用MVC模式,同样年代久远~也就贴上游戏效果和代码了

游戏效果:

这里写图片描述

BaseCode.cs
游戏基本要求的一些代码,我把物体的控制器都放在这里了

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MyGame;

namespace MyGame {

    public interface newUserActions {
        void priestSOnB();
        void priestEOnB();
        void devilSOnB();
        void devilEOnB();
        void moveBoat();
        void offBoatL();
        void offBoatR();
        void restart();
    }

    public interface SceneController {
        void loadResources ();
    }

    public class Director : System.Object {
        private static Director _instance;
        public SceneController currentSceneController { get; set; }

        public static Director getInstance() {
            if (_instance == null) {
                _instance = new Director ();
            }
            return _instance;
        }
    }

    //--------------------new movement---------------------------
    public class ObjAction : ScriptableObject
    {

        public bool enable = true;
        public bool destroy = false;

        public GameObject gameObject;
        public Transform transform;
        public ActionCallback whoToNotify;

        public virtual void Start()
        {
            throw new System.NotImplementedException();
        }

        public virtual void Update()
        {
            throw new System.NotImplementedException();
        }
    }

    public class MoveToAction : ObjAction
    {
        public Vector3 target;
        public float speed;

        private MoveToAction(){}
        public static MoveToAction getAction(Vector3 target, float speed) {
            MoveToAction action = ScriptableObject.CreateInstance<MoveToAction>();
            action.target = target;
            action.speed = speed;
            return action;
        }

        public override void Update() {
            this.transform.position = Vector3.MoveTowards(this.transform.position, target, speed*Time.deltaTime);
            if (this.transform.position == target) {
                this.destroy = true;
                this.whoToNotify.actionDone(this);
            }
        }

        public override void Start() {
            //
        }

    }

    public interface ActionCallback {
        void actionDone(ObjAction source);
    }

    public class ActionManager: MonoBehaviour, ActionCallback {
        private Dictionary<int, ObjAction> actions = new Dictionary<int, ObjAction>();
        private List<ObjAction> waitingToAdd = new List<ObjAction>();
        private List<int> watingToDelete = new List<int>();

        protected void Update() {
            foreach(ObjAction ac in waitingToAdd) {
                actions[ac.GetInstanceID()] = ac;
            }
            waitingToAdd.Clear();

            foreach(KeyValuePair<int, ObjAction> kv in actions) {
                ObjAction ac = kv.Value;
                if (ac.destroy) {
                    watingToDelete.Add(ac.GetInstanceID());
                } else if (ac.enable) {
                    ac.Update();
                }
            }

            foreach(int key in watingToDelete) {
                ObjAction ac = actions[key];
                actions.Remove(key);
                DestroyObject(ac);
            }
            watingToDelete.Clear();
        }

        public void addAction(GameObject gameObject, ObjAction action, ActionCallback whoToNotify) {
            action.gameObject = gameObject;
            action.transform = gameObject.transform;
            action.whoToNotify = whoToNotify;
            waitingToAdd.Add(action);
            action.Start();
        }

        public void actionDone(ObjAction source) {

        }

    }
    public class SequenceAction: ObjAction, ActionCallback {
        public List<ObjAction> sequence;
        public int repeat = 1; // 1->only do it for once, -1->repeat forever
        public int currentActionIndex = 0;

        public static SequenceAction getAction(int repeat, int currentActionIndex, List<ObjAction> sequence) {
            SequenceAction action = ScriptableObject.CreateInstance<SequenceAction>();
            action.sequence = sequence;
            action.repeat = repeat;
            action.currentActionIndex = currentActionIndex;
            return action;
        }

        public override void Update() {
            if (sequence.Count == 0)return;
            if (currentActionIndex < sequence.Count) {
                sequence[currentActionIndex].Update();
            }
        }

        public void actionDone(ObjAction source) {
            source.destroy = false;
            this.currentActionIndex++;
            if (this.currentActionIndex >= sequence.Count) {
                this.currentActionIndex = 0;
                if (repeat > 0) repeat--;
                if (repeat == 0) {
                    this.destroy = true;
                    this.whoToNotify.actionDone(this);
                }
            }
        }

        public override void Start() {
            foreach(ObjAction action in sequence) {
                action.gameObject = this.gameObject;
                action.transform = this.transform;
                action.whoToNotify = this;
                action.Start();
            }
        }

        void OnDestroy() {
            foreach(ObjAction action in sequence) {
                DestroyObject(action);
            }
        }
    }


    //-----------------------------------BoatController------------------------------------
    public class BoatController {
        readonly GameObject boat;
        readonly Vector3 fromPosition = new Vector3(4f, -0.5f, 0);
        readonly Vector3 toPosition = new Vector3 (-4f, -0.5f, 0);
        readonly Vector3[] from_positions;
        readonly Vector3[] to_positions;
        public readonly float movingSpeed = 20;

        GameObject cameraObj;
        GameObject lightObj;

        // change frequently
        int to_or_from; // to->-1; from->1
        MyCharacterController[] passenger = new MyCharacterController[2];

        public BoatController() {
            to_or_from = 1;

            from_positions = new Vector3[] { new Vector3 (4.5F, 1.25F, 0), new Vector3 (5.5F, 1.25F, 0) };
            to_positions = new Vector3[] { new Vector3 (-5.5F, 1.25F, 0), new Vector3 (-4.5F, 1.25F, 0) };

            boat = Object.Instantiate (Resources.Load ("prefabs/Boat", typeof(GameObject)), fromPosition, Quaternion.identity, null) as GameObject;
            boat.name = "boat";

            boat.GetComponent<Renderer>().material.color = new Color(0.7569F, 0.6039F, 0.4196F, 1);

        }
        public int currentCharactor(int flag) {  //left -> flag = 1
            if (flag == 1) {
                if (passenger [0] == null)
                    return 0;
                if (passenger [0].getName ().StartsWith ("devil")) {
                    return 1;
                } else if (passenger [0].getName ().StartsWith ("priest")) {
                    return 2;
                }
            } else {
                if (passenger [1] == null)
                    return 0;
                if (passenger [1].getName ().StartsWith ("devil")) {
                    return 1;
                } else if (passenger [1].getName ().StartsWith ("priest")) {
                    return 2;
                }
            }
            return 0;
        }
        public MyCharacterController getCurrentCharactor(int flag) {//1->left
            if (flag == 1) {
                return passenger [0];
            } else {
                return passenger [1];
            }
        }


        public void Move() {
            if (to_or_from == -1) {
                to_or_from = 1;
            } else {
                to_or_from = -1;
            }
        }

        public Vector3 getDestination() {
            if (to_or_from == -1) {
                return fromPosition;
            } else {
                return toPosition;
            }
        }

        public void moved() {
            if (to_or_from == -1) {
                to_or_from = 1;
            } else {
                to_or_from = -1;
            }
        }
        public int getEmptyIndex() {
            for (int i = 0; i < passenger.Length; i++) {
                if (passenger [i] == null) {
                    return i;
                }
            }
            return -1;
        }

        public bool isEmpty() {
            for (int i = 0; i < passenger.Length; i++) {
                if (passenger [i] != null) {
                    return false;
                }
            }
            return true;
        }

        public Vector3 getEmptyPosition() {
            Vector3 pos;
            int emptyIndex = getEmptyIndex ();
            if (to_or_from == -1) {
                pos = to_positions[emptyIndex];
            } else {
                pos = from_positions[emptyIndex];
            }
            return pos;
        }

        public int canGetOnBoat() {
            int index = getEmptyIndex ();
            if (index < 0 || index >= 2) {
                return -1;
            }
            return 0;
        }

        public int GetOnBoat(MyCharacterController characterCtrl) {
            int index = getEmptyIndex ();
            if (index >= 0 && index < 2) {
                passenger [index] = characterCtrl;
                return index;
            }
            return -1;
        }
        public void leftGetOffBoat () {
            passenger [0] = null;
        }
        public void rightGetOffBoat() {
            passenger [1] = null;
        }

        public GameObject getGameobj() {
            return boat;
        }

        public int get_to_or_from() { // to->-1; from->1
            return to_or_from;
        }

        public int[] getCharacterNum() {
            int[] count = {0, 0};
            for (int i = 0; i < passenger.Length; i++) {
                if (passenger [i] == null)
                    continue;
                if (passenger [i].getType () == 0) {    // 0->priest, 1->devil
                    count[0]++;
                } else {
                    count[1]++;
                }
            }
            return count;
        }

        public void reset() {
            if (to_or_from == -1) {
                moved ();
            }
            boat.transform.position = fromPosition;
            passenger [0] = null;
            passenger [1] = null;
        }
    }
//  //---------------------CoastController------------------------------
    public class CoastController {
        readonly GameObject coast;
        readonly Vector3 from_pos = new Vector3(-12f, 0, 0);
        readonly Vector3 to_pos = new Vector3(12f, 0, 0);
        readonly Vector3[] positions;
        readonly int to_or_from;    // to->-1, from->1

        public Stack<MyCharacterController> devils, priests;

        public CoastController(string _to_or_from) {
            devils = new Stack<MyCharacterController>();
            priests = new Stack<MyCharacterController>();
            positions = new Vector3[] {new Vector3(-0.7F,2.25F,0), new Vector3(-0.45F,2.25F,0), new Vector3(-0.2F,2.25F,0), 
                new Vector3(0.2F,2.25F,0), new Vector3(0.45F,2.25F,0), new Vector3(0.7F,2.25F,0)};
            if (_to_or_from == "from") {
                coast = Object.Instantiate (Resources.Load ("prefabs/Shore", typeof(GameObject)), from_pos, Quaternion.identity, null) as GameObject;
                coast.name = "from";
                to_or_from = 1;
            } else {
                coast = Object.Instantiate (Resources.Load ("prefabs/Shore", typeof(GameObject)), to_pos, Quaternion.identity, null) as GameObject;
                coast.name = "to";
                to_or_from = -1;
            }
        }

        public int getPositionEmptyIndex(int flag) {
            if (flag == 1) {
                return priests.Count;
            } else {
                return devils.Count + 3;
            }
        }

        public int getEmptyIndex(int flag) {
            if (flag == 1) {
                return priests.Count;
            } else {
                return devils.Count;
            }
        }

        public MyCharacterController getPriest() {
            if (priests.Count == 0)
                return null;
            return priests.Pop ();
        }
        public MyCharacterController getDevil() {
            if (devils.Count == 0)
                return null;
            return devils.Pop ();
        }

        public Vector3 getDevilsEmptyPosition() {
            Vector3 pos = positions [getPositionEmptyIndex (0)];
            pos.x *= to_or_from;
            return pos;
        }

        public Vector3 getPriestsEmptyPosition() {
            Vector3 pos = positions [getPositionEmptyIndex (1)];
            pos.x *= to_or_from;
            return pos;
        }

        public void devilGetOnCoast(MyCharacterController characterCtrl) {
            devils.Push (characterCtrl);
        }

        public void priestGetOnCoast(MyCharacterController characterCtrl) {
            priests.Push (characterCtrl);
        }

        public int get_to_or_from() {
            return to_or_from;
        }

        public int[] getCharacterNum() {
            int[] count = {0, 0};
            count [0] = priests.Count;
            count [1] = devils.Count;
            return count;
        }

        public GameObject getGameobj() {
            return this.coast;
        }
    }
////---------------------------CharactorController------------------------------
    public class MyCharacterController {
        readonly GameObject character;
        readonly int characterType; // 0->priest, 1->devil
        public readonly float movingSpeed = 20;
        public Vector3 left = new Vector3 (-0.2f, 3f, 0);
        public Vector3 right = new Vector3 (0.2f, 3f, 0);
        public Vector3 shorePos;

        bool _isOnBoat;
        CoastController coastController;


        public MyCharacterController(string which_character) {

            if (which_character == "priest") {
                character = Object.Instantiate (Resources.Load ("prefabs/Priest", typeof(GameObject)), Vector3.zero, Quaternion.identity, null) as GameObject;
                characterType = 0;
                character.GetComponent<Renderer>().material.color = Color.white;
            } else {
                character = Object.Instantiate (Resources.Load ("prefabs/Devil", typeof(GameObject)), Vector3.zero, Quaternion.identity, null) as GameObject;
                characterType = 1;
                character.GetComponent<Renderer>().material.color = Color.red;
            }
        }

        public void setName(string name) {
            character.name = name;
        }

        public void setPosition(Vector3 pos) {
            character.transform.position = pos;
        }
        public void setLocalPosition(Vector3 pos) {
            character.transform.localPosition = pos;
        }

        public void setShorePosition(Vector3 pos) {
            shorePos = pos;
        }


        public int getType() {  // 0->priest, 1->devil
            return characterType;
        }

        public string getName() {
            return character.name;
        }

        public void getOnBoat(BoatController boatCtrl) {
            coastController = null;
            character.transform.parent = boatCtrl.getGameobj().transform;
            _isOnBoat = true; 
        }

        public void getOnCoast(CoastController coastCtrl) {
            coastController = coastCtrl;
            character.transform.parent = coastCtrl.getGameobj().transform;
            _isOnBoat = false;
        }

        public bool isOnBoat() {
            return _isOnBoat;
        }

        public CoastController getCoastController() {
            return coastController;
        }

        public void reset(CoastController cc) {//   have problem
            character.transform.parent = cc.getGameobj ().transform;
            character.transform.localPosition = shorePos;
        }

        public Vector3 getPos() {
            return this.character.transform.position;
        }

        public GameObject getGameobj() {
            return this.character;
        }
    }
}

FirstSceneActionManager.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MyGame;

namespace MyGame {
    public class FirstSceneActionManager: ActionManager {
        public void test() {//test
            Debug.Log ("this is a test for action manager");
        }
        public void moveBoat(BoatController boat) {
            MoveToAction action = MoveToAction.getAction(boat.getDestination(), boat.movingSpeed);
            this.addAction(boat.getGameobj(), action, this);
        }

        public void moveCharacter(MyCharacterController characterCtrl, Vector3 destination) {
            Vector3 currentPos = characterCtrl.getPos();
            Vector3 middlePos = currentPos;
            if (destination.y > currentPos.y) {     //from low(boat) to high(coast)
                middlePos.y = destination.y;
            } else {    //from high(coast) to low(boat)
                middlePos.x = destination.x;
            }
            ObjAction action1 = MoveToAction.getAction(middlePos, characterCtrl.movingSpeed);
            ObjAction action2 = MoveToAction.getAction(destination, characterCtrl.movingSpeed);
            ObjAction seqAction = SequenceAction.getAction(1, 0, new List<ObjAction>{action1, action2});
            this.addAction(characterCtrl.getGameobj(), seqAction, this);
        }
    }
}

GameSceneController.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MyGame;

namespace MyGame {
    public class GameSceneController: MonoBehaviour, newUserActions, SceneController {
        public CoastController fromCoast;
        public CoastController toCoast;
        public BoatController boat;
        private static GameSceneController _instance;
        private GenGameObjects _GenGameObject;
        private MyCharacterController[] characters;
        public SceneController currentSceneController { get; set; }
        private FirstSceneActionManager actionManager;
        UI userGUI;

        int chPos;

        void Awake() {
            Director director = Director.getInstance ();
            director.currentSceneController = this;
            userGUI = gameObject.AddComponent <UI>() as UI;
            characters = new MyCharacterController[6];
            loadResources ();
        }

        void Start() {
            Debug.Log ("create actionManager...");
            actionManager = GetComponent<FirstSceneActionManager>();
        }

        public void loadResources() {

            fromCoast = new CoastController ("from");
            toCoast = new CoastController ("to");
            boat = new BoatController ();

            loadCharacter ();
        }

        private void loadCharacter() {
            for (int i = 0; i < 3; i++) {
                MyCharacterController cha = new MyCharacterController ("priest");
                cha.setName("priest" + i);
                cha.getOnCoast (toCoast);
                cha.setLocalPosition (toCoast.getPriestsEmptyPosition ());
                cha.setShorePosition (toCoast.getPriestsEmptyPosition ());
                toCoast.priestGetOnCoast (cha);
                characters [i] = cha;
            }

            for (int i = 0; i < 3; i++) {
                MyCharacterController cha = new MyCharacterController ("devil");
                cha.setName("devil" + i);
                cha.getOnCoast (toCoast);
                cha.setLocalPosition (toCoast.getDevilsEmptyPosition ());
                cha.setShorePosition(toCoast.getDevilsEmptyPosition ());
                toCoast.devilGetOnCoast (cha);

                characters [i+3] = cha;
            }
        }

        public void moveBoat() {
            Debug.Log ("enter moveBoat in GameSceneController");
            if (boat.isEmpty ())
                return;
            actionManager.moveBoat(boat);
            boat.moved();
            userGUI.status = check_game_over ();
            print (userGUI.status);
        }

        public void priestSOnB() {
            chPos = boat.canGetOnBoat ();
            if (chPos != -1) {
                MyCharacterController ch = toCoast.getPriest();//pop the priest
                if (ch == null)
                    return;
                chPos = boat.GetOnBoat (ch);
                ch.getOnBoat (boat);
                if (chPos == 0) {
                    ch.setLocalPosition (ch.left);
                } else {
                    ch.setLocalPosition (ch.right);
                }
            }
        }
        public void priestEOnB() {
            chPos = boat.canGetOnBoat ();
            if (chPos != -1) {
                MyCharacterController ch = fromCoast.getPriest();
                if (ch == null)
                    return;
                chPos = boat.GetOnBoat (ch);
                ch.getOnBoat (boat);
                if (chPos == 0) {
                    ch.setLocalPosition (ch.left);
                } else {
                    ch.setLocalPosition (ch.right);
                }
            }
        }
        public void devilSOnB() {
            chPos = boat.canGetOnBoat ();
//          Debug.Log (chPos);//test
            if (chPos != -1) {
                MyCharacterController ch = toCoast.getDevil();
                if (ch == null)
                    return;
                chPos = boat.GetOnBoat (ch);
                ch.getOnBoat (boat);
                if (chPos == 0) {
                    ch.setLocalPosition (ch.left);
                } else {
                    ch.setLocalPosition (ch.right);
                }
            }
        }
        public void devilEOnB() {
            chPos = boat.canGetOnBoat ();
            if (chPos != -1) {
                MyCharacterController ch = fromCoast.getDevil();
                if (ch == null)
                    return;
                chPos = boat.GetOnBoat (ch);
                ch.getOnBoat (boat);
                if (chPos == 0) {
                    ch.setLocalPosition (ch.left);
                } else {
                    ch.setLocalPosition (ch.right);
                }
            }
        }
        public void offBoatL() {// return 1 -> devil, 2 -> priest
            if (boat.currentCharactor (1) == 0) return;
            if (boat.currentCharactor (1) == 1) {
                if (boat.get_to_or_from() == -1) {
                    fromCoast.devilGetOnCoast (boat.getCurrentCharactor (1));
                    boat.getCurrentCharactor (1).getOnCoast (fromCoast);
                } else {
                    toCoast.devilGetOnCoast (boat.getCurrentCharactor (1));
                    boat.getCurrentCharactor (1).getOnCoast (toCoast);
                }
                boat.getCurrentCharactor (1).setLocalPosition (boat.getCurrentCharactor (1).shorePos);
            }
            else if (boat.currentCharactor(1) == 2) {
                if (boat.get_to_or_from() == -1) {
                    fromCoast.priestGetOnCoast (boat.getCurrentCharactor (1));
                    boat.getCurrentCharactor (1).getOnCoast (fromCoast);
                } else {
                    toCoast.priestGetOnCoast (boat.getCurrentCharactor (1));
                    boat.getCurrentCharactor (1).getOnCoast (toCoast);
                }
                boat.getCurrentCharactor (1).setLocalPosition (boat.getCurrentCharactor (1).shorePos);
            }
            boat.leftGetOffBoat ();
            userGUI.status = check_game_over ();
        }
        public void offBoatR() {
            if (boat.currentCharactor (2) == 0) return;
            if (boat.currentCharactor (2) == 1) {
                if (boat.get_to_or_from() == -1) {
                    fromCoast.devilGetOnCoast (boat.getCurrentCharactor (2));
                    boat.getCurrentCharactor (2).getOnCoast (fromCoast);
                } else {
                    toCoast.devilGetOnCoast (boat.getCurrentCharactor (2));
                    boat.getCurrentCharactor (2).getOnCoast (toCoast);
                }
                boat.getCurrentCharactor (2).setLocalPosition (boat.getCurrentCharactor (2).shorePos);
            }
            else if (boat.currentCharactor(2) == 2) {
                if (boat.get_to_or_from() == -1) {
                    fromCoast.priestGetOnCoast (boat.getCurrentCharactor (2));
                    boat.getCurrentCharactor (2).getOnCoast (fromCoast);
                } else {
                    toCoast.priestGetOnCoast (boat.getCurrentCharactor (2));
                    boat.getCurrentCharactor (2).getOnCoast (toCoast);
                }
                boat.getCurrentCharactor (2).setLocalPosition (boat.getCurrentCharactor (2).shorePos);
            }
            boat.rightGetOffBoat ();
            userGUI.status = check_game_over ();
        }

        public void restart() {
            if (boat.currentCharactor (1) == 1) {
                MyCharacterController cha = boat.getCurrentCharactor (1);
                cha.getOnCoast (toCoast);
                toCoast.devilGetOnCoast (cha);
                cha.reset (toCoast);
            } else if (boat.currentCharactor (1) == 2) {
                MyCharacterController cha = boat.getCurrentCharactor (1);
                cha.getOnCoast (toCoast);
                toCoast.priestGetOnCoast (cha);
                cha.reset (toCoast);
            }
            if (boat.currentCharactor (2) == 1) {
                MyCharacterController cha = boat.getCurrentCharactor (2);
                cha.getOnCoast (toCoast);
                toCoast.devilGetOnCoast (cha);
                cha.reset (toCoast);
            } else if (boat.currentCharactor (2) == 2) {
                MyCharacterController cha = boat.getCurrentCharactor (2);
                cha.getOnCoast (toCoast);
                toCoast.priestGetOnCoast (cha);
                cha.reset (toCoast);
            }
            int devil_num = fromCoast.devils.Count;
            for (int i = 0; i < devil_num; i++) {
                MyCharacterController cha = fromCoast.getDevil ();
                cha.reset (toCoast);
                toCoast.devils.Push (cha);
            } 
            int priest_num = fromCoast.priests.Count;
            for (int i = 0; i < priest_num; i++) {
                MyCharacterController cha = fromCoast.getPriest ();
                cha.reset (toCoast);
                toCoast.priests.Push (cha);
            }
            boat.reset ();
        }

        int check_game_over() { // 0->not finish, 1->lose, 2->win
            int from_priest = fromCoast.priests.Count;
            int from_devil = fromCoast.devils.Count;
            int to_priest = toCoast.priests.Count;
            int to_devil = toCoast.devils.Count;

            if (fromCoast.devils.Count + fromCoast.priests.Count == 6)      // win
                return 2;

            if (boat.get_to_or_from () == 1) {  // boat at toCoast
                if (boat.currentCharactor (1) == 1) {//left is devil
                    to_devil++;
                } else if (boat.currentCharactor (1) == 2) {//left is priest
                    to_priest++;
                }
                if (boat.currentCharactor (2) == 1) {//right is devil
                    to_devil++;
                }
                else if (boat.currentCharactor (2) == 2) {
                    to_priest++;
                }
            } else {    // boat at fromCoast
                if (boat.currentCharactor (1) == 1) {//left is devil
                    from_devil++;
                } else if (boat.currentCharactor (1) == 2) {//left is priest
                    from_priest++;
                }
                if (boat.currentCharactor (2) == 1) {//right is devil
                    from_devil++;
                }
                else if (boat.currentCharactor (2) == 2) {
                    from_priest++;
                }
            }
            if (from_priest < from_devil && from_priest > 0) {      // lose
                return 1;
            }
            if (to_priest < to_devil && to_priest > 0) {
                return 1;
            }
            return 0;           // not finish
        }

        public GenGameObjects getGenGameObject() {
            return _GenGameObject;
        }

        internal void setGenGameObject(GenGameObjects ggo) {
            if (_GenGameObject == null) {
                _GenGameObject = ggo;
            }
        }
    };
}

GenGameObj.cs:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MyGame;

public class GenGameObjects : MonoBehaviour {

    Stack<GameObject> priests_start = new Stack<GameObject>();
    Stack<GameObject> priests_end = new Stack<GameObject>();
    Stack<GameObject> devils_start = new Stack<GameObject>();
    Stack<GameObject> devils_end = new Stack<GameObject>();

    GameObject[] boat = new GameObject[2];
    GameObject boat_obj;
    public float speed = 100f;

    GameSceneController my;

    Vector3 shore_begin = new Vector3(-12f, 0, 0);
    Vector3 shore_end = new Vector3(12f, 0, 0);
    Vector3 boat_begin = new Vector3(-4f, -0.5f, 0);
    Vector3 boat_end = new Vector3(4f, -0.5f, 0);

    float gap = 1.5f;

    Vector3 priestStartPos = new Vector3(-11f, 2.7f, 0);
    Vector3 priestEndPos = new Vector3(8f, 2.7f, 0);
    Vector3 devilStartPos = new Vector3(-16f, 2.7f, 0);
    Vector3 devilEndPos = new Vector3(13f, 2.7f, 0);

    void setCharactorPosition(Stack<GameObject> stack, Vector3 pos) {
        GameObject[] arr = stack.ToArray ();
        for (int i = 0; i < stack.Count; i++) {
            arr [i].transform.position = new Vector3 (pos.x + gap * i, pos.y, pos.z);
        }
    }

    void Update () {

    }
} 

UI.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MyGame;

public class UI : MonoBehaviour {

    GameSceneController my;  
    newUserActions action;  
    public int status = 0;
    GUIStyle style;
    GUIStyle buttonStyle;

    float width, height;  

    float castw(float scale) {  
        return (Screen.width - width) / scale;  
    }  

    float casth(float scale) {  
        return (Screen.height - height) / scale;  
    }  

    void Start() {  
        Debug.Log ("Start GUI...");
        action = Director.getInstance().currentSceneController as newUserActions; 
        style = new GUIStyle();
        style.fontSize = 40;
        style.alignment = TextAnchor.MiddleCenter;

        buttonStyle = new GUIStyle("button");
        buttonStyle.fontSize = 30;
    }  

    void OnGUI() {  
        width = Screen.width / 12;  
        height = Screen.height / 12;  
        if (status == 1) {
            GUI.Label(new Rect(Screen.width/2-50, Screen.height/2-85, 100, 50), "Gameover!", 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-50, Screen.height/2-85, 100, 50), "You win!", style);
            if (GUI.Button(new Rect(Screen.width/2-70, Screen.height/2, 140, 70), "Restart", buttonStyle)) {
                status = 0;
                action.restart ();
            }
        }  
        else { 
                if (GUI.Button(new Rect(castw(2f), casth(6f), width, height), "Go")) {
                    action.moveBoat();  
                }  
                if (GUI.Button(new Rect(castw(10.5f), casth(4f), width, height), "On")) { 
                    action.devilEOnB();  
                }  
                if (GUI.Button(new Rect(castw(4.3f), casth(4f), width, height), "On")) {  
                    action.priestEOnB();  
                }  
                if (GUI.Button(new Rect(castw(1.1f), casth(4f), width, height), "On")) {
                print ("enter action.devilEonB");//test
                    action.priestSOnB();  
                }  
                if (GUI.Button(new Rect(castw(1.3f), casth(4f), width, height), "On")) {  
                    action.devilSOnB();  
                }  
                if (GUI.Button(new Rect(castw(2.5f), casth(1.3f), width, height), "Off")) {  
                    action.offBoatL();  
                }  
                if (GUI.Button(new Rect(castw(1.7f), casth(1.3f), width, height), "Off")) {  
                    action.offBoatR();  
                }  
//          }  
        }  
    }  
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值