Spaceship.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="Spaceship.cs" company="Exit Games GmbH">
  3. // Part of: Asteroid Demo,
  4. // </copyright>
  5. // <summary>
  6. // Spaceship
  7. // </summary>
  8. // <author>developer@exitgames.com</author>
  9. // --------------------------------------------------------------------------------------------------------------------
  10. using System.Collections;
  11. using UnityEngine;
  12. using Photon.Pun.UtilityScripts;
  13. using Hashtable = ExitGames.Client.Photon.Hashtable;
  14. namespace Photon.Pun.Demo.Asteroids
  15. {
  16. public class Spaceship : MonoBehaviour
  17. {
  18. public float RotationSpeed = 90.0f;
  19. public float MovementSpeed = 2.0f;
  20. public float MaxSpeed = 0.2f;
  21. public ParticleSystem Destruction;
  22. public GameObject EngineTrail;
  23. public GameObject BulletPrefab;
  24. private PhotonView photonView;
  25. #pragma warning disable 0109
  26. private new Rigidbody rigidbody;
  27. private new Collider collider;
  28. private new Renderer renderer;
  29. #pragma warning restore 0109
  30. private float rotation = 0.0f;
  31. private float acceleration = 0.0f;
  32. private float shootingTimer = 0.0f;
  33. private bool controllable = true;
  34. #region UNITY
  35. public void Awake()
  36. {
  37. photonView = GetComponent<PhotonView>();
  38. rigidbody = GetComponent<Rigidbody>();
  39. collider = GetComponent<Collider>();
  40. renderer = GetComponent<Renderer>();
  41. }
  42. public void Start()
  43. {
  44. foreach (Renderer r in GetComponentsInChildren<Renderer>())
  45. {
  46. r.material.color = AsteroidsGame.GetColor(photonView.Owner.GetPlayerNumber());
  47. }
  48. }
  49. public void Update()
  50. {
  51. if (!photonView.AmOwner || !controllable)
  52. {
  53. return;
  54. }
  55. // we don't want the master client to apply input to remote ships while the remote player is inactive
  56. if (this.photonView.CreatorActorNr != PhotonNetwork.LocalPlayer.ActorNumber)
  57. {
  58. return;
  59. }
  60. rotation = Input.GetAxis("Horizontal");
  61. acceleration = Input.GetAxis("Vertical");
  62. if (Input.GetButton("Jump") && shootingTimer <= 0.0)
  63. {
  64. shootingTimer = 0.2f;
  65. photonView.RPC("Fire", RpcTarget.AllViaServer, rigidbody.position, rigidbody.rotation);
  66. }
  67. if (shootingTimer > 0.0f)
  68. {
  69. shootingTimer -= Time.deltaTime;
  70. }
  71. }
  72. public void FixedUpdate()
  73. {
  74. if (!photonView.IsMine)
  75. {
  76. return;
  77. }
  78. if (!controllable)
  79. {
  80. return;
  81. }
  82. Quaternion rot = rigidbody.rotation * Quaternion.Euler(0, rotation * RotationSpeed * Time.fixedDeltaTime, 0);
  83. rigidbody.MoveRotation(rot);
  84. Vector3 force = (rot * Vector3.forward) * acceleration * 1000.0f * MovementSpeed * Time.fixedDeltaTime;
  85. rigidbody.AddForce(force);
  86. if (rigidbody.velocity.magnitude > (MaxSpeed * 1000.0f))
  87. {
  88. rigidbody.velocity = rigidbody.velocity.normalized * MaxSpeed * 1000.0f;
  89. }
  90. CheckExitScreen();
  91. }
  92. #endregion
  93. #region COROUTINES
  94. private IEnumerator WaitForRespawn()
  95. {
  96. yield return new WaitForSeconds(AsteroidsGame.PLAYER_RESPAWN_TIME);
  97. photonView.RPC("RespawnSpaceship", RpcTarget.AllViaServer);
  98. }
  99. #endregion
  100. #region PUN CALLBACKS
  101. [PunRPC]
  102. public void DestroySpaceship()
  103. {
  104. rigidbody.velocity = Vector3.zero;
  105. rigidbody.angularVelocity = Vector3.zero;
  106. collider.enabled = false;
  107. renderer.enabled = false;
  108. controllable = false;
  109. EngineTrail.SetActive(false);
  110. Destruction.Play();
  111. if (photonView.IsMine)
  112. {
  113. object lives;
  114. if (PhotonNetwork.LocalPlayer.CustomProperties.TryGetValue(AsteroidsGame.PLAYER_LIVES, out lives))
  115. {
  116. PhotonNetwork.LocalPlayer.SetCustomProperties(new Hashtable {{AsteroidsGame.PLAYER_LIVES, ((int) lives <= 1) ? 0 : ((int) lives - 1)}});
  117. if (((int) lives) > 1)
  118. {
  119. StartCoroutine("WaitForRespawn");
  120. }
  121. }
  122. }
  123. }
  124. [PunRPC]
  125. public void Fire(Vector3 position, Quaternion rotation, PhotonMessageInfo info)
  126. {
  127. float lag = (float) (PhotonNetwork.Time - info.SentServerTime);
  128. GameObject bullet;
  129. /** Use this if you want to fire one bullet at a time **/
  130. bullet = Instantiate(BulletPrefab, position, Quaternion.identity) as GameObject;
  131. bullet.GetComponent<Bullet>().InitializeBullet(photonView.Owner, (rotation * Vector3.forward), Mathf.Abs(lag));
  132. /** Use this if you want to fire two bullets at once **/
  133. //Vector3 baseX = rotation * Vector3.right;
  134. //Vector3 baseZ = rotation * Vector3.forward;
  135. //Vector3 offsetLeft = -1.5f * baseX - 0.5f * baseZ;
  136. //Vector3 offsetRight = 1.5f * baseX - 0.5f * baseZ;
  137. //bullet = Instantiate(BulletPrefab, rigidbody.position + offsetLeft, Quaternion.identity) as GameObject;
  138. //bullet.GetComponent<Bullet>().InitializeBullet(photonView.Owner, baseZ, Mathf.Abs(lag));
  139. //bullet = Instantiate(BulletPrefab, rigidbody.position + offsetRight, Quaternion.identity) as GameObject;
  140. //bullet.GetComponent<Bullet>().InitializeBullet(photonView.Owner, baseZ, Mathf.Abs(lag));
  141. }
  142. [PunRPC]
  143. public void RespawnSpaceship()
  144. {
  145. collider.enabled = true;
  146. renderer.enabled = true;
  147. controllable = true;
  148. EngineTrail.SetActive(true);
  149. Destruction.Stop();
  150. }
  151. #endregion
  152. private void CheckExitScreen()
  153. {
  154. if (Camera.main == null)
  155. {
  156. return;
  157. }
  158. if (Mathf.Abs(rigidbody.position.x) > (Camera.main.orthographicSize * Camera.main.aspect))
  159. {
  160. rigidbody.position = new Vector3(-Mathf.Sign(rigidbody.position.x) * Camera.main.orthographicSize * Camera.main.aspect, 0, rigidbody.position.z);
  161. rigidbody.position -= rigidbody.position.normalized * 0.1f; // offset a little bit to avoid looping back & forth between the 2 edges
  162. }
  163. if (Mathf.Abs(rigidbody.position.z) > Camera.main.orthographicSize)
  164. {
  165. rigidbody.position = new Vector3(rigidbody.position.x, rigidbody.position.y, -Mathf.Sign(rigidbody.position.z) * Camera.main.orthographicSize);
  166. rigidbody.position -= rigidbody.position.normalized * 0.1f; // offset a little bit to avoid looping back & forth between the 2 edges
  167. }
  168. }
  169. }
  170. }