Ejemplo n.º 1
0
    public void SaveGraph()
    {
        JSONObject json = JSONObject.obj;

        JSONObject nodes = JSONObject.arr;

        GameObject[] Nodes = GameObject.FindGameObjectsWithTag("Node");
        print(Nodes.Length);
        foreach (GameObject node in Nodes)
        {
            NodeScript scptNode = node.GetComponent <NodeScript>();
            JSONObject jNode    = JSONObject.obj;
            jNode.AddField("id", scptNode.Id);
            JSONObject position = JSONObject.obj;
            position.AddField("x", node.transform.position.x);
            position.AddField("y", node.transform.position.y);
            position.AddField("z", node.transform.position.z);
            jNode.AddField("position", position);
            jNode.AddField("arrivals", scptNode.occupants);
            jNode.AddField("isGoal", scptNode.goalState);
            //EDGES
            JSONObject jEdges = JSONObject.arr;
            foreach (EdgeScript edge in scptNode.GetComponentsInChildren <EdgeScript>())
            {
                //EdgeScript scptEdge = edge.GetComponent<EdgeScript>();
                NodeScript A = edge.start.GetComponent <NodeScript>();
                NodeScript B = edge.end.GetComponent <NodeScript>();

                JSONObject jEdge = JSONObject.obj;
                jEdge.AddField("start", A.Id);
                jEdge.AddField("end", B.Id);
                jEdge.AddField("cost", edge.cost);
                jEdge.AddField("flowRate", edge.flowRate);
                jEdges.Add(jEdge);
            }
            jNode.AddField("edges", jEdges);
            nodes.Add(jNode);
        }

        json.AddField("nodes", nodes);

        JSONObject elevators = JSONObject.arr;

        GameObject[] Elevators = GameObject.FindGameObjectsWithTag("Elevator");

        foreach (GameObject elevator in Elevators)
        {
            ElevatorScript elevate = elevator.GetComponent <ElevatorScript>();

            JSONObject jElevator = JSONObject.obj;
            jElevator.AddField("id", elevate.Id);
            jElevator.AddField("initialLocation", elevate.initialLocation);
            JSONObject jNodes = JSONObject.arr;

            foreach (GameObject node in elevate.nodes)
            {
                JSONObject jNode = JSONObject.obj;
                NodeScript scpt  = node.GetComponent <NodeScript>();

                jNode.AddField("id", scpt.Id);
                jNodes.Add(jNode);
            }
            jElevator.AddField("nodes", jNodes);
            elevators.Add(jElevator);
        }

        json.AddField("elevators", elevators);

        StreamWriter fileOut = File.CreateText(path);

        fileOut.WriteLine(json.ToString());
        fileOut.Close();
    }