|
|
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- using BansheeGz.BGSpline.Components;
- using BansheeGz.BGSpline.Curve;
-
- public class Catapult : MonoBehaviour
- {
- public BGCurve splineCurve;
- public float speed = 600f;
- public float rotateSpeed = 200f;
- public float timePadding = 0.5f;
- public float timeBetweenLaunches = 0.5f;
- public float minScale = 0.3f;
- public float maxScale = 1f;
- public float randomFactor = 50f;
- public AnimationCurve animationCurve;
- public Transform player;
-
- private bool isLaunching = false;
- private BGCcCursor splineCursor;
- private BGCcCursorObjectTranslate splineObjectTranslate;
- private Vector3 originalScale;
- private MeshRenderer meshRenderer;
- private Collider boulderCollider;
- private ParticleSystem boulderParticleSystem;
- private bool switchedOff = false;
-
- private Searchlight.SearchState searchState;
-
- void Start ()
- {
- splineCursor = splineCurve.GetComponent<BGCcCursor>();
- splineObjectTranslate = splineCurve.GetComponent<BGCcCursorObjectTranslate>();
- meshRenderer = splineObjectTranslate.ObjectToManipulate.GetComponent<MeshRenderer>();
- boulderCollider = splineObjectTranslate.ObjectToManipulate.GetComponent<Collider>();
- boulderParticleSystem = splineObjectTranslate.ObjectToManipulate.GetComponentInChildren<ParticleSystem>();
- meshRenderer.enabled = false;
- boulderCollider.enabled = false;
- switchedOff = false;
- boulderParticleSystem.Stop();
-
- originalScale = splineCurve.transform.parent.localScale;
-
- NotificationServer.register("statechange Searchlight", searchlightStateChanged);
- NotificationServer.register("BoulderHit", boulderHit);
- NotificationServer.register("restart scene", restartScene);
- NotificationServer.register("switch off", switchOff);
- }
-
- void Update()
- {
- splineObjectTranslate.ObjectToManipulate.Rotate(Vector3.forward, Time.deltaTime * rotateSpeed);
- }
-
- public void searchlightStateChanged()
- {
- if (searchState == Searchlight.state)
- return;
- if (switchedOff)
- return;
- if (!isActiveAndEnabled)
- return;
- searchState = Searchlight.state;
- LeanTween.cancel(splineObjectTranslate.ObjectToManipulate.gameObject, true);
- if (Searchlight.state == Searchlight.SearchState.Chasing)
- launchBoulder();
- }
-
- public void boulderHit(object boulderObj)
- {
- GameObject go = boulderObj as GameObject;
- if (go == splineObjectTranslate.ObjectToManipulate.gameObject)
- LeanTween.cancel(splineObjectTranslate.ObjectToManipulate.gameObject, true);
- }
-
- public void restartScene()
- {
- switchedOff = false;
- searchState = Searchlight.SearchState.Spline;
- LeanTween.cancel(splineObjectTranslate.ObjectToManipulate.gameObject, true);
- }
-
- private void switchOff(object offObject) {
- GameObject offGO = offObject as GameObject;
- // Debug.LogWarning(offGO + " vs " + transform.parent.gameObject);
- if (offGO == transform.parent.gameObject)
- switchedOff = true;
- }
-
- public void launchBoulder()
- {
- if (Searchlight.state != Searchlight.SearchState.Chasing || isLaunching)
- {
- isLaunching = false;
- return;
- }
-
- isLaunching = true;
- LeanTween.delayedCall(splineObjectTranslate.ObjectToManipulate.gameObject, timeBetweenLaunches, ()=>{
- if (Searchlight.state != Searchlight.SearchState.Chasing)
- {
- isLaunching = false;
- return;
- }
- boulderParticleSystem.Stop();
- splineCursor.DistanceRatio = 0f;
- splineCurve.transform.parent.position = player.position
- + Vector3.right * Random.Range(-randomFactor, randomFactor)
- + Vector3.forward * Random.Range(-randomFactor, randomFactor);
- splineCurve.transform.parent.localEulerAngles = Vector3.up * Random.Range(0f, 360f);
- splineCurve.transform.parent.localScale = new Vector3(Random.Range(minScale * originalScale.x, maxScale * originalScale.x), originalScale.y, originalScale.z);
- LeanTween.delayedCall(splineObjectTranslate.ObjectToManipulate.gameObject, 0.1f, ()=>{
- boulderParticleSystem.Play();
- meshRenderer.enabled = true;
- boulderCollider.enabled = true;
- LeanTween.value(splineObjectTranslate.ObjectToManipulate.gameObject, 0f, 1f, splineCursor.Math.GetDistance() / speed)
- .setEase(animationCurve)
- .setOnUpdate((float val)=>{
- splineCursor.DistanceRatio = val;
- }).setOnComplete(()=>{
- boulderParticleSystem.Stop();
- isLaunching = false;
- meshRenderer.enabled = false;
- LeanTween.delayedCall(splineObjectTranslate.ObjectToManipulate.gameObject, 0.5f, ()=>{
- boulderCollider.enabled = false;
- launchBoulder();
- });
- });
- });
- });
- }
- }
|