public void Generate() { var navMesh = new NavMesh(this.game); var intersectionMap = new Dictionary <Intersection, MergedIntersection>(); var intersectionSideMap = new Dictionary <MergedIntersection, Game.Street[]>(); var intersectionQuadMap = new Dictionary <MergedIntersection, NavQuad>(); foreach (var mergedIntersection in this.game.GetList <MergedIntersection>()) { var streets = new Game.Street[4]; foreach (var street in mergedIntersection.Streets) { var thisNode = mergedIntersection .Intersections.First(i => i == street.Node1 || i == street.Node2); var otherNode = street.OtherNode(thisNode); var diff = otherNode.Position - thisNode.Position; if (diff.X == 0.U()) { streets[diff.Y > 0.U() ? Block.Side.North : Block.Side.South] = street; } else if (diff.Y == 0.U()) { streets[diff.X > 0.U() ? Block.Side.East : Block.Side.West] = street; } else { throw new Exception(""); } } var padding = 0.3.U(); var cornerSW = getIntersectionCorner(streets, Block.Corner.SouthWest); var cornerSE = getIntersectionCorner(streets, Block.Corner.SouthEast); var cornerNW = getIntersectionCorner(streets, Block.Corner.NorthWest); var cornerNE = getIntersectionCorner(streets, Block.Corner.NorthEast); var navQuad = new NavQuad( cornerSW + new Difference2(padding, padding), cornerSE + new Difference2(-padding, padding), cornerNW + new Difference2(padding, -padding), cornerNE + new Difference2(-padding, -padding) ); navMesh.Add(navQuad); intersectionSideMap.Add(mergedIntersection, streets); intersectionQuadMap.Add(mergedIntersection, navQuad); foreach (var intersection in mergedIntersection.Intersections) { intersectionMap.Add(intersection, mergedIntersection); } } foreach (var mergedIntersection in this.game.GetList <MergedIntersection>()) { var intersectionQuad = intersectionQuadMap[mergedIntersection]; foreach (var street in mergedIntersection.Streets .Where(s => mergedIntersection.Intersections.Contains(s.Node1))) { var side = (Block.Side)Array.IndexOf(intersectionSideMap[mergedIntersection], street); var quad1 = intersectionQuad; var quad2 = intersectionQuadMap[intersectionMap[street.Node2]]; if (side == Block.Side.East || side == Block.Side.West) { if (side == Block.Side.East) { Do.Swap(ref quad1, ref quad2); } var navQuad = new NavQuad(quad2.SE, quad1.SW, quad2.NE, quad1.NW); NavLink.CreatePair(navQuad, quad1, quad1.SW, quad1.NW); NavLink.CreatePair(navQuad, quad2, quad2.SE, quad2.NE); navMesh.Add(navQuad); } else if (side == Block.Side.South || side == Block.Side.North) { if (side == Block.Side.South) { Do.Swap(ref quad1, ref quad2); } var navQuad = new NavQuad(quad1.NW, quad1.NE, quad2.SW, quad2.SE); NavLink.CreatePair(navQuad, quad1, quad1.NW, quad1.NE); NavLink.CreatePair(navQuad, quad2, quad2.SW, quad2.SE); navMesh.Add(navQuad); } } } }
public void Generate() { var navMesh = new NavMesh(this.game); var intersectionMap = new Dictionary<Intersection, MergedIntersection>(); var intersectionSideMap = new Dictionary<MergedIntersection, Game.Street[]>(); var intersectionQuadMap = new Dictionary<MergedIntersection, NavQuad>(); foreach (var mergedIntersection in this.game.GetList<MergedIntersection>()) { var streets = new Game.Street[4]; foreach (var street in mergedIntersection.Streets) { var thisNode = mergedIntersection .Intersections.First(i => i == street.Node1 || i == street.Node2); var otherNode = street.OtherNode(thisNode); var diff = otherNode.Position - thisNode.Position; if (diff.X == 0.U()) { streets[diff.Y > 0.U() ? Block.Side.North : Block.Side.South] = street; } else if (diff.Y == 0.U()) { streets[diff.X > 0.U() ? Block.Side.East : Block.Side.West] = street; } else { throw new Exception(""); } } var padding = 0.3.U(); var cornerSW = getIntersectionCorner(streets, Block.Corner.SouthWest); var cornerSE = getIntersectionCorner(streets, Block.Corner.SouthEast); var cornerNW = getIntersectionCorner(streets, Block.Corner.NorthWest); var cornerNE = getIntersectionCorner(streets, Block.Corner.NorthEast); var navQuad = new NavQuad( cornerSW + new Difference2(padding, padding), cornerSE + new Difference2(-padding, padding), cornerNW + new Difference2(padding, -padding), cornerNE + new Difference2(-padding, -padding) ); navMesh.Add(navQuad); intersectionSideMap.Add(mergedIntersection, streets); intersectionQuadMap.Add(mergedIntersection, navQuad); foreach (var intersection in mergedIntersection.Intersections) { intersectionMap.Add(intersection, mergedIntersection); } } foreach (var mergedIntersection in this.game.GetList<MergedIntersection>()) { var intersectionQuad = intersectionQuadMap[mergedIntersection]; foreach (var street in mergedIntersection.Streets .Where(s => mergedIntersection.Intersections.Contains(s.Node1))) { var side = (Block.Side)Array.IndexOf(intersectionSideMap[mergedIntersection], street); var quad1 = intersectionQuad; var quad2 = intersectionQuadMap[intersectionMap[street.Node2]]; if (side == Block.Side.East || side == Block.Side.West) { if (side == Block.Side.East) { Do.Swap(ref quad1, ref quad2); } var navQuad = new NavQuad(quad2.SE, quad1.SW, quad2.NE, quad1.NW); NavLink.CreatePair(navQuad, quad1, quad1.SW, quad1.NW); NavLink.CreatePair(navQuad, quad2, quad2.SE, quad2.NE); navMesh.Add(navQuad); } else if (side == Block.Side.South || side == Block.Side.North) { if (side == Block.Side.South) { Do.Swap(ref quad1, ref quad2); } var navQuad = new NavQuad(quad1.NW, quad1.NE, quad2.SW, quad2.SE); NavLink.CreatePair(navQuad, quad1, quad1.NW, quad1.NE); NavLink.CreatePair(navQuad, quad2, quad2.SW, quad2.SE); navMesh.Add(navQuad); } } } }