LandingSpot.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**************************************
  2. Copyright Unluck Software
  3. www.chemicalbliss.com
  4. ***************************************/
  5. using UnityEngine;
  6. using System.Collections;
  7. public class LandingSpot:MonoBehaviour{
  8. [HideInInspector]
  9. public FlockChild landingChild;
  10. [HideInInspector]
  11. public bool landing;
  12. int lerpCounter;
  13. [HideInInspector]
  14. public LandingSpotController _controller;
  15. bool _idle;
  16. public Transform _thisT; //Reference to transform component
  17. public bool _gotcha;
  18. public void Start() {
  19. if(_thisT == null) _thisT = transform;
  20. if (_controller == null)
  21. _controller = _thisT.parent.GetComponent<LandingSpotController>();
  22. if (_controller._autoCatchDelay.x > 0)
  23. StartCoroutine(GetFlockChild(_controller._autoCatchDelay.x, _controller._autoCatchDelay.y));
  24. }
  25. public void OnDrawGizmos() {
  26. if(_thisT == null) _thisT = transform;
  27. if (_controller == null)
  28. _controller = _thisT.parent.GetComponent<LandingSpotController>();
  29. Gizmos.color = Color.yellow;
  30. // Draw a yellow cube at the transforms position
  31. if ((landingChild != null) && landing)
  32. Gizmos.DrawLine(_thisT.position, landingChild._thisT.position);
  33. if (_thisT.rotation.eulerAngles.x != 0 || _thisT.rotation.eulerAngles.z != 0)
  34. _thisT.eulerAngles = new Vector3(0.0f, _thisT.eulerAngles.y, 0.0f);
  35. Gizmos.DrawCube(new Vector3(_thisT.position.x, _thisT.position.y, _thisT.position.z), Vector3.one * _controller._gizmoSize);
  36. Gizmos.DrawCube(_thisT.position + (_thisT.forward * _controller._gizmoSize), Vector3.one * _controller._gizmoSize *.5f);
  37. Gizmos.color = new Color(1.0f, 1.0f, 0.0f, .05f);
  38. Gizmos.DrawWireSphere(_thisT.position, _controller._maxBirdDistance);
  39. }
  40. public void LateUpdate() {
  41. if (landingChild == null) {
  42. _gotcha = false;
  43. _idle = false;
  44. lerpCounter = 0;
  45. return;
  46. }
  47. if(_gotcha){
  48. landingChild.transform.position = _thisT.position + landingChild._landingPosOffset;
  49. RotateBird();
  50. return;
  51. }
  52. if (_controller._flock.gameObject.activeInHierarchy && landing && (landingChild != null)) {
  53. if(!landingChild.gameObject.activeInHierarchy){
  54. Invoke("ReleaseFlockChild", 0.0f);
  55. }
  56. //Check distance to flock child
  57. float distance = Vector3.Distance(landingChild._thisT.position, _thisT.position+ landingChild._landingPosOffset);
  58. //Start landing if distance is close enough
  59. if (distance < 5 && distance > .5f) {
  60. if(_controller._soarLand){
  61. landingChild._model.GetComponent<Animation>().CrossFade(landingChild._spawner._soarAnimation, .5f);
  62. if (distance < 2)
  63. landingChild._model.GetComponent<Animation>().CrossFade(landingChild._spawner._flapAnimation, .5f);
  64. }
  65. landingChild._targetSpeed = landingChild._spawner._maxSpeed*_controller._landingSpeedModifier;
  66. landingChild._wayPoint = _thisT.position + landingChild._landingPosOffset;
  67. landingChild._damping = _controller._landingTurnSpeedModifier;
  68. landingChild._avoid = false;
  69. } else if (distance <= .5f) {
  70. landingChild._wayPoint = _thisT.position + landingChild._landingPosOffset;
  71. if (distance < _controller._snapLandDistance && !_idle) {
  72. _idle = true;
  73. landingChild._model.GetComponent<Animation>().CrossFade(landingChild._spawner._idleAnimation, .55f);
  74. }
  75. if (distance > _controller._snapLandDistance){
  76. landingChild._targetSpeed = landingChild._spawner._minSpeed*this._controller._landingSpeedModifier;
  77. landingChild._thisT.position += (_thisT.position + landingChild._landingPosOffset - landingChild._thisT.position) * Time.deltaTime *landingChild._speed*_controller._landingSpeedModifier *2;
  78. }else{
  79. _gotcha = true;
  80. }
  81. landingChild._move = false;
  82. RotateBird();
  83. // landingChild._damping = _controller._landingTurnSpeedModifier;
  84. } else {
  85. //Move towards landing spot
  86. landingChild._wayPoint = _thisT.position + landingChild._landingPosOffset;
  87. }
  88. landingChild._damping += .01f;
  89. }
  90. StraightenBird();
  91. }
  92. public void StraightenBird(){
  93. if (landingChild._thisT.eulerAngles.x == 0) return;
  94. Vector3 r = landingChild._thisT.eulerAngles;
  95. r.z = 0;
  96. landingChild._thisT.eulerAngles = r;
  97. }
  98. public void RotateBird(){
  99. if(_controller._randomRotate && _idle) return;
  100. lerpCounter++;
  101. Quaternion rot = landingChild._thisT.rotation;
  102. Vector3 rotE = rot.eulerAngles;
  103. rotE.y = Mathf.LerpAngle(landingChild._thisT.rotation.eulerAngles.y, _thisT.rotation.eulerAngles.y, lerpCounter * Time.deltaTime * _controller._landedRotateSpeed);
  104. rot.eulerAngles = rotE;
  105. landingChild._thisT.rotation = rot;
  106. }
  107. public IEnumerator GetFlockChild(float minDelay,float maxDelay) {
  108. yield return new WaitForSeconds(Random.Range(minDelay, maxDelay));
  109. if (_controller._flock.gameObject.activeInHierarchy && (landingChild == null)) {
  110. FlockChild fChild = null;
  111. for(int i = 0; i < _controller._flock._roamers.Count; i++) {
  112. FlockChild child = _controller._flock._roamers[i];
  113. if (!child._landing && !child._dived) {
  114. if(!_controller._onlyBirdsAbove){
  115. if ((fChild == null) && _controller._maxBirdDistance > Vector3.Distance(child._thisT.position, _thisT.position) && _controller._minBirdDistance < Vector3.Distance(child._thisT.position, _thisT.position)) {
  116. fChild = child;
  117. if (!_controller._takeClosest) break;
  118. } else if ((fChild != null) && Vector3.Distance(fChild._thisT.position, _thisT.position) > Vector3.Distance(child._thisT.position, _thisT.position)) {
  119. fChild = child;
  120. }
  121. }else{
  122. if ((fChild == null) && child._thisT.position.y > _thisT.position.y && _controller._maxBirdDistance > Vector3.Distance(child._thisT.position, _thisT.position) && _controller._minBirdDistance < Vector3.Distance(child._thisT.position, _thisT.position)) {
  123. fChild = child;
  124. if (!_controller._takeClosest) break;
  125. } else if ((fChild != null) && child._thisT.position.y > _thisT.position.y && Vector3.Distance(fChild._thisT.position, _thisT.position) > Vector3.Distance(child._thisT.position, _thisT.position)) {
  126. fChild = child;
  127. }
  128. }
  129. }
  130. }
  131. if (fChild != null) {
  132. landingChild = fChild;
  133. landing = true;
  134. landingChild._landing = true;
  135. if(_controller._autoDismountDelay.x > 0) Invoke("ReleaseFlockChild", Random.Range(_controller._autoDismountDelay.x, _controller._autoDismountDelay.y));
  136. _controller._activeLandingSpots++;
  137. } else if (_controller._autoCatchDelay.x > 0) {
  138. StartCoroutine(GetFlockChild(_controller._autoCatchDelay.x, _controller._autoCatchDelay.y));
  139. }
  140. }
  141. }
  142. public void InstantLand() {
  143. if (_controller._flock.gameObject.activeInHierarchy && (landingChild == null)) {
  144. FlockChild fChild = null;
  145. for(int i = 0; i < _controller._flock._roamers.Count; i++) {
  146. FlockChild child = _controller._flock._roamers[i];
  147. if (!child._landing && !child._dived) {
  148. fChild = child;
  149. }
  150. }
  151. if (fChild != null) {
  152. landingChild = fChild;
  153. landing = true;
  154. _controller._activeLandingSpots++;
  155. landingChild._landing = true;
  156. landingChild._thisT.position = _thisT.position + landingChild._landingPosOffset;
  157. landingChild._model.GetComponent<Animation>().Play(landingChild._spawner._idleAnimation);
  158. landingChild._thisT.Rotate(Vector3.up, Random.Range(0f, 360f));
  159. if(_controller._autoDismountDelay.x > 0) Invoke("ReleaseFlockChild", Random.Range(_controller._autoDismountDelay.x, _controller._autoDismountDelay.y));
  160. } else if (_controller._autoCatchDelay.x > 0) {
  161. StartCoroutine(GetFlockChild(_controller._autoCatchDelay.x, _controller._autoCatchDelay.y));
  162. }
  163. }
  164. }
  165. public void ReleaseFlockChild() {
  166. if (_controller._flock.gameObject.activeInHierarchy && (landingChild != null)) {
  167. _gotcha = false;
  168. lerpCounter = 0;
  169. if (_controller._featherPS != null){
  170. _controller._featherPS.position = landingChild._thisT.position;
  171. _controller._featherPS.GetComponent<ParticleSystem>().Emit(Random.Range(0,3));
  172. }
  173. landing = false;
  174. _idle = false;
  175. landingChild._avoid = true;
  176. //Reset flock child to flight mode
  177. landingChild._damping = landingChild._spawner._maxDamping;
  178. landingChild._model.GetComponent<Animation>().CrossFade(landingChild._spawner._flapAnimation, .2f);
  179. landingChild._dived = true;
  180. landingChild._speed = 0.0f;
  181. landingChild._move = true;
  182. landingChild._landing = false;
  183. landingChild.Flap();
  184. landingChild._wayPoint = new Vector3(landingChild._wayPoint.x, _thisT.position.y+10, landingChild._wayPoint.z);
  185. if (_controller._autoCatchDelay.x > 0) {
  186. StartCoroutine(GetFlockChild(_controller._autoCatchDelay.x + 0.1f, _controller._autoCatchDelay.y + 0.1f));
  187. }
  188. landingChild = null;
  189. _controller._activeLandingSpots--;
  190. }
  191. }
  192. }