Asteroid.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="Asteroid.cs" company="Exit Games GmbH">
  3. // Part of: Asteroid Demo
  4. // </copyright>
  5. // <summary>
  6. // Asteroid Component
  7. // </summary>
  8. // <author>developer@exitgames.com</author>
  9. // --------------------------------------------------------------------------------------------------------------------
  10. using UnityEngine;
  11. using Random = UnityEngine.Random;
  12. using Photon.Pun.UtilityScripts;
  13. namespace Photon.Pun.Demo.Asteroids
  14. {
  15. public class Asteroid : MonoBehaviour
  16. {
  17. public bool isLargeAsteroid;
  18. private bool isDestroyed;
  19. private PhotonView photonView;
  20. #pragma warning disable 0109
  21. private new Rigidbody rigidbody;
  22. #pragma warning restore 0109
  23. #region UNITY
  24. public void Awake()
  25. {
  26. photonView = GetComponent<PhotonView>();
  27. rigidbody = GetComponent<Rigidbody>();
  28. if (photonView.InstantiationData != null)
  29. {
  30. rigidbody.AddForce((Vector3) photonView.InstantiationData[0]);
  31. rigidbody.AddTorque((Vector3) photonView.InstantiationData[1]);
  32. isLargeAsteroid = (bool) photonView.InstantiationData[2];
  33. }
  34. }
  35. public void Update()
  36. {
  37. if (!photonView.IsMine)
  38. {
  39. return;
  40. }
  41. if (Mathf.Abs(transform.position.x) > Camera.main.orthographicSize * Camera.main.aspect || Mathf.Abs(transform.position.z) > Camera.main.orthographicSize)
  42. {
  43. // Out of the screen
  44. PhotonNetwork.Destroy(gameObject);
  45. }
  46. }
  47. public void OnCollisionEnter(Collision collision)
  48. {
  49. if (isDestroyed)
  50. {
  51. return;
  52. }
  53. if (collision.gameObject.CompareTag("Bullet"))
  54. {
  55. if (photonView.IsMine)
  56. {
  57. Bullet bullet = collision.gameObject.GetComponent<Bullet>();
  58. bullet.Owner.AddScore(isLargeAsteroid ? 2 : 1);
  59. DestroyAsteroidGlobally();
  60. }
  61. else
  62. {
  63. DestroyAsteroidLocally();
  64. }
  65. }
  66. else if (collision.gameObject.CompareTag("Player"))
  67. {
  68. if (photonView.IsMine)
  69. {
  70. collision.gameObject.GetComponent<PhotonView>().RPC("DestroySpaceship", RpcTarget.All);
  71. DestroyAsteroidGlobally();
  72. }
  73. }
  74. }
  75. #endregion
  76. private void DestroyAsteroidGlobally()
  77. {
  78. isDestroyed = true;
  79. if (isLargeAsteroid)
  80. {
  81. int numberToSpawn = Random.Range(3, 6);
  82. for (int counter = 0; counter < numberToSpawn; ++counter)
  83. {
  84. Vector3 force = Quaternion.Euler(0, counter * 360.0f / numberToSpawn, 0) * Vector3.forward * Random.Range(0.5f, 1.5f) * 300.0f;
  85. Vector3 torque = Random.insideUnitSphere * Random.Range(500.0f, 1500.0f);
  86. object[] instantiationData = {force, torque, false, PhotonNetwork.Time};
  87. PhotonNetwork.InstantiateRoomObject("SmallAsteroid", transform.position + force.normalized * 10.0f, Quaternion.Euler(0, Random.value * 180.0f, 0), 0, instantiationData);
  88. }
  89. }
  90. PhotonNetwork.Destroy(gameObject);
  91. }
  92. private void DestroyAsteroidLocally()
  93. {
  94. isDestroyed = true;
  95. GetComponent<Renderer>().enabled = false;
  96. }
  97. }
  98. }