Advertisement
Guest User

puzzlemanager

a guest
Jul 20th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 35.36 KB | None | 0 0
  1. using PuzzleSystem.PuzzlePiece.V1;
  2. using UnityEngine.UI;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5.  
  6. namespace PuzzleSystem.PuzzleManagers.V1
  7. {
  8. public enum GamePhase
  9. {
  10. FirstStep,
  11. SecondStep,
  12. ThirdStep,
  13. FourthStep,
  14. Win,
  15. Lose,
  16. }
  17. public class PuzzleManager : MonoBehaviour
  18. {
  19. public PuzzlePieceBehavior[] _puzzlePieces;
  20. public PuzzleSpotBehavior[] _puzzleSpot;
  21. public bool _pieceIsDragging;
  22. public float _pieceWidth;
  23. public float _pieceHeight;
  24. public int rowCount;
  25. public int colCount;
  26. public int _tounchedTarget;
  27. public int _draggedTarget;
  28. public Vector2[] _answer;
  29. //[SerializeField] private int[] _solvingField;
  30. public float _pieceGap;
  31. public GamePhase _playerProgress;
  32. public Vector2[] _numToPosition;
  33. public Text _winText;
  34. public Text _moveText;
  35. public int TotalMove = 9;
  36. public GameObject MoveCount;
  37. public GameObject Click2;
  38. public GameObject Doubleclick;
  39. public GameObject Swap;
  40. public GameObject connect;
  41. public CameraBehavior _mainCamera;
  42. public VideoBehavior _videoPlayer;
  43. public Button _skipButton;
  44. public Button _resetButton;
  45. public Button winButton;
  46. //[SerializeField] private int[] _puzzleSetup;
  47. public int _totalPiece;
  48. public bool isPosSet = false;
  49. public bool isFirstCall = true;
  50. public bool isFirstMistake = false;
  51. public int[] startedPosition;
  52. public AudioSource audioData;
  53. public AudioSource nadra1;
  54. public AudioSource nadra2;
  55. //public AudioSource nadra3;
  56. public AudioSource nadra4;
  57. public string otherAct;
  58. public string FinalScene;
  59. public bool PieceIsDragging
  60. {
  61. set
  62. {
  63. _pieceIsDragging = value;
  64. }
  65. get
  66. {
  67. return _pieceIsDragging;
  68. }
  69. }
  70.  
  71. public int TouchedTarget
  72. {
  73. set
  74. {
  75. _tounchedTarget = value;
  76. }
  77. get
  78. {
  79. return _tounchedTarget;
  80. }
  81. }
  82.  
  83. public int DraggedTarget
  84. {
  85. set
  86. {
  87. _draggedTarget = value;
  88. }
  89. get
  90. {
  91. return _draggedTarget;
  92. }
  93. }
  94. // Use this for initialization
  95. void Start()
  96. {
  97. audioData = GetComponent<AudioSource>();
  98. //make sure we have enough puzzle pieces
  99. Debug.Assert((rowCount * colCount) == _puzzlePieces.Length);
  100. //Debug.Assert(_answer.Length == _solvingField.Length);
  101.  
  102. _totalPiece = _puzzlePieces.Length;
  103. Debug.Log("totalPiece" + _totalPiece);
  104. _pieceIsDragging = false;
  105.  
  106. //get the size of the pieces so that we can set up the puzzle
  107. _pieceWidth = _puzzlePieces[0].GetComponent<SpriteRenderer>().bounds.size.x;
  108. _pieceHeight = _puzzlePieces[0].GetComponent<SpriteRenderer>().bounds.size.y;
  109.  
  110. _tounchedTarget = -1;
  111. _draggedTarget = -1;
  112.  
  113. //These part might not been used
  114. //set up the piece in the order
  115. //OrderPuzzle();
  116. //SetPieceNumber();
  117.  
  118. //Instantiate a dictionary
  119. _numToPosition = new Vector2[_totalPiece];
  120. _answer = new Vector2[_totalPiece];
  121.  
  122. //Set the game Progress to very beginning
  123. _playerProgress = GamePhase.FirstStep;
  124.  
  125. //Hide all the pieces
  126. StorePiecePosition();
  127. HideAllPieces();
  128. GameProgessTracktor();
  129.  
  130. _skipButton.onClick.AddListener(GameProgessTracktor);
  131. _resetButton.onClick.AddListener(Restart);
  132. _resetButton.gameObject.SetActive(false);
  133.  
  134. _winText.gameObject.SetActive(false);
  135.  
  136. _moveText.text = "Remaining: " + TotalMove;
  137.  
  138. nadra1.Play();
  139. //Test, play video at the begninning
  140. //We might not be using this
  141. //_videoPlayer.DisplayAndPlayOnPieces(0);
  142. }
  143.  
  144. private void StorePiecePosition()
  145. {
  146.  
  147. GameObject[] pieces = GameObject.FindGameObjectsWithTag("PuzzlePiece");
  148. //Debug.Log("Piece I find: " + pieces.Length);
  149. for(int i = 0; i < pieces.Length; i++)
  150. {
  151. _numToPosition[pieces[i].GetComponent<PuzzlePieceBehavior>().PieceNumber] = pieces[i].transform.position;
  152. }
  153.  
  154.  
  155. Vector2 botLeftPoint = GetBotLeftPoint();
  156. for (int i = 0; i < colCount; i++)
  157. {
  158. for (int j = 0; j < rowCount; j++)
  159. {
  160. int target = i * rowCount + j;
  161. Vector2 newPosition = new Vector2(botLeftPoint.x + i * (_pieceWidth + _pieceGap), botLeftPoint.y + j * (_pieceHeight + _pieceGap));
  162. newPosition.x = Mathf.Round(newPosition.x * 1000) / 1000;
  163. newPosition.y = Mathf.Round(newPosition.y * 1000) / 1000;
  164. _answer[target] = newPosition;
  165. }
  166. }
  167. }
  168.  
  169. private void OrderPuzzle()
  170. {
  171. Vector2 botLeftPoint = GetBotLeftPoint();
  172. //Debug.Log("botLeftPoint: " + botLeftPoint);
  173. for(int i = 0; i < rowCount; i++)
  174. {
  175. for(int j = 0; j < colCount; j++)
  176. {
  177. int target = i * colCount + j;
  178. //Vector2 newPosition = new Vector2(botLeftPoint.x + j * _pieceWidth, botLeftPoint.y + i * _pieceHeight);
  179. Vector2 newPosition = new Vector2(botLeftPoint.x + j * (_pieceWidth + _pieceGap), botLeftPoint.y + i * (_pieceHeight + _pieceGap));
  180. _puzzlePieces[target].transform.position = newPosition;
  181. }
  182. }
  183. return;
  184. }
  185.  
  186. private Vector2 GetBotLeftPoint()
  187. {
  188. float leftPoint, botPoint;
  189. int totalPuzzle = _puzzlePieces.Length;
  190. if (colCount % 2 == 0)
  191. {
  192. //The solution with the gap
  193. leftPoint = -(_pieceWidth + _pieceGap) * ((colCount / 2) - 1 + 0.5f);
  194. }
  195. else
  196. {
  197. leftPoint = -(_pieceWidth + _pieceGap) * ((colCount / 2));
  198. }
  199.  
  200. if (rowCount % 2 == 0)
  201. {
  202. botPoint = -(_pieceHeight + _pieceGap) * ((rowCount / 2) - 1 + 0.5f);
  203. }
  204. else
  205. {
  206. botPoint = -(_pieceHeight + _pieceGap) * ((rowCount / 2));
  207. }
  208.  
  209. return new Vector2(leftPoint, botPoint);
  210. }
  211.  
  212. public void SetPieceNumber()
  213. {
  214. /*Debug.Assert(_solvingField.Length == _puzzlePieces.Length);
  215. for(int i = 0; i < _solvingField.Length; i++)
  216. {
  217. _puzzlePieces[i].PieceNumber = _solvingField[i];
  218. //_puzzlePieces[i].SolvingNumber = i;
  219. }*/
  220. }
  221.  
  222. // Update is called once per frame
  223. void Update()
  224. {
  225. //Add it for debug
  226. //GameProgessTracktor();
  227. if(!isPosSet)
  228. {
  229. StorePiecePosition();
  230. isPosSet = true;
  231. }
  232. }
  233.  
  234. public void SwapPosition(Vector3 firstLastPosition)
  235. {
  236. //swap the position of the real object
  237. //Vector3 tempPos;
  238. _puzzlePieces[_draggedTarget].transform.position = _puzzlePieces[_tounchedTarget].transform.position;
  239. _puzzlePieces[_tounchedTarget].transform.position = firstLastPosition;
  240.  
  241. //swap the positoin in the dictionary
  242. /*_numToPosition[_draggedTarget] = _puzzlePieces[_draggedTarget].transform.position;
  243. _numToPosition[_tounchedTarget] = _puzzlePieces[_tounchedTarget].transform.position;*/
  244.  
  245.  
  246. //Detect the spot and get it's new number
  247. _puzzlePieces[_draggedTarget].DetectSpot();
  248. _puzzlePieces[_tounchedTarget].DetectSpot();
  249.  
  250. CheckAnswer();
  251.  
  252. }
  253.  
  254. public void CheckAnswer()
  255. {
  256. switch (_playerProgress)
  257. {
  258. case GamePhase.FirstStep:
  259. //-----start here-----
  260. if (!_puzzlePieces[1].IsAnswerCorrect())
  261. {
  262. _puzzlePieces[1].gameObject.GetComponent<SpriteRenderer>().color = new Color(1.0f, 0.7f, 0.7f);
  263. }
  264. else
  265. {
  266. if (_puzzlePieces[1].IsFlipped)
  267. {
  268. _puzzlePieces[1].gameObject.GetComponent<SpriteRenderer>().color = new Color(1.0f, 0.7f, 0.7f);
  269. }
  270. }
  271. //-----end here-----
  272. if (_puzzlePieces[0].IsAnswerCorrect() && _puzzlePieces[1].IsAnswerCorrect()
  273. && !_puzzlePieces[0].IsFlipped && !_puzzlePieces[1].IsFlipped)
  274. {
  275. _playerProgress = GamePhase.SecondStep;
  276. _puzzlePieces[1].SetIsDraggable(false);
  277. connect.SetActive(false);
  278. //change the color back-----
  279. _puzzlePieces[1].gameObject.GetComponent<SpriteRenderer>().color = new Color(1.0f, 1.0f, 1.0f);
  280. //--------------------------
  281. StartCoroutine(_puzzlePieces[0].playGifRoutine(0));
  282. }
  283. //start here for nadra audio
  284. else {
  285. if (isFirstCall)
  286. {
  287. isFirstCall = false;
  288.  
  289. }
  290. else
  291. {
  292. if (!isFirstMistake)
  293. {
  294. //play nadra 2
  295. //audioData.Play(3);
  296. nadra1.mute = true;
  297. nadra2.mute = false;
  298. nadra1.Stop();
  299. nadra2.Play();
  300. //Debug.Log("First Mistake");
  301. MoveCount.SetActive(true);
  302. Click2.SetActive(false);
  303.  
  304. Doubleclick.SetActive(true);
  305. //aSource.PlayOneShot(clips[1]);
  306. isFirstMistake = true;
  307. }
  308. else
  309. {
  310.  
  311. nadra1.mute = true;
  312. nadra2.mute = true;
  313. //nadra3.mute = false;
  314.  
  315. //nadra3.Play();
  316. //play nadra 3
  317. // audioData.Play(4);
  318. }
  319. }
  320. }
  321. //end here
  322. break;
  323. case GamePhase.SecondStep:
  324. Doubleclick.SetActive(true);
  325. if (!_puzzlePieces[2].IsAnswerCorrect())
  326. {
  327. _puzzlePieces[2].gameObject.GetComponent<SpriteRenderer>().color = new Color(1.0f, 0.7f, 0.7f);
  328.  
  329. }
  330. else
  331. {
  332. if (_puzzlePieces[2].IsFlipped)
  333. {
  334. _puzzlePieces[2].gameObject.GetComponent<SpriteRenderer>().color = new Color(1.0f, 0.7f, 0.7f);
  335. }
  336. }
  337. if (!_puzzlePieces[5].IsAnswerCorrect())
  338. {
  339. _puzzlePieces[5].gameObject.GetComponent<SpriteRenderer>().color = new Color(1.0f, 0.7f, 0.7f);
  340. }
  341. else
  342. {
  343. if (_puzzlePieces[5].IsFlipped)
  344. {
  345. _puzzlePieces[5].gameObject.GetComponent<SpriteRenderer>().color = new Color(1.0f, 0.7f, 0.7f);
  346. }
  347. }
  348. if (_puzzlePieces[2].IsAnswerCorrect() && _puzzlePieces[5].IsAnswerCorrect()
  349. && !_puzzlePieces[2].IsFlipped && !_puzzlePieces[5].IsFlipped)
  350. {
  351. Doubleclick.SetActive(false);
  352. _playerProgress = GamePhase.ThirdStep;
  353. _puzzlePieces[2].SetIsDraggable(false);
  354. _puzzlePieces[2].gameObject.GetComponent<SpriteRenderer>().color = new Color(1.0f, 1.0f, 1.0f);
  355. _puzzlePieces[5].SetIsDraggable(false);
  356. _puzzlePieces[5].gameObject.GetComponent<SpriteRenderer>().color = new Color(1.0f, 1.0f, 1.0f);
  357. StartCoroutine(_puzzlePieces[2].playGifRoutine(0));
  358. }
  359. break;
  360.  
  361. case GamePhase.ThirdStep:
  362. Swap.SetActive(true);
  363. //nadra4.Play();
  364.  
  365. if (!_puzzlePieces[3].IsAnswerCorrect())
  366. {
  367. _puzzlePieces[3].gameObject.GetComponent<SpriteRenderer>().color = new Color(1.0f, 0.7f, 0.7f);
  368.  
  369. // nadra4.Play();
  370.  
  371. }
  372.  
  373. else
  374. {
  375. nadra4.Stop();
  376. nadra4.mute = true;
  377.  
  378. if (_puzzlePieces[3].IsFlipped)
  379. {
  380. _puzzlePieces[3].gameObject.GetComponent<SpriteRenderer>().color = new Color(1.0f, 0.7f, 0.7f);
  381. nadra4.mute = true;
  382. }
  383.  
  384. nadra4.Stop();
  385. nadra4.mute = true;
  386.  
  387. }
  388. if (!_puzzlePieces[4].IsAnswerCorrect())
  389. {
  390. _puzzlePieces[4].gameObject.GetComponent<SpriteRenderer>().color = new Color(1.0f, 0.7f, 0.7f);
  391. //nadra4.Play();
  392. //nadra4.mute = true;
  393. }
  394.  
  395. else
  396. {
  397. nadra4.mute = true;
  398. if (_puzzlePieces[4].IsFlipped)
  399. {
  400. _puzzlePieces[4].gameObject.GetComponent<SpriteRenderer>().color = new Color(1.0f, 0.7f, 0.7f);
  401. nadra4.Stop();
  402. nadra4.mute = true;
  403. }
  404. nadra4.Stop();
  405. nadra4.mute = true;
  406.  
  407. }
  408. if (!_puzzlePieces[8].IsAnswerCorrect())
  409. {
  410. _puzzlePieces[8].gameObject.GetComponent<SpriteRenderer>().color = new Color(1.0f, 0.7f, 0.7f);
  411. //nadra4.Play();
  412. //nadra4.mute = true;
  413. }
  414. else
  415. {
  416. //nadra4.mute = true;
  417. if (_puzzlePieces[8].IsFlipped)
  418. {
  419. _puzzlePieces[8].gameObject.GetComponent<SpriteRenderer>().color = new Color(1.0f, 0.7f, 0.7f);
  420. nadra4.Stop();
  421. nadra4.mute = true;
  422. }
  423. nadra4.Stop();
  424. nadra4.mute = true;
  425.  
  426. }
  427. if (_puzzlePieces[3].IsAnswerCorrect() && _puzzlePieces[4].IsAnswerCorrect() && _puzzlePieces[8].IsAnswerCorrect()
  428. && !_puzzlePieces[3].IsFlipped && !_puzzlePieces[4].IsFlipped && !_puzzlePieces[8].IsFlipped)
  429. {
  430. _playerProgress = GamePhase.FourthStep;
  431. nadra4.Stop();
  432. //nadra4.mute = true;
  433. Swap.SetActive(false);
  434. _puzzlePieces[3].SetIsDraggable(false);
  435. _puzzlePieces[3].gameObject.GetComponent<SpriteRenderer>().color = new Color(1.0f, 1.0f, 1.0f);
  436. _puzzlePieces[4].SetIsDraggable(false);
  437. _puzzlePieces[4].gameObject.GetComponent<SpriteRenderer>().color = new Color(1.0f, 1.0f, 1.0f);
  438. //_puzzlePieces[6].SetIsDraggable(false);
  439. _puzzlePieces[8].SetIsDraggable(false);
  440. _puzzlePieces[8].gameObject.GetComponent<SpriteRenderer>().color = new Color(1.0f, 1.0f, 1.0f);
  441. StartCoroutine(_puzzlePieces[4].playGifRoutine(0));
  442. }
  443.  
  444. break;
  445. case GamePhase.FourthStep:
  446. Swap.SetActive(true);
  447. _puzzlePieces[6].gameObject.GetComponent<SpriteRenderer>().color = new Color(1.0f, 0.7f, 0.7f);
  448. if (!_puzzlePieces[6].IsAnswerCorrect())
  449. {
  450. _puzzlePieces[6].gameObject.GetComponent<SpriteRenderer>().color = new Color(1.0f, 0.7f, 0.7f);
  451.  
  452. }
  453. else
  454. {
  455. if (_puzzlePieces[6].IsFlipped)
  456. {
  457. _puzzlePieces[6].gameObject.GetComponent<SpriteRenderer>().color = new Color(1.0f, 0.7f, 0.7f);
  458. }
  459. }
  460. if (!_puzzlePieces[7].IsAnswerCorrect())
  461. {
  462. _puzzlePieces[7].gameObject.GetComponent<SpriteRenderer>().color = new Color(1.0f, 0.7f, 0.7f);
  463. }
  464. else
  465. {
  466. if (_puzzlePieces[7].IsFlipped)
  467. {
  468. _puzzlePieces[7].gameObject.GetComponent<SpriteRenderer>().color = new Color(1.0f, 0.7f, 0.7f);
  469. }
  470. }
  471. if (!_puzzlePieces[9].IsAnswerCorrect())
  472. {
  473. _puzzlePieces[9].gameObject.GetComponent<SpriteRenderer>().color = new Color(1.0f, 0.7f, 0.7f);
  474. }
  475. else
  476. {
  477. if (_puzzlePieces[9].IsFlipped)
  478. {
  479. _puzzlePieces[9].gameObject.GetComponent<SpriteRenderer>().color = new Color(1.0f, 0.7f, 0.7f);
  480. }
  481. }
  482. if (!_puzzlePieces[10].IsAnswerCorrect())
  483. {
  484. _puzzlePieces[10].gameObject.GetComponent<SpriteRenderer>().color = new Color(1.0f, 0.7f, 0.7f);
  485. }
  486. else
  487. {
  488. if (_puzzlePieces[10].IsFlipped)
  489. {
  490. _puzzlePieces[10].gameObject.GetComponent<SpriteRenderer>().color = new Color(1.0f, 0.7f, 0.7f);
  491. }
  492. }
  493. if (!_puzzlePieces[11].IsAnswerCorrect())
  494. {
  495. _puzzlePieces[11].gameObject.GetComponent<SpriteRenderer>().color = new Color(1.0f, 0.7f, 0.7f);
  496. }
  497. else
  498. {
  499. if (_puzzlePieces[11].IsFlipped)
  500. {
  501. _puzzlePieces[11].gameObject.GetComponent<SpriteRenderer>().color = new Color(1.0f, 0.7f, 0.7f);
  502. }
  503. }
  504. if (_puzzlePieces[7].IsAnswerCorrect() && _puzzlePieces[9].IsAnswerCorrect() && _puzzlePieces[10].IsAnswerCorrect() && _puzzlePieces[11].IsAnswerCorrect() &&_puzzlePieces[6].IsAnswerCorrect()
  505. && !_puzzlePieces[7].IsFlipped && !_puzzlePieces[9].IsFlipped && !_puzzlePieces[10].IsFlipped && !_puzzlePieces[11].IsFlipped && !_puzzlePieces[6].IsFlipped)
  506. {
  507. Swap.SetActive(false);
  508. _puzzlePieces[6].SetIsDraggable(false);
  509. _puzzlePieces[6].gameObject.GetComponent<SpriteRenderer>().color = new Color(1.0f, 1.0f, 1.0f);
  510. _puzzlePieces[7].SetIsDraggable(false);
  511. _puzzlePieces[7].gameObject.GetComponent<SpriteRenderer>().color = new Color(1.0f, 1.0f, 1.0f);
  512. _puzzlePieces[9].SetIsDraggable(false);
  513. _puzzlePieces[9].gameObject.GetComponent<SpriteRenderer>().color = new Color(1.0f, 1.0f, 1.0f);
  514. _puzzlePieces[10].SetIsDraggable(false);
  515. _puzzlePieces[10].gameObject.GetComponent<SpriteRenderer>().color = new Color(1.0f, 1.0f, 1.0f);
  516. _puzzlePieces[11].SetIsDraggable(false);
  517. _puzzlePieces[11].gameObject.GetComponent<SpriteRenderer>().color = new Color(1.0f, 1.0f, 1.0f);
  518.  
  519. Debug.Log("The fourth answer is correct");
  520. _playerProgress = GamePhase.Win;
  521. StartCoroutine(_puzzlePieces[7].playGifRoutine(0));
  522. //Debug.Log("You win");
  523. }
  524. break;
  525. }
  526. }
  527.  
  528. private void HideAllPieces()
  529. {
  530. for (int i = 0; i < _puzzlePieces.Length; i++)
  531. {
  532. _puzzlePieces[i].gameObject.SetActive(false);
  533. //_puzzlePieces[i].gameObject.GetComponent<SpriteRenderer>().enabled = false;
  534. //_puzzlePieces[i].SetIsDraggable(false);
  535. }
  536. }
  537.  
  538. public void GameProgessTracktor()
  539. {
  540. //The Camera Behavior was temporary commented out
  541. //each steps should disable the movement of pieces from last move
  542. switch(_playerProgress)
  543. {
  544. case GamePhase.FirstStep:
  545. //_puzzlePieces[0].gameObject.GetComponent<SpriteRenderer>().enabled = true;
  546. //_puzzlePieces[1].gameObject.GetComponent<SpriteRenderer>().enabled = true;
  547. _puzzlePieces[0].gameObject.SetActive(true);
  548. _puzzlePieces[1].gameObject.SetActive(true);
  549.  
  550. _puzzlePieces[0].transform.position = new Vector3(_puzzleSpot[startedPosition[0]].transform.position.x, _puzzleSpot[startedPosition[0]].transform.position.y, 0);
  551. _puzzlePieces[1].transform.position = new Vector3(_puzzleSpot[startedPosition[1]].transform.position.x, _puzzleSpot[startedPosition[1]].transform.position.y, 0);
  552.  
  553. _puzzlePieces[0].SolvingNumber = _puzzleSpot[startedPosition[0]].SpotNum;
  554. _puzzlePieces[1].SolvingNumber = _puzzleSpot[startedPosition[1]].SpotNum;
  555.  
  556. //The starting point should not be draggable
  557. _puzzlePieces[0].SetIsSwappable(false);
  558. _puzzlePieces[1].SetIsDraggable(true);
  559. //i set piece one to be flipped
  560. _puzzlePieces[1].SetIsFlipped(true);
  561. _mainCamera.ChangeCameraPhase(0);
  562. break;
  563. case GamePhase.SecondStep:
  564. //_puzzlePieces[2].gameObject.GetComponent<SpriteRenderer>().enabled = true;
  565. //_puzzlePieces[5].gameObject.GetComponent<SpriteRenderer>().enabled = true;
  566. _puzzlePieces[2].gameObject.SetActive(true);
  567. _puzzlePieces[2].SetIsFlipped(true);
  568. _puzzlePieces[5].gameObject.SetActive(true);
  569.  
  570. _puzzlePieces[2].transform.position = new Vector3(_puzzleSpot[startedPosition[2]].transform.position.x, _puzzleSpot[startedPosition[2]].transform.position.y, 0);
  571. _puzzlePieces[5].transform.position = new Vector3(_puzzleSpot[startedPosition[5]].transform.position.x, _puzzleSpot[startedPosition[5]].transform.position.y, 0);
  572.  
  573. _puzzlePieces[2].SolvingNumber = _puzzleSpot[startedPosition[2]].SpotNum;
  574. _puzzlePieces[5].SolvingNumber = _puzzleSpot[startedPosition[5]].SpotNum;
  575.  
  576. _puzzlePieces[2].SetIsDraggable(true);
  577. _puzzlePieces[5].SetIsDraggable(true);
  578. _puzzlePieces[1].SetIsSwappable(false);
  579. _mainCamera.ChangeCameraPhase(1);
  580. break;
  581. case GamePhase.ThirdStep:
  582. //_puzzlePieces[3].gameObject.GetComponent<SpriteRenderer>().enabled = true;
  583. //_puzzlePieces[4].gameObject.GetComponent<SpriteRenderer>().enabled = true;
  584. //_puzzlePieces[8].gameObject.GetComponent<SpriteRenderer>().enabled = true;
  585. _puzzlePieces[3].gameObject.SetActive(true);
  586. _puzzlePieces[3].SetIsFlipped(true);
  587. _puzzlePieces[4].gameObject.SetActive(true);
  588. _puzzlePieces[8].gameObject.SetActive(true);
  589.  
  590. _puzzlePieces[3].transform.position = new Vector3(_puzzleSpot[startedPosition[3]].transform.position.x, _puzzleSpot[startedPosition[3]].transform.position.y, 0);
  591. _puzzlePieces[4].transform.position = new Vector3(_puzzleSpot[startedPosition[4]].transform.position.x, _puzzleSpot[startedPosition[4]].transform.position.y, 0);
  592. _puzzlePieces[8].transform.position = new Vector3(_puzzleSpot[startedPosition[8]].transform.position.x, _puzzleSpot[startedPosition[8]].transform.position.y, 0);
  593.  
  594. _puzzlePieces[3].SolvingNumber = _puzzleSpot[startedPosition[3]].SpotNum;
  595. _puzzlePieces[4].SolvingNumber = _puzzleSpot[startedPosition[4]].SpotNum;
  596. _puzzlePieces[8].SolvingNumber = _puzzleSpot[startedPosition[8]].SpotNum;
  597.  
  598. _puzzlePieces[3].SetIsDraggable(true);
  599. _puzzlePieces[4].SetIsDraggable(true);
  600. //_puzzlePieces[6].SetIsDraggable(true);
  601. _puzzlePieces[8].SetIsDraggable(true);
  602.  
  603. _puzzlePieces[2].SetIsSwappable(false);
  604. _puzzlePieces[5].SetIsSwappable(false);
  605.  
  606. _mainCamera.ChangeCameraPhase(2);
  607. break;
  608. case GamePhase.FourthStep:
  609. //_puzzlePieces[7].gameObject.GetComponent<SpriteRenderer>().enabled = true;
  610. //_puzzlePieces[9].gameObject.GetComponent<SpriteRenderer>().enabled = true;
  611. //_puzzlePieces[10].gameObject.GetComponent<SpriteRenderer>().enabled = true;
  612. //_puzzlePieces[11].gameObject.GetComponent<SpriteRenderer>().enabled = true;
  613. _puzzlePieces[6].gameObject.SetActive(true);
  614. _puzzlePieces[7].gameObject.SetActive(true);
  615. // _puzzlePieces[7].SetIsFlipped(true);
  616. _puzzlePieces[9].gameObject.SetActive(true);
  617. _puzzlePieces[10].gameObject.SetActive(true);
  618. _puzzlePieces[11].gameObject.SetActive(true);
  619. //_puzzlePieces[11].SetIsFlipped(true);
  620. _puzzlePieces[6].transform.position = new Vector3(_puzzleSpot[startedPosition[6]].transform.position.x, _puzzleSpot[startedPosition[6]].transform.position.y, 0);
  621. _puzzlePieces[7].transform.position = new Vector3(_puzzleSpot[startedPosition[7]].transform.position.x, _puzzleSpot[startedPosition[7]].transform.position.y, 0);
  622. _puzzlePieces[9].transform.position = new Vector3(_puzzleSpot[startedPosition[9]].transform.position.x, _puzzleSpot[startedPosition[9]].transform.position.y, 0);
  623. _puzzlePieces[10].transform.position = new Vector3(_puzzleSpot[startedPosition[10]].transform.position.x, _puzzleSpot[startedPosition[10]].transform.position.y, 0);
  624. _puzzlePieces[11].transform.position = new Vector3(_puzzleSpot[startedPosition[11]].transform.position.x, _puzzleSpot[startedPosition[11]].transform.position.y, 0);
  625.  
  626. _puzzlePieces[7].SolvingNumber = _puzzleSpot[startedPosition[7]].SpotNum;
  627. _puzzlePieces[9].SolvingNumber = _puzzleSpot[startedPosition[9]].SpotNum;
  628. _puzzlePieces[10].SolvingNumber = _puzzleSpot[startedPosition[10]].SpotNum;
  629. _puzzlePieces[11].SolvingNumber = _puzzleSpot[startedPosition[11]].SpotNum;
  630.  
  631. //_puzzlePieces[6].gameObject.GetComponent<SpriteRenderer>().enabled = true;
  632. _puzzlePieces[6].SetIsDraggable(true);
  633. //The destination should not be draggable
  634. _puzzlePieces[7].SetIsDraggable(true);
  635. _puzzlePieces[10].SetIsDraggable(true);
  636. _puzzlePieces[11].SetIsDraggable(true);
  637.  
  638. _puzzlePieces[3].SetIsSwappable(false);
  639. _puzzlePieces[4].SetIsSwappable(false);
  640. _puzzlePieces[8].SetIsSwappable(false);
  641. _mainCamera.ChangeCameraPhase(3);
  642. break;
  643. default:
  644. break;
  645. }
  646. CheckAnswer();
  647. }
  648.  
  649. public void GifPlayedHandler(int pieceNum, int gifNum = 0)
  650. {
  651. switch(pieceNum)
  652. {
  653. case 0:
  654. StartCoroutine(_puzzlePieces[1].playGifRoutine(0));
  655. break;
  656. case 1:
  657.  
  658. nadra1.mute = true;
  659. nadra2.mute = true;
  660. Doubleclick.SetActive(false);
  661. _videoPlayer.DisplayAndPlay( getVidPath("puzzlematch8.mp4") );
  662.  
  663. break;
  664. case 2:
  665. StartCoroutine(_puzzlePieces[5].playGifRoutine(0));
  666. break;
  667. case 3:
  668. StartCoroutine(_puzzlePieces[4].playGifRoutine(1));
  669. break;
  670. case 4:
  671. if (gifNum == 0)
  672. {
  673. StartCoroutine(_puzzlePieces[3].playGifRoutine(0));
  674. }
  675. else if(gifNum == 1)
  676. {
  677. StartCoroutine(_puzzlePieces[5].playGifRoutine(1));
  678. }
  679. break;
  680. case 5:
  681. if (gifNum == 0)
  682. {
  683. _videoPlayer.DisplayAndPlay(Application.dataPath + "/StreamingAssets/puzzlematch7.mp4");
  684. }
  685. else if(gifNum == 1)
  686. {
  687. _videoPlayer.DisplayAndPlay(Application.dataPath + "/StreamingAssets/puzzlematch6.mp4");
  688. //StartCoroutine(_puzzlePieces[7].playGifRoutine(0));
  689. }
  690. break;
  691. case 6:
  692. break;
  693. case 7:
  694. if (gifNum == 0)
  695. {
  696. StartCoroutine(_puzzlePieces[10].playGifRoutine(0));
  697. }
  698. /*else if(gifNum == 1)
  699. {
  700. StartCoroutine(_puzzlePieces[10].playGifRoutine(0));
  701. }*/
  702. break;
  703. case 8:
  704. break;
  705. case 9:
  706. _videoPlayer.DisplayAndPlay(Application.dataPath + "/StreamingAssets/puzzlematch5.mp4");
  707. Debug.Log("You win!!");
  708. _winText.text = "Win";
  709. _winText.gameObject.SetActive(true);
  710. _resetButton.gameObject.SetActive(true);
  711. winButton.gameObject.SetActive(true);
  712. DisablePieceMovement();
  713. break;
  714. case 10:
  715. StartCoroutine(_puzzlePieces[9].playGifRoutine(0));
  716. break;
  717. case 11:
  718. break;
  719. default:
  720. Debug.Log("Someone pass a invalid values to the function");
  721. break;
  722. }
  723. }
  724.  
  725. public string getVidPath(string s)
  726. {
  727. #if UNITY_EDITOR
  728. return Application.dataPath + "/StreamingAssets/" + s;
  729. #elif UNITY_IOS
  730. return Application.dataPath + "/Raw/" + s;
  731. #else
  732. return string.Empty;
  733. #endif
  734. }
  735.  
  736. public bool CanSwap()
  737. {
  738. if(TouchedTarget == -1 || !_puzzlePieces[TouchedTarget].IsSwappable)
  739. {
  740. return false;
  741. }
  742. return true;
  743. }
  744.  
  745. public void PutPiecesInOrder()
  746. {
  747. Vector2 calculatedResult;
  748. _pieceWidth = _puzzlePieces[0].GetComponent<SpriteRenderer>().bounds.size.x;
  749. _pieceHeight = _puzzlePieces[0].GetComponent<SpriteRenderer>().bounds.size.y;
  750. for (int i = 0; i < startedPosition.Length; i++)
  751. {
  752. calculatedResult = CalculatePuzzlePos(startedPosition[i]);
  753. _puzzlePieces[i].transform.position = calculatedResult;
  754. }
  755. }
  756.  
  757. private Vector2 CalculatePuzzlePos(int pieceNumber)
  758. {
  759. Vector2 botLeftPoint = GetBotLeftPoint();
  760. Debug.Log("botLeftPoint: " + botLeftPoint);
  761. int i = pieceNumber % 3;
  762. int j = pieceNumber / 3;
  763. //Vector2 newPosition = new Vector2(botLeftPoint.x + j * _pieceWidth, botLeftPoint.y + i * _pieceHeight);
  764. Vector2 newPosition = new Vector2(botLeftPoint.x + j * (_pieceWidth + _pieceGap), botLeftPoint.y + i * (_pieceHeight + _pieceGap));
  765.  
  766. return newPosition;
  767. }
  768.  
  769. public void DeductOneMove()
  770. {
  771. TotalMove--;
  772. _moveText.text = "Remaining: " + TotalMove;
  773. if (TotalMove <= 0 && _playerProgress != GamePhase.Win)
  774. {
  775. _playerProgress = GamePhase.Lose;
  776. _winText.gameObject.SetActive(true);
  777. _winText.text = "Lose";
  778. _resetButton.gameObject.SetActive(true);
  779. DisablePieceMovement();
  780. SceneManager.LoadScene("PuzzleYouLose", LoadSceneMode.Additive);
  781. }
  782. }
  783.  
  784. public void Restart()
  785. {
  786. SceneManager.LoadSceneAsync(SceneManager.GetActiveScene().buildIndex);
  787. }
  788.  
  789. private void DisablePieceMovement()
  790. {
  791. for (int i = 0; i < _puzzlePieces.Length; i++)
  792. {
  793. _puzzlePieces[i].SetIsDraggable(false);
  794. }
  795. }
  796.  
  797. public void winCase(){
  798. // If we have completed the other game, transition. If not, go back.
  799. if (PlayerPrefs.GetInt("gameLevel") == 0)
  800. {
  801. PlayerPrefs.SetInt("gameLevel", 1);
  802. SceneManager.LoadScene(otherAct);
  803. }
  804. else if (PlayerPrefs.GetInt("gameLevel") == 1)
  805. {
  806. SceneManager.LoadScene(FinalScene);
  807. }
  808. }
  809.  
  810. }
  811. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement