Example #1
0
    // Use this for initialization
    override protected void Start()
    {
        // Base call
        base.Start();
        // Check arguments
        require(F > 0, "F has to be greater than 0");
        require(obstacle != null, "Obstacle GameObject has to be set");

        // It is a discrete car, make it smaller
        transform.localScale = new Vector3(0.9f, 1.0f, 0.9f);

        // Initializes some variables for graph creation
        GraphState     graph;
        IState         start, goal;
        List <Vector3> obstacles = new List <Vector3>();

        // Create graph
        GraphFactory.CreateDiscreteFromFile("Assets/_Data/disc.dat",
                                            neighborhood, out graph, out start, out goal, obstacles);

        // Parent so it makes a nice structure
        parent = new GameObject("Obstacles");

        // Generate all obstacles
        foreach (Vector3 v in obstacles)
        {
            Vector3    pos  = new Vector3(v.x, -0.5f, v.z);
            GameObject temp = (GameObject)
                              Instantiate(obstacle, pos, Quaternion.identity);
            temp.SetActive(true);
            temp.transform.parent = parent.transform;
        }

        // Now run astar and find path, add path to list
        AStar          ast    = new AStar(graph, start, goal, AStar.HDisc);
        List <Vector3> points = new List <Vector3>();

        foreach (IState s in ast.path)
        {
            points.Add(s.ToVector3());
        }

        // Generate path
        PathGenerator.Init(points);

        // Move to starting position
        transform.position = start.ToVector3();

        // Just to initialize and set color
        labelStyle = new GUIStyle();
        labelStyle.normal.textColor = Color.black;
        labelRect = new Rect(20, 20, 20, 20);
        strCost   = "Cost: " + ast.cost;
    }