private IEnumerator LoadAgent(string agentName)
        {
            UnityEngine.GameObject agentClone;

            UnityEngine.GameObject playerObject = GameObject.FindGameObjectWithTag("Player");
            if (playerObject == null)
            {
                Debug.Log("No object tagged with player.");
                yield return("No object tagged with player.");
            }

            // Record the player's position and make the OCAvatar spawn near it.
            UnityEngine.Vector3 playerPos = playerObject.transform.position;

            //Debug.Log ("PlayerPos = [" + playerPos.x + ", " + playerPos.y + ", " + playerPos.z + "]");

            // Calculate the player's forward direction
            UnityEngine.Vector3 eulerAngle = playerObject.transform.rotation.eulerAngles;

            //Debug.Log ("eulerAngle = [" + eulerAngle.x + ", " + eulerAngle.y + ", " + eulerAngle.z + "]");

            float zFront = 3.0f * (float)Math.Cos((eulerAngle.y / 180) * Math.PI);
            float xFront = 3.0f * (float)Math.Sin((eulerAngle.y / 180) * Math.PI);

            UnityEngine.Vector3 spawnPosition = new UnityEngine.Vector3(playerPos.x + xFront, playerPos.y + 2, playerPos.z + zFront);
            spawnPosition.x = (float)((int)spawnPosition.x);
            spawnPosition.y = (float)((int)spawnPosition.y);
            spawnPosition.z = (float)((int)spawnPosition.z);

            //Debug.Log ("spawnPosition = [" + spawnPosition.x + ", " + spawnPosition.y + ", " + spawnPosition.z + "]");

            // Instantiate an OCAvatar in front of the player.

            //Debug.Log ("_NPCAgent is" + (_NPCAgent == null ? " null " : " not null"));

            agentClone = (GameObject)UnityEngine.Object.Instantiate(_NPCAgent, spawnPosition, Quaternion.identity);
            agentClone.transform.parent = GameObject.Find("Characters").transform;
            OCActionController agiAC = agiAC = agentClone.GetComponent <OCActionController>();

            agiAC.DefaultEndTarget   = GameObject.Find("EndPointStub");
            agiAC.DefaultStartTarget = GameObject.Find("StartPointStub");

            //Debug.Log ("agentClone is" + (agentClone == null ? " null " : " not null"));

            OCConnectorSingleton connector = OCConnectorSingleton.Instance;

            UnityEngine.Debug.Log("The GUID of our OCC instance in LoadAgent is " + connector.VerificationGuid);

            //Debug.Log ("connector is" + (connector == null ? " null " : " not null"));

            if (agentName == "")
            {
                agentName = CreateRandomAgentName();
            }

            //Debug.Log("We shall name him '" + agentName + "'");

            agentClone.name = agentName;

//			if (agentClone != null) {
//				if (!OCARepository.AddOCA (agentClone)) {
//					// An avatar with given name is already there.
//					yield break;
//				}
//				Debug.Log ("Add avatar[" + agentName + "] to avatar map.");
//			}

            // Get the player id as the master id of the avatar.
            // TODO Currently we use the tag "player". However, when there are multiple
            // players in the world, we need to figure out a way to identify.
            string masterId   = playerObject.GetInstanceID().ToString();
            string masterName = playerObject.name;

            // TODO Set agentType and agentTraits in the future.
            // leave agentType and agentTraits to null just for test.
            connector.Init(agentName, null, null, masterId, masterName);

            yield return(StartCoroutine(connector.ConnectOAC()));

            if (!connector.IsInitialized)
            {
                // OAC is not loaded normally, destroy the avatar instance.
                Debug.LogError("Cannot connect to the OAC, avatar loading failed.");
                connector.SaveAndExit();
                Destroy(agentClone);
                yield break;
            }
        }
Exemple #2
0
            //DEPRECATED: This was useful once;
            //if (!OCARepository.AddOCA (agentClone)) {

            // Note, lambda expressions (ie myLamb = (x) => successBool = (bool)(x == "true")) are *terrifying* to look at
            // and can use up a lot of memory, but they're really good for returning values from IEnumerators/coroutines;
            // especially ones we don't expect will be called frquently

            /// <summary>
            /// Allows entities to be loaded to the screen at any point while the game is running
            /// </summary>
            /// <returns>Failure, success, or a coroutine concerned with making a connection to the embodiment.</returns>
            /// <param name="spawnPosition">The position at which to instantiate the agent.</param>
            /// <param name="agentPrefab">The prefab to instantiate.</param>
            /// <param name="agentName">The name of the agent to instantiate.</param>
            /// <param name="masterName">The owner of the instance</param>
            /// <param name="masterId">The id of the instance</param>
            /// <param name="report">An Action that can be constructed easily with a lambda expression which will report back the string "true" if it successfully
            ///  created the agent and the string "false" if it was unsuccessful in creating the agent.</param>
            public IEnumerator AtRunTime(UnityEngine.Vector3 spawnPosition, GameObject agentPrefab, string agentName = "", string masterName = "", string masterId = "",
                                         System.Action <string> report = null)             //The lambda expression enter-er gets its own line, because it's SCARY!
            {
                //get the spawn position in terms of the grid
                spawnPosition.x = (float)((int)spawnPosition.x);
                spawnPosition.y = (float)((int)spawnPosition.y);
                spawnPosition.z = (float)((int)spawnPosition.z);

                //Debug.Log ("_NPCAgent is" + (_NPCAgent == null ? " null " : " not null"));

                //instantiate the game object, specified by NPCAgent at  point spawn position and rotated as per the identity Quaternion (that is, not at all)
                UnityEngine.GameObject agentClone = (GameObject)UnityEngine.Object.Instantiate(agentPrefab, spawnPosition, Quaternion.identity);

                //All agents belong to the Characters parent game object.
                agentClone.transform.parent = GameObject.Find("Characters").transform;

                //the _NPCAgent should have come with an OCActionController
                OCActionController agiAC = agentClone.GetComponent <OCActionController>();

                //
                agiAC.DefaultEndTarget   = GameObject.Find("EndPointStub");
                agiAC.DefaultStartTarget = GameObject.Find("StartPointStub");

                //Debug.Log ("agentClone is" + (agentClone == null ? " null " : " not null"));

                OCConnectorSingleton connector = OCConnectorSingleton.Instance;

                //Debug.Log ("connector is" + (connector == null ? " null " : " not null"));

                //Ensure our agent is properly named
                if (agentName == "")
                {
                    agentName = CreateRandomAgentName();
                }
                agentClone.name = agentName;


                //initialize the connector
                connector.InitAvatar(agentName, null, null, masterId, masterName);

                //and try to connect
                yield return(StartCoroutine(connector.ConnectOAC()));

                //if we failed to initialize the connector
                if (!connector.IsInitialized)
                {
                    // OAC is not loaded normally, destroy the avatar instance.
                    Debug.LogError(OCLogSymbol.ERROR + "Could not connect to Embodiment");
                    System.Console.WriteLine(OCLogSymbol.DETAILEDINFO + "LoadMethods.AtRunTime is reporting !connector.Initialize. Cannot connect to the OAC, avatar loading failed.");
                    connector.SaveAndExit();
                    Destroy(agentClone);

                    if (report != null)
                    {
                        report("false");
                    }
                }
                else
                {
                    Debug.Log(OCLogSymbol.CLEARED + "LoadMethods.AtRunTime is reporting connector.Initialized");
                    if (report != null)
                    {
                        report("true");
                    }
                }

                yield break;
            }