Beispiel #1
0
        static public List <Node> GetFractured(Data.Layout.Environment toFracture_, Data.Layout.Environment container_, List <Data.Layout.Environment> obstacles_, float randomness_ = 3.0f)
        {
            var         rtn    = new List <Node>();
            AStarSolver solver = new AStarSolver();

            Vector2 min = new Vector2(float.MaxValue, float.MaxValue);
            Vector2 max = new Vector2(float.MinValue, float.MinValue);

            foreach (var edge in toFracture_.EdgeList)
            {
                min.x = Mathf.Min(min.x, edge.Position.x);
                min.y = Mathf.Min(min.y, edge.Position.y);
                max.x = Mathf.Max(max.x, edge.Position.x);
                max.y = Mathf.Max(max.y, edge.Position.y);
            }
            var grid = Node.GenerateGridMap(min, max, 0.1f, 2, obstacles_, container_, randomness_);

            if (grid.Nodes.Count == 0)
            {
                grid = null;
            }
            solver.Nodes = grid.Nodes;

            for (var i = 0; i < toFracture_.NumEdges; ++i)
            {
                solver.Start = null;
                solver.End   = null;
                var desriedStart       = toFracture_[i].Position;
                var desriedEnd         = toFracture_[i + 1].Position;
                var closestStartDistSq = float.MaxValue;
                var closestEndDistSq   = float.MaxValue;
                foreach (var node in solver.Nodes)
                {
                    if (node.Connections.Count > 0 && (node.Position - desriedStart).sqrMagnitude < closestStartDistSq)
                    {
                        solver.Start       = node;
                        closestStartDistSq = (node.Position - desriedStart).sqrMagnitude;
                    }
                    else if (node.Connections.Count > 0 && (node.Position - desriedEnd).sqrMagnitude < closestEndDistSq)
                    {
                        solver.End       = node;
                        closestEndDistSq = (node.Position - desriedEnd).sqrMagnitude;
                    }
                }

                var path = solver.GetShortestPath();
                if (path.Count == 0)
                {
                    rtn.Add(solver.End);
                    continue;
                }
                for (int j = 1; j < path.Count - 2; ++j)
                {
                    path[j].Disconnect();
                }
                path.RemoveAt(0);
                rtn.AddRange(path);
            }
            return(rtn);
        }
Beispiel #2
0
 public Environment(Data.Layout.Environment data_)
 {
     data = data_;
     foreach (var environment in data.NestedEnvironments)
     {
         nestedEnvironments.Add(new Environment(environment));
     }
 }
Beispiel #3
0
            static public Grid GenerateGridMap(Vector2 start_, Vector2 end_, float minRes_, int buffer_, List <Data.Layout.Environment> obstacles_, Data.Layout.Environment container_, float randomness_)
            {
                Debug.Assert(minRes_ > 0.001);
                Debug.Assert(buffer_ > 0);
                Vector2 min           = new Vector2(Mathf.Min(start_.x, end_.x), Mathf.Min(start_.y, end_.y));
                float   spreadX       = Mathf.Abs(end_.x - start_.x);
                float   spreadY       = Mathf.Abs(end_.y - start_.y);
                float   spread        = Mathf.Max(spreadX, spreadY);
                float   size          = Mathf.Min(spread / minRes_, 20.0f);
                float   cellSize      = spread / size;
                float   cellRadiusSqr = 0.25f * cellSize * cellSize;

                Node[,] map = new Node[2 * buffer_ + Mathf.RoundToInt(size * spreadX / spread), 2 * buffer_ + Mathf.RoundToInt(size * spreadY / spread)];
                for (int i = 0; i < map.GetLength(0); ++i)
                {
                    for (int j = 0; j < map.GetLength(1); ++j)
                    {
                        var node = new Vector2(min.x + (i - buffer_) * cellSize, min.y + (j - buffer_) * cellSize);

                        //snap node to start and end
                        if ((node - start_).sqrMagnitude < cellRadiusSqr)
                        {
                            node = start_;
                        }
                        else if ((node - end_).sqrMagnitude < cellRadiusSqr)
                        {
                            node = end_;
                        }

                        //skip if masked
                        if (container_ != null && !container_.Contains(node))
                        {
                            continue;
                        }
                        if (obstacles_ != null)
                        {
                            foreach (var obstacle in obstacles_)
                            {
                                if (obstacle.Contains(node))
                                {
                                    continue;
                                }
                            }
                        }

                        map[i, j] = new Node(node);
                        float rnd = cellSize * Random.value * randomness_;
                        if (i > 0)
                        {
                            Connection.Connect(map[i, j], map[i - 1, j], rnd);
                        }
                        if (j > 0)
                        {
                            Connection.Connect(map[i, j], map[i, j - 1], rnd);
                        }
                        if (i > 0 && j > 0)
                        {
                            Connection.Connect(map[i, j], map[i - 1, j - 1], rnd * 1.414f);
                        }
                        if (i > 0 && j < map.GetLength(1) - 1)
                        {
                            Connection.Connect(map[i, j], map[i - 1, j + 1], rnd * 1.414f);
                        }
                    }
                }

                List <Node> rtn = new List <Node>(map.GetLength(0) * map.GetLength(1));

                for (int i = 0; i < map.GetLength(0); ++i)
                {
                    for (int j = 0; j < map.GetLength(1); ++j)
                    {
                        if (map[i, j] != null)
                        {
                            rtn.Add(map[i, j]);
                        }
                    }
                }
                if (rtn.Count == 0)
                {
                    return(null);
                }
                return(new Grid(rtn, cellSize));
            }