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.
 
 
 
 
 
 

128 lines
3.7 KiB

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<float> splineDistantPoints = new List<float>();
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;
private Searchlight.SearchState searchState;
void Start()
{
splineCursor = splineCurve.GetComponent<BGCcCursor>();
splineObjectTranslate = splineCurve.GetComponent<BGCcCursorObjectTranslate>();
if (splineDistantPoints == null || splineDistantPoints.Count == 0)
{
splineDistantPoints.Add(0f);
for (int i=1; i<splineCurve.PointsCount; i++)
{
BGCurvePointI point = splineCurve.Points[i];
float distance = 0f;
splineCursor.Math.CalcPositionByClosestPoint(point.PositionWorld, out distance);
splineDistantPoints.Add(distance);
}
splineDistantPoints.Add(splineCursor.Math.GetDistance());
}
splineCursor.Distance = 0f;
searchState = Searchlight.SearchState.Spline;
moveToNextPoint();
NotificationServer.register("statechange Searchlight", searchlightStateChanged);
NotificationServer.register("restart scene", restartScene);
}
public void moveToNextPoint()
{
LeanTween.delayedCall(gameObject, timeBetweenPoints, ()=>{
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()
{
if (searchState == Searchlight.state)
return;
searchState = Searchlight.state;
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");
});
}
}
public void restartScene()
{
LeanTween.cancel(gameObject, false);
Searchlight.state = Searchlight.SearchState.Spline;
splineObjectTranslate.enabled = true;
splineCursor.Distance = 0f;
midpoint = false;
reverseDirection = false;
targetPoint = 0;
splineObjectTranslate.ObjectToManipulate.position = splineCursor.CalculatePosition();
searchState = Searchlight.SearchState.Spline;
moveToNextPoint();
NotificationServer.notify("statechange Searchlight safe");
}
}