// Update is called once per frame void Update() { return; Vector3 mPos; Ray ray; float ray_distance; if (Input.GetMouseButtonDown(0)) { if (playerIsMoving) { return; } ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (plane.Raycast(ray, out ray_distance)) { mPos = ray.GetPoint(ray_distance); mPos = new Vector3((float)System.Math.Round(mPos.x, 3), (float)System.Math.Round(mPos.y, 3), 0); // Debug.Log(string.Format("Clicked at ({0},{1})", mPos.x, mPos.y)); Strand clickedStrand = null; bool clickedOnExistingStrand = StrandListContainsPoint(mPos, out clickedStrand); bool clickedOnExistingNode = GetOverlappingObjects(out node1, "Vertex", mPos, 0.1f); // Spider spiderScript = spider.GetComponent<Spider>(); if (clickedOnExistingNode) { // Debug.Log(string.Format("Clicked on existing node: {0}", node1.name)); existingNode = true; } else if (clickedOnExistingStrand) { startStrand = clickedStrand; // Debug.Log(string.Format("Clicked on {0}", clickedStrand.PositionString())); //Debug.Log(string.Format("mPos = {0}", mPos.ToString())); Vector3 pos = clickedStrand.ClosestPoint(mPos); // Debug.Log(string.Format("Closest point to mPos on strand: {0}", pos.ToString())); // Debug.Log( // string.Format( // "Approximated {0} as {1} on {2}", // mPos.ToString(), // pos.ToString(), // clickedStrand.PositionString() // ) // ); // spiderScript.SetPositionToMove(pos); node1 = Instantiate(node, pos, Quaternion.identity) as GameObject; node1.name = string.Format("Node{0}", (nodeList.Count - 4)); // node1.SetActive(false); node1.GetComponent <Renderer>().enabled = false; // Node newNode = new Node(pos.x, pos.y); // nodeList.Add(newNode); existingNode = false; // Debug.Log("Created new node at: " + newNode.PositionString()); } } } if (Input.GetMouseButtonUp(0)) { ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (plane.Raycast(ray, out ray_distance)) { mPos = ray.GetPoint(ray_distance); mPos = new Vector3((float)System.Math.Round(mPos.x, 3), (float)System.Math.Round(mPos.y, 3), 0); // Debug.Log(string.Format("node1 at: ({0},{1})", node1.transform.position.x, node1.transform.position.y)); if (node1) { positionToMove = node1.transform.position; CalculatePlayerMovePath(player.transform.position, positionToMove); Renderer nodeRenderer = node1.GetComponent <Renderer>(); Bounds nodeBounds = nodeRenderer.bounds; if (!nodeBounds.Contains(mPos)) { float startX, startY, endX, endY; Strand clickedStrand = null; bool clickedOnExistingStrand = StrandListContainsPoint(mPos, out clickedStrand); if (clickedOnExistingStrand) { nodeList.Add(new Node(node1.transform.position.x, node1.transform.position.y)); bool clickedOnExistingNode = GetOverlappingObjects(out node2, "Vertex", mPos, 0.1f); if (!clickedOnExistingNode) { endStrand = clickedStrand; //Debug.Log(string.Format("mPos = {0}", mPos.ToString())); Vector3 pos = clickedStrand.ClosestPoint(mPos); //Debug.Log(string.Format("Closest point to mPos on strand: {0}", pos.ToString())); node2 = Instantiate(node, pos, Quaternion.identity) as GameObject; node2.name = string.Format("Node{0}", (nodeList.Count - 4)); // node2.SetActive(false); node2.GetComponent <Renderer>().enabled = false; Node newNode = new Node(pos.x, pos.y); nodeList.Add(newNode); // Debug.Log("Created new node at: " + newNode.PositionString()); } startX = node1.transform.position.x; startY = node1.transform.position.y; endX = node2.transform.position.x; endY = node2.transform.position.y; float lineDistance = Mathf.Sqrt(Mathf.Pow(endX - startX, 2) + Mathf.Pow(endY - startY, 2)); float lineAngle = Mathf.Atan2(endY - startY, endX - startX) * (180 / Mathf.PI); float spriteSize = strand.GetComponent <SpriteRenderer>().bounds.size.x; float scale = lineDistance / spriteSize; if (node2) { Node n1 = new Node(startX, startY); Node n2 = new Node(endX, endY); Strand strandToAdd = new Strand(n1, n2); bool strandPresent = false; foreach (var str in strandList) { if (strandToAdd.Equals(str)) { strandPresent = true; } } if (!strandPresent) { drawnStrand = Instantiate( strand, node1.transform.position, Quaternion.AngleAxis(lineAngle, Vector3.forward) ) as GameObject; // Debug.Log(strandToAdd.PositionString()); strandList.Add(strandToAdd); drawnStrand.name = string.Format("Strand{0}", (strandList.Count - 4)); drawnStrand.GetComponent <Renderer>().enabled = false; // drawnStrand.SetActive(false); Vector3 originalVector = drawnStrand.transform.localScale; drawnStrand.transform.localScale = new Vector3( scale, drawnStrand.transform.localScale.y, drawnStrand.transform.localScale.z ); Vector3 intersectPt = Vector3.zero; drawnStrandIsSplit = false; for (int i = strandList.Count - 1; i >= 0; i--) { Strand str = strandList[i]; if (str.Equals(strandToAdd)) { // Skipping self continue; } ; if (str.Equals(startStrand) && !existingNode) { splitStrands = SplitStrand(str, new Node[] { new Node(startX, startY) }); } if (str.Equals(endStrand) && !clickedOnExistingNode) { splitStrands = SplitStrand(str, new Node[] { new Node(endX, endY) }); } if (strandToAdd.Intersects(str, out intersectPt)) { splitStrands = SplitStrand(str, new Node[] { new Node(intersectPt.x, intersectPt.y) }); GameObject newNode = Instantiate(node, intersectPt, Quaternion.identity) as GameObject; newNode.GetComponent <Renderer>().enabled = false; intersectNodes.Add(newNode); newNode.name = string.Format("Node{0}", (nodeList.Count - 4)); nodeList.Add(new Node(intersectPt.x, intersectPt.y)); } } if (intersectNodes.Count > 0) { drawnStrandIsSplit = true; Node[] iNodes = new Node[intersectNodes.Count]; // Debug.Log(string.Format("{0} intersection points", iNodes.Length)); for (int j = 0; j < intersectNodes.Count; j++) { GameObject goTemp = intersectNodes[j]; iNodes[j] = new Node(goTemp.transform.position.x, goTemp.transform.position.y); } splitStrands = SplitStrand(strandToAdd, iNodes, true); Destroy(drawnStrand); } newStrandDrawn = false; } } } else { // Debug.Log("Did not click on an existing strand for node2"); DeleteFirstNode(); } } else { DeleteFirstNode(); } } // node1 = null; // node2 = null; } //ShowNodeList(); //ShowStrandList(); } if (Input.GetMouseButton(0)) { ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (node1) { // TODO: add intermediate animation } } if (player.transform.position != positionToMove) { // Debug.Log(string.Format("Player at {0}", player.transform.position.ToString())); // Debug.Log(string.Format("Destination = {0}", positionToMove.ToString())); // Debug.Log("player is moving"); // Vector3 directionVector; if (playerMovePath.Count > 0) { playerIsMoving = true; Node nodeToMove = playerMovePath[0]; nextNode = new Vector3(nodeToMove.xPos(), nodeToMove.yPos(), 0); // directionVector = nextNode - player.transform.position; // player.transform.position += Vector3.Normalize(directionVector) * (Time.deltaTime * playerMoveSpeed); // player.transform.position = Vector3.Lerp(player.transform.position, nextNode, 0.1f); player.transform.position = Vector3.MoveTowards(player.transform.position, nextNode, (Time.deltaTime * playerMoveSpeed)); if ((player.transform.position - nextNode).magnitude < 0.2) { playerMovePath.RemoveAt(0); } // if(player.transform.position == nextNode){ // playerMovePath.RemoveAt(0); // } } else { player.transform.position = Vector3.MoveTowards(player.transform.position, positionToMove, (Time.deltaTime * playerMoveSpeed)); // directionVector = positionToMove - player.transform.position; // player.transform.position += Vector3.Normalize(directionVector) * (Time.deltaTime * playerMoveSpeed); // player.transform.position = Vector3.Lerp(player.transform.position, positionToMove, 0.1f); if ((player.transform.position - positionToMove).magnitude < 0.2) { playerIsMoving = false; } else { playerIsMoving = true; } } } else { playerIsMoving = false; } if (!playerIsMoving && !newStrandDrawn) { // Debug.Log("player has reached clicked point, time to draw strand"); if (!node1 || !node2) { return; } // node1.SetActive(true); node1.GetComponent <Renderer>().enabled = true; // node2.SetActive(true); node2.GetComponent <Renderer>().enabled = true; // drawnStrand.SetActive(true); if (!drawnStrandIsSplit) { // Debug.Log("Strand with no intersection"); drawnStrand.GetComponent <Renderer>().enabled = true; } else { // Debug.Log(string.Format("Strand split into {0}", splitStrands.Count)); foreach (GameObject go in splitStrands) { go.GetComponent <Renderer>().enabled = true; } } foreach (GameObject go in intersectNodes) { go.GetComponent <Renderer>().enabled = true; } // Vector3 insectSpawnPos = (node1.transform.position + node2.transform.position) / 2f; // insectSpawnPos[2] = -11; // // Instantiate(smallInsect, insectSpawnPos, Quaternion.identity); UpdateAdjacencyMatrix(); // DisplayAdjacencyMatrix(); newStrandDrawn = true; player.transform.position = node1.transform.position; // reset nodes and strands for next click node1 = null; node2 = null; drawnStrand = null; intersectNodes.Clear(); splitStrands.Clear(); } }
public void nameTBD(Vector3 begin, Vector3 finish) { drawnStrand = drawStrandBetween(begin, finish, true); int strandIndex; string[] splitName = drawnStrand.name.Split(new string[] { "d" }, StringSplitOptions.RemoveEmptyEntries); // foreach(string s in splitName){ // Debug.Log(s); // } strandIndex = Int32.Parse(splitName[1]) + 3; // Debug.Log(string.Format("strandList.Count = {0}, strandIndex = {1}", strandList.Count, strandIndex)); if (strandIndex > strandList.Count) { return; } Strand addedStrand = strandList[strandIndex]; Strand startStrand, endStrand; bool startStrandValid = StrandListContainsPoint(begin, out startStrand); bool endStrandValid = StrandListContainsPoint(finish, out endStrand); List <GameObject> intersectNodes = new List <GameObject>(), splitStrands = null; bool existingStartNodeCopy = existingStartNode, existingEndNodeCopy = existingEndNode; Vector3 intersectPt = Vector3.zero; // bool drawnStrandIsSplit = false; for (int i = strandList.Count - 1; i >= 0; i--) { Strand str = strandList[i]; // Skipping self if (str.Equals(addedStrand)) { continue; } ; if (str.Equals(startStrand) && !existingStartNodeCopy) { splitStrands = SplitStrand(str, new Node[] { addedStrand.GetStartNode() }); } if (str.Equals(endStrand) && !existingEndNodeCopy) { splitStrands = SplitStrand(str, new Node[] { addedStrand.GetEndNode() }); } if (addedStrand.Intersects(str, out intersectPt)) { splitStrands = SplitStrand(str, new Node[] { new Node(intersectPt.x, intersectPt.y) }); GameObject newNode = Instantiate(nodePrefab, intersectPt, Quaternion.identity) as GameObject; // newNode.GetComponent<Renderer>().enabled = false; intersectNodes.Add(newNode); newNode.name = string.Format("Node{0}", nodeList.Count - 3); nodeList.Add(new Node(intersectPt.x, intersectPt.y)); } } if (intersectNodes.Count > 0) { // drawnStrandIsSplit = true; Node[] iNodes = new Node[intersectNodes.Count]; // Debug.Log(string.Format("{0} intersection points", iNodes.Length)); for (int j = 0; j < intersectNodes.Count; j++) { GameObject goTemp = intersectNodes[j]; iNodes[j] = new Node(goTemp.transform.position.x, goTemp.transform.position.y); } // string debugString = "Split strands are: "; splitStrands = SplitStrand(addedStrand, iNodes, false); // foreach(var split in splitStrands){ // debugString += string.Format("{0} at {1}", split.name, split.transform.position.ToString()); // } // Debug.Log(debugString); // string dString = ""; // foreach (var g in GameObject.FindGameObjectsWithTag("Edge")) { // dString += string.Format("{0}, ", g.name); // } // Debug.Log(dString); // Destroy(drawnStrand); } // Debug.Log("after destroying original"); // string debugString = ""; // foreach (var g in GameObject.FindGameObjectsWithTag("Edge")) { // debugString += string.Format("{0}, ", g.name); // } // Debug.Log(debugString); drawnStrand = null; UpdateAdjacencyMatrix(); }
void CalculatePlayerMovePath(Vector3 playerStart, Vector3 playerDestination) { Strand startStrand, endStrand; Node s1, s2, e1, e2; int startIndex, finishIndex; List <Node> path1, path2, path3, path4; path1 = new List <Node>(); path2 = new List <Node>(); path3 = new List <Node>(); path4 = new List <Node>(); int matrixSize = adjacencyMatrix.GetLength(0); Dictionary <Node, Dictionary <Node, float> > vertexList = new Dictionary <Node, Dictionary <Node, float> >(); Dictionary <Node, float> nodeMap = new Dictionary <Node, float>(); for (int i = 0; i < matrixSize; i++) { for (int j = 0; j < matrixSize; j++) { if (adjacencyMatrix[i, j] > 0) { nodeMap.Add(nodeList[j], adjacencyMatrix[i, j]); } } vertexList[nodeList[i]] = new Dictionary <Node, float>(nodeMap); nodeMap.Clear(); } if (StrandListContainsPoint(playerStart, out startStrand) && StrandListContainsPoint(playerDestination, out endStrand)) { if (startStrand.Equals(endStrand)) { return; } s1 = startStrand.GetStartNode(); s2 = startStrand.GetEndNode(); e1 = endStrand.GetStartNode(); e2 = endStrand.GetEndNode(); bool startOnFrame, endOnFrame, startStrandIsFrame = false, endStrandIsFrame = false; for (int i = 0; i < 4; i++) { startOnFrame = strandList[i].ContainsPointWithEnds(s1.xPos(), s1.yPos()); endOnFrame = strandList[i].ContainsPointWithEnds(s2.xPos(), s2.yPos()); if (startOnFrame && endOnFrame) { startStrandIsFrame = true; } startOnFrame = strandList[i].ContainsPointWithEnds(e1.xPos(), e1.yPos()); endOnFrame = strandList[i].ContainsPointWithEnds(e2.xPos(), e2.yPos()); if (startOnFrame && endOnFrame) { endStrandIsFrame = true; } } Node temp; if (startStrandIsFrame && nodeList.Count > 4) { // Debug.Log("start is frame"); temp = startStrand.GetClosestNodeOnStrand(playerStart, nodeList); if (temp.xPos() != float.MaxValue && temp.yPos() != float.MaxValue) { s1 = temp; s2 = startStrand.GetClosestEnd(playerStart); } } if (endStrandIsFrame && nodeList.Count > 4) { // Debug.Log("end is frame"); temp = endStrand.GetClosestNodeOnStrand(playerDestination, nodeList); if (temp.xPos() != float.MaxValue && temp.yPos() != float.MaxValue) { e1 = temp; e2 = endStrand.GetClosestEnd(playerDestination); } } Node playerStartNode = new Node(playerStart.x, playerStart.y), playerDestinationNode = new Node(playerDestination.x, playerDestination.y); if (nodeListContainsAt(playerStartNode, out startIndex)) { // Debug.Log(string.Format("Player position on existing node at {0}", playerStartNode.PositionString())); s1 = playerStartNode; s2 = playerStartNode; } // end node is always existing! if (nodeListContainsAt(playerDestinationNode, out finishIndex)) { // Debug.Log(string.Format("Destination is existing node at {0}", playerDestinationNode.PositionString())); e1 = playerDestinationNode; e2 = playerDestinationNode; } graph = new Graph(vertexList); if (nodeListContainsAt(s1, out startIndex) && nodeListContainsAt(e1, out finishIndex)) { if (s1.Equals(e1)) { path1.Add(s1); } else { path1 = graph.ShortestPath(s1, e1); } } if (nodeListContainsAt(s1, out startIndex) && nodeListContainsAt(e2, out finishIndex)) { if (s1.Equals(e2)) { path2.Add(s1); } else { path2 = graph.ShortestPath(s1, e2); } } if (nodeListContainsAt(s2, out startIndex) && nodeListContainsAt(e1, out finishIndex)) { if (s2.Equals(e1)) { path3.Add(s2); } else { path3 = graph.ShortestPath(s2, e1); } } if (nodeListContainsAt(s2, out startIndex) && nodeListContainsAt(e2, out finishIndex)) { if (s2.Equals(e2)) { path4.Add(s2); } else { path4 = graph.ShortestPath(s2, e2); } } List <Node>[] paths = new List <Node>[] { path1, path2, path3, path4 }; playerMovePath = ShortestDistance(paths); // string pathlist = ""; // foreach(var moveNode in playerMovePath){ // if(playerMovePath.FindIndex(a => a.Equals(moveNode)) == playerMovePath.Count-1){ // pathlist += string.Format("{0}", moveNode.PositionString()); // } else { // pathlist += string.Format("{0} => ", moveNode.PositionString()); // } // } // Debug.Log(pathlist); } }