LandingSpotController.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**************************************
  2. LandingSpotController
  3. Copyright Unluck Software
  4. www.chemicalbliss.com
  5. ***************************************/
  6. using UnityEngine;
  7. using System.Collections;
  8. public class LandingSpotController:MonoBehaviour{
  9. public bool _randomRotate = true; // Random rotation when a bird lands
  10. public Vector2 _autoCatchDelay = new Vector2(10.0f, 20.0f); // Random Min/Max time for landing spot to make a bird land
  11. public Vector2 _autoDismountDelay = new Vector2(10.0f, 20.0f); // Random Min/Max time for birds to automaticly fly away from landing spot
  12. public float _maxBirdDistance = 20.0f; // The maximum distance to a bird for it to land
  13. public float _minBirdDistance = 5.0f; // The minimum distance to a bird for it to land
  14. public bool _takeClosest; // Toggle this to make landingspots make the closest bird to it land
  15. public FlockController _flock; // Assign the FlockController to pick birds from
  16. public bool _landOnStart; // Put birds on the landing spots at start
  17. public bool _soarLand = true; // Birds will soar while aproaching landing spot
  18. public bool _onlyBirdsAbove; // Only birds above landing spot will land
  19. public float _landingSpeedModifier = .5f; // Adjust bird movement speed while clost to the landing spot
  20. public float _landingTurnSpeedModifier = 5.0f;
  21. public Transform _featherPS; // Update: Changed from GameObject to Transform
  22. public Transform _thisT; // Transform reference
  23. public int _activeLandingSpots;
  24. public float _snapLandDistance = 0.1f; // Increase this if landing spots are moving
  25. public float _landedRotateSpeed = 0.01f;
  26. public float _gizmoSize = 0.2f;
  27. public void Start() {
  28. if(_thisT == null) _thisT = transform;
  29. if(_flock == null){
  30. _flock = (FlockController)GameObject.FindObjectOfType(typeof(FlockController));
  31. Debug.Log(this + " has no assigned FlockController, a random FlockController has been assigned");
  32. }
  33. #if UNITY_EDITOR
  34. if(_autoCatchDelay.x >0 &&(_autoCatchDelay.x < 5||_autoCatchDelay.y < 5)){
  35. Debug.Log(this.name + ": autoCatchDelay values set low, this might result in strange behaviours");
  36. }
  37. #endif
  38. if(_landOnStart){
  39. StartCoroutine(InstantLandOnStart(.1f));
  40. }
  41. }
  42. public void ScareAll() {
  43. ScareAll(0.0f,1.0f);
  44. }
  45. public void ScareAll(float minDelay,float maxDelay) {
  46. for(int i=0; i< _thisT.childCount; i++){
  47. if(_thisT.GetChild(i).GetComponent<LandingSpot>() != null){
  48. LandingSpot spot = _thisT.GetChild(i).GetComponent<LandingSpot>();
  49. spot.Invoke("ReleaseFlockChild", Random.Range(minDelay,maxDelay));
  50. }
  51. }
  52. }
  53. public void LandAll() {
  54. for(int i=0; i< _thisT.childCount; i++){
  55. if(_thisT.GetChild(i).GetComponent<LandingSpot>() != null){
  56. LandingSpot spot = _thisT.GetChild(i).GetComponent<LandingSpot>();
  57. StartCoroutine(spot.GetFlockChild(0.0f,2.0f));
  58. }
  59. }
  60. }
  61. //This function was added to fix a error with having a button calling InstantLand
  62. public IEnumerator InstantLandOnStart(float delay) {
  63. yield return new WaitForSeconds(delay);
  64. for(int i=0; i< _thisT.childCount; i++){
  65. if(_thisT.GetChild(i).GetComponent<LandingSpot>() != null){
  66. LandingSpot spot = _thisT.GetChild(i).GetComponent<LandingSpot>();
  67. spot.InstantLand();
  68. }
  69. }
  70. }
  71. public IEnumerator InstantLand(float delay) {
  72. yield return new WaitForSeconds(delay);
  73. for(int i=0; i< _thisT.childCount; i++){
  74. if(_thisT.GetChild(i).GetComponent<LandingSpot>() != null){
  75. LandingSpot spot = _thisT.GetChild(i).GetComponent<LandingSpot>();
  76. spot.InstantLand();
  77. }
  78. }
  79. }
  80. }