public void Draw(PuzzleImage.TilePosition screenPos, PuzzleImage.TilePosition imgPos, bool solved) { Game game = Game.Get(); // Create object GameObject gameObject = new GameObject("PuzzleTile"); gameObject.tag = "dialog"; gameObject.transform.parent = game.uICanvas.transform; RectTransform trans = gameObject.AddComponent <RectTransform>(); trans.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, UIScaler.GetPixelsPerUnit() + (UIScaler.GetPixelsPerUnit() * screenPos.y * height), height * UIScaler.GetPixelsPerUnit()); trans.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, (UIScaler.GetHCenter(-11.5f) * UIScaler.GetPixelsPerUnit()) + (UIScaler.GetPixelsPerUnit() * screenPos.x * width), width * UIScaler.GetPixelsPerUnit()); gameObject.AddComponent <CanvasRenderer>(); // Create the image UnityEngine.UI.Image image = gameObject.AddComponent <UnityEngine.UI.Image>(); image.sprite = imageSprite[imgPos.x][imgPos.y]; image.rectTransform.sizeDelta = new Vector2(width * UIScaler.GetPixelsPerUnit(), height * UIScaler.GetPixelsPerUnit()); if (solved) { return; } BlockSlider slider = gameObject.AddComponent <BlockSlider>(); slider.screenPos = screenPos; slider.win = this; }
// Update is called once per frame void Update() { if (!sliding && !Input.GetMouseButtonDown(0)) { return; } if (Input.GetMouseButtonDown(0)) { if (Input.mousePosition.x < trans.position.x - (trans.rect.width / 2f)) { return; } if (Input.mousePosition.y < trans.position.y - (trans.rect.height / 2f)) { return; } if (Input.mousePosition.x > trans.position.x + (trans.rect.width / 2f)) { return; } if (Input.mousePosition.y > trans.position.y + (trans.rect.height / 2f)) { return; } sliding = true; mouseStart = Input.mousePosition; transStart = trans.anchoredPosition; } if (!sliding) { return; } float yDiff = Input.mousePosition.y - mouseStart.y; float xDiff = Input.mousePosition.x - mouseStart.x; if (screenPos.x == 0 && xDiff < 0) { xDiff = 0; } if (screenPos.y == 0 && yDiff > 0) { yDiff = 0; } if (screenPos.x == (win.questPuzzle.puzzleLevel - 1) && xDiff > 0) { xDiff = 0; } if (screenPos.y == (win.questPuzzle.puzzleAltLevel - 1) && yDiff < 0) { yDiff = 0; } if (xDiff > (win.width * UIScaler.GetPixelsPerUnit())) { xDiff = (win.width * UIScaler.GetPixelsPerUnit()); } if (xDiff < -(win.width * UIScaler.GetPixelsPerUnit())) { xDiff = -(win.width * UIScaler.GetPixelsPerUnit()); } if (yDiff > (win.height * UIScaler.GetPixelsPerUnit())) { yDiff = (win.height * UIScaler.GetPixelsPerUnit()); } if (yDiff < -(win.height * UIScaler.GetPixelsPerUnit())) { yDiff = -(win.height * UIScaler.GetPixelsPerUnit()); } if (Mathf.Abs(yDiff) > Mathf.Abs(xDiff)) { xDiff = 0; } else { yDiff = 0; } Vector3 pos = new Vector3(transStart.x, transStart.y, 0); pos.x += xDiff; pos.y += yDiff; trans.anchoredPosition = pos; trans.SetAsLastSibling(); if (!Input.GetMouseButton(0)) { sliding = false; int Xshift = Mathf.RoundToInt(xDiff / (win.width * UIScaler.GetPixelsPerUnit())); int Yshift = -Mathf.RoundToInt(yDiff / (win.height * UIScaler.GetPixelsPerUnit())); if (Yshift != 0 || Xshift != 0) { win.puzzle.moves++; PuzzleImage.TilePosition swap = null; foreach (KeyValuePair <PuzzleImage.TilePosition, PuzzleImage.TilePosition> kv in win.puzzle.state) { if ((kv.Key.x == screenPos.x + Xshift) && (kv.Key.y == screenPos.y + Yshift)) { swap = kv.Key; } } swap.x = screenPos.x; swap.y = screenPos.y; screenPos.x += Xshift; screenPos.y += Yshift; win.CreateWindow(); } else { pos = new Vector3(transStart.x, transStart.y, 0); trans.anchoredPosition = pos; } } }