Main.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using TMPro;
  5. public class Main : MonoBehaviour
  6. {
  7. // ----------------------------------------------------------------------------------------------------------------
  8. #region properties
  9. [SerializeField] public float tileSize = 32f;
  10. [SerializeField] public float offset;
  11. [SerializeField] public float ppu = 100f;
  12. [Space]
  13. [SerializeField] public GameMapsAsset mapsAsset;
  14. public GameObject radiusBox;
  15. [Space]
  16. public float dragSpeed = 2.0f; // 拖动速度
  17. [HideInInspector] public Vector3 dragOrigin; // 拖动起始点
  18. [HideInInspector] public Transform TowerContainer;
  19. [HideInInspector] public Transform EnemyTowerContainer;
  20. [HideInInspector] public Transform EnemyContainer;
  21. [HideInInspector] public Transform BoxContainer;
  22. public Transform swimlaneContainer;
  23. public Transform timelineContainer;
  24. public TextMeshPro text;
  25. //import
  26. public ButtonTest buttonTest;
  27. #endregion
  28. // ----------------------------------------------------------------------------------------------------------------
  29. #region system
  30. private void Start()
  31. {
  32. //refreshHasOut(0);
  33. //LoadMap(0);
  34. }
  35. private void Update()
  36. {
  37. if (Input.GetKeyDown(KeyCode.R))
  38. {
  39. LoadMap(0);
  40. }
  41. if (Input.GetKeyDown(KeyCode.Space))
  42. {
  43. Camera.main.transform.localPosition = Vector3.zero;
  44. }
  45. if (Input.GetMouseButtonDown(2))
  46. {
  47. dragOrigin = Input.mousePosition;
  48. return;
  49. }
  50. if (Input.GetMouseButton(2))
  51. {
  52. Vector3 offset = Camera.main.ScreenToViewportPoint(dragOrigin - Input.mousePosition);
  53. Vector3 move = dragSpeed * offset.x * Vector3.right;
  54. Camera.main.transform.Translate(move, Space.World);
  55. dragOrigin = Input.mousePosition;
  56. }
  57. ShowMousePos();
  58. }
  59. public void ShowMousePos()
  60. {
  61. }
  62. public void refreshHasOut(int mapIdx)
  63. {
  64. GameMap map = mapsAsset.maps[mapIdx];
  65. for (int i = 0; i < map.LayerCount; i++)
  66. {
  67. int[] grid = map.GetlayerData(i);
  68. int idx = 0;
  69. for (int y = 0; y < map.height; y++)
  70. {
  71. for (int x = 0; x < map.width; x++)
  72. {
  73. GameMapTile t = mapsAsset.tileAsset.GetTile(grid[idx++]);
  74. if (t == null) continue;
  75. t.hasOut = false;
  76. t.index = -1;
  77. }
  78. }
  79. }
  80. }
  81. // an example of how map data could be loaded
  82. private void LoadMap(int mapIdx)
  83. {
  84. GameMap map = mapsAsset.maps[mapIdx];
  85. float sz = tileSize / ppu;
  86. float offsX = -((map.width / 2f * sz) - (sz / 2f));
  87. float offsY = -offset;
  88. // create containers for the various map objects
  89. if (!TowerContainer)
  90. {
  91. TowerContainer = new GameObject("Tower").transform;
  92. EnemyTowerContainer = new GameObject("EnemyTower").transform;
  93. EnemyContainer = new GameObject("Enemy").transform;
  94. BoxContainer = new GameObject("Box").transform;
  95. }
  96. else
  97. {
  98. for (int i= 0; i < TowerContainer.transform.childCount; i++)
  99. {
  100. Destroy(TowerContainer.transform.GetChild(i).gameObject);
  101. }
  102. for (int i = 0; i < EnemyTowerContainer.transform.childCount; i++)
  103. {
  104. Destroy(EnemyTowerContainer.transform.GetChild(i).gameObject);
  105. }
  106. for (int i = 0; i < EnemyContainer.transform.childCount; i++)
  107. {
  108. Destroy(EnemyContainer.transform.GetChild(i).gameObject);
  109. }
  110. for (int i = 0; i < BoxContainer.transform.childCount; i++)
  111. {
  112. Destroy(BoxContainer.transform.GetChild(i).gameObject);
  113. }
  114. for(int i = 0; i < swimlaneContainer.childCount; i++)
  115. {
  116. Destroy(swimlaneContainer.transform.GetChild(i).gameObject);
  117. Destroy(timelineContainer.transform.GetChild(i).gameObject);
  118. }
  119. }
  120. // place tiles and objects. GameMap supports laters but if you choose not to use them then you only need to read from map.grid[]
  121. // if you do use layers then you should also read data from map.layers[].grid[] while also still using map.grid[] as layer-0
  122. // to make it easier to read from these two sources of data you can simply use map.LayerCount and map.GetLayerData
  123. List<GameMapTile> ts = new();
  124. int startTime = 0;
  125. for (int i = 1; i < map.LayerCount; i++)
  126. {
  127. int[] grid = map.GetlayerData(i);
  128. int idx = 0;
  129. for (int y = 0; y < map.height; y++)
  130. {
  131. for (int x = 0; x < map.width; x++)
  132. {
  133. GameMapTile t = mapsAsset.tileAsset.GetTile(grid[idx++]);
  134. if (t == null) continue;
  135. SpriteRenderer ren = new GameObject($"{t.ch}").AddComponent<SpriteRenderer>();
  136. ren.sprite = t.sprite;
  137. switch (t.type)
  138. {
  139. case GameMapTile.Type.Tower:
  140. ren.transform.SetParent(TowerContainer, false);
  141. ren.transform.localScale = new Vector3(tileSize / ren.sprite.rect.width * 2, tileSize / ren.sprite.rect.height * 9, 1f);
  142. break;
  143. case GameMapTile.Type.Enemy:
  144. ren.transform.SetParent(EnemyContainer, false);
  145. ren.transform.localScale = new Vector3(tileSize / ren.sprite.rect.width, tileSize / ren.sprite.rect.height, 1f);
  146. break;
  147. }
  148. if (ren == null) continue;
  149. if (!t.hasOut)
  150. {
  151. t.hasOut = true;
  152. ts.Add(t);
  153. buttonTest.AddSwimLane(t);
  154. //foreach(SpawnTimeList spawnTimes in t.spawnTime)
  155. // {
  156. // if(spawnTimes.curLayer == map.layers[i - 1].name)
  157. // {
  158. // for (int j = 0; j < spawnTimes.spawnTimes.Length; j++)
  159. // {
  160. // buttonTest.AddEventToTimeline(spawnTimes.spawnTimes[j], t.index, t.color, startTime);
  161. // }
  162. // break;
  163. // }
  164. // }
  165. }
  166. ren.color = t.color;
  167. ren.transform.localPosition = new Vector3(x * sz + offsX, y * sz + offsY, 0f);
  168. Transform box = PoolManager.Instantiate(radiusBox).transform;
  169. box.SetParent(BoxContainer, false);
  170. box.GetComponent<SpriteRenderer>().color = t.color;
  171. box.localScale = new Vector3(t.radius.x * 0.4f, t.radius.y*0.4f, 1);
  172. box.position = ren.transform.position;
  173. }
  174. }
  175. startTime += map.layers[i - 1].duration;
  176. }
  177. for(int i = 0; i < ts.Count; i++)
  178. {
  179. ts[i].hasOut = false;
  180. ts[i].index = -1;
  181. }
  182. }
  183. #endregion
  184. // ----------------------------------------------------------------------------------------------------------------
  185. }