Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. // Attached to Main Camera
  6. public class CameraController : MonoBehaviour {
  7. // Set manually in inspector
  8. public GameObject[] players;
  9. public float movementSpeed = 1.0f;
  10. public float rotationSpeed = 1.0f;
  11.  
  12. StateManager theStateManager;
  13. bool isAnimating = false;
  14.  
  15. private int currentPlayer;
  16. private float startTime;
  17. private float distanceToPlayer;
  18. private Vector3 startPosition;
  19. private Quaternion startOrientation;
  20.  
  21. //Use this for initialization
  22. void start()
  23. {
  24. theStateManager = GameObject.FindObjectOfType<StateManager> ();
  25. currentPlayer = 0;
  26. ResetCamera ();
  27. }
  28.  
  29. //Update is called once per frame
  30. void Update()
  31. {
  32. float distanceCovered;
  33. float rotationCovered;
  34. float fractionTraveled;
  35.  
  36. if (isAnimating == false)
  37. {
  38. return;
  39. }
  40.  
  41. // switch to previous
  42. if (isAnimating == true)
  43. {
  44. if (currentPlayer == 0)
  45. currentPlayer = players.Length - 1;
  46. else
  47. currentPlayer++;
  48. ResetCamera ();
  49. }
  50.  
  51. // Keep moving camera
  52. if (transform.position != players [currentPlayer].transform.position) {
  53. distanceCovered = (Time.time - startTime) * movementSpeed;
  54. fractionTraveled = distanceCovered / distanceToPlayer;
  55. rotationCovered = (Time.time - startTime) * rotationSpeed;
  56. // Lerp to players position
  57. transform.position = Vector3.Lerp (
  58. startPosition, players [currentPlayer].transform.position, rotationCovered);
  59. // Stop moving camera
  60. }
  61. else
  62. {
  63. // Match orientation
  64. if (transform.rotation != players[currentPlayer].transform.rotation)
  65. transform.rotation = players[currentPlayer].transform.rotation;
  66.  
  67. // Set parent transform to current player
  68. transform.parent = players[currentPlayer].transform;
  69. }
  70.  
  71. if (theStateManager.IsDoneAnimating == true)
  72. {
  73. isAnimating = false;
  74. return;
  75. }
  76.  
  77. if (theStateManager.IsDoneAnimating == false)
  78. {
  79. isAnimating = true;
  80. }
  81.  
  82. isAnimating = false;
  83. }
  84. void ResetCamera()
  85. {
  86. transform.parent = null;
  87. startTime = Time.time;
  88. startPosition = transform.position;
  89. startOrientation = transform.rotation;
  90. distanceToPlayer = Vector3.Distance (transform.position, players[currentPlayer].transform.position);
  91. }
  92. }
  93.  
  94. using System.Collections;
  95. using System.Collections.Generic;
  96. using UnityEngine;
  97.  
  98. public class StateManager : MonoBehaviour {
  99.  
  100. // Use this for initialization
  101. void Start () {
  102.  
  103. }
  104.  
  105. public int NumberofPlayers = 4;
  106. public int CurrentPlayerId = 0;
  107.  
  108. public int Dicetotal;
  109.  
  110. public bool IsDoneRolling = false;
  111. public bool IsDoneClicking = false;
  112. public bool IsDoneAnimating = false;
  113.  
  114. public GameObject NoLegalMovesPopup;
  115.  
  116. public void Newturn () {
  117. // This is the start of a player's turn.
  118. // We don have roll for them yet.
  119. IsDoneRolling = false;
  120. IsDoneClicking = false;
  121. IsDoneAnimating = false;
  122.  
  123. CurrentPlayerId = (CurrentPlayerId + 1) % NumberofPlayers;
  124. }
  125.  
  126.  
  127. // Update is called once per frame
  128. void Update () {
  129.  
  130. // Is the turn done?
  131. if (IsDoneRolling && IsDoneClicking && IsDoneAnimating)
  132. {
  133. Debug.Log ("Turn is done!");
  134. Newturn ();
  135. }
  136.  
  137. }
  138.  
  139. public void CheckLegalMoves()
  140. {
  141. // If we rolled a zero, then we clearly have no legal moves.
  142. if (Dicetotal == 0)
  143. {
  144. StartCoroutine ( NoLegalMoveCoroutine() );
  145. return;
  146. }
  147.  
  148. // Loop through all of a player's stones.
  149. // Now I know everyone only has one stone, but just to be sure that
  150. // I don miss a part of the code. Besides, being extra secure never hurt anyone
  151. PlayerStone[] pss = GameObject.FindObjectsOfType<PlayerStone> ();
  152. bool hasLegalMove = false;
  153. foreach (PlayerStone ps in pss)
  154. {
  155. if (ps.PlayerId == CurrentPlayerId)
  156. {
  157.  
  158. if (ps.CanLegallyMoveAhead (Dicetotal))
  159. {
  160. // TODO: Highlight tiles that are legal move spaces
  161. hasLegalMove = true;
  162. }
  163. }
  164. }
  165.  
  166. // If no legal moves are possible, wait a sec then move to next player (and give a message)
  167. if(hasLegalMove == false)
  168. {
  169. StartCoroutine ( NoLegalMoveCoroutine() );
  170. return;
  171. }
  172. }
  173.  
  174. IEnumerator NoLegalMoveCoroutine()
  175. {
  176. // Display message
  177. NoLegalMovesPopup.SetActive (true);
  178. // Wait 1 second
  179. yield return new WaitForSeconds(1f);
  180.  
  181. NoLegalMovesPopup.SetActive (false);
  182.  
  183. Newturn ();
  184. }
  185.  
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement