Inheritance: NetworkBehaviour
示例#1
0
        void Start()
        {
            //If you need to use a different design instead of checking for hasAuthority, then it means
            //you will have to figure out how to do what you need to do, and this example will not
            //be sufficient enough to teach you more than given.
            if (!this.hasAuthority)
            {
                return;
            }

            this.isDead = false;

            this.InitializeList();
            this.selectionBox = new Rect();

            GameObject[] selectionManagers = GameObject.FindGameObjectsWithTag("SelectionManager");
            foreach (GameObject manager in selectionManagers)
            {
                SelectionManager selectManager = manager.GetComponent <SelectionManager>();
                if (selectManager == null || !selectManager.hasAuthority)
                {
                    continue;
                }

                GameObject[] units = GameObject.FindGameObjectsWithTag("Unit");
                foreach (GameObject unit in units)
                {
                    GameUnit gameUnit = unit.GetComponent <GameUnit>();
                    if (gameUnit != null && !gameUnit.hasAuthority)
                    {
                        continue;
                    }
                    selectManager.allObjects.Add(unit);
                }
            }
        }
示例#2
0
        public void RpcSplit(GameObject obj, GameObject split, float angle, bool hasAuthority, float splitFactor)
        {
            //We do not call on NetworkServer methods here. This is used only to sync up with the original game unit for all clients.
            //This includes adding the newly spawned game unit into the Selection Manager that handles keeping track of all game units.
            if (obj == null || split == null)
            {
                return;
            }

            GameUnit original = obj.GetComponent <GameUnit>();
            GameUnit copy     = split.GetComponent <GameUnit>();

            if (original.unitAttributes == null)
            {
                GameObject attrObj = GameObject.FindGameObjectWithTag("UnitAttributes");
                if (obj != null)
                {
                    original.unitAttributes = attrObj.GetComponent <UnitAttributes>();
                    if (original.unitAttributes == null)
                    {
                        Debug.LogError("Unit attributes are missing from original unit.");
                    }
                }
            }
            if (copy.unitAttributes == null)
            {
                GameObject attrObj = GameObject.FindGameObjectWithTag("UnitAttributes");
                if (obj != null)
                {
                    copy.unitAttributes = attrObj.GetComponent <UnitAttributes>();
                    if (copy.unitAttributes == null)
                    {
                        Debug.LogError("Unit attributes are missing from copy unit.");
                    }
                }
            }

            NavMeshAgent originalAgent = obj.GetComponent <NavMeshAgent>();

            originalAgent.ResetPath();
            NavMeshAgent copyAgent = split.GetComponent <NavMeshAgent>();

            copyAgent.ResetPath();

            GameObject[] splitManagerGroup = GameObject.FindGameObjectsWithTag("SplitManager");
            if (splitManagerGroup.Length > 0)
            {
                for (int i = 0; i < splitManagerGroup.Length; i++)
                {
                    SplitManager manager = splitManagerGroup[i].GetComponent <SplitManager>();
                    if (manager != null && manager.hasAuthority == hasAuthority)
                    {
                        manager.splitGroupList.Add(new SplitGroup(original, copy, angle, splitFactor));
                        if (manager.selectionManager == null)
                        {
                            GameObject[] objs = GameObject.FindGameObjectsWithTag("SelectionManager");
                            foreach (GameObject select in objs)
                            {
                                SelectionManager selectManager = select.GetComponent <SelectionManager>();
                                if (selectManager.hasAuthority)
                                {
                                    manager.selectionManager = selectManager;
                                }
                            }
                        }
                        manager.selectionManager.allObjects.Add(split);
                    }
                }
            }
        }
示例#3
0
        //Split Manager is designed to streamline the creation of new game units.
        //To achieve this, there needs to be two different array list that keeps track of all the creations, called Split Groups.
        //One keeps track of the Split Groups, the other removes them from the tracking list.

        public void Start()
        {
            if (!this.hasAuthority)
            {
                return;
            }

            if (this.splitGroupList == null)
            {
                this.splitGroupList = new List <SplitGroup>();
            }
            if (this.selectionManager == null)
            {
                GameObject[] managers = GameObject.FindGameObjectsWithTag("SelectionManager");
                foreach (GameObject manager in managers)
                {
                    SelectionManager select = manager.GetComponent <SelectionManager>();
                    if (select != null && select.hasAuthority)
                    {
                        this.selectionManager = select;
                        break;
                    }
                }
                if (this.selectionManager == null)
                {
                    Debug.LogError("Cannot find Selection Manager. Aborting");
                }
            }
            if (this.unitAttributes == null)
            {
                GameObject[] attributes = GameObject.FindGameObjectsWithTag("UnitAttributes");
                foreach (GameObject attribute in attributes)
                {
                    UnitAttributes attr = attribute.GetComponent <UnitAttributes>();
                    if (attr != null && attr.hasAuthority)
                    {
                        this.unitAttributes = attr;
                        break;
                    }
                }
                if (this.unitAttributes == null)
                {
                    Debug.LogError("Split Manager: Unit Attributes Tracker is null. Please check.");
                }
            }
            if (this.spawner == null)
            {
                GameObject[] spawners = GameObject.FindGameObjectsWithTag("Spawner");
                foreach (GameObject obj in spawners)
                {
                    Spawner spawner = obj.GetComponent <Spawner>();
                    if (spawner != null && spawner.hasAuthority)
                    {
                        this.spawner = spawner;
                        break;
                    }
                }
                if (this.spawner == null)
                {
                    Debug.LogError("Spawner is never set. Please check.");
                }
            }
            if (this.unitParent == null)
            {
                this.unitParent = new GameObject("Units Parent").transform;
                NetworkIdentity identity = this.unitParent.gameObject.AddComponent <NetworkIdentity>();
                identity.localPlayerAuthority = true;
                ClientScene.RegisterPrefab(this.unitParent.gameObject);
                this.unitParent.SetParent(this.transform);
                if (this.selectionManager != null)
                {
                    foreach (GameObject obj in this.selectionManager.allObjects)
                    {
                        obj.transform.SetParent(this.unitParent);
                    }
                }
                NetworkIdentity ident = this.GetComponent <NetworkIdentity>();
                if (ident != null)
                {
                    ident.localPlayerAuthority = true;
                    CmdSpawn(this.unitParent.gameObject);
                    Debug.Log("Spawning a new unit parent with client authority owner.");
                }
                else
                {
                    Debug.LogError("Check to make sure this is created in the spawner.");
                }
            }
        }
示例#4
0
        //Split Manager is designed to streamline the creation of new game units.
        //To achieve this, there needs to be two different array list that keeps track of all the creations, called Split Groups.
        //One keeps track of the Split Groups, the other removes them from the tracking list.
        public void Start()
        {
            if (!this.hasAuthority) {
                return;
            }

            if (this.splitGroupList == null) {
                this.splitGroupList = new List<SplitGroup>();
            }
            if (this.selectionManager == null) {
                GameObject[] managers = GameObject.FindGameObjectsWithTag("SelectionManager");
                foreach (GameObject manager in managers) {
                    SelectionManager select = manager.GetComponent<SelectionManager>();
                    if (select != null && select.hasAuthority) {
                        this.selectionManager = select;
                        break;
                    }
                }
                if (this.selectionManager == null) {
                    Debug.LogError("Cannot find Selection Manager. Aborting");
                }
            }
            if (this.unitAttributes == null) {
                GameObject[] attributes = GameObject.FindGameObjectsWithTag("UnitAttributes");
                foreach (GameObject attribute in attributes) {
                    UnitAttributes attr = attribute.GetComponent<UnitAttributes>();
                    if (attr != null && attr.hasAuthority) {
                        this.unitAttributes = attr;
                        break;
                    }
                }
                if (this.unitAttributes == null) {
                    Debug.LogError("Split Manager: Unit Attributes Tracker is null. Please check.");
                }
            }
            if (this.spawner == null) {
                GameObject[] spawners = GameObject.FindGameObjectsWithTag("Spawner");
                foreach (GameObject obj in spawners) {
                    Spawner spawner = obj.GetComponent<Spawner>();
                    if (spawner != null && spawner.hasAuthority) {
                        this.spawner = spawner;
                        break;
                    }
                }
                if (this.spawner == null) {
                    Debug.LogError("Spawner is never set. Please check.");
                }
            }
            if (this.unitParent == null) {
                this.unitParent = new GameObject("Units Parent").transform;
                NetworkIdentity identity = this.unitParent.gameObject.AddComponent<NetworkIdentity>();
                identity.localPlayerAuthority = true;
                ClientScene.RegisterPrefab(this.unitParent.gameObject);
                this.unitParent.SetParent(this.transform);
                if (this.selectionManager != null) {
                    foreach (GameObject obj in this.selectionManager.allObjects) {
                        obj.transform.SetParent(this.unitParent);
                    }
                }
                NetworkIdentity ident = this.GetComponent<NetworkIdentity>();
                if (ident != null) {
                    ident.localPlayerAuthority = true;
                    CmdSpawn(this.unitParent.gameObject);
                    Debug.Log("Spawning a new unit parent with client authority owner.");
                }
                else {
                    Debug.LogError("Check to make sure this is created in the spawner.");
                }
            }
        }
示例#5
0
        public void ServerInitialize()
        {
            //Server code
            //This is run for spawning new non-player objects. Since it is a server calling to all clients (local and remote), it needs to pass in a
            //NetworkConnection that connects from server to THAT PARTICULAR client, who is going to own client authority on the spawned object.

            //Checking if global manager exists;
            GlobalManager globalManagerObject = GameObject.FindObjectOfType <GlobalManager>();

            if (globalManagerObject != null)
            {
                this.doesGlobalManagerExist = true;
            }
            else
            {
                this.doesGlobalManagerExist = false;
            }

            //Setting up Player
            GameObject playerUmbrellaObject     = new GameObject("Player");
            GameObject playerUnitUmbrellaObject = new GameObject("Units");

            playerUnitUmbrellaObject.transform.SetParent(playerUmbrellaObject.transform);
            playerUmbrellaObject.tag = "Player";

            //Player unit
            GameObject playerObject = MonoBehaviour.Instantiate(this.spawnPrefab) as GameObject;

            playerObject.transform.SetParent(playerUnitUmbrellaObject.transform);
            playerObject.transform.position = this.transform.position;
            NetworkIdentity objIdentity = playerObject.GetComponent <NetworkIdentity>();

            NetworkServer.SpawnWithClientAuthority(playerObject, this.connectionToClient);

            //Player selection manager
            GameObject manager = MonoBehaviour.Instantiate(this.selectionManagerPrefab) as GameObject;

            manager.transform.SetParent(playerUmbrellaObject.transform);
            SelectionManager selectionManager = manager.GetComponent <SelectionManager>();

            if (selectionManager != null)
            {
                selectionManager.allObjects.Add(playerObject);
                selectionManager.authorityOwner = objIdentity.clientAuthorityOwner;
            }
            NetworkServer.SpawnWithClientAuthority(manager, this.connectionToClient);

            //Player split manager
            manager = MonoBehaviour.Instantiate(this.splitManagerPrefab) as GameObject;
            manager.transform.SetParent(playerUmbrellaObject.transform);
            SplitManager splitManager = manager.GetComponent <SplitManager>();

            if (splitManager != null)
            {
                splitManager.selectionManager = selectionManager;
                splitManager.unitParent       = playerUnitUmbrellaObject.transform;
                if (this.doesGlobalManagerExist && globalManagerObject != null)
                {
                    splitManager.globalManagerObject = globalManagerObject;
                    splitManager.maxUnitCount        = globalManagerObject.playerMaxUnitCount;
                }
            }
            NetworkServer.SpawnWithClientAuthority(manager, this.connectionToClient);

            //Player merge manager
            manager = MonoBehaviour.Instantiate(this.mergeManagerPrefab) as GameObject;
            manager.transform.SetParent(playerUmbrellaObject.transform);
            MergeManager mergeManager = manager.GetComponent <MergeManager>();

            if (mergeManager != null)
            {
                mergeManager.selectionManager = selectionManager;
            }
            NetworkServer.SpawnWithClientAuthority(manager, this.connectionToClient);

            //Unit Attributes Tracker
            manager = MonoBehaviour.Instantiate(this.unitAttributesPrefab) as GameObject;
            manager.transform.SetParent(playerUmbrellaObject.transform);
            UnitAttributes attributes = manager.GetComponent <UnitAttributes>();

            if (attributes != null)
            {
                splitManager.unitAttributes = attributes;
                mergeManager.unitAttributes = attributes;
            }
            NetworkServer.SpawnWithClientAuthority(manager, this.connectionToClient);

            RpcCameraSetup(playerObject);

            int colorValue;

            switch (Spawner.colorCode)
            {
            default:
                colorValue = -1;
                break;

            case 0:
                colorValue = 0;
                break;

            case 1:
                colorValue = 1;
                break;

            case 2:
                colorValue = 2;
                break;
            }

            Spawner.colorCode++;
            if (Spawner.colorCode > 2)
            {
                Spawner.colorCode = 0;
            }
            RpcUnitAttributesSetup(manager, playerObject, colorValue);

            NetworkServer.SpawnWithClientAuthority(this.remotePrefab, this.connectionToClient);
        }
示例#6
0
        void Start()
        {
            if (!this.hasAuthority) {
                return;
            }

            this.doNotAllowMerging = false;

            if (this.mergeList == null) {
                this.mergeList = new List<MergeGroup>();
            }
            if (this.removeList == null) {
                this.removeList = new List<MergeGroup>();
            }
            if (this.selectionManager == null) {
                GameObject[] managers = GameObject.FindGameObjectsWithTag("SelectionManager");
                foreach (GameObject select in managers) {
                    SelectionManager selectManager = select.GetComponent<SelectionManager>();
                    if (selectManager != null && selectManager.hasAuthority) {
                        this.selectionManager = selectManager;
                        break;
                    }
                }
                if (this.selectionManager == null) {
                    Debug.LogError("Merge Manager: Selection Manager is null. Please check.");
                }
            }
            if (this.unitAttributes == null) {
                GameObject[] attributes = GameObject.FindGameObjectsWithTag("UnitAttributes");
                foreach (GameObject attribute in attributes) {
                    UnitAttributes attr = attribute.GetComponent<UnitAttributes>();
                    if (attr != null && attr.hasAuthority) {
                        this.unitAttributes = attr;
                        break;
                    }
                }
                if (this.unitAttributes == null) {
                    Debug.LogError("Merge Manager: Unit Attributes Tracker is null. Please check.");
                }
            }
        }