public void CustomTransformHandler(LightningCustomTransformStateInfo state)
 {
     if (!enabled)
     {
         return;
     }
     else if (LightningScript == null)
     {
         Debug.LogError("LightningScript property must be set to non-null.");
         return;
     }
     else if (state.State == LightningCustomTransformState.Executing)
     {
         UpdateTransform(state, LightningScript, ScaleLimit);
     }
     else if (state.State == LightningCustomTransformState.Started)
     {
         // mark the start and end positions to base rotation and scale changes on
         state.StartTransform = StartTarget;
         state.EndTransform   = EndTarget;
         transformStartPositions[transform] = state;
     }
     else
     {
         // remove the transform, this bolt is done
         transformStartPositions.Remove(transform);
     }
 }
Exemple #2
0
 private void CustomTransform(LightningCustomTransformStateInfo state)
 {
     if (CustomTransformHandler != null)
     {
         CustomTransformHandler.Invoke(state);
     }
 }
 public static void ReturnStateInfoToCache(LightningCustomTransformStateInfo info)
 {
     if (info != null)
     {
         info.Transform = info.StartTransform = info.EndTransform = null;
         info.UserInfo  = null;
         cache.Add(info);
     }
 }
        public static LightningCustomTransformStateInfo GetOrCreateStateInfo()
        {
            if (cache.Count == 0)
            {
                return(new LightningCustomTransformStateInfo());
            }
            int idx = cache.Count - 1;
            LightningCustomTransformStateInfo result = cache[idx];

            cache.RemoveAt(idx);
            return(result);
        }
        private static void UpdateTransform(LightningCustomTransformStateInfo state, LightningBoltPrefabScript script, RangeOfFloats scaleLimit)
        {
            if (state.Transform == null || state.StartTransform == null)
            {
                return;
            }
            else if (state.EndTransform == null)
            {
                // just put the lightning at the start and be done
                state.Transform.position = state.StartTransform.position - state.BoltStartPosition;
                return;
            }

            Quaternion rotation;

            if ((script.CameraMode == CameraMode.Auto && script.Camera.orthographic) ||
                (script.CameraMode == CameraMode.OrthographicXY))
            {
                // 2D rotation delta (xy)
                float startAngle   = AngleBetweenVector2(state.BoltStartPosition, state.BoltEndPosition);
                float currentAngle = AngleBetweenVector2(state.StartTransform.position, state.EndTransform.position);
                rotation = Quaternion.AngleAxis((currentAngle - startAngle), Vector3.forward);
            }
            if (script.CameraMode == CameraMode.OrthographicXZ)
            {
                // 2D rotation delta (xz)
                float startAngle   = AngleBetweenVector2(new Vector2(state.BoltStartPosition.x, state.BoltStartPosition.z), new Vector2(state.BoltEndPosition.x, state.BoltEndPosition.z));
                float currentAngle = AngleBetweenVector2(new Vector2(state.StartTransform.position.x, state.StartTransform.position.z), new Vector2(state.EndTransform.position.x, state.EndTransform.position.z));
                rotation = Quaternion.AngleAxis((currentAngle - startAngle), Vector3.up);
            }
            else
            {
                // 3D rotation delta
                Quaternion look1 = Quaternion.LookRotation((state.BoltEndPosition - state.BoltStartPosition).normalized);
                Quaternion look2 = Quaternion.LookRotation((state.EndTransform.position - state.StartTransform.position).normalized);
                rotation = look2 * Quaternion.Inverse(look1);
            }
            state.Transform.rotation = rotation;

            // scale based on how much the objects have moved relative to each other
            float startDistance = Vector3.Distance(state.BoltStartPosition, state.BoltEndPosition);
            float endDistance   = Vector3.Distance(state.EndTransform.position, state.StartTransform.position);
            float scale         = Mathf.Clamp((startDistance < Mathf.Epsilon ? 1.0f : endDistance / startDistance), scaleLimit.Minimum, scaleLimit.Maximum);

            state.Transform.localScale = new Vector3(scale, scale, scale);

            // anchor lightning to start position and account for rotation and scale
            Vector3 offset = rotation * (scale * state.BoltStartPosition);

            state.Transform.position = state.StartTransform.position - offset;
        }
        private static IEnumerator NotifyBolt(LightningBoltDependencies dependencies, LightningBoltParameters p, Transform transform, Vector3 start, Vector3 end)
        {
            float delay    = p.delaySeconds;
            float lifeTime = p.LifeTime;

            yield return(new WaitForSecondsLightning(delay));

            if (dependencies.LightningBoltStarted != null)
            {
                dependencies.LightningBoltStarted(p, start, end);
            }
            LightningCustomTransformStateInfo state = (p.CustomTransform == null ? null : LightningCustomTransformStateInfo.GetOrCreateStateInfo());

            if (state != null)
            {
                state.Parameters        = p;
                state.BoltStartPosition = start;
                state.BoltEndPosition   = end;
                state.State             = LightningCustomTransformState.Started;
                state.Transform         = transform;
                p.CustomTransform(state);
                state.State = LightningCustomTransformState.Executing;
            }

            if (p.CustomTransform == null)
            {
                yield return(new WaitForSecondsLightning(lifeTime));
            }
            else
            {
                while (lifeTime > 0.0f)
                {
                    p.CustomTransform(state);
                    lifeTime -= LightningBoltScript.DeltaTime;
                    yield return(null);
                }
            }

            if (p.CustomTransform != null)
            {
                state.State = LightningCustomTransformState.Ended;
                p.CustomTransform(state);
                LightningCustomTransformStateInfo.ReturnStateInfoToCache(state);
            }
            if (dependencies.LightningBoltEnded != null)
            {
                dependencies.LightningBoltEnded(p, start, end);
            }
            LightningBoltParameters.ReturnParametersToCache(p);
        }