GetQuadTreeItemFor() public method

public GetQuadTreeItemFor ( Vector3 vec, Vector3 size, int maxDepth ) : List
vec Vector3
size Vector3
maxDepth int
return List
Example #1
0
    List <GameObject> GetGameObjectsOnTheWay(Vector3 start, Vector3 end)
    {
        List <GameObject> result = new List <GameObject>();

        QuadTree qt = GameObject.Find("QuadTreeGenerator").GetComponent <QuadTree>();

        float dx = end.x - start.x;
        float dy = end.y - start.y;

        float lastX = start.x, lastY = y1;

        for (float x = start.x; x < end.x; x++)
        {
            float y = y1 + (dy) * (x - x1) / (dx);

            if (Mathf.Abs(lastX - x) > 1 || Mathf.Abs(lastY - y) > 1)
            {
                QuadTreeItem qti = qt.GetQuadTreeItemFor(new Vector3(x, 0.0f, y), 1);

                result.AddRange(qti.GameObjects);

                lastX = x;
                lastY = y;
            }
        }

        return(result);
    }
Example #2
0
    void Update()
    {
        if (!LoadingCorrupted && Generator.generatorDone && !FileLoaded && SaveFileExist() && !GenerationDone)
        {
            FileLoaded = LoadFile();

            if (FileLoaded == false)
            {
                LoadingCorrupted = true;
            }
            else
            {
                GenerationDone = true;
                Debug.Log("Waypoints succesfully loaded from file");
                SaveToFile();
            }
        }
        else if (!GenerationDone && QuadTree.generationDone && Generator.generatorDone)
        {
            for (int i = 0; i < pathNodes.Count; i++)
            {
                if (pathNodes[i] != null)
                {
                    pathNodes[i].nodeValid = false;
                    pathNodes[i]           = null;
                }
            }

            pathNodes.Clear();

            Debug.Log("Waypoint generation");

            QuadTree qt = GameObject.Find("QuadTreeGenerator").GetComponent <QuadTree>();

            float startX = CenterX - Width / 2.0f;
            float startY = CenterY - Width / 2.0f;

            float endX = CenterX + Width / 2.0f;
            float endY = CenterY + Width / 2.0f;


            matrix = new PathNode[(int)Width][];

            for (int x = 0; x < Width; x += 1)
            {
                matrix[x] = new PathNode[(int)Width];

                for (int y = 0; y < Width; y += 1)
                {
                    matrix[x][y] = null;
                }
            }

            int   x1, y1;
            float xf;
            float yf;

            for (xf = startX, x1 = 0; xf < endX && x1 < Width; xf += Step, x1++)
            {
                for (yf = startY, y1 = 0; yf < endY && y1 < Width; yf += Step, y1++)
                {
                    Vector3 position = new Vector3(xf, 0.5f, yf);

                    //List<GameObject> result = qt.GetObjectsInside(position);

                    QuadTreeItem qi = qt.GetQuadTreeItemFor(position, 4);

                    if (qi == null || qi.GameObjects == null)
                    {
                        continue;
                    }

                    GameObject closest = GetClosestObject(qi, position);

                    if (IsInside(qi.GameObjects, position))
                    {
                        continue;
                    }

                    if (closest == null || (closest != null && Vector3.Distance(closest.transform.position, position) > 1.0f))
                    {
                        PrepareNode(x1, y1, position, "regular generation");
                    }
                    else
                    {
                        matrix[x1][y1] = null;
                    }
                }
            }

            //SaveToFile();

            GenerationDone = true;

            GameObject enemyGenerator = GameObject.Find("EnemyGenerator");

            if (QuadTree.waypointsNeedRegeneration)
            {
                enemyGenerator.GetComponent <EnemyGenerator>().WaypointSystemChangedCallback();
            }

            QuadTree.waypointsNeedRegeneration = false;
        }
        else if (QuadTree.waypointsNeedRegeneration && !QuadTree.rebuildingQuadTree)
        {
            QuadTree qt = GameObject.Find("QuadTreeGenerator").GetComponent <QuadTree>();

            Debug.Log("Regenerating waypoints (selective)");

            float startX = CenterX - Width / 2.0f;
            float startY = CenterY - Width / 2.0f;

            float endX = CenterX + Width / 2.0f;
            float endY = CenterY + Width / 2.0f;

            int   x1, y1;
            float xf, yf;

            Debug.Log("Qti's to regenerate: " + qt.qtisToRegenerate.Count);

            foreach (QuadTreeItem qti in qt.qtisToRegenerate)
            {
                for (xf = startX, x1 = 0; xf < endX && x1 < Width; xf += Step, x1++)
                {
                    for (yf = startY, y1 = 0; yf < endY && y1 < Width; yf += Step, y1++)
                    {
                        Vector3 position = new Vector3(xf, 0.5f, yf);

                        if (OOBCollisionDetection.AreBoxexOverlapping(qti.Size, qti.Position, new Vector3(Step, Step, Step), position))
                        {
                            if (IsInside(qti.GameObjects, position))
                            {
                                PathNodeDelete(position);
                                continue;
                            }

                            if (matrix[x1][y1] != null && matrix[x1][y1].nodeEnabled)
                            {
                                continue;
                            }

                            GameObject closest = GetClosestObject(qti, position);

                            if (closest == null || (closest != null && Vector3.Distance(closest.transform.position, position) > 1.0f))
                            {
                                PrepareNode(x1, y1, position, "regenerate" + qti.name);
                            }
                        }
                    }
                }
            }

            qt.qtisToRegenerate.Clear();

            GameObject enemyGenerator = GameObject.Find("EnemyGenerator");
            enemyGenerator.GetComponent <EnemyGenerator>().WaypointSystemChangedCallback();
            QuadTree.waypointsNeedRegeneration = false;
        }
    }