You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
4.4 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using BansheeGz.BGSpline.Components;
  5. using BansheeGz.BGSpline.Curve;
  6. public class Catapult : MonoBehaviour
  7. {
  8. public BGCurve splineCurve;
  9. public float speed = 600f;
  10. public float rotateSpeed = 200f;
  11. public float timePadding = 0.5f;
  12. public float timeBetweenLaunches = 0.5f;
  13. public float minScale = 0.3f;
  14. public float maxScale = 1f;
  15. public float randomFactor = 50f;
  16. public AnimationCurve animationCurve;
  17. public Transform player;
  18. private bool isLaunching = false;
  19. private BGCcCursor splineCursor;
  20. private BGCcCursorObjectTranslate splineObjectTranslate;
  21. private Vector3 originalScale;
  22. private MeshRenderer meshRenderer;
  23. private Collider boulderCollider;
  24. private ParticleSystem boulderParticleSystem;
  25. private bool switchedOff = false;
  26. private Searchlight.SearchState searchState;
  27. void Start ()
  28. {
  29. splineCursor = splineCurve.GetComponent<BGCcCursor>();
  30. splineObjectTranslate = splineCurve.GetComponent<BGCcCursorObjectTranslate>();
  31. meshRenderer = splineObjectTranslate.ObjectToManipulate.GetComponent<MeshRenderer>();
  32. boulderCollider = splineObjectTranslate.ObjectToManipulate.GetComponent<Collider>();
  33. boulderParticleSystem = splineObjectTranslate.ObjectToManipulate.GetComponentInChildren<ParticleSystem>();
  34. meshRenderer.enabled = false;
  35. boulderCollider.enabled = false;
  36. switchedOff = false;
  37. boulderParticleSystem.Stop();
  38. originalScale = splineCurve.transform.parent.localScale;
  39. NotificationServer.register("statechange Searchlight", searchlightStateChanged);
  40. NotificationServer.register("BoulderHit", boulderHit);
  41. NotificationServer.register("restart scene", restartScene);
  42. NotificationServer.register("switch off", switchOff);
  43. }
  44. void Update()
  45. {
  46. splineObjectTranslate.ObjectToManipulate.Rotate(Vector3.forward, Time.deltaTime * rotateSpeed);
  47. }
  48. public void searchlightStateChanged()
  49. {
  50. if (searchState == Searchlight.state)
  51. return;
  52. if (switchedOff)
  53. return;
  54. if (!isActiveAndEnabled)
  55. return;
  56. searchState = Searchlight.state;
  57. LeanTween.cancel(splineObjectTranslate.ObjectToManipulate.gameObject, true);
  58. if (Searchlight.state == Searchlight.SearchState.Chasing)
  59. launchBoulder();
  60. }
  61. public void boulderHit(object boulderObj)
  62. {
  63. GameObject go = boulderObj as GameObject;
  64. if (go == splineObjectTranslate.ObjectToManipulate.gameObject)
  65. LeanTween.cancel(splineObjectTranslate.ObjectToManipulate.gameObject, true);
  66. }
  67. public void restartScene()
  68. {
  69. switchedOff = false;
  70. searchState = Searchlight.SearchState.Spline;
  71. LeanTween.cancel(splineObjectTranslate.ObjectToManipulate.gameObject, true);
  72. }
  73. private void switchOff(object offObject) {
  74. GameObject offGO = offObject as GameObject;
  75. // Debug.LogWarning(offGO + " vs " + transform.parent.gameObject);
  76. if (offGO == transform.parent.gameObject)
  77. switchedOff = true;
  78. }
  79. public void launchBoulder()
  80. {
  81. if (Searchlight.state != Searchlight.SearchState.Chasing || isLaunching)
  82. {
  83. isLaunching = false;
  84. return;
  85. }
  86. isLaunching = true;
  87. LeanTween.delayedCall(splineObjectTranslate.ObjectToManipulate.gameObject, timeBetweenLaunches, ()=>{
  88. if (Searchlight.state != Searchlight.SearchState.Chasing)
  89. {
  90. isLaunching = false;
  91. return;
  92. }
  93. boulderParticleSystem.Stop();
  94. splineCursor.DistanceRatio = 0f;
  95. splineCurve.transform.parent.position = player.position
  96. + Vector3.right * Random.Range(-randomFactor, randomFactor)
  97. + Vector3.forward * Random.Range(-randomFactor, randomFactor);
  98. splineCurve.transform.parent.localEulerAngles = Vector3.up * Random.Range(0f, 360f);
  99. splineCurve.transform.parent.localScale = new Vector3(Random.Range(minScale * originalScale.x, maxScale * originalScale.x), originalScale.y, originalScale.z);
  100. LeanTween.delayedCall(splineObjectTranslate.ObjectToManipulate.gameObject, 0.1f, ()=>{
  101. boulderParticleSystem.Play();
  102. meshRenderer.enabled = true;
  103. boulderCollider.enabled = true;
  104. LeanTween.value(splineObjectTranslate.ObjectToManipulate.gameObject, 0f, 1f, splineCursor.Math.GetDistance() / speed)
  105. .setEase(animationCurve)
  106. .setOnUpdate((float val)=>{
  107. splineCursor.DistanceRatio = val;
  108. }).setOnComplete(()=>{
  109. boulderParticleSystem.Stop();
  110. isLaunching = false;
  111. meshRenderer.enabled = false;
  112. LeanTween.delayedCall(splineObjectTranslate.ObjectToManipulate.gameObject, 0.5f, ()=>{
  113. boulderCollider.enabled = false;
  114. launchBoulder();
  115. });
  116. });
  117. });
  118. });
  119. }
  120. }