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.

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