Guest User

Untitled

a guest
May 24th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. public class StateMachine : MonoBehaviour
  2.  
  3.  
  4. private IState currentlyRunningState;
  5.  
  6. private IState PreviousState;
  7.  
  8. public void ChangeState(IState newState)
  9. {
  10. if (currentlyRunningState != null)
  11. {
  12. currentlyRunningState.Exit();
  13. }
  14.  
  15. PreviousState = currentlyRunningState;
  16.  
  17. currentlyRunningState = newState;
  18.  
  19. currentlyRunningState.Enter();
  20. }
  21.  
  22. public void ExecuteStateUpdate()
  23. {
  24.  
  25. var runningState = this.currentlyRunningState;
  26.  
  27. if (runningState != null)
  28. {
  29. runningState.Execute();
  30. }
  31. }
  32. public void SwitchToPreviousState()
  33. {
  34. this.currentlyRunningState.Exit();
  35.  
  36. this.currentlyRunningState = this.PreviousState;
  37.  
  38. this.currentlyRunningState.Enter();
  39.  
  40. }
  41.  
  42. public interface IState
  43.  
  44. void Enter();
  45.  
  46. void Execute();
  47. void Exit();
  48.  
  49. public class SearchFor : IState
  50.  
  51. LayerMask SearchLayer;
  52. GameObject OwnerGameObject;
  53. private float SearchRadius;
  54. private string TagForSearch;
  55. private NavMeshAgent NavMeshAgentForSearch;
  56.  
  57. public bool SearchComplated;
  58.  
  59. private System.Action<SearchResult> SearchResultCallBack;
  60.  
  61. public SearchFor(LayerMask searchlayer, GameObject owner, float searchradios, string tagforsearch, NavMeshAgent navmeshagent, System.Action<SearchResult>
  62. searchresultcallback)
  63. {
  64. SearchLayer = searchlayer;
  65. OwnerGameObject = owner;
  66. SearchRadius = searchradios;
  67. TagForSearch = tagforsearch;
  68. NavMeshAgentForSearch = navmeshagent;
  69. SearchResultCallBack = searchresultcallback;
  70. }
  71. public void Enter()
  72. {
  73.  
  74. }
  75. public void Execute()
  76. {
  77. var HitObjects = Physics.OverlapSphere(OwnerGameObject.transform.position, SearchRadius);
  78. var AllObjectsWithRequaredTag = new List<Collider>();
  79.  
  80. for (int i = 0; i < HitObjects.Length; i++)
  81. {
  82. if (HitObjects[i].CompareTag(TagForSearch))
  83. {
  84. this.NavMeshAgentForSearch.SetDestination(HitObjects[i].transform.position);
  85. AllObjectsWithRequaredTag.Add(HitObjects[i]);
  86.  
  87. }
  88. }
  89. var SearchResult = new SearchResult(HitObjects, AllObjectsWithRequaredTag);
  90.  
  91. SearchResultCallBack(SearchResult);
  92. }
  93. public void Exit()
  94. {
  95.  
  96. }
  97.  
  98. public List<Collider> AllHitObjectsInRequaredTag;
  99.  
  100. public SearchResult(Collider[] allhitobjectsinseardrad, List<Collider> allhitobjectsinreqtag)
  101. {
  102. AllhitObjectInSearchRadius = allhitobjectsinseardrad;
  103. AllHitObjectsInRequaredTag = allhitobjectsinreqtag;
  104. }
  105.  
  106. public class CubePlayer : MonoBehaviour
  107.  
  108. private StateMachine stateMachine = new StateMachine();
  109.  
  110. [SerializeField]
  111. private LayerMask foodItems;
  112. [SerializeField]
  113. private float ViewRadius;
  114. [SerializeField]
  115. private string FoodItemsTag;
  116. //private SearchFor searchForFood;
  117. private NavMeshAgent navMeshAgent;
  118.  
  119. public bool SwithState = false;
  120.  
  121. private void Start()
  122. {
  123. this.navMeshAgent = GetComponent<NavMeshAgent>();
  124.  
  125. // Изменение стадии на поиск чего-либо
  126. stateMachine.ChangeState(new SearchFor(foodItems, this.gameObject, ViewRadius, tag, navMeshAgent, FoodFound));
  127. }
  128. private void Update()
  129. {
  130. // Выполняет стадию
  131. stateMachine.ExecuteStateUpdate();
  132.  
  133. }
  134.  
  135. public void FoodFound(SearchResult searchResult)
  136. {
  137. var FoundItemsFood = searchResult.AllHitObjectsInRequaredTag;
  138.  
  139. }
  140.  
  141. public void EatFood()
  142. {
  143.  
  144. }
Add Comment
Please, Sign In to add comment