/Assets/Scripts/ProjectileLauncher.cs

https://bitbucket.org/jiseongkim/3did_shared · C# · 116 lines · 72 code · 17 blank · 27 comment · 7 complexity · 0b5f160ed67a055e696efe83c4e7ba18 MD5 · raw file

  1. using UnityEngine;
  2. using Academy.HoloToolkit.Sharing;
  3. public class ProjectileLauncher : MonoBehaviour
  4. {
  5. /// <summary>
  6. /// Keep Track of the last shot time to throttle users' shots
  7. /// </summary>
  8. float LastShotTime = 0;
  9. void Start()
  10. {
  11. CustomMessages.Instance.MessageHandlers[CustomMessages.TestMessageID.ShootProjectile] = this.ProcessRemoteProjectile;
  12. // We will use the camera's audio source to play sounds whenever a projectile hits the user's avatar
  13. // or when the user hits another player's avatar with a projectile.
  14. if (Camera.main.gameObject.GetComponent<AudioSource>() == null)
  15. {
  16. // Add an AudioSource and spatialize it.
  17. AudioSource audio = Camera.main.gameObject.AddComponent<AudioSource>() as AudioSource;
  18. audio.playOnAwake = false;
  19. audio.spatialize = true;
  20. audio.spatialBlend = 1.0f;
  21. audio.rolloffMode = AudioRolloffMode.Custom;
  22. }
  23. }
  24. /// <summary>
  25. /// OnSelect is sent by gesture manager.
  26. /// </summary>
  27. void OnSelect()
  28. {
  29. if (Time.realtimeSinceStartup - LastShotTime > 1)
  30. {
  31. LastShotTime = Time.realtimeSinceStartup;
  32. SpawnProjectile(0);
  33. }
  34. }
  35. /// <summary>
  36. /// Spawns a new projectile in the world if the user
  37. /// doesn't already have one.
  38. /// </summary>
  39. void SpawnProjectile(long UserId)
  40. {
  41. Vector3 ProjectilePosition = Camera.main.transform.position + Camera.main.transform.forward * 0.85f;
  42. Vector3 ProjectileDirection = Camera.main.transform.forward;
  43. ShootProjectile(ProjectilePosition,
  44. ProjectileDirection, UserId);
  45. Transform anchor = ImportExportAnchorManager.Instance.gameObject.transform;
  46. CustomMessages.Instance.SendShootProjectile(anchor.InverseTransformPoint(ProjectilePosition), anchor.InverseTransformDirection(ProjectileDirection));
  47. }
  48. /// <summary>
  49. /// Adds a new projectile to the world.
  50. /// </summary>
  51. /// <param name="start">Position to shoot from</param>
  52. /// <param name="direction">Position to shoot toward</param>
  53. /// <param name="radius">Size of destruction when colliding.</param>
  54. void ShootProjectile(Vector3 start, Vector3 direction, long OwningUser)
  55. {
  56. // Need to know the index in the PlayerAvatarStore to grab for this projectile's behavior.
  57. int AvatarIndex = 0;
  58. // Special case ID 0 to mean the local user.
  59. if (OwningUser == 0)
  60. {
  61. AvatarIndex = LocalPlayerManager.Instance.AvatarIndex;
  62. }
  63. else
  64. {
  65. RemotePlayerManager.RemoteHeadInfo headInfo = RemotePlayerManager.Instance.GetRemoteHeadInfo(OwningUser);
  66. AvatarIndex = headInfo.PlayerAvatarIndex;
  67. }
  68. PlayerAvatarParameters ownerAvatarParameters = PlayerAvatarStore.Instance.PlayerAvatars[AvatarIndex].GetComponent<PlayerAvatarParameters>();
  69. GameObject spawnedProjectile = (GameObject)Instantiate(ownerAvatarParameters.PlayerShotObject);
  70. spawnedProjectile.transform.position = start;
  71. // Set projectile color to be the same as the avatar color.
  72. FriendlyDrone drone = PlayerAvatarStore.Instance.PlayerAvatars[AvatarIndex].GetComponentInChildren<FriendlyDrone>();
  73. if (drone != null)
  74. {
  75. spawnedProjectile.GetComponentInChildren<Renderer>().materials[1].SetColor("_EmissionColor", drone.EmissiveColor);
  76. foreach(ParticleSystem particleSystem in spawnedProjectile.transform.GetComponentsInChildren<ParticleSystem>())
  77. {
  78. ParticleSystem.MainModule main = particleSystem.main;
  79. main.startColor = drone.EmissiveColor;
  80. }
  81. }
  82. ProjectileBehavior pc = spawnedProjectile.GetComponentInChildren<ProjectileBehavior>();
  83. pc.startDir = direction;
  84. pc.OwningUserId = OwningUser;
  85. }
  86. /// <summary>
  87. /// Process user hit.
  88. /// </summary>
  89. /// <param name="msg"></param>
  90. void ProcessRemoteProjectile(NetworkInMessage msg)
  91. {
  92. // Parse the message
  93. long userID = msg.ReadInt64();
  94. Vector3 remoteProjectilePosition = CustomMessages.Instance.ReadVector3(msg);
  95. Vector3 remoteProjectileDirection = CustomMessages.Instance.ReadVector3(msg);
  96. Transform anchor = ImportExportAnchorManager.Instance.gameObject.transform;
  97. ShootProjectile(anchor.TransformPoint(remoteProjectilePosition), anchor.TransformDirection(remoteProjectileDirection), userID);
  98. }
  99. }