InteractionObjManager.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. //==========================================================
  2. //
  3. // Copyright (c) Guangzhou Shixiang Technology Co.,Ltd.
  4. // All rights reserved.
  5. //
  6. //==========================================================
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using GxrSdk;
  10. using UnityEngine;
  11. public class InteractionObjManager : MonoBehaviour
  12. {
  13. public static InteractionObjManager Instance { get; private set; }
  14. private Dictionary<int, GameObject> _dictionaryObj = null;
  15. private GameObject _Obj = null;
  16. public static Vector3 FirstV3 = new Vector3(-0.6f, 0.2f, 3);
  17. public static float IntervalX = 0.3f;
  18. public static float IntervalY = -0.28f;
  19. private int _rowNum = 2;
  20. private int _columnNum = 3;
  21. private int _curCreateIndex = 0;
  22. private void Awake()
  23. {
  24. Instance = this;
  25. }
  26. private void OnDestroy()
  27. {
  28. Instance = null;
  29. }
  30. void Start()
  31. {
  32. _dictionaryObj = new Dictionary<int, GameObject>();
  33. _Obj = Resources.Load("Prefabs/InteractionObj") as GameObject;
  34. if (_Obj == null)
  35. {
  36. GxrDebug.LogError("Cannot find Prefabs/InteractionObj.");
  37. }
  38. InitObjs();
  39. }
  40. private void InitObjs()
  41. {
  42. for (int i = 0; i < _rowNum; ++i)
  43. {
  44. for (int j = 0; j < _columnNum; ++j)
  45. {
  46. CreateObj(i, j);
  47. }
  48. }
  49. }
  50. public void DelayToCreateObj(int row, int column)
  51. {
  52. StartCoroutine(DelayToCreate(row, column));
  53. }
  54. private IEnumerator DelayToCreate(int row, int column)
  55. {
  56. yield return new WaitForSeconds(0.1f);
  57. CreateObj(row, column);
  58. }
  59. public void CreateObj(int row, int column)
  60. {
  61. if ((row < 0) || (column < 0))
  62. {
  63. return;
  64. }
  65. GameObject o = Instantiate(_Obj);
  66. o.transform.parent = transform;
  67. InteractionObj objCtrl = o.GetComponent<InteractionObj>();
  68. objCtrl.Init(row, column, _curCreateIndex);
  69. int key = _curCreateIndex;
  70. _dictionaryObj.Add(key, o);
  71. ++_curCreateIndex;
  72. }
  73. public void DestroyObj(InteractionObj obj)
  74. {
  75. GameObject o = null;
  76. int key = obj.Index;
  77. if (_dictionaryObj.TryGetValue(key, out o))
  78. {
  79. _dictionaryObj.Remove(key);
  80. o.transform.parent = null;
  81. Destroy(o);
  82. }
  83. }
  84. private int GetObjKey(int row, int column)
  85. {
  86. return (row * _columnNum + column);
  87. }
  88. public static Vector3 GetObjectLocalPosition(int row, int column)
  89. {
  90. float tmpX = FirstV3.x + IntervalX * column;
  91. float tmpY = FirstV3.y + IntervalY * row;
  92. return new Vector3(tmpX, tmpY, FirstV3.z);
  93. }
  94. public void ScaleAll(float scale)
  95. {
  96. float x = 1 + scale;
  97. Vector3 newScale = new Vector3(x, x, 1);
  98. transform.localScale = newScale;
  99. }
  100. public void RotateAll()
  101. {
  102. foreach (var item in _dictionaryObj)
  103. {
  104. InteractionObj objCtrl = item.Value.GetComponent<InteractionObj>();
  105. if (objCtrl != null)
  106. {
  107. objCtrl.RotateCube();
  108. }
  109. }
  110. }
  111. public void ChangeColorAll()
  112. {
  113. foreach (var item in _dictionaryObj)
  114. {
  115. InteractionObj objCtrl = item.Value.GetComponent<InteractionObj>();
  116. if (objCtrl != null)
  117. {
  118. objCtrl.ChangeColor();
  119. }
  120. }
  121. }
  122. public void LockColorAll(bool isLock)
  123. {
  124. foreach (var item in _dictionaryObj)
  125. {
  126. InteractionObj objCtrl = item.Value.GetComponent<InteractionObj>();
  127. if (objCtrl != null)
  128. {
  129. objCtrl.LockColor(isLock);
  130. }
  131. }
  132. }
  133. public void AddAll()
  134. {
  135. foreach (var item in _dictionaryObj)
  136. {
  137. InteractionObj objCtrl = item.Value.GetComponent<InteractionObj>();
  138. if (objCtrl != null)
  139. {
  140. objCtrl.Add();
  141. }
  142. }
  143. }
  144. public void SubAll()
  145. {
  146. foreach (var item in _dictionaryObj)
  147. {
  148. InteractionObj objCtrl = item.Value.GetComponent<InteractionObj>();
  149. if (objCtrl != null)
  150. {
  151. objCtrl.Sub();
  152. }
  153. }
  154. }
  155. /// <summary>
  156. /// 松开时查找离得最近的对象,如果其距离小于0.1则交换位置,否则回到原位置
  157. /// </summary>
  158. /// <param name="obj"></param>
  159. public void DropObj(InteractionObj dropObj)
  160. {
  161. float maxDis = 1000;
  162. InteractionObj nearestObj = null;
  163. int dropRow = dropObj.Row;
  164. int dropColumn = dropObj.Column;
  165. foreach (var item in _dictionaryObj)
  166. {
  167. InteractionObj objCtrl = item.Value.GetComponent<InteractionObj>();
  168. if ((objCtrl != null) && ((objCtrl.Row != dropRow) || (objCtrl.Column != dropColumn)))
  169. {
  170. float distance = Vector3.Distance(dropObj.transform.localPosition, objCtrl.transform.localPosition);
  171. if (distance < maxDis)
  172. {
  173. maxDis = distance;
  174. nearestObj = objCtrl;
  175. }
  176. }
  177. }
  178. if ((maxDis < 0.1f) && !nearestObj.IsMoving && !nearestObj.IsMoving)
  179. {
  180. if (nearestObj.Number == dropObj.Number)
  181. {
  182. DestroyObj(dropObj);
  183. DelayToCreateObj(dropRow, dropColumn);
  184. DestroyObj(nearestObj);
  185. DelayToCreateObj(nearestObj.Row, nearestObj.Column);
  186. }
  187. else
  188. {
  189. dropObj.SwapPosition(nearestObj.Row, nearestObj.Column);
  190. nearestObj.SwapPosition(dropRow, dropColumn);
  191. }
  192. }
  193. else
  194. {
  195. dropObj.SwapPosition(dropRow, dropColumn);
  196. }
  197. }
  198. }