From c0819f5fa66e38456cab4574cd869889697f68e0 Mon Sep 17 00:00:00 2001 From: Sagittaeri Date: Sun, 22 Jan 2017 01:33:19 +1100 Subject: [PATCH] added searchlight chase v2 --- .../Assets/Scripts/AlertController.cs | 56 +++++++++ .../Assets/Scripts/AlertController.cs.meta | 12 ++ .../Assets/Scripts/Lighthouse.cs | 107 +++++++++++++++++ .../Assets/Scripts/Lighthouse.cs.meta | 12 ++ .../Assets/Scripts/Searchlight.cs | 111 ++++++++++++++++++ .../Assets/Scripts/Searchlight.cs.meta | 12 ++ .../Assets/Scripts/TestMoveController.cs | 20 ++++ .../Assets/Scripts/TestMoveController.cs.meta | 12 ++ IronToad_UnityProject/Assets/Sound.meta | 9 ++ 9 files changed, 351 insertions(+) create mode 100644 IronToad_UnityProject/Assets/Scripts/AlertController.cs create mode 100644 IronToad_UnityProject/Assets/Scripts/AlertController.cs.meta create mode 100644 IronToad_UnityProject/Assets/Scripts/Lighthouse.cs create mode 100644 IronToad_UnityProject/Assets/Scripts/Lighthouse.cs.meta create mode 100644 IronToad_UnityProject/Assets/Scripts/Searchlight.cs create mode 100644 IronToad_UnityProject/Assets/Scripts/Searchlight.cs.meta create mode 100644 IronToad_UnityProject/Assets/Scripts/TestMoveController.cs create mode 100644 IronToad_UnityProject/Assets/Scripts/TestMoveController.cs.meta create mode 100644 IronToad_UnityProject/Assets/Sound.meta diff --git a/IronToad_UnityProject/Assets/Scripts/AlertController.cs b/IronToad_UnityProject/Assets/Scripts/AlertController.cs new file mode 100644 index 0000000..605023d --- /dev/null +++ b/IronToad_UnityProject/Assets/Scripts/AlertController.cs @@ -0,0 +1,56 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.UI; + +public class AlertController : MonoBehaviour +{ + public Text text; + public Slider slider; + public float hideTime = 5f; + + private Vector3 originalPos; + private float lastSpotted; + + void Start () + { + NotificationServer.register("spotted boat", spottedBoat); + NotificationServer.register("lost boat", lostBoat); + originalPos = text.rectTransform.anchoredPosition3D; + } + + public void spottedBoat() + { + LeanTween.cancel(text.gameObject, false); + LeanTween.cancel(slider.gameObject, false); + lastSpotted = Time.timeSinceLevelLoad; + NotificationServer.notify("show AlertText"); + text.text = "hide!"; + setTimer(0f); + } + + public void lostBoat() + { + LeanTween.cancel(text.gameObject, false); + LeanTween.cancel(slider.gameObject, false); + NotificationServer.notify("show AlertText"); + NotificationServer.notify("show AlertTimer"); + text.text = "stay hidden"; + LeanTween.value(slider.gameObject, 0f, 1f, hideTime).setOnUpdate((float val)=>{ + setTimer(val); + }).setOnComplete(()=>{ + NotificationServer.notify("hide AlertTimer"); + NotificationServer.notify("statechange Searchlight returning"); + + text.text = "hidden..."; + LeanTween.delayedCall(text.gameObject, 2f, ()=>{ + NotificationServer.notify("hide AlertText"); + }); + }); + } + + public void setTimer(float val) + { + slider.value = val; + } +} diff --git a/IronToad_UnityProject/Assets/Scripts/AlertController.cs.meta b/IronToad_UnityProject/Assets/Scripts/AlertController.cs.meta new file mode 100644 index 0000000..efb9868 --- /dev/null +++ b/IronToad_UnityProject/Assets/Scripts/AlertController.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 89a715dd2260f413b89b4b9cdb979f3c +timeCreated: 1485005279 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/IronToad_UnityProject/Assets/Scripts/Lighthouse.cs b/IronToad_UnityProject/Assets/Scripts/Lighthouse.cs new file mode 100644 index 0000000..256ecf8 --- /dev/null +++ b/IronToad_UnityProject/Assets/Scripts/Lighthouse.cs @@ -0,0 +1,107 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +using BansheeGz.BGSpline.Components; +using BansheeGz.BGSpline.Curve; + +public class Lighthouse : MonoBehaviour +{ + public BGCurve splineCurve; + public List splineDistantPoints = new List(); + public float speed = 200f; + public float timePadding = 0.5f; + public float timeBetweenPoints = 3f; + public AnimationCurve animationCurve; + + private BGCcCursor splineCursor; + private BGCcCursorObjectTranslate splineObjectTranslate; + private int targetPoint = 0; + private bool reverseDirection = false; + private bool midpoint = false; + + void Start() + { + splineCursor = splineCurve.GetComponent(); + splineObjectTranslate = splineCurve.GetComponent(); + + if (splineDistantPoints == null || splineDistantPoints.Count == 0) + { + splineDistantPoints.Add(0f); + for (int i=1; i{ + if (reverseDirection) + targetPoint--; + else + targetPoint++; + if (targetPoint >= splineDistantPoints.Count) + { + if (splineCurve.Closed) + { + targetPoint = 1; + splineCursor.Distance = 0f; + } + else + { + targetPoint = splineDistantPoints.Count - 2; + reverseDirection = !reverseDirection; + } + } + + midpoint = true; + float start = splineCursor.Distance; + float end = splineDistantPoints[targetPoint]; + float distance = Mathf.Abs(end - start); + LeanTween.value(gameObject, start, end, distance / speed + timePadding).setOnUpdate((float val)=>{ + splineCursor.Distance = val; + }).setEase(animationCurve).setOnComplete(()=>{ + midpoint = false; + moveToNextPoint(); + }); + }); + } + + public void searchlightStateChanged() + { + LeanTween.cancel(gameObject, false); + if (Searchlight.state == Searchlight.SearchState.Chasing) + { + splineObjectTranslate.enabled = false; + } + else if (Searchlight.state == Searchlight.SearchState.Returning) + { + splineObjectTranslate.enabled = false; + Vector3 start = splineObjectTranslate.ObjectToManipulate.position; + Vector3 end = splineCursor.CalculatePosition(); + float distance = (end - start).magnitude; + LeanTween.move(splineObjectTranslate.ObjectToManipulate.gameObject, end, distance / speed + timePadding).setEase(animationCurve).setOnComplete(()=>{ + splineObjectTranslate.enabled = true; + if (midpoint) + { + if (reverseDirection) + targetPoint++; + else + targetPoint--; + } + moveToNextPoint(); + NotificationServer.notify("statechange Searchlight safe"); + }); + } + } +} diff --git a/IronToad_UnityProject/Assets/Scripts/Lighthouse.cs.meta b/IronToad_UnityProject/Assets/Scripts/Lighthouse.cs.meta new file mode 100644 index 0000000..c35ad1b --- /dev/null +++ b/IronToad_UnityProject/Assets/Scripts/Lighthouse.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: e529d146693e04d5983a4fbbb25de5ab +timeCreated: 1484975905 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/IronToad_UnityProject/Assets/Scripts/Searchlight.cs b/IronToad_UnityProject/Assets/Scripts/Searchlight.cs new file mode 100644 index 0000000..02acfca --- /dev/null +++ b/IronToad_UnityProject/Assets/Scripts/Searchlight.cs @@ -0,0 +1,111 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Searchlight : MonoBehaviour +{ + public enum SearchState + { + Spline, + Chasing, + Returning + } + + public float speed = 500f; + public float timePadding = 0.5f; + public float timeBetweenChase = 0.5f; + public float randomFactor = 50f; + public AnimationCurve animationCurve; + + [HideInInspector] + public bool isTriggering = false; + public static SearchState state = SearchState.Spline; + public static Collider chased; + + private static List instants = new List(); + + public static bool isTriggeringAtLeastOne() + { + foreach (Searchlight sl in instants) + { + if (sl.isTriggering) + return true; + } + return false; + } + + void Awake() + { + NotificationServer.register("statechange Searchlight", stateChanged); + NotificationServer.register("statechange Searchlight safe", stateSafe); + NotificationServer.register("statechange Searchlight returning", stateReturning); + if (!instants.Contains(this)) + instants.Add(this); + + } + + void OnTriggerEnter(Collider other) + { + chased = other; + isTriggering = true; + if (state != SearchState.Chasing) + { + state = SearchState.Chasing; + NotificationServer.notify("statechange Searchlight"); + NotificationServer.notify("chasing boat"); + } + NotificationServer.notify("spotted boat"); + } + + void OnTriggerExit(Collider other) + { + isTriggering = false; + if (state == SearchState.Chasing && !isTriggeringAtLeastOne()) + { +// chased = null; +// state = SearchState.Returning; +// NotificationServer.notify("statechange Searchlight"); + NotificationServer.notify("lost boat"); + } + } + + public void stateSafe() + { + state = SearchState.Spline; +// NotificationServer.notify("statechange Searchlight"); + } + + public void stateReturning() + { + state = SearchState.Returning; + NotificationServer.notify("statechange Searchlight"); + } + + public void stateChanged() + { + LeanTween.cancel(gameObject, false); + if (state == SearchState.Chasing) + LeanTween.move(gameObject, chased.transform.position, 0.5f).setEase(animationCurve).setOnComplete(chase); + } + + public void chase() + { + if (isTriggering) + { + LeanTween.delayedCall(gameObject, timeBetweenChase, chase); + } + else + { + LeanTween.delayedCall(gameObject, timeBetweenChase, ()=>{ + Vector3 start = transform.localPosition; + Vector3 end = chased.transform.localPosition + + Vector3.right * Random.Range(-randomFactor, randomFactor) + + Vector3.forward * Random.Range(-randomFactor, randomFactor); + float distance = (end - start).magnitude; + LeanTween.value(gameObject, 0f, 1f, distance / speed + timePadding).setOnUpdate((float val)=>{ + transform.localPosition = start + (end-start)*val; + }).setEase(animationCurve).setOnComplete(chase); + }); + } + } +} diff --git a/IronToad_UnityProject/Assets/Scripts/Searchlight.cs.meta b/IronToad_UnityProject/Assets/Scripts/Searchlight.cs.meta new file mode 100644 index 0000000..d66b18e --- /dev/null +++ b/IronToad_UnityProject/Assets/Scripts/Searchlight.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 51b93e628614140229e5d1f43186a375 +timeCreated: 1484985371 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/IronToad_UnityProject/Assets/Scripts/TestMoveController.cs b/IronToad_UnityProject/Assets/Scripts/TestMoveController.cs new file mode 100644 index 0000000..adf5328 --- /dev/null +++ b/IronToad_UnityProject/Assets/Scripts/TestMoveController.cs @@ -0,0 +1,20 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class TestMoveController : MonoBehaviour +{ + public float speed = 100f; + + void Update () + { + if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow)) + transform.Translate(Vector3.back * Time.deltaTime * speed); + if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) + transform.Translate(Vector3.left * Time.deltaTime * speed); + if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)) + transform.Translate(Vector3.forward * Time.deltaTime * speed); + if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) + transform.Translate(Vector3.right * Time.deltaTime * speed); + } +} diff --git a/IronToad_UnityProject/Assets/Scripts/TestMoveController.cs.meta b/IronToad_UnityProject/Assets/Scripts/TestMoveController.cs.meta new file mode 100644 index 0000000..3331dbe --- /dev/null +++ b/IronToad_UnityProject/Assets/Scripts/TestMoveController.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 89a55db40871a4d4c920b5f69d6bfc03 +timeCreated: 1484997652 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/IronToad_UnityProject/Assets/Sound.meta b/IronToad_UnityProject/Assets/Sound.meta new file mode 100644 index 0000000..c6dafb0 --- /dev/null +++ b/IronToad_UnityProject/Assets/Sound.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: 0fbabcae9313543ffbf1429aeac3664d +folderAsset: yes +timeCreated: 1484963877 +licenseType: Free +DefaultImporter: + userData: + assetBundleName: + assetBundleVariant: