// Awake is being called before Start; this makes sure we have a matrix // to begin with when we add the blocks void Awake() { // because of how we wrote the accessor this will also immediately // build the matrix of our level var grid = gameObject.GetComponent <RectGrid>(); var para = gameObject.GetComponent <Parallelepiped>(); SlidingPuzzle.InitializePuzzle(grid, para); }
// visualizes the matrix in text form to let you see what's going on void OnGUI() { const int w = 100; const int h = 100; const int x = 10; var y = Screen.height - x - h; GUI.TextArea(new Rect(x, y, w, h), SlidingPuzzle.MatrixToString()); }
void OnMouseUp() { var position = transform.position; _beingDragged = false; /* transform.position = ClampPosition(position); */ SlidingPuzzle._grid.AlignTransform(transform); lastSnap = position; SlidingPuzzle.RegisterObstacle(transform, false); }
void OnMouseDown() { var touchPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition); var position = transform.position; var scale = transform.lossyScale; _beingDragged = true; touchOffset = touchPoint - position; lastSnap = position; _bounds = SlidingPuzzle.CalculateSlidingBounds(position, scale); SlidingPuzzle.RegisterObstacle(transform, true); }
/// <summary> /// This is where the dragging logic takes places. /// </summary> private void Drag() { var input = Camera.main.ScreenToWorldPoint(Input.mousePosition); var scale = transform.lossyScale; var destination = ClampPosition(input - touchOffset); destination.z = transform.position.z; // Now use that information to get the new bounds. _bounds = SlidingPuzzle.CalculateSlidingBounds(lastSnap, scale); // Simulate a snap to the grid so we can get potentially new bounds // in the next step. lastSnap = ClampPosition(SlidingPuzzle._grid.AlignVector3(destination, scale)); // Move to the destination! transform.position = destination; }
void Start() { _beingDragged = false; SlidingPuzzle.RegisterObstacle(transform, false); }