コード例 #1
0
ファイル: Main.cs プロジェクト: atgithub20196/socketBook3
    private void OnEnter(string msgArgs)
    {
        Debug.Log("OnEnter " + msgArgs);
        //解析参数
        string[] split = msgArgs.Split(',');
        string   desc  = split[0];
        float    x     = float.Parse(split[1]);
        float    y     = float.Parse(split[2]);
        float    z     = float.Parse(split[3]);
        float    eulY  = float.Parse(split[4]);

        //是自己
        if (desc == NetManager.GetDesc())
        {
            return;
        }
        //添加一个角色
        GameObject obj = (GameObject)Instantiate(humanPrefab);

        obj.transform.position    = new Vector3(x, y, z);
        obj.transform.eulerAngles = new Vector3(0, eulY, 0);
        BaseHuman h = obj.AddComponent <SyncHuman>();

        h.desc = desc;
        otherHumans.Add(desc, h);
    }
コード例 #2
0
ファイル: Main.cs プロジェクト: atgithub20196/socketBook3
    void Start()
    {
        //网络模块
        NetManager.AddListener("Enter", OnEnter);
        NetManager.AddListener("List", OnList);
        NetManager.AddListener("Move", OnMove);
        NetManager.AddListener("Leave", OnLeave);
        NetManager.AddListener("Attack", OnAttack);
        NetManager.AddListener("Die", OnDie);
        NetManager.Connect("127.0.0.1", 8888);

        //添加一个角色
        GameObject obj = (GameObject)Instantiate(humanPrefab);
        float      x   = UnityEngine.Random.Range(-5, 5);
        float      z   = UnityEngine.Random.Range(-5, 5);

        obj.transform.position = new Vector3(x, 0, z);
        myHuman      = obj.AddComponent <CtrlHuman>();
        myHuman.desc = NetManager.GetDesc();
        //发送协议
        Vector3 pos     = myHuman.transform.position;
        Vector3 eul     = myHuman.transform.eulerAngles;
        string  sendStr = "Enter|";

        sendStr += NetManager.GetDesc() + ",";
        sendStr += pos.x + ",";
        sendStr += pos.y + ",";
        sendStr += pos.z + ",";
        sendStr += eul.y + ",";
        NetManager.Send(sendStr);
        //NetManager.sendDone.WaitOne();
        NetManager.Send("List|");
    }
コード例 #3
0
ファイル: Main.cs プロジェクト: atgithub20196/socketBook3
    private void OnList(string msgArgs)
    {
        Debug.Log("OnList " + msgArgs);
        //解析参数
        string[] split = msgArgs.Split(',');
        int      count = (split.Length - 1) / 6;

        for (int i = 0; i < count; i++)
        {
            string desc = split[i * 6 + 0];
            float  x    = float.Parse(split[i * 6 + 1]);
            float  y    = float.Parse(split[i * 6 + 2]);
            float  z    = float.Parse(split[i * 6 + 3]);
            float  eulY = float.Parse(split[i * 6 + 4]);
            int    hp   = int.Parse(split[i * 6 + 5]);
            //是自己
            if (desc == NetManager.GetDesc())
            {
                continue;
            }
            //添加一个角色
            GameObject obj = (GameObject)Instantiate(humanPrefab);
            obj.transform.position    = new Vector3(x, y, z);
            obj.transform.eulerAngles = new Vector3(0, eulY, 0);
            BaseHuman h = obj.AddComponent <SyncHuman>();
            h.desc = desc;
            otherHumans.Add(desc, h);
        }
    }
コード例 #4
0
    // Start is called before the first frame update
    void Start()
    {
        NetManager.AddListener("Enter", OnEnter);
        NetManager.AddListener("Move", OnMove);
        NetManager.AddListener("Leave", OnLeave);
        NetManager.Connect("127.0.0.1", 8888);
        GameObject obj = (GameObject)Instantiate(humanPrefab);
        float      x   = Random.Range(-5, 5);
        float      z   = Random.Range(-5, 5);

        obj.transform.position = new Vector3(x, 0, z);
        myHuman      = obj.AddComponent <CtrlHuman>();
        myHuman.desc = NetManager.GetDesc();

        Vector3 position = myHuman.transform.position;
        Vector3 eul      = myHuman.transform.eulerAngles;
        string  sendStr  = "Enter|";

        sendStr += NetManager.GetDesc() + ",";
        sendStr += position.x + ",";
        sendStr += position.y + ",";
        sendStr += position.z + ",";
        sendStr += eul.y;
        NetManager.Send(sendStr);

        NetManager.Send("List|");
    }
