ign="justify">//set the correct positions to both objects (Matrix [i, j]!=null) [i, j] .GameObject.transform.position=GetScreenCoordinatesFromVieport (i, j); [random_i, random_j] .GameObject.transform.position
=GetScreenCoordinatesFromVieport (random_i, random_j);
//set the required properties (Matrix [i, j]!=null) {[i, j] .CurrentI=i; [i, j] .CurrentJ=j;
} [random_i, random_j] .CurrentI=random_i; [random_i, random_j] .CurrentJ=random_j;
}
//Update is called once per frameUpdate ()
{(gameState)
{GameState.Start: (Input.GetMouseButtonUp (0))
{();=GameState.Playing;
}; GameState.Playing :() ;; GameState.Animating: (PieceToAnimate, Time.deltaTime); () ;; GameState.End:=GetComponent lt; Text gt; () ;. Find ( puzzleend ). GetComponent lt; Text gt; () .enabled=true; (Input.GetMouseButtonUp (0))
{//reload.LoadLevel(Application.loadedLevel);
};:;
}
} CheckPieceInput ()
{(Input.GetMouseButtonUp (0)) {ray=Camera.main.ScreenPointToRay (Input.mousePosition); D hit=Physics2D.Raycast (ray.origin, ray.direction);
//check if a piece was hit (hit.collider!=null) {name=hit.collider.gameObject.name; [] parts=name.Split ( - ); iPart=int.Parse (parts [1]); jPart=int.Parse (parts [2]); iFound=- 1, jFound=- 1;
//find which one was hit, in our 2D array
//there must be a better way than this one (int i=0; i lt; Constants.MaxColumns; i ++) {(iFound!=- 1); (int j=0; j lt; Constants.MaxRows; j ++) {(iFound!=- 1); (Matrix [i, j] == null); (Matrix [i, j] .OriginalI == iPart
amp; amp; Matrix [i, j] .OriginalJ == jPart) {= i;=j;
}
}
} foundPiece=Matrix [iFound, jFound];
//check for the null piece, taking into account the game boundspieceFound=false; (iFound gt; 0 amp; amp; Matrix [iFound - 1, jFound] == null) {= true; =iFound - 1;=jFound;
} else if (jFound gt; 0 amp; amp; Matrix [iFound, jFound - 1] == null) {= true;=iFound;=jFound - 1;
} else if (iFound lt; Constants.MaxColumns - 1 amp; amp; Matrix [iFound + 1, jFound] == null) {= true;=iFound + 1;=jFound;
} else if (jFound lt; Constants.MaxRows - 1 amp; amp; Matrix [iFound, jFound + 1] == null) {= true;=iFound;=jFound + 1;
} (pieceFound) {
//get the coordinates of the empty object=GetScreenCoordinatesFromVieport (toAnimateI, toAnimateJ);=Matrix [iFound, jFound];=GameState.Animating;
}
}
}
} void AnimateMovement (Piece toMove, float time)
{
//animate it
//Lerp could also be used, but I prefer the MoveTowards approach :). GameObject.transform.position=Vector2.MoveTowards (toMove.GameObject.transform.position ,, time * AnimSpeed);
}
/// lt; summary gt;
/// A simple check to see if the animation has finished
/// lt;/summary gt; void CheckIfAnimationEnded ()
{(Vector2.Distance (PieceToAnimate.GameObject.transform.position,) lt; 0.1f)
{
//make sure they swap, exchange positions and stuff (PieceToAnimate.CurrentI, PieceToAnimate.CurrentJ, toAnimateI, toAnimateJ);=GameState.Playing;
//check if the use has won ();
}
} void CheckForVictory ()
{(int i=0; i lt; Constants.MaxColumns; i ++)
{(int j=0; j lt; Constants.MaxRows; j ++)
{(Matrix [i, j] == null) continue; ((Matrix [i, j] .CurrentJ!=Matrix [i, j] .OriginalJ) || (Matrix [i, j] .CurrentI!=Matrix [i, j] .OriginalI)) return;
//at least one wrong piece, so we have not won (yet!)
}
} (index);
//go [index] .SetActive (true);=GameState.End;
} Vector3 GetScreenCoordinatesFromVieport (int i, int j)
{point=Camera.main.ViewportToWorldPoint (new Vector3 (ratio * j + fromleft, 1f - 0.205f * i - 0.2f, 0)) ;. z=0; point;
} screenPositionToAnimate; Piece PieceToAnimate; int toAnimateI, toAnimateJ; Piece [,] Matrix=new Piece [Constants.MaxColumns, Constants.MaxRows]; GameState gameSta...