1
0

FireSource.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: This object can be set on fire
  4. //
  5. //=============================================================================
  6. using UnityEngine;
  7. using System.Collections;
  8. namespace Valve.VR.InteractionSystem
  9. {
  10. //-------------------------------------------------------------------------
  11. public class FireSource : MonoBehaviour
  12. {
  13. public GameObject fireParticlePrefab;
  14. public bool startActive;
  15. private GameObject fireObject;
  16. public ParticleSystem customParticles;
  17. public bool isBurning;
  18. public float burnTime;
  19. public float ignitionDelay = 0;
  20. private float ignitionTime;
  21. private Hand hand;
  22. public AudioSource ignitionSound;
  23. public bool canSpreadFromThisSource = true;
  24. //-------------------------------------------------
  25. void Start()
  26. {
  27. if ( startActive )
  28. {
  29. StartBurning();
  30. }
  31. }
  32. //-------------------------------------------------
  33. void Update()
  34. {
  35. if ( ( burnTime != 0 ) && ( Time.time > ( ignitionTime + burnTime ) ) && isBurning )
  36. {
  37. isBurning = false;
  38. if ( customParticles != null )
  39. {
  40. customParticles.Stop();
  41. }
  42. else
  43. {
  44. Destroy( fireObject );
  45. }
  46. }
  47. }
  48. //-------------------------------------------------
  49. void OnTriggerEnter( Collider other )
  50. {
  51. if ( isBurning && canSpreadFromThisSource )
  52. {
  53. other.SendMessageUpwards( "FireExposure", SendMessageOptions.DontRequireReceiver );
  54. }
  55. }
  56. //-------------------------------------------------
  57. private void FireExposure()
  58. {
  59. if ( fireObject == null )
  60. {
  61. Invoke( "StartBurning", ignitionDelay );
  62. }
  63. if ( hand = GetComponentInParent<Hand>() )
  64. {
  65. hand.TriggerHapticPulse( 1000 );
  66. }
  67. }
  68. //-------------------------------------------------
  69. private void StartBurning()
  70. {
  71. isBurning = true;
  72. ignitionTime = Time.time;
  73. // Play the fire ignition sound if there is one
  74. if ( ignitionSound != null )
  75. {
  76. ignitionSound.Play();
  77. }
  78. if ( customParticles != null )
  79. {
  80. customParticles.Play();
  81. }
  82. else
  83. {
  84. if ( fireParticlePrefab != null )
  85. {
  86. fireObject = Instantiate( fireParticlePrefab, transform.position, transform.rotation ) as GameObject;
  87. fireObject.transform.parent = transform;
  88. }
  89. }
  90. }
  91. }
  92. }