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.

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