/Frog_Fight_Club/How To Dematerialize Completely/Assets/GoKit-master/Assets/Plugins/GoKit/properties/specificTypes/PositionPathTweenProperty.cs

https://bitbucket.org/yamasakai/frog-fight-club · C# · 132 lines · 86 code · 30 blank · 16 comment · 14 complexity · f94a93a96642e48709f3b669bfbb9194 MD5 · raw file

  1. using UnityEngine;
  2. using System.Collections;
  3. /// <summary>
  4. /// tweens position along a path at constant speed between nodes. isRelative makes the path movement
  5. /// relative to the start position of the object. a "from" tween will reverse the path and make the start
  6. /// position be the last node in the path.
  7. /// </summary>
  8. public sealed class PositionPathTweenProperty : AbstractTweenProperty
  9. {
  10. private bool _useLocalPosition;
  11. public bool useLocalPosition { get { return _useLocalPosition; } }
  12. private Transform _target;
  13. private Vector3 _startValue;
  14. private GoSpline _path;
  15. private GoLookAtType _lookAtType = GoLookAtType.None;
  16. private Transform _lookTarget;
  17. private GoSmoothedQuaternion _smoothedRotation;
  18. public PositionPathTweenProperty( GoSpline path, bool isRelative = false, bool useLocalPosition = false, GoLookAtType lookAtType = GoLookAtType.None, Transform lookTarget = null ) : base( isRelative )
  19. {
  20. _path = path;
  21. _useLocalPosition = useLocalPosition;
  22. _lookAtType = lookAtType;
  23. _lookTarget = lookTarget;
  24. }
  25. #region Object overrides
  26. public override int GetHashCode()
  27. {
  28. return base.GetHashCode();
  29. }
  30. public override bool Equals( object obj )
  31. {
  32. // start with a base check and then compare if we are both using local values
  33. if( base.Equals( obj ) )
  34. return this._useLocalPosition == ((PositionPathTweenProperty)obj)._useLocalPosition;
  35. // if we get here, we need to see if the other object is a position tween of the same kind
  36. var otherAsPosition = obj as PositionTweenProperty;
  37. if( otherAsPosition != null )
  38. return this._useLocalPosition == otherAsPosition.useLocalPosition;
  39. return false;
  40. }
  41. #endregion
  42. public override void prepareForUse()
  43. {
  44. _target = _ownerTween.target as Transform;
  45. // if this is a from tween first reverse the path then build it
  46. if( _ownerTween.isFrom )
  47. _path.reverseNodes();
  48. else
  49. _path.unreverseNodes();
  50. _path.buildPath();
  51. // a from tween means the start value is the last node
  52. if( _ownerTween.isFrom )
  53. {
  54. _startValue = _path.getLastNode();
  55. }
  56. else
  57. {
  58. if( _useLocalPosition )
  59. _startValue = _target.localPosition;
  60. else
  61. _startValue = _target.position;
  62. }
  63. // validate the lookTarget if we are set to look at it
  64. if( _lookAtType == GoLookAtType.TargetTransform )
  65. {
  66. if( _lookTarget == null )
  67. _lookAtType = GoLookAtType.None;
  68. }
  69. // prep our smoothed rotation
  70. _smoothedRotation = _target.rotation;
  71. }
  72. public override void tick( float totalElapsedTime )
  73. {
  74. var easedTime = _easeFunction( totalElapsedTime, 0, 1, _ownerTween.duration );
  75. var vec = _path.getPointOnPath( easedTime );
  76. // if we are relative, add the vec to our startValue
  77. if( _isRelative )
  78. vec += _startValue;
  79. // handle look types
  80. switch( _lookAtType )
  81. {
  82. case GoLookAtType.NextPathNode:
  83. {
  84. _smoothedRotation.smoothValue = vec.Equals( _target.position ) ? Quaternion.identity : Quaternion.LookRotation( vec - _target.position );
  85. _target.rotation = _smoothedRotation.smoothValue;
  86. //var lookAtNode = ( _ownerTween.isReversed || _ownerTween.isLoopoingBackOnPingPong ) ? _path.getPreviousNode() : _path.getNextNode();
  87. //_target.LookAt( lookAtNode, Vector3.up );
  88. break;
  89. }
  90. case GoLookAtType.TargetTransform:
  91. {
  92. _target.LookAt( _lookTarget, Vector3.up );
  93. break;
  94. }
  95. }
  96. // assign the position
  97. if( _useLocalPosition )
  98. _target.localPosition = vec;
  99. else
  100. _target.position = vec;
  101. }
  102. }