SafetyAreaMono.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. public class SafetyAreaMono : MonoBehaviour
  6. {
  7. private const string PLAY_AREA_NAME = "PlayArea";
  8. private const string STATIONARY_NAME = "StationaryArea";
  9. public GroundHeightUI groundHeightUI;
  10. public PlayAreaWaitingDrawUI playAreaWaitingDrawUI;
  11. public PlayAreaOKUI playAreaOKUI;
  12. public StationaryAreaUI stationaryAreaUI;
  13. public ConfirmPlayAreaUI confirmPlayAreaUI;
  14. public SafetyPlaneMono safetyPlaneMono;
  15. private GameObject safetyPlaneObject;
  16. private StationaryAreaMono stationaryAreaMono;
  17. private GameObject stationaryAreaObject;
  18. private GroundHeightStep groundHeightStep;
  19. private PlayAreaStep playAreaStep;
  20. private StationaryAreaStep stationaryAreaStep;
  21. private ConfirmPlayAreaStep confirmPlayAreaStep;
  22. private PlayAreaStateMachine playAreaStateMachine;
  23. public void Init()
  24. {
  25. if (groundHeightStep == null)
  26. {
  27. groundHeightStep = SafetyAreaManager.Instance.GetStep<GroundHeightStep>(SafetyAreaStepEnum.GroundHeight);
  28. }
  29. if (playAreaStep == null)
  30. {
  31. playAreaStep = SafetyAreaManager.Instance.GetStep<PlayAreaStep>(SafetyAreaStepEnum.PlayArea);
  32. }
  33. if (stationaryAreaStep == null)
  34. {
  35. stationaryAreaStep = SafetyAreaManager.Instance.GetStep<StationaryAreaStep>(SafetyAreaStepEnum.StationaryArea);
  36. }
  37. if (confirmPlayAreaStep == null)
  38. {
  39. confirmPlayAreaStep = SafetyAreaManager.Instance.GetStep<ConfirmPlayAreaStep>(SafetyAreaStepEnum.ConfirmPlayArea);
  40. }
  41. if (playAreaStateMachine == null)
  42. {
  43. playAreaStateMachine = new PlayAreaStateMachine();
  44. playAreaStateMachine.InitStateMachine(this);
  45. }
  46. groundHeightStep.RegistOnEnterStepCallback(OnEnterGroundHeightStep);
  47. groundHeightStep.RegistOnExitStepCallback(OnExitGroundHeightStep);
  48. playAreaStep.RegistOnEnterStepCallback(OnEnterPlayAreaStep);
  49. playAreaStep.RegistOnExitStepCallback(OnExitPlayAreaStep);
  50. stationaryAreaStep.RegistOnEnterStepCallback(OnEnterStationAreaStep);
  51. stationaryAreaStep.RegistOnExitStepCallback(OnExitStationAreaStep);
  52. confirmPlayAreaStep.RegistOnEnterStepCallback(OnEnterConfirmAreaStep);
  53. confirmPlayAreaStep.RegistOnExitStepCallback(OnExitConfirmAreaStep);
  54. SafetyAreaManager.Instance.ChangeStep(SafetyAreaStepEnum.GroundHeight);
  55. }
  56. public void Release()
  57. {
  58. groundHeightStep.UnRegistOnEnterStepCallback(OnEnterGroundHeightStep);
  59. groundHeightStep.UnRegistOnExitStepCallback(OnExitGroundHeightStep);
  60. playAreaStep.UnRegistOnEnterStepCallback(OnEnterPlayAreaStep);
  61. playAreaStep.UnRegistOnExitStepCallback(OnExitPlayAreaStep);
  62. stationaryAreaStep.UnRegistOnEnterStepCallback(OnEnterStationAreaStep);
  63. stationaryAreaStep.UnRegistOnExitStepCallback(OnExitStationAreaStep);
  64. confirmPlayAreaStep.UnRegistOnEnterStepCallback(OnEnterConfirmAreaStep);
  65. confirmPlayAreaStep.UnRegistOnExitStepCallback(OnExitConfirmAreaStep);
  66. }
  67. //进入设置地面高度步骤
  68. private void OnEnterGroundHeightStep()
  69. {
  70. groundHeightUI.gameObject.SetActive(true);
  71. groundHeightUI.Init();
  72. CreateSafetyPlane();
  73. ResetSafetyPlane();
  74. UnFreezePlaneHeight();
  75. groundHeightUI.OnConfirmClick += SwitchToPlayAreaStep;
  76. groundHeightUI.OnResetClick += ResetSafetyPlane;
  77. groundHeightUI.OnCancelClick += ExitSafetyAreaProcess;
  78. }
  79. //退出设置地面高度步骤
  80. private void OnExitGroundHeightStep()
  81. {
  82. FreezePlaneHeight();
  83. groundHeightUI.OnConfirmClick -= SwitchToPlayAreaStep;
  84. groundHeightUI.OnResetClick -= ResetSafetyPlane;
  85. groundHeightUI.OnCancelClick -= ExitSafetyAreaProcess;
  86. groundHeightUI.Release();
  87. groundHeightUI.gameObject.SetActive(false);
  88. }
  89. //进入设置游戏区域步骤
  90. private void OnEnterPlayAreaStep()
  91. {
  92. safetyPlaneMono.RegistPointerDownFillEvent();
  93. safetyPlaneMono.RegistPointerUpFillEvent();
  94. ShowPlane();
  95. ClearPlaneColor();
  96. ChangePlayAreaState(PlayAreaStateEnum.WaitingDraw);
  97. }
  98. //退出设置游戏区域步骤
  99. private void OnExitPlayAreaStep()
  100. {
  101. safetyPlaneMono.UnRegistPointerDownFillEvent();
  102. safetyPlaneMono.UnRegistPointerUpFillEvent();
  103. if (playAreaStateMachine != null)
  104. {
  105. playAreaStateMachine.ExitCurrentState();
  106. }
  107. }
  108. //进入原地区域步骤
  109. private void OnEnterStationAreaStep()
  110. {
  111. stationaryAreaUI.gameObject.SetActive(true);
  112. stationaryAreaUI.Init();
  113. CreateStationarySafetyArea();
  114. HidePlane();
  115. stationaryAreaUI.OnSwitchToPlayAreaClick += OnSwitchPlayAreaClick;
  116. stationaryAreaUI.OnCancelClick += OnStationaryAreaCancelClick;
  117. stationaryAreaUI.OnConfirmClick += OnStationaryAreaConfirmClick;
  118. }
  119. //退出原地区域步骤
  120. private void OnExitStationAreaStep()
  121. {
  122. stationaryAreaUI.OnSwitchToPlayAreaClick -= OnSwitchPlayAreaClick;
  123. stationaryAreaUI.OnCancelClick -= OnStationaryAreaCancelClick;
  124. stationaryAreaUI.OnConfirmClick -= OnStationaryAreaConfirmClick;
  125. stationaryAreaUI.Release();
  126. stationaryAreaUI.gameObject.SetActive(false);
  127. }
  128. //进入确定游戏区域步骤
  129. private void OnEnterConfirmAreaStep()
  130. {
  131. safetyPlaneMono.GenerateEdgeMesh((mesh) =>
  132. {
  133. CreateSafetyArea(mesh);
  134. });
  135. confirmPlayAreaUI.gameObject.SetActive(true);
  136. confirmPlayAreaUI.Init();
  137. confirmPlayAreaUI.OnConfirmClick += ExitSafetyAreaProcess;
  138. confirmPlayAreaUI.OnBackClick += OnConfirmPlayAreaCancel;
  139. }
  140. //退出确定游戏区域步骤
  141. private void OnExitConfirmAreaStep()
  142. {
  143. confirmPlayAreaUI.OnConfirmClick -= ExitSafetyAreaProcess;
  144. confirmPlayAreaUI.OnBackClick -= OnConfirmPlayAreaCancel;
  145. confirmPlayAreaUI.Release();
  146. confirmPlayAreaUI.gameObject.SetActive(false);
  147. }
  148. //切换游戏区域步骤的状态
  149. public void ChangePlayAreaState(PlayAreaStateEnum playAreaStateEnum, object data = null)
  150. {
  151. if (playAreaStateMachine != null)
  152. {
  153. playAreaStateMachine.ChangeState(playAreaStateEnum, data);
  154. }
  155. }
  156. //创建平面
  157. public void CreateSafetyPlane()
  158. {
  159. if (safetyPlaneMono == null)
  160. {
  161. safetyPlaneObject = new GameObject("SafetyPlane");
  162. safetyPlaneMono = safetyPlaneObject.AddComponent<SafetyPlaneMono>();
  163. safetyPlaneMono.Init();
  164. }
  165. }
  166. //重置平面高度
  167. public void ResetSafetyPlane()
  168. {
  169. safetyPlaneMono.ResetPlaneHeight();
  170. }
  171. //冻结平面高度
  172. public void FreezePlaneHeight()
  173. {
  174. if (safetyPlaneMono == null)
  175. {
  176. Debug.LogError("safetyPlaneMono is Null FreezePlaneHeight");
  177. return;
  178. }
  179. safetyPlaneMono.FreezePlaneHeight();
  180. }
  181. //允许平面高度变化
  182. public void UnFreezePlaneHeight()
  183. {
  184. if (safetyPlaneMono == null)
  185. {
  186. Debug.LogError("safetyPlaneMono is Null UnFreezePlaneHeight");
  187. return;
  188. }
  189. safetyPlaneMono.UnFreezePlaneHeight();
  190. }
  191. //暂时隐藏平面
  192. public void HidePlane()
  193. {
  194. safetyPlaneMono.gameObject.SetActive(false);
  195. }
  196. //显示平面
  197. public void ShowPlane()
  198. {
  199. safetyPlaneMono.gameObject.SetActive(true);
  200. }
  201. //清空平面颜色
  202. public void ClearPlaneColor()
  203. {
  204. safetyPlaneMono.ClearMeshColor();
  205. }
  206. //销毁平面
  207. private void DestroySafetyPlane()
  208. {
  209. if (safetyPlaneObject != null)
  210. {
  211. GameObject.Destroy(safetyPlaneObject);
  212. }
  213. safetyPlaneObject = null;
  214. safetyPlaneMono = null;
  215. }
  216. //创建原地区域
  217. private void CreateStationarySafetyArea()
  218. {
  219. if (stationaryAreaMono == null)
  220. {
  221. stationaryAreaObject = new GameObject(STATIONARY_NAME);
  222. stationaryAreaMono = stationaryAreaObject.AddComponent<StationaryAreaMono>();
  223. }
  224. }
  225. //销毁原地区域
  226. private void DestroyStationaryArea()
  227. {
  228. if (stationaryAreaObject != null)
  229. {
  230. GameObject.Destroy(stationaryAreaObject);
  231. }
  232. stationaryAreaObject = null;
  233. stationaryAreaMono = null;
  234. }
  235. private void FreezeStationarySafetyArea()
  236. {
  237. if (stationaryAreaMono == null)
  238. {
  239. Debug.LogError("stationaryAreaMono is Null FreezeStationarySafetyArea");
  240. }
  241. stationaryAreaMono.FreezeStationaryAreaPosition();
  242. }
  243. private void UnFreezeStationarySafetyArea()
  244. {
  245. if (stationaryAreaMono == null)
  246. {
  247. Debug.LogError("stationaryAreaMono is Null UnFreezeStationarySafetyArea");
  248. }
  249. stationaryAreaMono.UnFreezeStationaryAreaPosition();
  250. }
  251. //创建安全网格
  252. private void CreateSafetyArea(Mesh mesh)
  253. {
  254. GameObject safetyArea = GameObject.Find(PLAY_AREA_NAME);
  255. if (safetyArea != null)
  256. {
  257. GameObject.Destroy(safetyArea);
  258. }
  259. safetyArea = new GameObject(PLAY_AREA_NAME);
  260. PlayAreaMono playAreaMono = safetyArea.AddComponent<PlayAreaMono>();
  261. playAreaMono.SetMesh(mesh);
  262. }
  263. //切换到游戏区域步骤
  264. private void SwitchToPlayAreaStep()
  265. {
  266. SafetyAreaManager.Instance.ChangeStep(SafetyAreaStepEnum.PlayArea);
  267. }
  268. //确认安全区域的步骤点击取消
  269. private void OnConfirmPlayAreaCancel()
  270. {
  271. GameObject safetyArea = GameObject.Find(PLAY_AREA_NAME);
  272. if (safetyArea != null)
  273. {
  274. GameObject.Destroy(safetyArea);
  275. }
  276. SafetyAreaManager.Instance.ChangeStep(SafetyAreaStepEnum.PlayArea);
  277. }
  278. //原地区域切换游戏区域
  279. private void OnSwitchPlayAreaClick()
  280. {
  281. DestroyStationaryArea();
  282. SwitchToPlayAreaStep();
  283. }
  284. //原地区域确认点击
  285. private void OnStationaryAreaConfirmClick()
  286. {
  287. FreezeStationarySafetyArea();
  288. ExitSafetyAreaProcess();
  289. }
  290. //原地区域点击取消
  291. private void OnStationaryAreaCancelClick()
  292. {
  293. DestroyStationaryArea();
  294. ExitSafetyAreaProcess();
  295. }
  296. //退出设定安全区域流程
  297. private void ExitSafetyAreaProcess()
  298. {
  299. DestroySafetyPlane();
  300. SafetyAreaManager.Instance.ExitSafeAreaStep();
  301. }
  302. }