public static PipeElement.Orientation rotateClockwise(this PipeElement.Orientation cur) { switch (cur) { case PipeElement.Orientation.NORTH: return(PipeElement.Orientation.EAST); case PipeElement.Orientation.EAST: return(PipeElement.Orientation.SOUTH); case PipeElement.Orientation.SOUTH: return(PipeElement.Orientation.WEST); default: return(PipeElement.Orientation.NORTH); } }
public static float toDegrees(this PipeElement.Orientation cur) { switch (cur) { case PipeElement.Orientation.NORTH: return(0f); case PipeElement.Orientation.WEST: return(90f); case PipeElement.Orientation.SOUTH: return(180f); case PipeElement.Orientation.EAST: return(270f); } return(0f); }
private bool isValidPath(int x, int y, PipeElement.Orientation from) { // before anything else, check if (x;y) is the valid output if (x == outputX && y == outputY && from == outputOrientation) { // create the win path winPath = new LinkedList <PipeElementScript>(); return(true); } // check if the given (x;y) is a valid pipe PipeElement target = getPipeFromGrid(x, y); if (target == null) { return(false); } // check if the (x;y) pipe may be connected with the origin direction PipeElement.Orientation outDir; if (!target.getDirectionConnected(from, out outDir)) { return(false); } // it's the time for... recursion \o/ int newX, newY; getNeighborCoordinates(x, y, outDir, out newX, out newY); if (isValidPath(newX, newY, outDir.opposite())) { // okay, we are part of the win path, add ourself on top winPath.AddFirst(objectGrid[x, y]); return(true); } return(false); }
private void getNeighborCoordinates(int x, int y, PipeElement.Orientation dir, out int newX, out int newY) { newX = x; newY = y; switch (dir) { case PipeElement.Orientation.NORTH: newY--; break; case PipeElement.Orientation.SOUTH: newY++; break; case PipeElement.Orientation.EAST: newX++; break; case PipeElement.Orientation.WEST: newX--; break; } }
public static PipeElement.Orientation opposite(this PipeElement.Orientation cur) { return(cur.rotateClockwise().rotateClockwise()); }
/** * Read a level from an XML document. */ private PipeElement[,] instanciateLevelFromXml(TextAsset document) { XmlDocument xml = new XmlDocument(); PipeElement[,] grid = new PipeElement[GRID_SIZE, GRID_SIZE]; // load the document and get the level(s) xml.LoadXml(document.text); XmlNodeList levelNodes = xml.GetElementsByTagName("level"); XmlAttribute levelEstimatedTime = levelNodes[0].Attributes["estimatedTime"]; if (levelEstimatedTime != null) { // use it as the base time for countdown timer timebar.maxTime = int.Parse(levelEstimatedTime.Value); } // start and destination pipes // TODO init XmlNodeList pipeNodes = levelNodes[0].ChildNodes; foreach (XmlNode curNode in pipeNodes) { if (curNode.Name == "pipe") { int x = 0; int y = 0; PipeElement.Orientation orientation = PipeElement.Orientation.NORTH; PipeElement.Type type = PipeElement.Type.PIPE_I; // add the pipe if possible x = int.Parse(curNode.Attributes["x"].Value); y = int.Parse(curNode.Attributes["y"].Value); switch (curNode.Attributes["type"].Value) { case "L": type = PipeElement.Type.PIPE_L; break; case "I": type = PipeElement.Type.PIPE_I; break; case "in": type = PipeElement.Type.PIPE_IN; break; case "out": type = PipeElement.Type.PIPE_OUT; break; } XmlAttribute dirAttribute = curNode.Attributes["dir"]; if (dirAttribute == null) { orientation = OrientationMethods.randomOrientation(); } else { orientation = OrientationMethods.fromString(dirAttribute.Value); } if (x >= 0 && x < GRID_SIZE && y >= 0 && y < GRID_SIZE) { // add the new pipe to the grid, and set its neighbors PipeElement toAdd = new PipeElement(type, orientation); grid[x, y] = toAdd; if (toAdd.type == PipeElement.Type.PIPE_IN) { inputX = x; inputY = y; inputOrientation = orientation; } else if (toAdd.type == PipeElement.Type.PIPE_OUT) { outputX = x; outputY = y; outputOrientation = orientation; } // set neighbors // TODO neighbors } } else { } } return(grid); }
/** * Read a level from an XML document. */ private PipeElement[,] instanciateLevelFromXml(TextAsset document) { XmlDocument xml = new XmlDocument (); PipeElement[,] grid = new PipeElement[GRID_SIZE, GRID_SIZE]; // load the document and get the level(s) xml.LoadXml(document.text); XmlNodeList levelNodes = xml.GetElementsByTagName("level"); XmlAttribute levelEstimatedTime = levelNodes[0].Attributes["estimatedTime"]; if(levelEstimatedTime != null) { // use it as the base time for countdown timer timebar.maxTime = int.Parse(levelEstimatedTime.Value); } // start and destination pipes // TODO init XmlNodeList pipeNodes = levelNodes[0].ChildNodes; foreach (XmlNode curNode in pipeNodes) { if(curNode.Name == "pipe") { int x=0; int y=0; PipeElement.Orientation orientation = PipeElement.Orientation.NORTH; PipeElement.Type type = PipeElement.Type.PIPE_I; // add the pipe if possible x = int.Parse(curNode.Attributes["x"].Value); y = int.Parse(curNode.Attributes["y"].Value); switch(curNode.Attributes["type"].Value) { case "L": type = PipeElement.Type.PIPE_L; break; case "I": type = PipeElement.Type.PIPE_I; break; case "in": type = PipeElement.Type.PIPE_IN; break; case "out": type = PipeElement.Type.PIPE_OUT; break; } XmlAttribute dirAttribute = curNode.Attributes["dir"]; if(dirAttribute == null) { orientation = OrientationMethods.randomOrientation(); } else { orientation = OrientationMethods.fromString(dirAttribute.Value); } if(x >= 0 && x < GRID_SIZE && y >= 0 && y < GRID_SIZE) { // add the new pipe to the grid, and set its neighbors PipeElement toAdd = new PipeElement(type, orientation); grid[x, y] = toAdd; if(toAdd.type == PipeElement.Type.PIPE_IN) { inputX = x; inputY = y; inputOrientation = orientation; } else if(toAdd.type == PipeElement.Type.PIPE_OUT) { outputX = x; outputY = y; outputOrientation = orientation; } // set neighbors // TODO neighbors } } else { } } return grid; }