Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

...erLuminal\SlidingTilePuzzle\Scripts\ST_PuzzleDisplay.

cs 1
1 using UnityEngine;
2 using System.Collections;
3 using System.Collections.Generic;
4
5 public class ST_PuzzleDisplay : MonoBehaviour
6 {
7 // this puzzle texture.
8 public Texture PuzzleImage;
9
10 // the width and height of the puzzle in tiles.
11 public int Height = 3;
12 public int Width = 3;
13
14 // additional scaling value.
15 public Vector3 PuzzleScale = new Vector3(1.0f, 1.0f, 1.0f);
16
17 // additional positioning offset.
18 public Vector3 PuzzlePosition = new Vector3(0.0f, 0.0f, 0.0f);
19
20 // seperation value between puzzle tiles.
21 public float SeperationBetweenTiles = 0.5f;
22
23 // the tile display object.
24 public GameObject Tile;
25
26 // the shader used to render the puzzle.
27 public Shader PuzzleShader;
28
29 // array of the spawned tiles.
30 private GameObject[,] TileDisplayArray;
31 private List<Vector3> DisplayPositions = new List<Vector3>();
32
33 // position and scale values.
34 private Vector3 Scale;
35 private Vector3 Position;
36
37 // has the puzzle been completed?
38 public bool Complete = false;
39
40 // Use this for initialization
41 void Start ()
42 {
43 // create the games puzzle tiles from the provided image.
44 CreatePuzzleTiles();
45
46 // mix up the puzzle.
47 StartCoroutine(JugglePuzzle());
48
49 }
50
51 // Update is called once per frame
52 void Update ()
53 {
54 // move the puzzle to the position set in the inspector.
55 this.transform.localPosition = PuzzlePosition;
56
...erLuminal\SlidingTilePuzzle\Scripts\ST_PuzzleDisplay.cs 2
57 // set the scale of the entire puzzle object as set in the inspector.
58 this.transform.localScale = PuzzleScale;
59 }
60
61 public Vector3 GetTargetLocation(ST_PuzzleTile thisTile)
62 {
63 // check if we can move this tile and get the position we can move to.
64 ST_PuzzleTile MoveTo = CheckIfWeCanMove((int)thisTile.GridLocation.x,
(int)thisTile.GridLocation.y, thisTile);
65
66 if(MoveTo != thisTile)
67 {
68 // get the target position for this new tile.
69 Vector3 TargetPos = MoveTo.TargetPosition;
70 Vector2 GridLocation = thisTile.GridLocation;
71 thisTile.GridLocation = MoveTo.GridLocation;
72
73 // move the empty tile into this tiles current position.
74 MoveTo.LaunchPositionCoroutine(thisTile.TargetPosition);
75 MoveTo.GridLocation = GridLocation;
76
77 // return the new target position.
78 return TargetPos;
79 }
80
81 // else return the tiles actual position (no movement).
82 return thisTile.TargetPosition;
83 }
84
85 private ST_PuzzleTile CheckMoveLeft(int Xpos, int Ypos, ST_PuzzleTile
thisTile)
86 {
87 // move left
88 if((Xpos - 1) >= 0)
89 {
90 // we can move left, is the space currently being used?
91 return GetTileAtThisGridLocation(Xpos - 1, Ypos, thisTile);
92 }
93
94 return thisTile;
95 }
96
97 private ST_PuzzleTile CheckMoveRight(int Xpos, int Ypos, ST_PuzzleTile
thisTile)
98 {
99 // move right
100 if((Xpos + 1) < Width)
101 {
102 // we can move right, is the space currently being used?
103 return GetTileAtThisGridLocation(Xpos + 1, Ypos , thisTile);
104 }
105
106 return thisTile;
107 }
108
109 private ST_PuzzleTile CheckMoveDown(int Xpos, int Ypos, ST_PuzzleTile
...erLuminal\SlidingTilePuzzle\Scripts\ST_PuzzleDisplay.cs 3
thisTile)
110 {
111 // move down
112 if((Ypos - 1) >= 0)
113 {
114 // we can move down, is the space currently being used?
115 return GetTileAtThisGridLocation(Xpos, Ypos - 1, thisTile);
116 }
117
118 return thisTile;
119 }
120
121 private ST_PuzzleTile CheckMoveUp(int Xpos, int Ypos, ST_PuzzleTile
thisTile)
122 {
123 // move up
124 if((Ypos + 1) < Height)
125 {
126 // we can move up, is the space currently being used?
127 return GetTileAtThisGridLocation(Xpos, Ypos + 1, thisTile);
128 }
129
130 return thisTile;
131 }
132
133 private ST_PuzzleTile CheckIfWeCanMove(int Xpos, int Ypos, ST_PuzzleTile
thisTile)
134 {
135 // check each movement direction
136 if(CheckMoveLeft(Xpos, Ypos, thisTile) != thisTile)
137 {
138 return CheckMoveLeft(Xpos, Ypos, thisTile);
139 }
140
141 if(CheckMoveRight(Xpos, Ypos, thisTile) != thisTile)
142 {
143 return CheckMoveRight(Xpos, Ypos, thisTile);
144 }
145
146 if(CheckMoveDown(Xpos, Ypos, thisTile) != thisTile)
147 {
148 return CheckMoveDown(Xpos, Ypos, thisTile);
149 }
150
151 if(CheckMoveUp(Xpos, Ypos, thisTile) != thisTile)
152 {
153 return CheckMoveUp(Xpos, Ypos, thisTile);
154 }
155
156 return thisTile;
157 }
158
159 private ST_PuzzleTile GetTileAtThisGridLocation(int x, int y,
ST_PuzzleTile thisTile)
160 {
161 for(int j = Height - 1; j >= 0; j--)
...erLuminal\SlidingTilePuzzle\Scripts\ST_PuzzleDisplay.cs 4
162 {
163 for(int i = 0; i < Width; i++)
164 {
165 // check if this tile has the correct grid display location.
166 if((TileDisplayArray[i,j].GetComponent<ST_PuzzleTile>
().GridLocation.x == x)&&
167 (TileDisplayArray[i,j].GetComponent<ST_PuzzleTile>
().GridLocation.y == y))
168 {
169 if(TileDisplayArray[i,j].GetComponent<ST_PuzzleTile>
().Active == false)
170 {
171 // return this tile active property.
172 return TileDisplayArray
[i,j].GetComponent<ST_PuzzleTile>();
173 }
174 }
175 }
176 }
177
178 return thisTile;
179 }
180
181 private IEnumerator JugglePuzzle()
182 {
183 yield return new WaitForSeconds(1.0f);
184
185 // hide a puzzle tile (one is always missing to allow the puzzle
movement).
186 TileDisplayArray[0,0].GetComponent<ST_PuzzleTile>().Active = false;
187
188 yield return new WaitForSeconds(1.0f);
189
190 for(int k = 0; k < 20; k++)
191 {
192 // use random to position each puzzle section in the array delete
the number once the space is filled.
193 for(int j = 0; j < Height; j++)
194 {
195 for(int i = 0; i < Width; i++)
196 {
197 // attempt to execute a move for this tile.
198 TileDisplayArray[i,j].GetComponent<ST_PuzzleTile>
().ExecuteAdditionalMove();
199
200 yield return new WaitForSeconds(0.02f);
201 }
202 }
203 }
204
205 // continually check for the correct answer.
206 StartCoroutine(CheckForComplete());
207
208 yield return null;
209 }
210
...erLuminal\SlidingTilePuzzle\Scripts\ST_PuzzleDisplay.cs 5
211 public IEnumerator CheckForComplete()
212 {
213 while(Complete == false)
214 {
215 // iterate over all the tiles and check if they are in the correct
position.
216 Complete = true;
217 for(int j = Height - 1; j >= 0; j--)
218 {
219 for(int i = 0; i < Width; i++)
220 {
221 // check if this tile has the correct grid display
location.
222 if(TileDisplayArray[i,j].GetComponent<ST_PuzzleTile>
().CorrectLocation == false)
223 {
224 Complete = false;
225 }
226 }
227 }
228
229 yield return null;
230 }
231
232 // if we are still complete then all the tiles are correct.
233 if(Complete)
234 {
235 Debug.Log("Puzzle Complete!");
236 }
237
238 yield return null;
239 }
240
241 private Vector2 ConvertIndexToGrid(int index)
242 {
243 int WidthIndex = index;
244 int HeightIndex = 0;
245
246 // take the index value and return the grid array location X,Y.
247 for(int i = 0; i < Height; i++)
248 {
249 if(WidthIndex < Width)
250 {
251 return new Vector2(WidthIndex, HeightIndex);
252 }
253 else
254 {
255 WidthIndex -= Width;
256 HeightIndex++;
257 }
258 }
259
260 return new Vector2(WidthIndex, HeightIndex);
261 }
262
263 private void CreatePuzzleTiles()
...erLuminal\SlidingTilePuzzle\Scripts\ST_PuzzleDisplay.cs 6
264 {
265 // using the width and height variables create an array.
266 TileDisplayArray = new GameObject[Width,Height];
267
268 // set the scale and position values for this puzzle.
269 Scale = new Vector3(1.0f/Width, 1.0f, 1.0f/Height);
270 Tile.transform.localScale = Scale;
271
272 // used to count the number of tiles and assign each tile a correct
value.
273 int TileValue = 0;
274
275 // spawn the tiles into an array.
276 for(int j = Height - 1; j >= 0; j--)
277 {
278 for(int i = 0; i < Width; i++)
279 {
280 // calculate the position of this tile all centred around
Vector3(0.0f, 0.0f, 0.0f).
281 Position = new Vector3(((Scale.x * (i + 0.5f))-(Scale.x *
(Width/2.0f))) * (10.0f + SeperationBetweenTiles),
282 0.0f,
283 ((Scale.z * (j + 0.5f))-(Scale.z *
(Height/2.0f))) * (10.0f + SeperationBetweenTiles));
284
285 // set this location on the display grid.
286 DisplayPositions.Add(Position);
287
288 // spawn the object into play.
289 TileDisplayArray[i,j] = Instantiate(Tile, new Vector3(0.0f,
0.0f, 0.0f) , Quaternion.Euler(90.0f, -180.0f, 0.0f)) as
GameObject;
290 TileDisplayArray[i,j].gameObject.transform.parent =
this.transform;
291
292 // set and increment the display number counter.
293 ST_PuzzleTile thisTile = TileDisplayArray
[i,j].GetComponent<ST_PuzzleTile>();
294 thisTile.ArrayLocation = new Vector2(i,j);
295 thisTile.GridLocation = new Vector2(i,j);
296 thisTile.LaunchPositionCoroutine(Position);
297 TileValue++;
298
299 // create a new material using the defined shader.
300 Material thisTileMaterial = new Material(PuzzleShader);
301
302 // apply the puzzle image to it.
303 thisTileMaterial.mainTexture = PuzzleImage;
304
305 // set the offset and tile values for this material.
306 thisTileMaterial.mainTextureOffset = new Vector2(1.0f/Width *
i, 1.0f/Height * j);
307 thisTileMaterial.mainTextureScale = new Vector2(1.0f/Width,
1.0f/Height);
308
309 // assign the new material to this tile for display.
...erLuminal\SlidingTilePuzzle\Scripts\ST_PuzzleDisplay.cs 7
310 TileDisplayArray[i,j].GetComponent<Renderer>().material =
thisTileMaterial;
311 }
312 }
313
314 /*
315 // Enable an impossible puzzle for fun!
316 // switch the second and third grid location textures.
317 Material thisTileMaterial2 = TileDisplayArray
[1,3].GetComponent<Renderer>().material;
318 Material thisTileMaterial3 = TileDisplayArray
[2,3].GetComponent<Renderer>().material;
319 TileDisplayArray[1,3].GetComponent<Renderer>().material =
thisTileMaterial3;
320 TileDisplayArray[2,3].GetComponent<Renderer>().material =
thisTileMaterial2;
321 */
322 }
323 }
324

You might also like