/// <summary> /// Populates new invisible dots and links throughout the grid /// </summary> private void PopulateInvisibleDotsAndLinks() { float gridW = dotsBox.sprite.bounds.size.x / Config.rowCount; float offsetx = -1 * gridW * Config.rowCount / 2 + gridW / 2; float offsety = offsetx; for (int i = 0; i < Config.rowCount * Config.rowCount; i++) { int tx = Mathf.FloorToInt(i % Config.rowCount); int ty = Mathf.FloorToInt(i / Config.rowCount); DotsController dot = Instantiate(dotsPrefab, dotsHolderTransform); //Spawning each invisible and interactive dots float tscale = gridW / dot.GetSpriteRenderer().bounds.size.x; dot.transform.localScale *= tscale; dot.transform.localPosition = new Vector2(dotsHolderTransform.localPosition.x + gridW * tx + offsetx, dotsHolderTransform.localPosition.y + gridW * ty + offsety); dot.GetComponent <SpriteRenderer>().sortingOrder = 1; dot.name = "bg" + tx + "_" + ty; dot.GetSpriteRenderer().color = Color.clear; dotsList.Add(dot); Config.dotsDict.Add(dot.name, dot); dot.SetData(tx, ty); //Setting data to each dot Config.ColorData[i] = 0; Config.DotColorData[i] = 0; int[] rotation = new int[] { 0, 90, 180, 270 }; for (int j = 0; j < Config.linksPerDot; j++) { SpriteRenderer link = Instantiate(linkPrefab, dotsHolderTransform); //Spawning each links per dots link.transform.localPosition = dot.transform.localPosition; link.transform.localScale = dot.transform.localScale; link.transform.localEulerAngles = new Vector3(0, 0, rotation[j]); link.color = Color.clear; link.sortingOrder = 2; linksList.Add(link); switch (j) { case 0: //right Config.linDict.Add("linkr" + tx + "_" + ty, link); break; case 1: //up Config.linDict.Add("linku" + tx + "_" + ty, link); break; case 2: //left Config.linDict.Add("linkl" + tx + "_" + ty, link); break; case 3: //down Config.linDict.Add("linkd" + tx + "_" + ty, link); break; } } } }
/// <summary> /// Clears old path on clicking old dot or being intersected by other link /// </summary> /// <param name="tcolorid"></param> private void ClearOldPath(int tcolorid) { int tlen = Config.paths[tcolorid].Count; while (Config.paths[tcolorid].Count > 0) { int oldid = Config.paths[tcolorid][Config.paths[tcolorid].Count - 1]; int oldx = Mathf.FloorToInt(oldid % Config.rowCount); int oldy = Mathf.FloorToInt(oldid / Config.rowCount); DotsController oldBg = Config.dotsDict["bg" + oldx + "_" + oldy]; Color tcolor = Config.colors[0]; oldBg.GetSpriteRenderer().color = tcolor; Config.ColorData[oldid] = 0; RemovePath(tcolorid, Config.paths[tcolorid].Count - 1); Config.linkedLines[tcolorid] = 0; } }
/// <summary> /// Handles functionalities for OnMouseDown event /// </summary> private void HandleOnMouseDown() { if (Config.isWin) { return; } if (Config.isHolding) { return; } Debug.Log("DotsController, OnMouseDown"); int tdotColor = Config.DotColorData[id]; if (tdotColor != 0) { ClearOldPath(tdotColor); Config.pickColor = tdotColor; Color tcolor = Config.colors[tdotColor]; tcolor.a = .5f; if (colorPath) { spriteRenderer.color = tcolor; } Config.ColorData[id] = tdotColor; Config.paths[tdotColor] = new List <int>(); AddPath(tdotColor, id); } else { int cBlockColor = Config.ColorData[id]; if (cBlockColor == 0) { return; } else { Config.pickColor = cBlockColor; Config.linkedLines[cBlockColor] = 0; for (int i = Config.paths[cBlockColor].Count - 1; i > 0; i--) { int oldid = Config.paths[cBlockColor][i]; if (oldid != id) { int oldx = Mathf.FloorToInt(oldid % Config.rowCount); int oldy = Mathf.FloorToInt(oldid / Config.rowCount); DotsController oldBg = Config.dotsDict["bg" + oldx + "_" + oldy]; Color tcolor = Config.colors[0]; oldBg.GetSpriteRenderer().color = tcolor; Config.ColorData[oldid] = 0; RemovePath(cBlockColor, i); AddPath(cBlockColor, id); } else { break; } } } } Config.isHolding = true; Config.startId = id; Config.lasttx = tx; Config.lastty = ty; CheckForWin(); }
/// <summary> /// Handles functionalities for OnMouseOver event /// </summary> private void HandleOnMouseOver() { if (Config.isWin) { return; } if (Config.isHolding) { if (Config.pickColor != 0) { int tdotColor = Config.DotColorData[id]; int tColorid = Config.ColorData[id]; if ((tdotColor == 0 && tColorid == 0) || Config.pickColor == tdotColor && Config.paths[tdotColor][0] != id) { if ((Mathf.Abs(tx - Config.lasttx) == 1 && ty == Config.lastty) || (Mathf.Abs(ty - Config.lastty) == 1 && tx == Config.lasttx)) { SetColor(); } } else { if (tColorid != 0) { int tlen = Config.paths[tColorid].Count; if ((Mathf.Abs(tx - Config.lasttx) == 1 && ty == Config.lastty) || (Mathf.Abs(ty - Config.lastty) == 1 && tx == Config.lasttx)) { if (tColorid == Config.pickColor) { int oldId = Config.paths[tColorid][Config.paths[tColorid].Count - 1]; while (oldId != id) { int oldx = Mathf.FloorToInt(oldId % Config.rowCount); int oldy = Mathf.FloorToInt(oldId / Config.rowCount); DotsController oldBg = Config.dotsDict["bg" + oldx + "_" + oldy]; Color tcolor = Config.colors[0]; oldBg.GetSpriteRenderer().color = tcolor; RemovePath(tColorid, Config.paths[tColorid].Count - 1); Config.ColorData[oldId] = 0; oldId = Config.paths[tColorid][Config.paths[tColorid].Count - 1]; Config.lasttx = tx; Config.lastty = ty; } } else { int tOtherColorId = Config.ColorData[id]; int oldId = Config.paths[tOtherColorId][Config.paths[tOtherColorId].Count - 1]; if (Config.DotColorData[oldId] == 0 || tOtherColorId != Config.pickColor) { if (Config.DotColorData[id] == 0) { while (oldId != id) { int oldx = Mathf.FloorToInt(oldId % Config.rowCount); int oldy = Mathf.FloorToInt(oldId / Config.rowCount); DotsController oldBg = Config.dotsDict["bg" + oldx + "_" + oldy]; Color tcolor = Config.colors[0]; oldBg.GetSpriteRenderer().color = tcolor; RemovePath(tOtherColorId, Config.paths[tOtherColorId].Count - 1); Config.ColorData[oldId] = 0; oldId = Config.paths[tOtherColorId][Config.paths[tOtherColorId].Count - 1]; } if (oldId == id) { int oldx = Mathf.FloorToInt(oldId % Config.rowCount); int oldy = Mathf.FloorToInt(oldId / Config.rowCount); DotsController oldBg = Config.dotsDict["bg" + oldx + "_" + oldy]; Color tcolor = Config.colors[0]; oldBg.GetSpriteRenderer().color = tcolor; RemovePath(tOtherColorId, Config.paths[tOtherColorId].Count - 1); Config.ColorData[oldId] = 0; } } } } } } } } CheckForWin(); } }