Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.41 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PlayerControl : Photon.MonoBehaviour
  5. {
  6. private static int playerCount = 0;
  7. public static int PlayerCount
  8. {
  9. get
  10. {
  11. return playerCount;
  12. }
  13. }
  14.  
  15. public float rotationSpeed;
  16.  
  17. private Vector3 lastVelocity;
  18.  
  19. private Vector3 currentPosition;
  20. private Quaternion currentRotation;
  21.  
  22. public OnJoinedInstantiate BubbleSpawner;
  23.  
  24. public enum HorizontalRestriction { None, LeftOnly, RightOnly };
  25. public enum VerticalRestriction { None, UpOnly, DownOnly };
  26.  
  27. public HorizontalRestriction HorizontalRestr;
  28. public VerticalRestriction VerticalRestr;
  29. private CharacterController charc;
  30.  
  31. public GameObject popParticle;
  32. private PhotonView myPhotonView;
  33.  
  34. public int sprintCapacity;
  35. private int maxSprintCapacity;
  36. private CameraFollowMe camFollow;
  37.  
  38. public float speed;
  39. public float sprintSpeedAdd = 0;
  40. private float addSpeed;
  41. private float baseSpeed;
  42. private bool sprintRefiled;
  43.  
  44. void Start()
  45. {
  46. playerCount++;
  47. if (!photonView.isMine)
  48. {
  49. GetComponent<Rigidbody>().isKinematic = true;
  50. }
  51.  
  52. charc = GetComponent<CharacterController>();
  53.  
  54. if (BubbleSpawner == null)
  55. BubbleSpawner = GameObject.Find("Scripts").GetComponent<OnJoinedInstantiate>();
  56. BubbleSpawner.players.Add(gameObject.GetComponent<PhotonView>());
  57.  
  58. camFollow = Camera.main.GetComponent<CameraFollowMe>();
  59.  
  60. baseSpeed = speed;
  61. maxSprintCapacity = sprintCapacity;
  62. sprintCapacity = 0;
  63.  
  64. myPhotonView = gameObject.GetComponent<PhotonView>();
  65.  
  66. }
  67.  
  68. // Update is called once per frame
  69. void Update()
  70. {
  71. if (!photonView.isMine)
  72. {
  73. transform.position = Vector3.Slerp(transform.position, currentPosition, Time.deltaTime * 2);
  74. transform.rotation = Quaternion.Slerp(transform.rotation, currentRotation, Time.deltaTime * 2);
  75. }
  76. else
  77. {
  78. getInput();
  79. }
  80.  
  81.  
  82. }
  83.  
  84. public void AddKill()
  85. {
  86. int kills = 0;
  87. if (myPhotonView.owner.customProperties.ContainsKey("kills"))
  88. kills = (int)myPhotonView.owner.customProperties["kills"];
  89. kills++;
  90. myPhotonView.owner.SetCustomProperties(new ExitGames.Client.Photon.Hashtable() { { "kills", kills } });
  91. }
  92.  
  93. public int GetKills()
  94. {
  95. int kills = 0;
  96. if (myPhotonView.owner.customProperties.ContainsKey("kills"))
  97. kills = (int)myPhotonView.owner.customProperties["kills"];
  98. return kills;
  99.  
  100. }
  101.  
  102. [RPC]
  103. void die()
  104. {
  105.  
  106.  
  107. PhotonNetwork.Destroy(gameObject);
  108. if (BubbleSpawner == null)
  109. BubbleSpawner = GameObject.Find("Scripts").GetComponent<OnJoinedInstantiate>();
  110.  
  111.  
  112.  
  113. }
  114.  
  115. void OnDestroy()
  116. {
  117. CreatePopParticle();
  118. /*
  119. if (BubbleSpawner == null)
  120. BubbleSpawner = GameObject.Find("Scripts").GetComponent<OnJoinedInstantiate>();
  121. */
  122. //BubbleSpawner.SpawnWithDelay(1.5f);
  123.  
  124. }
  125.  
  126. void getInput()
  127. {
  128. Vector3 moveDirection = transform.forward;
  129.  
  130. float yawMouse = Input.GetAxis("Mouse X");
  131. float pitchMouse = Input.GetAxis("Mouse Y");
  132. Vector3 targetFlyRotation = Vector3.zero;
  133.  
  134. Screen.lockCursor = true;
  135.  
  136. if (Mathf.Abs(yawMouse) > 0.1f || Mathf.Abs(pitchMouse) > 0.1f)
  137. {
  138. targetFlyRotation = yawMouse * transform.right + pitchMouse * transform.up;
  139. targetFlyRotation.Normalize();
  140. targetFlyRotation *= Time.deltaTime * 3.0f;
  141.  
  142. //limit x rotation if looking too much up or down
  143. //Log out the limitX value for this to make sense
  144. float limitX = Quaternion.LookRotation(moveDirection + targetFlyRotation).eulerAngles.x;
  145.  
  146. //70 sets the rotation limit in the down direction
  147. //290 sets limit for up direction
  148. if (limitX < 90 && limitX > 70 || limitX > 270 && limitX < 290)
  149. {
  150. }
  151. else
  152. {
  153. moveDirection += targetFlyRotation;
  154. //does the actual rotation on the object if no limits are breached
  155. transform.rotation = Quaternion.LookRotation(moveDirection);
  156. }
  157.  
  158. }
  159. float Vertical = Input.GetAxis("Vertical");
  160. float Horizontal = Input.GetAxis("Horizontal");
  161. Vector3 moveDir = transform.forward * Vertical + transform.right * Horizontal;
  162.  
  163. if (Input.GetKey(KeyCode.Space) && sprintCapacity > 0 && sprintRefiled)
  164. {
  165. addSpeed = sprintSpeedAdd;
  166.  
  167. camFollow.preferedDistance = 3f;
  168.  
  169.  
  170. sprintCapacity--;
  171. }
  172. else
  173. {
  174. sprintRefiled = false;
  175.  
  176. camFollow.preferedDistance = 2f;
  177. addSpeed = 0f;
  178.  
  179. if (sprintCapacity < maxSprintCapacity)
  180. sprintCapacity++;
  181. else
  182. sprintCapacity = maxSprintCapacity;
  183.  
  184. if (sprintCapacity == maxSprintCapacity)
  185. sprintRefiled = true;
  186. }
  187.  
  188. speed = Mathf.Lerp(speed, baseSpeed + addSpeed, Time.deltaTime);
  189.  
  190. charc.Move(moveDir * Time.deltaTime * speed);
  191. }
  192.  
  193. void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
  194. {
  195. if (stream.isWriting)
  196. {
  197. stream.SendNext(transform.position);
  198. stream.SendNext(transform.rotation);
  199. }
  200. else
  201. {
  202. currentPosition = (Vector3)stream.ReceiveNext();
  203. currentRotation = (Quaternion)stream.ReceiveNext();
  204. }
  205. }
  206.  
  207. void OnTriggerEnter(Collider coll)
  208. {
  209. if (coll.gameObject.tag == "Spike" && coll.gameObject.transform.parent != gameObject)
  210. {
  211. PlayerControl pc = coll.gameObject.transform.parent.gameObject.GetComponent<PlayerControl>();
  212. if (pc != null)
  213. {
  214. pc.AddKill();
  215. }
  216.  
  217. myPhotonView.RPC("die", PhotonTargets.All);
  218.  
  219.  
  220. }
  221. }
  222.  
  223. [RPC]
  224. public void CreatePopParticle()
  225. {
  226. GameObject go = Instantiate(popParticle, transform.position, Quaternion.identity) as GameObject;
  227. Destroy(go, 1.5f);
  228. }
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement