Esempio n. 1
0
    /// <summary>
    /// 非房主产生NPC对象的方法,非房主产生的NPC无AI系统也无同步系统,其主要根据房主的NPC进行同步.
    /// </summary>
    /// <param name="id"></param>
    public void AddNetworkNpc(string id, Vector3 pos, string unitFaction)
    {
        if (!networkPlayers.ContainsKey(id))
        {
            CharacterModel characterModel = NpcPrefab.GetComponent <CharacterMono>().characterModel.DeepCopy();
            if (unitFaction == "Red")
            {
                characterModel.unitFaction = UnitFaction.Red;
            }
            else
            {
                characterModel.unitFaction = UnitFaction.Blue;
            }

            // 如果本次生成的对象不是旧对象,那么将该对象放到networkPlayers字典中去,同时给对象增加AI系统和同步系统
            CharacterMono NPC = CharacterMonoFactory.AcquireObject(characterModel, NpcPrefab, pos);

            networkPlayers[id] = NPC.gameObject;

            // 设置网络ID
            NPC.GetComponent <CharacterMono>().NetWorkPlayerID     = id;
            NPC.GetComponent <CharacterMono>().characterModel.Name = id;

            // 给产生的网络对象附加伤害同步的监听方法
            NPC.GetComponent <CharacterMono>().characterModel.OnDamaged += DamageSynchronize;
        }
    }
Esempio n. 2
0
    // 开始游戏的方法
    private void StartGame()
    {
        characterMono = CharacterMonoFactory.AcquireObject(TestDatabase.Instance.characterModels[0], characterPrafab, new Vector3(16, 0f, 15)) as HeroMono;
        characterMono.Init();
        #region Test
        characterMono.IsOperateByNowPlayer = true;
        GameObject test = GameObject.Instantiate(operateCharacterProjector, characterMono.transform, false);
        test.transform.localPosition = new Vector3(0, 2, 0);
        #endregion

        InitUI();
        InitGamePlay();

        if (NetWorkManager.Instance == null)
        {
            new GameObject().AddComponent <NetWorkManager>();
        }


        // 判断此时是在联机对战还是在单人游戏
        if (NetWorkManager.Instance.IsConnect())
        {
            gamePlayManager.OnCreateNPCGameObject += NetWorkManager.Instance.AddNetworkNpc;

            // 联机对战

            Debug.Log("根据当前玩家的阵营:" + NetWorkManager.Instance.NowPlayerFaction + "来创建玩家");
            if (NetWorkManager.Instance.NowPlayerFaction == "Red")
            {
                characterMono.characterModel.unitFaction = UnitFaction.Red;
                NetWorkManager.Instance.StartGame(new Vector3(17, 0, 18), characterMono);
                Camera.main.transform.position = new Vector3(15, Camera.main.transform.position.y, 6);
            }
            else
            {
                characterMono.characterModel.unitFaction = UnitFaction.Blue;
                NetWorkManager.Instance.StartGame(new Vector3(181, 0, 179), characterMono);
                Camera.main.transform.position = new Vector3(181, Camera.main.transform.position.y, 179);
            }

            Debug.Log("当前玩家是否是房主:" + NetWorkManager.Instance.IsHomeowner);
            // 判断当前玩家是否是房主,如果是房主,开启出兵,并监听
            if (NetWorkManager.Instance.IsHomeowner)
            {
                gamePlayManager.Init();
            }
        }
        else
        {
            gamePlayManager.Init();
            Camera.main.transform.position = new Vector3(15, Camera.main.transform.position.y, 6);
            characterMono.GetComponent <CharacterOperationFSM>().enabled = true;
        }
    }
Esempio n. 3
0
    /// <summary>
    /// 产生一个网络对象
    /// </summary>
    /// <param name="id"></param>
    /// <param name="position"></param>
    public void AddNetworkPlayer(string id, Vector3 position, CharacterModel characterModel)
    {
        //GameObject player = GameObject.Instantiate(playerPrefab, position, Quaternion.identity);
        CharacterMono player = CharacterMonoFactory.AcquireObject(characterModel, playerPrefab, position);

        player.transform.position = new Vector3(player.transform.position.x, 0.5f, player.transform.position.z);

        // 设置CharacterMono的网络ID
        player.GetComponent <CharacterMono>().NetWorkPlayerID = id;

        // 给产生的网络对象附加伤害同步的监听方法
        player.GetComponent <HeroMono>().characterModel.OnDamaged += DamageSynchronize;

        networkPlayers.Add(id, player.gameObject);
    }