//This is run once for each file (JSON) file found
 void RunInit()
 {
     //This method is called for each agent within the Agent Definition File
     for (int i = 0; i < loadedData.Total_Number; i++)
     {
         //this passes the integer number along with the call, this could be useful in determining which child object to use as the true home object
         Controller.Initialize_Agent(loadedData, i);
     }
     //make sure the AgentInit variable is empty
     loadedData = null;
 }
Ejemplo n.º 2
0
    //This is a master agent initialization script to set all starting variables and object pointers
    public void Init_Agent_From_JSON(AgentInit a)
    {
        //set camera? why
        //set
        Ward  = a.Ward;
        Block = a.Block;
        Sex   = a.Sex;
        Type  = a.Type;
        State = a.State;
        ID    = a.ID;


        //Debug.Log("Instance of new Agent Init method called");
        if (a.Color == "Red")
        {
            //Debug.Log("Agent color definition in JSON registers as Red");
            SetColor(Color.red);
        }
        else
        {
            if (a.Color == "Yellow")
            {
                //Debug.Log("Agent color definition in JSON registers as Yellow");
                SetColor(Color.yellow);
            }
            else
            {
                if (a.Color == "White")
                {
                    //Debug.Log("Agent color definition in JSON registers as White");
                    SetColor(Color.white);
                }
                else
                {
                    //Debug.Log("Agent color definition in JSON does NOT register as Red");
                    SetColor(Color.blue);
                }
            }
        }
    }
    //This method takes as input a string pointing to the JSON Agent Init file and makes the agents
    private void LoadGameData(string filepath_string)
    {
        //make sure the AgentInit variable is empty
        loadedData = null;
        // Path.Combine combines strings into a file path
        // Application.StreamingAssets points to Assets/StreamingAssets in the Editor, and the StreamingAssets folder in a build
        string filePath = Path.Combine(Application.dataPath, subfolder);

        filePath = Path.Combine(filePath, filepath_string);

        if (File.Exists(filePath))
        {
            // Read the json from the file into a string
            //Debug.Log("Looking for an Agent Init file in " + filePath);
            string dataAsJson = File.ReadAllText(filePath);
            //Debug.Log("loadedData as deserialized string direct from imported file text = " + dataAsJson);

            /*
             * StreamReader reader = new StreamReader(filePath);
             * //Debug.Log("StreamReader");
             * Debug.Log("StreamReader" + reader.ReadToEnd());
             * reader.Close();
             */


            //UltimateJSON library - in an attempt to deserialize a dictionary in the Json, meaning unstructured data
            loadedData = UltimateJson.JsonObject.Deserialise <AgentInit>(dataAsJson);

            //Debug.Log("Json cast as object into a ToString function   = " + loadedData);
            //loadedData.ReturnDictionary();
        }
        else
        {
            Debug.LogError("Cannot load json AgentInit object!    for filename  =  " + filepath_string);
        }
    }
Ejemplo n.º 4
0
    //This is called by the JSON parser iteratively to make each new agent, it is handed a Dictionary built from the JSON importer
    public void Initialize_Agent(AgentInit a, int i)
    {
        //used to push out the AgentInit data
        //Debug.Log(a);


        //finds the single home object outlined in the Agent Definition
        GameObject home_obj = GameObject.Find(a.HomeObject);
        //Makes a new Vector3 location position to instantiate new agent into
        Vector3 AddAgentLocation;
        //Not sure what this is?
        GameObject Home;

        //test if the Home Object has the coordinator script assigned.
        if (home_obj.GetComponent <SScholar_Agent_HomeObjectCoordinator>())
        {
            //This block tries to search for sub objects under home to assign to the actual singular "home" variable in the specific agent
            try
            {
                //if the number of agents in the JSON file does not match the number of unique home objects in the array default to the first object in the array for all extra agents
                if (home_obj.GetComponent <SScholar_Agent_HomeObjectCoordinator>().Object_Array[i] == null)
                {
                    AddAgentLocation = home_obj.GetComponent <SScholar_Agent_HomeObjectCoordinator>().Object_Array[1].transform.position;
                    Home             = home_obj.GetComponent <SScholar_Agent_HomeObjectCoordinator>().Object_Array[1];

                    //initialize a rotation for each agent, no reason yet to make this specific to the agent so it is 0,0,0,0
                    Quaternion AddAgentRotation = new Quaternion(0.0f, 0.0f, 0.0f, 0.0f);
                    //Instantiates a single agent
                    NavMeshAgent     newAgent = Instantiate(SSAgent, AddAgentLocation, AddAgentRotation);
                    PlayerController PC       = newAgent.GetComponent <PlayerController>();
                    PC.Itinerary  = a.Itinerary;
                    PC.HomeObject = Home;
                    //what should become a global init that transfers AgentInit values to the Agent Instance
                    //Need to call a single master new agent initialization script - Might need logic or variables to hold the objects this will use to populat
                    PC.Init_Agent_From_JSON(a);

                    AgentList.Add(newAgent);
                }
                else
                {
                    AddAgentLocation = home_obj.GetComponent <SScholar_Agent_HomeObjectCoordinator>().Object_Array[i].transform.position;
                    Home             = home_obj.GetComponent <SScholar_Agent_HomeObjectCoordinator>().Object_Array[i];

                    Quaternion       AddAgentRotation = new Quaternion(0.0f, 0.0f, 0.0f, 0.0f);
                    NavMeshAgent     newAgent         = Instantiate(SSAgent, AddAgentLocation, AddAgentRotation);
                    PlayerController PC = newAgent.GetComponent <PlayerController>();
                    PC.Itinerary  = a.Itinerary;
                    PC.HomeObject = Home;
                    //what should become a global init that transfers AgentInit values to the Agent Instance
                    PC.Init_Agent_From_JSON(a);
                    AgentList.Add(newAgent);
                }
            }
            catch (Exception e)
            {
                print("error");
            }
        }
        else
        //This happens if there is no sub object coordinator
        {
            Debug.Log("This should be called once in the test scene");
            AddAgentLocation = home_obj.transform.position;
            Home             = home_obj;

            Quaternion       AddAgentRotation = new Quaternion(0.0f, 0.0f, 0.0f, 0.0f);
            NavMeshAgent     newAgent         = Instantiate(SSAgent, AddAgentLocation, AddAgentRotation);
            PlayerController PC = newAgent.GetComponent <PlayerController>();
            PC.Itinerary  = a.Itinerary;
            PC.HomeObject = Home;
            Debug.Log("set the home object of the PlayerController = " + PC.HomeObject);
            //what should become a global init that transfers AgentInit values to the Agent Instance
            PC.Init_Agent_From_JSON(a);
            AgentList.Add(newAgent);
        }
    }