示例#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);
    }
示例#2
0
    public QuadTreeItem GetQuadTreeItemFor(Vector3 vec, int maxDepth)
    {
        GameObject act = root;

        QuadTreeItem result = null;

        int depth = 0;

        while (true)
        {
            Vector3 actPosition = act.GetComponent <QuadTreeItem>().Position;

            if (act.GetComponent <QuadTreeItem>().Children.Count == 0 && (IsGameObjectIn(act, vec)))
            {
                result = act.GetComponent <QuadTreeItem>();
                break;
            }

            if (act.GetComponent <QuadTreeItem>().Children.Count == 0)
            {
                break;
            }

            if (maxDepth != 0 && depth == maxDepth && IsGameObjectIn(act, vec))
            {
                result = act.GetComponent <QuadTreeItem>();
                break;
            }

            if (vec.x <= actPosition.x && vec.z <= actPosition.z)
            {
                act = act.GetComponent <QuadTreeItem>().Children[0];
            }
            else if (vec.x <= actPosition.x && vec.z >= actPosition.z)
            {
                act = act.GetComponent <QuadTreeItem>().Children[3];
            }
            else if (vec.x >= actPosition.x && vec.z >= actPosition.z)
            {
                act = act.GetComponent <QuadTreeItem>().Children[2];
            }
            else if (vec.x >= actPosition.x && vec.z <= actPosition.z)
            {
                act = act.GetComponent <QuadTreeItem>().Children[1];
            }
            else
            {
                break;
            }

            depth++;
        }

        return(result);
    }
    GameObject GetClosestObject(QuadTreeItem qi, Vector3 actPosition)
    {
        double min = double.MaxValue;
        GameObject closest = null;

        foreach (GameObject go in qi.GameObjects)
        {
            double distance = Vector3.Distance(go.transform.position, actPosition);

            if (distance < min)
            {
                min = distance;
                closest = go;
            }
        }

        return closest;
    }
示例#4
0
    GameObject GetClosestObject(QuadTreeItem qi, Vector3 actPosition)
    {
        double     min     = double.MaxValue;
        GameObject closest = null;

        foreach (GameObject go in qi.GameObjects)
        {
            double distance = Vector3.Distance(go.transform.position, actPosition);

            if (distance < min)
            {
                min     = distance;
                closest = go;
            }
        }

        return(closest);
    }
        public void RegisterItem(T item, RectangleF bounds)
        {
            if (this.QuadTreeItemMap.ContainsKey(item))
            {
                throw new ArgumentException("item already registered", nameof(item));
            }

            var quadTreeItems = new QuadTreeItem[4];

            quadTreeItems[0] = new QuadTreeItem(item, new PointF(bounds.Left, bounds.Top));
            quadTreeItems[1] = new QuadTreeItem(item, new PointF(bounds.Left, bounds.Bottom));
            quadTreeItems[2] = new QuadTreeItem(item, new PointF(bounds.Right, bounds.Top));
            quadTreeItems[3] = new QuadTreeItem(item, new PointF(bounds.Right, bounds.Bottom));

            foreach (var quadTreeItem in quadTreeItems)
            {
                this.QuadTree.Insert(quadTreeItem);
            }

            this.QuadTreeItemMap[item] = quadTreeItems;
        }
示例#6
0
 public void Insert(int x, int y, QuadTreeItem Item, int maxdepth)
 {
     if (x < xstart || y < ystart || x >= xend || y >= yend)
     {
         return;
     }
     contained++;
     if (maxdepth > 0)
     {
         if (Children.Count == 0)
         {
             Split();
         }
         for (int i = 0; i < Children.Count; i++)
         {
             Children[i].Insert(x, y, Item, maxdepth - 1);
         }
     }
     else
     {
         Items.Add(Item);
     }
 }
示例#7
0
 public void DrawItem(Graphics g, QuadTreeItem a)
 {
     g.DrawRectangle(Pens.LightBlue, a.x, a.y, 1, 1);
 }
示例#8
0
 public void Insert(QuadTreeItem Item, int maxdepth)
 {
     Insert(Item.x, Item.y, Item, maxdepth);
 }
示例#9
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;
        }
    }