Ejemplo n.º 1
0
        public override string Run(ArrayList arguments)
        {
            string message;

            if (arguments.Count == 0)
            {
                message = "Unload needs avatar name as argument";
                return(message);
            }

            string agentName = (string)arguments[0];

            // Get Avatar from OCARepository
            GameObject agentToDestroy = GameObject.Find(agentName);

            if (agentToDestroy == null)
            {
                return("No agent called " + agentName);
            }

            agentToDestroy.SendMessage("saveAndExit");
            //OCARepository.RemoveOCA(agentToDestroy);
            message = "Avatar[" + agentName + "] successfully unloaded.";
            return(message);
        }
        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;
            }
        }
Ejemplo n.º 3
0
        public override string Run(ArrayList arguments)
        {
            //if (arguments.Count != 2) return "Wrong number of arguments";
            string sourceName    = (string)arguments [0];
            string targetName    = (string)arguments [1];         //also targetStartName
            string actionName    = (string)arguments [2];
            string targetEndName = (string)arguments [3];

            // Get the appropriate agent and gameobject
            OCActionController actionController =
                GameObject.FindSceneObjectsOfType(typeof(OCActionController))
                .Where(ac => (ac as OCActionController).gameObject.name == sourceName)
                .Cast <OCActionController>()
                .FirstOrDefault()
            ;

            if (actionController == null)
            {
                return("No Action Controller script on agent \"" + sourceName + "\".");
            }
            if (actionController.gameObject.tag == "Player")
            {
                return("Agent \"" + sourceName + "\" is controlled by the player!");
            }

            OCID sourceID = actionController.ID;

            // Get the object
            // if objectName is "self" then assume the script is on the agent
            OCID targetStartID;

            if (targetName != null && targetName != "self")
            {
                GameObject target = GameObject.Find(targetName);
                if (target == null)
                {
                    return("No object called " + targetName);
                }
                targetStartID = target.GetComponent <OCActionController>().ID;
            }
            else
            {
                targetStartID = actionController.ID;
            }

            OCID targetEndID;

            if (targetEndName != null && targetEndName != "self")
            {
                GameObject targetEnd = GameObject.Find(targetEndName);
                if (targetEnd == null)
                {
                    return("No object called " + targetEndName);
                }
                targetEndID = targetEnd.GetComponent <OCActionController>().ID;
            }
            else
            {
                targetEndID = actionController.ID;
            }

            // Get the action from the Action Controller
            OCAction action =
                actionController.GetComponentsInChildren <OCAction>()
                .Where(a => a.name == actionName)
                .Cast <OCAction>()
                .FirstOrDefault()
            ;

            if (action == null)
            {
                return("No action called \"" + actionName + "\".");
            }

            actionController.StartAction(action, sourceID, targetStartID, targetEndID);

//			System.Reflection.ParameterInfo[] pinfo = action.pinfo; //getFreeArguments();
//
//			if (pinfo.Length > 0) {
//				ArrayList args = new ArrayList ();
//				int i = 0;
//				int jmod = 3;
//				if (action.componentType == typeof(Avatar)) {
//					// Check we don't have too many arguments
//					if (pinfo.Length < arguments.Count - 3)
//						return "Expected " + pinfo.Length + " arguments, got " + (arguments.Count - 3) + ".";
//				} else {
//					// Check we don't have too many arguments, we don't need to
//					// provide the avatar
//					if (pinfo.Length - 1 < arguments.Count - 3)
//						return "Expected " + (pinfo.Length - 1) + " arguments, got " + (arguments.Count - 3) + ".";
//					i = 1;
//					jmod = 2;
//				}
//				// ignore last parameter if action uses a callback
//				int lengthModififer = 0;
//				if (action.usesCallback)
//					lengthModififer = 1;
//				for (; i < pinfo.Length-lengthModififer; i++) {
//					// do type checking and conversion from strings to the expected type
//					// ignore 3 console arguments: the action name, avatar name,
//					//    and the object with the action (from console arguments)
//					int j = i + jmod;
//					if (j >= arguments.Count) {
//						// Not enough arguments, so must be a default argument
//						if (!pinfo [i].IsOptional)
//							return "Missing parameter " + pinfo [i].Name + " is not optional.";
//						args.Add (pinfo [i].DefaultValue);
//					} else {
//						arguments [j] = ((string)arguments [j]).Replace ("\"", "");
//						// Depending on the expected type we convert it differently
//						if (pinfo [i].ParameterType == typeof(GameObject)) {
//							// Parameters that are gameobjects... we just search for
//							// the name.
//							args.Add (GameObject.Find ((string)arguments [j]));
//							if (((GameObject)args [i]) == null) {
//								return "No game object called \"" + (string)arguments [j] + "\".";
//							}
//						} else if (pinfo [i].ParameterType == typeof(Avatar)) {
//							// Parameters that are Avatars... we just search for
//							// the name.
//							args.Add (OCARepository.GetOCA ((string)arguments [j]).GetComponent ("Avatar") as Avatar);
//							if ((Avatar)args [i] == null) {
//								return "No Avatar called \"" + (string)arguments [j] + "\".";
//							}
//						} else if (pinfo [i].ParameterType == typeof(int)) {
//							try {
//								args.Add (System.Int32.Parse ((string)arguments [j]));
//							} catch (System.FormatException ex) {
//								return "Error parsing string as int32: " + (string)arguments [j];
//							}
//						} else if (pinfo [i].ParameterType == typeof(float)) {
//							try {
//								args.Add (float.Parse ((string)arguments [j]));
//							} catch (System.FormatException ex) {
//								return "Error parsing string as float: " + (string)arguments [j];
//							}
//						} else if (pinfo [i].ParameterType == typeof(string)) {
//							args.Add ((string)arguments [j]);
//						} else {
//							return "Method " + actionName + " at slot " + i + " has argument of unsupported type \"" + pinfo [i].ParameterType +
//                            "\". Ask Joel how to implement support or ask him nicely to do it ;-).";
//						}
//					}
//				}
//				// even if this action supports callbacks, we don't pass our actionComplete callback because
//				// it's already called by the global event (which the Console class listens for).
//				AM.doAction (actionObjectID, actionName, args);
//			}

            return("Told avatar \"" + sourceName + "\" to do action \"" + actionName + "\"");
        }