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.

97 lines
3.1 KiB

  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 ParticleSystem particleSystem;
  24. void Start ()
  25. {
  26. splineCursor = splineCurve.GetComponent<BGCcCursor>();
  27. splineObjectTranslate = splineCurve.GetComponent<BGCcCursorObjectTranslate>();
  28. meshRenderer = splineObjectTranslate.ObjectToManipulate.GetComponent<MeshRenderer>();
  29. particleSystem = splineObjectTranslate.ObjectToManipulate.GetComponentInChildren<ParticleSystem>();
  30. meshRenderer.enabled = false;
  31. particleSystem.Stop();
  32. originalScale = splineCurve.transform.parent.localScale;
  33. NotificationServer.register("statechange Searchlight", searchlightStateChanged);
  34. NotificationServer.register("BoulderHit", boulderHit);
  35. }
  36. void Update()
  37. {
  38. splineObjectTranslate.ObjectToManipulate.Rotate(Vector3.forward, Time.deltaTime * rotateSpeed);
  39. }
  40. public void searchlightStateChanged()
  41. {
  42. LeanTween.cancel(splineObjectTranslate.ObjectToManipulate.gameObject, true);
  43. if (Searchlight.state == Searchlight.SearchState.Chasing)
  44. launchBoulder();
  45. }
  46. public void boulderHit()
  47. {
  48. LeanTween.cancel(splineObjectTranslate.ObjectToManipulate.gameObject, true);
  49. }
  50. public void launchBoulder()
  51. {
  52. if (Searchlight.state != Searchlight.SearchState.Chasing || isLaunching)
  53. {
  54. isLaunching = false;
  55. return;
  56. }
  57. isLaunching = true;
  58. LeanTween.delayedCall(splineObjectTranslate.ObjectToManipulate.gameObject, timeBetweenLaunches, ()=>{
  59. if (Searchlight.state != Searchlight.SearchState.Chasing)
  60. {
  61. isLaunching = false;
  62. return;
  63. }
  64. particleSystem.Stop();
  65. meshRenderer.enabled = true;
  66. splineCursor.DistanceRatio = 0f;
  67. splineCurve.transform.parent.position = player.position
  68. + Vector3.right * Random.Range(-randomFactor, randomFactor)
  69. + Vector3.forward * Random.Range(-randomFactor, randomFactor);
  70. splineCurve.transform.parent.localEulerAngles = Vector3.up * Random.Range(0f, 360f);
  71. splineCurve.transform.parent.localScale = new Vector3(Random.Range(minScale * originalScale.x, maxScale * originalScale.x), originalScale.y, originalScale.z);
  72. particleSystem.Play();
  73. LeanTween.value(splineObjectTranslate.ObjectToManipulate.gameObject, 0f, 1f, splineCursor.Math.GetDistance() / speed)
  74. .setEase(animationCurve)
  75. .setOnUpdate((float val)=>{
  76. splineCursor.DistanceRatio = val;
  77. }).setOnComplete(()=>{
  78. particleSystem.Stop();
  79. // splineCursor.DistanceRatio = 0f;
  80. isLaunching = false;
  81. meshRenderer.enabled = false;
  82. launchBoulder();
  83. });
  84. });
  85. }
  86. }