1 using UnityEngine;
2 using
System.Collections;
3 using
UnityStandardAssets.Characters.ThirdPerson;
4 using
UnityStandardAssets.CrossPlatformInput;
5 using
System.Threading;
6
7 public
class LevelScene : MonoBehaviour {
8
9     
public Texture pauseBackgroundTexture;
10
11     
public Texture pause;
12     
public Texture play;
13
14     
public Texture cameraMoveUp;
15     
public Texture cameraMoveLeft;
16     
public Texture cameraMoveRight;
17     
public Texture cameraMoveDown;
18
19     
public Texture cameraMoveForward;
20     
public Texture cameraMoveBack;
21
22     
public Texture cameraRotateUp;
23     
public Texture cameraRotateLeft;
24     
public Texture cameraRotateRight;
25     
public Texture cameraRotateDown;
26
27     
public Texture playerMoveUp;
28     
public Texture playerMoveLeft;
29     
public Texture playerMoveRight;
30     
public Texture playerMoveDown;
31
32     
public GameObject floor;
33     
public GameObject wall;
34     
public GameObject runner;
35
36     
public int level;
37
38     
public float scale = 1;
39     
public int rows = 10;
40     
public int columns = 10;
41
42     
public float translateRate = 3;
43     
public float rotateRate = 2;
44
45     
//*********************
46     ThirdPersonCharacter runnerChar;
47
48     
int buttonWidth = 25;
49     
int buttonHeight = 25;
50     
int textWidth = 100;
51     
int textHeight = 25;
52
53     
float moveUp = 0.05f;
54     
float moveDown = 0.05f;
55     
float moveLeft = 0.05f;
56     
float moveRight = 0.05f;
57
58     // Use
this for initialization
59     
void Start()
60     {
61         PlayerPrefs.SetInt(
"Level", level);
62         Game.SetLevelStats(PlayerPrefs.GetInt(
"Level"));
63
64         GameObject floor = (GameObject)GameObject.Instantiate(
this.floor);
65         //floor.transform.localScale =
new Vector3(scale, scale, scale);
66         floor.transform.localScale =
new Vector3(Game.MazeScale, Game.MazeScale, Game.MazeScale);
67
68         //transform.position =
new Vector3(0, 11 * scale, 0);
69         transform.position =
new Vector3(0, 11 * Game.MazeScale, 0);
70
71         //Maze maze =
new Maze(rows, columns, scale);
72         Maze maze =
new Maze(Game.MazeRows, Game.MazeColumns, Game.MazeScale);
73         maze.Initialize();
74         maze.Draw(runner, wall);
75
76         ResumeLevel();
77     }
78
79     
void Update()
80     {
81         
if (Game.GameState == GameStates.InPlay)
82         {
83             
if (GetSecondsLeft() <= 0)
84             {
85                 Game.GameState = GameStates.Loss;
86                 
var currentLives = PlayerPrefs.GetInt("Lives");
87                 currentLives--;
88                 PlayerPrefs.SetInt(
"Lives", currentLives);
89             }
90
91             
if (/*(Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) && */Input.GetKey(KeyCode.F4))
92             {
93                 
//ResetCameraLocation();
94             }
95             
else if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) //rotate
96             {
97                 RotateUp(CrossPlatformInputManager.GetAxis(
"Vertical"));
98                 RotateRight(CrossPlatformInputManager.GetAxis(
"Horizontal"));
99             }
100             
//else if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) //forward/back
101             
//{
102             
// MoveForward(CrossPlatformInputManager.GetAxis("Vertical"));
103             
//}
104             
else if (((Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) && Input.GetKey(KeyCode.Plus))
105                 || Input.GetKey(KeyCode.KeypadPlus))
106             {
107                 
//camera move forward
108                 MoveForward(
1);
109             }
110             
else if (Input.GetKey(KeyCode.Minus) || Input.GetKey(KeyCode.KeypadMinus))
111             {
112                 
//camera move back
113                 MoveForward(-
1);
114             }
115             
//else if (Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt)) //up/down, left/right
116             
//{
117             
// MoveUp(CrossPlatformInputManager.GetAxis("Vertical"));
118             
// MoveRight(CrossPlatformInputManager.GetAxis("Horizontal"));
119             
//}
120             
else if (Input.GetKey(KeyCode.PageUp))
121             {
122                 
//camera move up
123                 MoveUp(
1);
124             }
125             
else if (Input.GetKey(KeyCode.Home))
126             {
127                 
//camera move left
128                 MoveRight(-
1);
129             }
130             
else if (Input.GetKey(KeyCode.End))
131             {
132                 
//camera move right
133                 MoveRight(
1);
134             }
135             
else if (Input.GetKey(KeyCode.PageDown))
136             {
137                 
//camera move down
138                 MoveUp(-
1);
139             }
140         }
141         
else if (Game.GameState == GameStates.Win)
142         {
143             
var currentLevel = PlayerPrefs.GetInt("Level");
144             currentLevel++;
145             PlayerPrefs.SetInt(
"Level", currentLevel);
146             Thread.Sleep(
1000); //just for good user experience
147             Game.LoadWelcomeLevel();
148         }
149         
else if (Game.GameState == GameStates.Loss)
150         {
151             Thread.Sleep(
1000); //just for good user experience
152             Game.LoadWelcomeLevel();
153         }
154     }
155
156     
void OnGUI()
157     {
158         
if (Game.GameState == GameStates.Pause)
159         {
160             GUI.DrawTexture(
new Rect(Screen.width / 4, Screen.height / 4, Screen.width / 2, Screen.height / 2), pauseBackgroundTexture);
161
162             
//**********PLAY
163             
if (GUI.Button(new Rect(Screen.width - (buttonWidth * 1 + 10), 10, buttonWidth, buttonHeight), play))
164             {
165                 Game.GameState = GameStates.InPlay;
166                 ResumeLevel();
167             }
168         }
169         
else if (Game.GameState == GameStates.InPlay)
170         {
171             //**********STATS
172             GUI.Label(
new Rect(10, 10, 150, 50), "Time: " + GetSecondsLeft());
173             GUI.Label(
new Rect(10, 40, 150, 50), "Level: " + PlayerPrefs.GetInt("Level"));
174             GUI.Label(
new Rect(10, 70, 150, 50), "Lives: " + PlayerPrefs.GetInt("Lives"));
175
176             //**********PAUSE
177             
if (GUI.Button(new Rect(Screen.width - (buttonWidth * 1 + 10), 10, buttonWidth, buttonHeight), pause))
178             {
179                 Game.GameState = GameStates.Pause;
180                 PauseLevel();
181             }
182
183             //**********PLAYER
184             GUI.Label(
new Rect(Screen.width - textWidth + 10, Screen.height - (buttonHeight * 4 + 10), textWidth, textHeight), "Move Player");
185
186             
if (GUI.RepeatButton(new Rect(Screen.width - (buttonWidth * 2 + 10), Screen.height - (buttonHeight * 3 + 10), buttonWidth, buttonHeight), playerMoveUp))
187             {
188                 moveUp +=
0.07f;
189                 moveDown =
0.05f;
190                 moveLeft =
0.05f;
191                 moveRight =
0.05f;
192                 //player move up
193                 MovePlayerForward(-moveUp);
194             }
195             
if (GUI.RepeatButton(new Rect(Screen.width - (buttonWidth * 3 + 10), Screen.height - (buttonHeight * 2 + 10), buttonWidth, buttonHeight), playerMoveLeft))
196             {
197                 moveUp =
0.05f;
198                 moveDown =
0.05f;
199                 moveLeft +=
0.07f;
200                 moveRight =
0.05f;
201                 //player move left
202                 MovePlayerRight(-moveLeft);
203             }
204             
if (GUI.RepeatButton(new Rect(Screen.width - (buttonWidth * 1 + 10), Screen.height - (buttonHeight * 2 + 10), buttonWidth, buttonHeight), playerMoveRight))
205             {
206                 moveUp =
0.05f;
207                 moveDown =
0.05f;
208                 moveLeft =
0.05f;
209                 moveRight +=
0.07f;
210                 //player move right
211                 MovePlayerRight(moveRight);
212             }
213             
if (GUI.RepeatButton(new Rect(Screen.width - (buttonWidth * 2 + 10), Screen.height - (buttonHeight + 10), buttonWidth, buttonHeight), playerMoveDown))
214             {
215                 moveUp =
0.05f;
216                 moveDown +=
0.07f;
217                 moveLeft =
0.05f;
218                 moveRight =
0.05f;
219                 //player move down
220                 MovePlayerForward(moveDown);
221             }
222
223             //*********CAMERA
224
225             GUI.Label(
new Rect(10, Screen.height - (buttonHeight * 10 + 10), textWidth, textHeight), "Rotate Camera");
226
227             
if (GUI.RepeatButton(new Rect(buttonWidth + 10, Screen.height - (buttonHeight * 9 + 10), buttonWidth, buttonHeight), cameraRotateUp))
228             {
229                 //camera rotate up
230                 RotateUp(
0.1f);
231             }
232             
if (GUI.RepeatButton(new Rect(10, Screen.height - (buttonHeight * 8 + 10), buttonWidth, buttonHeight), cameraRotateLeft))
233             {
234                 //camera rotate left
235                 RotateRight(-
0.1f);
236             }
237             
if (GUI.RepeatButton(new Rect(buttonWidth * 2 + 10, Screen.height - (buttonHeight * 8 + 10), buttonWidth, buttonHeight), cameraRotateRight))
238             {
239                 //camera rotate right
240                 RotateRight(
0.1f);
241             }
242             
if (GUI.RepeatButton(new Rect(buttonWidth + 10, Screen.height - (buttonHeight * 7 + 10), buttonWidth, buttonHeight), cameraRotateDown))
243             {
244                 //camera rotate down
245                 RotateUp(-
0.1f);
246             }
247
248             GUI.Label(
new Rect(textWidth + 20, Screen.height - (buttonHeight * 5 + 10), textWidth, textHeight * 2), "Move Camera\nZ-axis");
249
250             
if (GUI.RepeatButton(new Rect(buttonWidth * 5 + 20, Screen.height - (buttonHeight * 3 + 10), buttonWidth, buttonHeight), cameraMoveForward))
251             {
252                 //camera move forward
253                 MoveForward(
1);
254             }
255             
if (GUI.RepeatButton(new Rect(buttonWidth * 5 + 20, Screen.height - (buttonHeight + 10), buttonWidth, buttonHeight), cameraMoveBack))
256             {
257                 //camera move back
258                 MoveForward(-
1);
259             }
260
261             GUI.Label(
new Rect(10, Screen.height - (buttonHeight * 5 + 10), textWidth, textHeight * 2), "Move Camera\nXY-axes");
262
263             
if (GUI.RepeatButton(new Rect(buttonWidth + 10, Screen.height - (buttonHeight * 3 + 10), buttonWidth, buttonHeight), cameraMoveUp))
264             {
265                 //camera move up
266                 MoveUp(
1);
267             }
268             
if (GUI.RepeatButton(new Rect(10, Screen.height - (buttonHeight * 2 + 10), buttonWidth, buttonHeight), cameraMoveLeft))
269             {
270                 //camera move left
271                 MoveRight(-
1);
272             }
273             
if (GUI.RepeatButton(new Rect(buttonWidth * 2 + 10, Screen.height - (buttonHeight * 2 + 10), buttonWidth, buttonHeight), cameraMoveRight))
274             {
275                 //camera move right
276                 MoveRight(
1);
277             }
278             
if (GUI.RepeatButton(new Rect(buttonWidth + 10, Screen.height - (buttonHeight + 10), buttonWidth, buttonHeight), cameraMoveDown))
279             {
280                 //camera move down
281                 MoveUp(-
1);
282             }
283         }
284     }
285
286     
void MoveForward(float axis)
287     {
288         
var forwardMoveAmount = axis * translateRate;
289         transform.position += transform.forward * forwardMoveAmount * Time.deltaTime;
290     }
291     
void MoveRight(float axis)
292     {
293         
var rightMoveAmount = axis * translateRate;
294         transform.position += transform.right * rightMoveAmount * Time.deltaTime;
295     }
296     
void MoveUp(float axis)
297     {
298         
var forwardMoveAmount = axis * translateRate;
299         transform.position += transform.up * forwardMoveAmount * Time.deltaTime;
300     }
301     
void RotateRight(float axis)
302     {
303         
var rightTurnForce = axis * rotateRate;
304         transform.Rotate(
0, -rightTurnForce, 0);
305     }
306     
void RotateUp(float axis)
307     {
308         
var forwardTurnForce = axis * rotateRate;
309         transform.Rotate(forwardTurnForce,
0, 0);
310     }
311
312     
void MovePlayerForward(float axis)
313     {
314         //
var m_CamForward = Vector3.Scale(transform.forward, new Vector3(1, 0, 1)).normalized;
315         //
var m_Move = axis * m_CamForward;
316         
var m_Move = axis * Vector3.forward;
317         ThirdPersonUserControl.ThirdPersonCharacter.Move(m_Move,
false, false);
318
319         ResumeLevel();
320     }
321     
void MovePlayerRight(float axis)
322     {
323         //
var m_CamForward = Vector3.Scale(transform.forward, new Vector3(1, 0, 1)).normalized;
324         
var m_Move = axis * transform.right;
325         //
var m_Move = axis * Vector3.right;
326         ThirdPersonUserControl.ThirdPersonCharacter.Move(m_Move,
false, false);
327
328         //ThirdPersonUserControl.ThirdPersonCharacter.GhostMode(
true);
329         PauseLevel();
330     }
331
332     
float GetSecondsLeft()
333     {
334         
var ellapsed = Time.timeSinceLevelLoad;
335         
var timeLeft = Game.Duration - ellapsed;
336         
return timeLeft;
337     }
338     
void ResumeLevel()
339     {
340         Time.timeScale =
1;
341     }
342
343     
void PauseLevel()
344     {
345         Time.timeScale =
0;
346         //
var mainCam = GameObject.FindGameObjectsWithTag("MainCamera")[0];
347         //mainCam.GetComponent<Camera>().backgroundColor =
new Color(0, 0, 0);
348     }
349 }


Gõ tìm kiếm nhanh...