IEnumerator getPositionUpdates() { //Debug.Log("getText"); UnityWebRequest www = UnityWebRequest.Get("http://localhost:5000/fishtank/connections"); yield return(www.SendWebRequest()); if (www.isNetworkError || www.isHttpError) { Debug.Log(www.error); } else { string result = www.downloadHandler.text; while (result != "empty") { Debug.Log("!"); Debug.Log(result); ConnectionInstruction info = getConnectionInfo(result); newConnections.Enqueue(info); www = UnityWebRequest.Get("http://localhost:5000/fishtank/connections"); yield return(www.SendWebRequest()); if (www.isNetworkError || www.isHttpError) { Debug.Log(www.error); break; } result = www.downloadHandler.text; } } StartCoroutine(getDisconnectionUpdates()); }
private BoxInfo getThisBoxInst(ConnectionInstruction inst) { BoxInfo thisBoxInst = inst.firstBox; if (thisBoxInst.boxNo != boxNumber) { thisBoxInst = inst.secondBox; } return(thisBoxInst); }
public bool sameInstruction(ConnectionInstruction inst) { if (!firstBox.compareBoxInfo(inst.firstBox)) { return(false); } if (!secondBox.compareBoxInfo(inst.secondBox)) { return(false); } return(true); }
public void breakConnection(ConnectionInstruction instruction, FishBox otherBox) { string otherCorner = instruction.secondBox.corner; string otherWall = instruction.secondBox.wall; List <ConnectionInstruction> sharedConnections = new List <ConnectionInstruction>(); if (connectedBoxes.ContainsKey(otherBox)) { sharedConnections = connectedBoxes[otherBox]; } Queue <ConnectionInstruction> keepConnections = new Queue <ConnectionInstruction>(); ConnectionInstruction trashInstruction = null; foreach (ConnectionInstruction inst in sharedConnections) { if (inst.sameInstruction(instruction)) { Debug.Log("AA"); trashInstruction = inst; } else { keepConnections.Enqueue(inst); } } if (trashInstruction != null) { if (keepConnections.Count >= 2 /*||is diagonal connection*/) { connectedBoxes[otherBox].Remove(trashInstruction); otherBox.connectedBoxes[this].Remove(trashInstruction); } else { separateFrom(otherBox, true); while (keepConnections.Count != 0) { ConnectionInstruction inst = keepConnections.Dequeue(); createConnection(inst, otherBox); } } } else { Debug.Log("Could not break connection " + instruction + " because the boxes dont share this connection"); } }
IEnumerator UpdatePosition() { while (newDisconnections.Count != 0) { ConnectionInstruction inst = newDisconnections.Dequeue(); FishBox firstBox = boxes[inst.firstBox.boxNo]; FishBox secondBox = boxes[inst.secondBox.boxNo]; firstBox.breakConnection(inst, secondBox); } while (newConnections.Count != 0) { ConnectionInstruction inst = newConnections.Dequeue(); FishBox firstBox = boxes[inst.firstBox.boxNo]; FishBox secondBox = boxes[inst.secondBox.boxNo]; firstBox.createConnection(inst, secondBox); } Debug.Log("hey"); avoidPositionCoroutine = null; yield return(null); }
private void addConnection(ConnectionInstruction inst, FishBox otherBox) { BoxInfo thisBoxInst = inst.firstBox; BoxInfo otherBoxInst = inst.secondBox; string thisCorner = thisBoxInst.corner; string thisWall = thisBoxInst.wall; string otherCorner = otherBoxInst.corner; string otherWall = otherBoxInst.wall; if (connectedBoxes[otherBox][0].secondBox.wall != otherWall || connectedBoxes[otherBox][0].firstBox.wall != thisWall) { Debug.Log("Cannot complete instruction " + inst.ToString() + ". A box cannot be connected to two different walls on the same box"); return; } if (connectedBoxes[otherBox].Count >= 2) { connectedBoxes[otherBox].Add(inst); otherBox.connectedBoxes[this].Add(inst); return; } else { Vector3 otherPos = otherBox.getPosition(); //apply to all connections - write a helper method would you int direction = 1; if (otherBoxInst.wall == "bottom") { direction = -1; } //so we need to do movements relative otherbox fishTank.transform.rotation = otherBox.fishTank.transform.rotation; fishTank.transform.position = otherPos + (otherBox.fishTank.transform.up * boxWidthZ * direction); connectWalls(thisWall, otherBox, otherWall, true); connectedBoxes[otherBox].Add(inst); otherBox.connectedBoxes[this].Add(inst); return; } }
private ConnectionInstruction makeInst(int boxno1, string wall1, string corner1, int boxno2, string wall2, string corner2) { BoxInfo box1 = new BoxInfo(); box1.boxNo = boxno1; box1.corner = corner1; box1.wall = wall1; BoxInfo box2 = new BoxInfo(); box2.boxNo = boxno2; box2.corner = corner2; box2.wall = wall2; ConnectionInstruction inst = new ConnectionInstruction(); inst.timeStamp = -1; inst.firstBox = box1; inst.secondBox = box2; return(inst); }
IEnumerator getDisconnectionUpdates() { UnityWebRequest www = UnityWebRequest.Get("http://localhost:5000/fishtank/disconnections"); yield return(www.SendWebRequest()); if (www.isNetworkError || www.isHttpError) { Debug.Log(www.error); } else { string result = www.downloadHandler.text; while (result != "empty") { Debug.Log(result); ConnectionInstruction info = getConnectionInfo(result); newDisconnections.Enqueue(info); www = UnityWebRequest.Get("http://localhost:5000/fishtank/disconnections"); yield return(www.SendWebRequest()); if (www.isNetworkError || www.isHttpError) { Debug.Log(www.error); break; } result = www.downloadHandler.text; } } avoidWifiCoroutine = null; //Debug.Log(newConnections.Count); if ((newDisconnections.Count > 0 || newConnections.Count > 0) /*&& avoidPositionCoroutine == null*/) { //Debug.Log("you"); avoidPositionCoroutine = StartCoroutine(UpdatePosition()); } }
// Update is called once per frame void Update() { //Debug.Log("pos: " + avoidPositionCoroutine); //Debug.Log(avoidWifiCoroutine); if (avoidWifiCoroutine == null /*(&& avoidPositionCoroutine == null*/) { //Debug.Log("A"); avoidWifiCoroutine = StartCoroutine(getPositionUpdates()); } ConnectionInstruction inst1 = makeInst(1, "bottom", "B", 0, "top", "B"); ConnectionInstruction inst2 = makeInst(1, "bottom", "C", 0, "top", "A"); ConnectionInstruction inst3 = makeInst(1, "bottom", "C", 0, "top", "A"); ConnectionInstruction inst4 = makeInst(1, "bottom", "B", 0, "top", "B"); ConnectionInstruction inst5 = makeInst(1, "bottom", "A", 0, "top", "B"); ConnectionInstruction inst6 = makeInst(1, "bottom", "C", 0, "top", "D"); ConnectionInstruction inst7 = makeInst(1, "bottom", "C", 0, "top", "D"); //remember to set a coroutine block // //avoidPositionCoroutine = StartCoroutine(UpdatePosition()); if (Input.GetKeyDown("1")) { /*foreach (Instruction inst in instructions) * { * //run disconnections first to avoid confusion about positions * //if (!inst.connect) { inst.firstBox.breakConnections(inst.secondBox, inst.relationship); } * }*/ //foreach (Instruction inst in instructions) //{ FishBox firstBox = boxes[inst1.firstBox.boxNo]; FishBox secondBox = boxes[inst1.secondBox.boxNo]; firstBox.createConnection(inst1, secondBox); } else if (Input.GetKeyDown("2")) { FishBox firstBox = boxes[inst2.firstBox.boxNo]; FishBox secondBox = boxes[inst2.secondBox.boxNo]; firstBox.createConnection(inst2, secondBox); } else if (Input.GetKeyDown("3")) { FishBox firstBox = boxes[inst3.firstBox.boxNo]; FishBox secondBox = boxes[inst3.secondBox.boxNo]; firstBox.breakConnection(inst3, secondBox); } else if (Input.GetKeyDown("4")) { FishBox firstBox = boxes[inst4.firstBox.boxNo]; FishBox secondBox = boxes[inst4.secondBox.boxNo]; firstBox.breakConnection(inst4, secondBox); } /*GameObject wall = boxes[0].getTankComponent("bottomWall"); * Renderer renderer = wall.transform.Find("A").GetComponent<Renderer>(); * Material mat = renderer.material; * * float emission = 0 + Mathf.PingPong(Time.time * (0.003f), .02f); * Color baseColor = new Color(26, 55, 50); //Replace this with whatever you want for your base color at emission level '1' * * Color finalColor = baseColor * Mathf.LinearToGammaSpace(emission); * * mat.SetColor("_EmissionColor", finalColor);*/ }
/*private void addConnection(ConnectionInstruction inst, FishBox otherBox) { * BoxInfo thisBoxInst = inst.firstBox; * BoxInfo otherBoxInst = inst.secondBox; * string thisCorner = thisBoxInst.corner; * string thisWall = thisBoxInst.wall; * string otherCorner = otherBoxInst.corner; * string otherWall = otherBoxInst.wall; * * if (connectedBoxes[otherBox][0].secondBox.wall != otherWall || * connectedBoxes[otherBox][0].firstBox.wall != thisWall) { * Debug.Log("Cannot complete instruction " + inst.ToString() + * ". A box cannot be connected to two different walls on the same box"); * return; * } * if (connectedBoxes[otherBox].Count > 2) * { * connectedBoxes[otherBox].Add(inst); * otherBox.connectedBoxes[this].Add(inst); * return; * } * else if (!getTankComponent(thisWall + "Wall").activeSelf && * !otherBox.getTankComponent(otherWall + "Wall").activeSelf) * { * connectedBoxes[otherBox].Add(inst); * otherBox.connectedBoxes[this].Add(inst); * return; * } * else if (connectedBoxes[otherBox].Count == 2) * { * //if already 2 connections check for if it could touch and set wall, else do nothing * connectedBoxes[otherBox].Add(inst); * otherBox.connectedBoxes[this].Add(inst); * } * else { * List<string> thisAdj = getAdjacentCorners(thisCorner); * List<string> otherAdj = getAdjacentCorners(otherCorner); * string thisDia = getDiagonalCorner(thisCorner); * string otherDia = getDiagonalCorner(otherCorner); * * BoxInfo thisPrevInst = connectedBoxes[otherBox][0].firstBox; * BoxInfo otherPrevInst = connectedBoxes[otherBox][0].secondBox; * if (otherPrevInst.boxNo == thisBoxInst.boxNo) { * thisPrevInst = connectedBoxes[otherBox][0].secondBox; * otherPrevInst = connectedBoxes[otherBox][0].firstBox; * } * //dia dia * if (thisDia == thisPrevInst.corner * && otherDia == otherPrevInst.corner) { * fishTank.transform.rotation = (fishTank.transform.rotation * Quaternion.Euler(Vector3.up * 180)); * Vector3 newPos = otherBox.getPosition(); * newPos.y = getPosition().y; * fishTank.transform.position = newPos; * connectCorners(inst, otherBox, false); * connectWalls(thisWall, otherBox, otherWall, true); * } * //adj adj * else if (thisAdj.Contains(thisPrevInst.corner) * && otherAdj.Contains(otherPrevInst.corner)) * { * * GameObject wall = otherBox.getTankComponent(otherPrevInst.wall + "Wall"); * GameObject corner = wall.transform.Find(otherPrevInst.corner).gameObject; * Debug.Log(corner.transform.position); * fishTank.transform.RotateAround(corner.transform.position, Vector3.up, 90); * //makeCornersTouch(getThisBoxInst(inst), otherPrevInst, otherBox, corner.transform.position); * * //fishTank.transform * //rotate so newthis corner is on prevothercorner * //slide * } * else { * Debug.Log("not implemented"); * } * * //adj adj * //dia adj * //adj dia * * //rotate * * } * /*check wall status - if off do nothing * if already 3 connections - do nothing * * if already 2 connections check for if it could touch and set wall, else do nothing * if 1 connection figure out how to rotate, if diagonal set wall * */ // if were at 2 already then you can rotate anymore, so if were on a good side then go full wall //if 3+ or diag then go for full wall, //}*/ private void connectCorners(ConnectionInstruction inst, FishBox otherBox, bool animation) { connectedBoxes[otherBox].Add(inst); otherBox.connectedBoxes[this].Add(inst); //animation }
public void createConnection(ConnectionInstruction inst, FishBox otherBox) { Debug.Log(inst.ToString()); BoxInfo thisBoxInst = inst.firstBox; BoxInfo otherBoxInst = inst.secondBox; string thisCorner = thisBoxInst.corner; string thisWall = thisBoxInst.wall; string otherCorner = otherBoxInst.corner; string otherWall = otherBoxInst.wall; if (connectedBoxes.ContainsKey(otherBox)) { foreach (ConnectionInstruction i in connectedBoxes[otherBox]) { if (inst.sameInstruction(i)) { Debug.Log("Cannot complete instruction " + inst.ToString() + " because the connection already exists"); return; } } addConnection(inst, otherBox); } else if (this == otherBox) { Debug.Log("Cannot complete instruction " + inst.ToString() + ". Cant connect box to itself"); } else { Vector3 otherPos = otherBox.getPosition(); //apply to all connections - write a helper method would you int direction = 1; if (otherBoxInst.wall == "bottom") { direction = -1; } //so we need to do movements relative otherbox fishTank.transform.position = otherPos + (otherBox.fishTank.transform.up * boxWidthZ * direction); if (otherCorner == "A" || otherCorner == "B") { fishTank.transform.position += (otherBox.fishTank.transform.forward * (boxWidthZ / 2)); } else { fishTank.transform.position += (otherBox.fishTank.transform.forward * (-boxWidthZ / 2)); } if (otherCorner == "B" || otherCorner == "D") { fishTank.transform.position += (otherBox.fishTank.transform.right * (boxWidthX / 2)); } else { fishTank.transform.position += (otherBox.fishTank.transform.right * (-boxWidthX / 2)); } makeCornersTouch(thisBoxInst, otherBoxInst, otherBox, fishTank.transform.position); setWallCorner(false, thisWall, thisCorner); otherBox.setWallCorner(false, otherWall, otherCorner); connectedBoxes[otherBox] = new List <ConnectionInstruction> { inst }; otherBox.connectedBoxes[this] = new List <ConnectionInstruction> { inst }; } }
public static ConnectionInstruction getConnectionInfo(string json) { ConnectionInstruction info = JsonUtility.FromJson <ConnectionInstruction>(json); return(info); }