public bool TouchIsOnMe(Vector3 touchpos) { PieceController floatcheck = EditorController.GetFloatingPiece(); if (floatcheck != this && floatcheck != null) { if (floatcheck.TouchIsOnMe(touchpos)) { return(false); } } RectTransform rt = (RectTransform)transform; UIRectTranslator translate = transform.gameObject.GetComponent <UIRectTranslator>(); bool rectangleOverlap = rt.rect.Contains(rt.InverseTransformPoint(new Vector2(touchpos.x, touchpos.y))); //sr.bounds.IntersectRay(new Ray(touchpos,transform.forward)); if (!rectangleOverlap) { return(false); } //find local x and y of touch Vector3 worldSize = translate.WorldSize(rt); //Debug.Log("Rsize: " + rt.rect.size.x + "," + rt.rect.size.y); //Debug.Log("worldsize: " + worldSize.x + "," + worldSize.y); float squareWidth = worldSize.y / (float)codes.GetLength(0); Vector3[] corners = new Vector3[4]; rt.GetWorldCorners(corners); Vector3 rCorner = corners[1]; Vector3 relativePos = new Vector3(touchpos.x - rCorner.x, rCorner.y - touchpos.y, transform.position.z); if (squareWidth <= 0) { Debug.Log("BAD BAD TIMES"); return(false); } int xcount = -1; int ycount = -1; while (relativePos.x >= 0) { relativePos = new Vector3(relativePos.x - squareWidth, relativePos.y, relativePos.z); xcount++; } while (relativePos.y >= 0) { relativePos = new Vector3(relativePos.x, relativePos.y - squareWidth, relativePos.z); ycount++; } int code = codes[ycount, xcount]; bool result = TouchHelper(relativePos, squareWidth, code); return(result); }
public void PutThatThingBackWhereItCameFromOrSoHelpMe(PieceController pc, int x, int y) { RectTransform prt = (RectTransform)pc.transform; RectTransform rt = (RectTransform)transform; UIRectTranslator translate = transform.gameObject.GetComponent <UIRectTranslator>(); Vector3[] corners = new Vector3[4]; rt.GetWorldCorners(corners); Vector3 gridTopCorner = corners[1]; //new Vector3(gridCorner.x,gridCorner.y+gridLength,gridCorner.z); Vector3 extents = translate.WorldSize(prt) / 2; pc.transform.position = gridTopCorner + new Vector3(squareWidth * x + extents.x, -squareWidth * (y) - extents.y, pc.transform.position.z); }
public Vector3 GetTopLeftCorner() { RectTransform rt = (RectTransform)transform; UIRectTranslator translate = transform.gameObject.GetComponent <UIRectTranslator>(); return(translate.WorldTopLeftCorner(rt)); //return new Vector3(boundsmin.x,boundsmax.y,transform.position.z); //all this stuff was supposed to compensate for rotation, but it apparently doesn't matter /*if(rotation == 0){ * return sprite.bounds.min; * }else if(rotation == 90){ * return new Vector3(sprite.bounds.min.x,sprite.bounds.max.y,0f); * }else if(rotation == 180){ * return sprite.bounds.max; * }else if(rotation == 270){ * return new Vector3(sprite.bounds.max.x,sprite.bounds.max.y,0f); * }else{ * Debug.Log ("piece has rotation that should be impossible"); * return sprite.bounds.min; * }*/ }
//Timer test = new Timer(); public void Start() { Debug.Log(Application.persistentDataPath); RectTransform rt = (RectTransform)transform; UIRectTranslator translate = transform.gameObject.GetComponent <UIRectTranslator>(); canvas = GameObject.Find("Canvas").GetComponent <Canvas>(); nameEntry = canvas.gameObject.transform.Find("NameEntry").GetComponent <InputField>(); //SpriteRenderer gridSprite = transform.gameObject.GetComponent<SpriteRenderer>(); Vector3[] corners = new Vector3[4]; rt.GetWorldCorners(corners); gridCorner = corners[0]; //rt.TransformVector(rt.rect.min) + transform.position;//gridSprite.bounds.min; gridLength = translate.WorldSize(rt).x; //rt.TransformVector(rt.rect.size).x; //gridSprite.bounds.size.x; squareWidth = gridLength / (float)GRID_SIZE; PieceController.gridSquareWidth = squareWidth; //Debug.Log("SW: " + squareWidth); for (int i = 0; i < GRID_SIZE; i++) { for (int j = 0; j < GRID_SIZE; j++) { grid[i, j] = new Occupancy(); GameObject go = Instantiate(Resources.Load("Prefabs/ValidOverlay")) as GameObject; go.transform.SetParent(EditorController.overlaysLayer, false); go.transform.position = new Vector3(gridCorner.x + (squareWidth / 2) + j * squareWidth, gridCorner.y + gridLength - (squareWidth / 2) - i * squareWidth, go.transform.position.z); //Debug.Log (go.transform.position.x + "," + go.transform.position.y); //Debug.Log ("GC: " + gridCorner.x + "," + gridCorner.y); overlays[i, j] = go.GetComponent <Image>(); } } string blankfile = "GridEmptyOverlay"; Texture2D tBlank = Resources.Load <Texture2D> ("Sprites/" + blankfile); blank = UnityEngine.Sprite.Create( tBlank, new Rect(0, 0, tBlank.width, tBlank.height), new Vector2(0.5f, 0.5f), 80f); string fullfile = "GridSquareOverlay"; Texture2D tFull = Resources.Load <Texture2D> ("Sprites/" + fullfile); full = UnityEngine.Sprite.Create( tFull, new Rect(0, 0, tFull.width, tFull.height), new Vector2(0.5f, 0.5f), 80f); string nwfile = "GridNWOverlay"; Texture2D tNW = Resources.Load <Texture2D> ("Sprites/" + nwfile); nw = UnityEngine.Sprite.Create( tNW, new Rect(0, 0, tNW.width, tNW.height), new Vector2(0.5f, 0.5f), 80f); string nefile = "GridNEOverlay"; Texture2D tNE = Resources.Load <Texture2D> ("Sprites/" + nefile); ne = UnityEngine.Sprite.Create( tNE, new Rect(0, 0, tNE.width, tNE.height), new Vector2(0.5f, 0.5f), 80f); string sefile = "GridSEOverlay"; Texture2D tSE = Resources.Load <Texture2D> ("Sprites/" + sefile); se = UnityEngine.Sprite.Create( tSE, new Rect(0, 0, tSE.width, tSE.height), new Vector2(0.5f, 0.5f), 80f); string swfile = "GridSWOverlay"; Texture2D tSW = Resources.Load <Texture2D> ("Sprites/" + swfile); sw = UnityEngine.Sprite.Create( tSW, new Rect(0, 0, tSW.width, tSW.height), new Vector2(0.5f, 0.5f), 80f); //LoadTower("drainpunch"); //test.Restart(); //GameObject loader = GameObject.Find ("NameHolder"); //LoadTower (loader.GetComponent<TowerLoad> ().towerName); //Destroy (loader); }
public void LoadTower(string filename) { towerFileName = filename; //Debug.Log(towerFileName); FileLoader fl = new FileLoader(Application.persistentDataPath, "Towers", filename); //FileLoader fl = new FileLoader ("JSONData" + Path.DirectorySeparatorChar + "Towers",filename); string json = fl.Read(); Dictionary <string, System.Object> data = (Dictionary <string, System.Object>)Json.Deserialize(json); towerName = (string)data["towerName"]; decalFilename = (string)data["decalFilename"]; towerType = (string)data["towerType"]; List <System.Object> pieces = data["pieces"] as List <System.Object>; foreach (System.Object pObj in pieces) { Dictionary <string, System.Object> pdata = pObj as Dictionary <string, System.Object>; int x = (int)(long)pdata["anchorX"]; int y = (int)(long)pdata["anchorY"]; int rot = (int)(long)pdata["rotation"]; bool flipped = false; int fliprot = 0; if (pdata.ContainsKey("flipped")) { flipped = (bool)pdata["flipped"]; fliprot = (int)(long)pdata["flipRotation"]; } string piecefile = (string)pdata["pieceFilename"]; GameObject go = Instantiate(Resources.Load("Prefabs/ExistingPiece")) as GameObject; go.transform.SetParent(EditorController.piecesLayer, false); PieceController p = go.GetComponent <PieceController>(); p.SetGridLock(true); p.ConfigureFromJSON(piecefile); p.SetRotation(rot); p.SetFlippedLoadOnly(flipped); p.SetFlipRotationLoadOnly(fliprot); if (flipped) { p.CalibrateFlip(); } allPieces.Add(new PieceRecord(p, x, y)); //SpriteRenderer sr = go.GetComponent<SpriteRenderer>(); //PutThatThingBackWhereItCameFromOrSoHelpMe(go.GetComponent<PieceController>()); RectTransform prt = (RectTransform)go.transform; RectTransform rt = (RectTransform)transform; UIRectTranslator translate = transform.gameObject.GetComponent <UIRectTranslator>(); Vector3[] corners = new Vector3[4]; rt.GetWorldCorners(corners); Vector3 gridTopCorner = corners[1]; //new Vector3(gridCorner.x,gridCorner.y+gridLength,gridCorner.z); Vector3 extents = translate.WorldSize(prt) / 2; go.transform.position = gridTopCorner + new Vector3(squareWidth * x + extents.x, -squareWidth * (y) - extents.y, go.transform.position.z); int[,] values = p.GetArray(); //we assume all pieces fit- no error checking for (int i = 0; i < values.GetLength(0); i++) { for (int j = 0; j < values.GetLength(1); j++) { int value = values[i, j]; Occupancy o = grid[y + i, x + j]; if (value == 1) { o.north = p; o.east = p; o.south = p; o.west = p; } else if (value == PieceController.NORTHWEST_CODE) { o.north = p; o.west = p; } else if (value == PieceController.NORTHEAST_CODE) { o.north = p; o.east = p; } else if (value == PieceController.SOUTHEAST_CODE) { o.east = p; o.south = p; } else if (value == PieceController.SOUTHWEST_CODE) { o.south = p; o.west = p; } } } } UpdateReadout(); }