public SettlementPathNode CreatePathFromNode(SettlementPathNode node, int width, bool extraLength = false, int chosenDirection = -1, int length = -1) { //If no direction is given, choose a null one if (chosenDirection == -1) { List <int> nullDirection = new List <int>(); for (int i = 0; i < 4; i++) { if (node.Connected[i] == null) { nullDirection.Add(i); } } Debug.Log(nullDirection.Count); //Choose a valid direction and find the vector step chosenDirection = GenerationRandom.RandomFromList(nullDirection); } Vec2i step = SettlementPathNode.GetDirection(chosenDirection); //If no length is given or given length is invalid, choose a path length if (length == -1 || !InBounds(node.Position + step * length)) { int attemptLength = length == -1 ? GenerationRandom.RandomInt(40, TileSize) : length; while (!InBounds(node.Position + step * attemptLength)) { attemptLength -= 1; } length = attemptLength; } int halfWidth = width / 2; Vec2i perpDirection = SettlementPathNode.GetPerpendicular(chosenDirection); if (extraLength) { length += halfWidth; } for (int l = 0; l < length; l++) { for (int w = -halfWidth; w <= halfWidth; w++) { Vec2i pos = node.Position + step * l + perpDirection * w; SetTile(pos.x, pos.z, Tile.TEST_BLUE); } } SettlementPathNode endNode = new SettlementPathNode(node.Position + step * length); node.AddConnection(chosenDirection, endNode); endNode.AddConnection(SettlementPathNode.OppositeDirection(chosenDirection), node); return(endNode); }
public void ConnectPathNodes(SettlementPathNode first, SettlementPathNode second, int width) { Vec2i diff = second.Position - first.Position; //Find the vector between the two nodes //First generate the path in the x direction Vec2i xDiff = new Vec2i(diff.x, 0); int xDiffDirection = SettlementPathNode.GetDirection(xDiff); SettlementPathNode midNode = CreatePathFromNode(first, width, chosenDirection: xDiffDirection, length: Mathf.Abs(diff.x)); //TestNodes.Add(midNode); //Add connections first.AddConnection(xDiffDirection, midNode); midNode.AddConnection(SettlementPathNode.OppositeDirection(xDiffDirection), first); Vec2i zDiff = new Vec2i(0, diff.z); ConnectNodes(midNode, second, width); }
public void ConnectNodes(SettlementPathNode first, SettlementPathNode second, int width) { Vec2i diff = second.Position - first.Position; //Find the vector between the two nodes int length = diff.x + diff.z; int direction = SettlementPathNode.GetDirection(diff); //first.AddConnection(direction, second); //second.AddConnection(SettlementPathNode.OppositeDirection(direction), first); Vec2i step = SettlementPathNode.GetDirection(direction); Vec2i perpDirection = SettlementPathNode.GetPerpendicular(direction); int halfWidth = width / 2; for (int l = 0; l < length; l++) { for (int w = -halfWidth; w <= halfWidth; w++) { Vec2i pos = first.Position + step * l + perpDirection * w; SetTile(pos.x, pos.z, Tile.TEST_BLUE); } } }
public void CreatePathBetweenNodes(SettlementPathNode firstNode, int secondNodeDirection, int width, int length = -1) { SettlementPathNode second = firstNode.Connected[secondNodeDirection]; if (second == null) { Debug.Error("Paths are not connected"); return; } Vec2i pathDiff = second.Position - firstNode.Position; //Find the vector that describes first->second int pathDiffLen = pathDiff.x + pathDiff.z; //Vector should have only 1 component, so total length can be written as sum int startPointOffset = pathDiffLen / 4; int startPoint = MiscMaths.RandomRange(startPointOffset, pathDiffLen - startPointOffset); //Generate node between two existing nodes SettlementPathNode interPathNode = new SettlementPathNode(firstNode.Position + SettlementPathNode.GetDirection(secondNodeDirection) * startPoint); int oppositeDir = SettlementPathNode.OppositeDirection(secondNodeDirection); //TestNodes.Add(interPathNode); //Update connections firstNode.AddConnection(secondNodeDirection, interPathNode); interPathNode.AddConnection(oppositeDir, firstNode); second.AddConnection(oppositeDir, interPathNode); interPathNode.AddConnection(secondNodeDirection, second); CreatePathFromNode(interPathNode, width, length: length); }