コード例 #5
0
    void Start()
    {
        startPos = player.transform.position;

        foreach (UnityEngine.UI.Button btn in objs)
        {
            btn.onClick.AddListener(() => { triggerAttack = true; });
        }

        aggressor     = playerChar.GetComponent <BaseHuman>();
        defendor      = enemyChar.GetComponent <BaseHuman>();
        aggressorSTR  = aggressor.STR;
        defendorCurHP = defendor.curHP;
        defendorDEF   = defendor.DEF;
    }
コード例 #6
0
    void OnLeave(string msg)
    {
        Debug.Log("OnLeave:" + msg);
        string[] split = msg.Split(',');
        string   desc  = split[0];

        if (!otherHumans.ContainsKey(desc))
        {
            return;
        }
        BaseHuman h = otherHumans[desc];

        Destroy(h.gameObject);
        otherHumans.Remove(desc);
    }
コード例 #7
0
ファイル: Main.cs プロジェクト: atgithub20196/socketBook3
    private void OnLeave(string msgArgs)
    {
        Debug.Log("OnLeave " + msgArgs);
        //解析参数
        string[] split = msgArgs.Split(',');
        string   desc  = split[0];

        //删除
        if (!otherHumans.ContainsKey(desc))
        {
            return;
        }
        BaseHuman h = otherHumans[desc];

        Destroy(h.gameObject);
        otherHumans.Remove(desc);
    }
コード例 #8
0
    void OnMove(string msg)
    {
        Debug.Log("OnMove:" + msg);
        string[] split = msg.Split(',');
        string   desc  = split[0];
        float    x     = float.Parse(split[1]);
        float    y     = float.Parse(split[2]);
        float    z     = float.Parse(split[3]);

        if (!otherHumans.ContainsKey(desc))
        {
            return;
        }
        BaseHuman h         = otherHumans[desc];
        Vector3   targetPos = new Vector3(x, y, z);

        h.MoveTo(targetPos);
    }
コード例 #9
0
    void OnDie(string msgArgs)
    {
        Debug.Log("OnLeave:" + msgArgs);
        //解析参数
        string[] split   = msgArgs.Split(',');
        string   hitDesc = split[0];

        if (hitDesc == myHuman.desc)
        {
            Debug.Log("Game Over");
            return;
        }
        if (!otherHumans.ContainsKey(hitDesc))
        {
            return;
        }
        BaseHuman h = otherHumans[hitDesc];

        h.gameObject.SetActive(false);
    }
コード例 #10
0
    private void InitHumanSelf()
    {
        //添加一个角色
        GameObject obj = (GameObject)Instantiate(humanPrefab);
        float      x   = Random.Range(-5, 5);
        float      z   = Random.Range(-5, 5);

        obj.transform.position = new Vector3(x, 0, z);
        myHuman      = obj.AddComponent <CtrlHuman>();
        myHuman.desc = SimpleNetManager.GetDesc();

        //发送协议
        Vector3 pos     = myHuman.transform.position;
        Vector3 eul     = myHuman.transform.eulerAngles;
        string  sendStr = "Enter|";

        sendStr += SimpleNetManager.GetDesc() + ",";
        sendStr += pos.x + ",";
        sendStr += pos.y + ",";
        sendStr += pos.z + ",";
        sendStr += eul.y;
        SimpleNetManager.Send(sendStr);
    }
コード例 #11
0
        public static void MenuClientMain()
        {
            bool pleaseContinue = true;
            //field for the menu
            string reply    = "";
            string replyZoo = "";

            //a do while declaration
            do
            {
                //displaying the menu
                DisplayMenu();
                //getting input from the user
                reply = Input();
                //using a switch case for their answer
                switch (reply)
                {
                //case 1 | Human
                case "1":
                    //clear the screen
                    ClearScreen();
                    //create a new base human
                    BaseHuman aHuman = BaseHuman.getDecoupledHuman();
                    //using the interfaces
                    aHuman.Name("Karna");
                    aHuman.Age(25);
                    aHuman.Gender("female");
                    aHuman.Eat();
                    aHuman.Eat("spagetti");
                    aHuman.Eat("spagetti", "utensils");
                    aHuman.Fly();
                    aHuman.Fly(35000);
                    aHuman.Swim();
                    aHuman.Swim(10);
                    aHuman.Reproduce();
                    aHuman.Reproduce(2);
                    aHuman.Move();
                    aHuman.Move(8);
                    //LogIt method saying that the user instantiated a BaseHuman
                    Utilities.LogIt("MenuClient::The user instantiated a BaseHuman.\n",
                                    Utilities.MessageSeverity.INFORMATIONAL, true);
                    #region Old Code for Human...
                    //Human aHuman = new Human();
                    //aHuman.Name = "Karna";
                    //aHuman.Age = 25;
                    //aHuman.BrainSize = Primate.BrainSIZE.HUMAN_LARGE;
                    //aHuman.GenderType = Animal.GenderTYPE.FEMALE;
                    //aHuman.HasHairOrFur = true;
                    //aHuman.HasOpposableThumbs = true;
                    //aHuman.IsWarmBlooded = true;
                    //aHuman.JobTitle = "Programmer";
                    //aHuman.Salary = 65432;
                    //aHuman.Eat();
                    //aHuman.Eat("mashed potatoes");
                    //aHuman.Eat("mashed potatoes", "utensils");
                    //aHuman.Reproduce();
                    //aHuman.Play();
                    //aHuman.Move();
                    //aHuman.Work();
                    #endregion
                    //making sure to break
                    break;

                //case 2 | Duck
                case "2":
                    //clearing the screen
                    ClearScreen();
                    //create a new base Duck
                    BaseDuck aDuck = BaseDuck.getDecoupledDuck();
                    //using the interfaces
                    aDuck.Name("Daffy");
                    aDuck.Age(2);
                    aDuck.Gender("Male");
                    aDuck.Eat();
                    aDuck.Eat("fish");
                    aDuck.Eat("fish", "mouth");
                    aDuck.Reproduce();
                    aDuck.Reproduce(5);
                    aDuck.Fly();
                    aDuck.Fly(500);
                    aDuck.Swim();
                    aDuck.Swim(1);
                    aDuck.Move();
                    aDuck.Move(2);
                    //LogIt method saying that the user instantiated a BaseDuck
                    Utilities.LogIt("MenuClient::The user instantiated a BaseDuck.\n",
                                    Utilities.MessageSeverity.INFORMATIONAL, true);
                    #region Old Code For Duck...
                    //Duck aDuck = new Duck();
                    //aDuck.Name = "Joe";
                    //aDuck.Age = 2;
                    //aDuck.BirdAnatomy = Bird.BirdANATOMY.FEATHERS;
                    //aDuck.CanFly = true;
                    //aDuck.Color = "green";
                    //aDuck.GenderType = Animal.GenderTYPE.MALE;
                    //aDuck.IsAquatic = false;
                    //aDuck.Type = "Mallard";
                    //aDuck.Swim();
                    //aDuck.Reproduce();
                    //aDuck.Move();
                    //aDuck.LayEggs();
                    //aDuck.Fly();
                    //aDuck.Eat();
                    //aDuck.Eat("fish");
                    //aDuck.Eat("fish", "mouth");
                    #endregion
                    break;

                //case 3 | Trout
                case "3":
                    //clearing the screen
                    ClearScreen();
                    //create a new base Trout
                    BaseTrout aTrout = BaseTrout.getDecoupledTrout();
                    //using the interfaces
                    aTrout.Name("Phil");
                    aTrout.Age(1);
                    aTrout.Gender("Male");
                    aTrout.Eat();
                    aTrout.Eat("fish");
                    aTrout.Eat("fish", "mouth");
                    aTrout.Reproduce();
                    aTrout.Reproduce(15);
                    aTrout.Swim();
                    aTrout.Swim(4);
                    aTrout.Move();
                    aTrout.Move(3);
                    //LogIt method saying that the user instantiated a BaseTrout
                    Utilities.LogIt("MenuClient::The user instantiated a BaseTrout.\n",
                                    Utilities.MessageSeverity.INFORMATIONAL, true);
                    #region Old Code for Trout...
                    //Trout aTrout = new Trout();
                    //aTrout.Name = "Adele";
                    //aTrout.Age = 3;
                    //aTrout.CanLayEggs = true;
                    //aTrout.Diet = "other fish";
                    //aTrout.FishWaterType = Fish.FishWaterTYPE.FRESH_WATER;
                    //aTrout.GenderType = Animal.GenderTYPE.FEMALE;
                    //aTrout.HasScales = true;
                    //aTrout.TroutHabitatLocation = Trout.TroutHabitatLOCATION.NORTH_AMERICA;
                    //aTrout.Type = "Rainbow";
                    //aTrout.Eat();
                    //aTrout.Eat("other fish");
                    //aTrout.Eat("other fish", "mouth");
                    //aTrout.AvoidAnglers();
                    //aTrout.Swim();
                    //aTrout.Reproduce();
                    //aTrout.LayEggs();
                    #endregion
                    break;

                //Case 4 | Zoo
                case "4":
                    //creating a new zoo collection object
                    ZooCollection myZoo = new ZooCollection();

                    //adding objects into the collection
                    myZoo.Add(new Human());
                    myZoo.Add(new Duck());
                    myZoo.Add(new Trout());

                    //using a Each method in the ZooCollection class
                    //that has a foreach method to go through the items
                    //and print out the different methods
                    myZoo.Each();

                    //method demonstrating the casting
                    myZoo.Cast();

                    //LogIt method saying that the user created a zoo.
                    Utilities.LogIt("MenuClient::The user created a zoo.\n",
                                    Utilities.MessageSeverity.INFORMATIONAL, true);

                    //creating a new interface zoo collection object
                    InterfaceZooCollection interfaceZoo = new InterfaceZooCollection();

                    //adding decoupled objects into the collection
                    interfaceZoo.Add(BaseHuman.getDecoupledHuman());
                    interfaceZoo.Add(BaseDuck.getDecoupledDuck());
                    interfaceZoo.Add(BaseTrout.getDecoupledTrout());

                    //using a Each method in the InterfaceZooCollection class
                    //that has a foreach method to go through the items
                    //and print out the different methods
                    interfaceZoo.Each();

                    //method demonstrating the casting
                    interfaceZoo.Cast();

                    //LogIt method saying that the user created a zoo.
                    Utilities.LogIt("MenuClient::The user created a interface zoo.\n",
                                    Utilities.MessageSeverity.INFORMATIONAL, true);

                    //clearing the zoos
                    myZoo.Clear();
                    interfaceZoo.Clear();

                    //breaking from the case
                    break;

                //Case 5 | Exit
                case "5":
                    //clearing the screen
                    ClearScreen();
                    //setting pleaseContinue to false
                    pleaseContinue = false;
                    Utilities.LogIt("MenuClient::The user exited the MenuClient's menu.\n",
                                    Utilities.MessageSeverity.INFORMATIONAL, true);
                    //breaking from the case
                    break;

                //the default case
                default:
                    //clearing the screen
                    ClearScreen();
                    //try to throw a new exception
                    try
                    {
                        //throwing my custom exception
                        throw new CustomExceptions();
                    }
                    //catching the exception
                    catch (CustomExceptions)
                    {
                        //logging the exception
                        Utilities.LogIt("You entered an invalid option in the MenuClient menu!\nTry again!\n",
                                        Utilities.MessageSeverity.ERROR, true);
                    }
                    //breaking from the case
                    break;
                }
                //while pleaseContinue is true
            } while (pleaseContinue);
        }