| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using TMPro;
- public class Main : MonoBehaviour
- {
- // ----------------------------------------------------------------------------------------------------------------
- #region properties
- [SerializeField] public float tileSize = 32f;
- [SerializeField] public float offset;
- [SerializeField] public float ppu = 100f;
- [Space]
- [SerializeField] public GameMapsAsset mapsAsset;
- public GameObject radiusBox;
- [Space]
- public float dragSpeed = 2.0f; // 拖动速度
- [HideInInspector] public Vector3 dragOrigin; // 拖动起始点
- [HideInInspector] public Transform TowerContainer;
- [HideInInspector] public Transform EnemyTowerContainer;
- [HideInInspector] public Transform EnemyContainer;
- [HideInInspector] public Transform BoxContainer;
- public Transform swimlaneContainer;
- public Transform timelineContainer;
- public TextMeshPro text;
- //import
- public ButtonTest buttonTest;
- #endregion
- // ----------------------------------------------------------------------------------------------------------------
- #region system
- private void Start()
- {
- //refreshHasOut(0);
- //LoadMap(0);
- }
- private void Update()
- {
- if (Input.GetKeyDown(KeyCode.R))
- {
- LoadMap(0);
- }
- if (Input.GetKeyDown(KeyCode.Space))
- {
- Camera.main.transform.localPosition = Vector3.zero;
- }
- if (Input.GetMouseButtonDown(2))
- {
- dragOrigin = Input.mousePosition;
- return;
- }
- if (Input.GetMouseButton(2))
- {
- Vector3 offset = Camera.main.ScreenToViewportPoint(dragOrigin - Input.mousePosition);
- Vector3 move = dragSpeed * offset.x * Vector3.right;
- Camera.main.transform.Translate(move, Space.World);
- dragOrigin = Input.mousePosition;
- }
- ShowMousePos();
- }
- public void ShowMousePos()
- {
- }
- public void refreshHasOut(int mapIdx)
- {
- GameMap map = mapsAsset.maps[mapIdx];
- for (int i = 0; i < map.LayerCount; i++)
- {
- int[] grid = map.GetlayerData(i);
- int idx = 0;
- for (int y = 0; y < map.height; y++)
- {
- for (int x = 0; x < map.width; x++)
- {
- GameMapTile t = mapsAsset.tileAsset.GetTile(grid[idx++]);
- if (t == null) continue;
- t.hasOut = false;
- t.index = -1;
- }
- }
- }
- }
- // an example of how map data could be loaded
- private void LoadMap(int mapIdx)
- {
- GameMap map = mapsAsset.maps[mapIdx];
- float sz = tileSize / ppu;
- float offsX = -((map.width / 2f * sz) - (sz / 2f));
- float offsY = -offset;
- // create containers for the various map objects
- if (!TowerContainer)
- {
- TowerContainer = new GameObject("Tower").transform;
- EnemyTowerContainer = new GameObject("EnemyTower").transform;
- EnemyContainer = new GameObject("Enemy").transform;
- BoxContainer = new GameObject("Box").transform;
- }
- else
- {
- for (int i= 0; i < TowerContainer.transform.childCount; i++)
- {
- Destroy(TowerContainer.transform.GetChild(i).gameObject);
- }
- for (int i = 0; i < EnemyTowerContainer.transform.childCount; i++)
- {
- Destroy(EnemyTowerContainer.transform.GetChild(i).gameObject);
- }
- for (int i = 0; i < EnemyContainer.transform.childCount; i++)
- {
- Destroy(EnemyContainer.transform.GetChild(i).gameObject);
- }
- for (int i = 0; i < BoxContainer.transform.childCount; i++)
- {
- Destroy(BoxContainer.transform.GetChild(i).gameObject);
- }
- for(int i = 0; i < swimlaneContainer.childCount; i++)
- {
- Destroy(swimlaneContainer.transform.GetChild(i).gameObject);
- Destroy(timelineContainer.transform.GetChild(i).gameObject);
- }
- }
- // place tiles and objects. GameMap supports laters but if you choose not to use them then you only need to read from map.grid[]
- // if you do use layers then you should also read data from map.layers[].grid[] while also still using map.grid[] as layer-0
- // to make it easier to read from these two sources of data you can simply use map.LayerCount and map.GetLayerData
- List<GameMapTile> ts = new();
- int startTime = 0;
- for (int i = 1; i < map.LayerCount; i++)
- {
- int[] grid = map.GetlayerData(i);
- int idx = 0;
- for (int y = 0; y < map.height; y++)
- {
- for (int x = 0; x < map.width; x++)
- {
- GameMapTile t = mapsAsset.tileAsset.GetTile(grid[idx++]);
- if (t == null) continue;
- SpriteRenderer ren = new GameObject($"{t.ch}").AddComponent<SpriteRenderer>();
- ren.sprite = t.sprite;
- switch (t.type)
- {
- case GameMapTile.Type.Tower:
- ren.transform.SetParent(TowerContainer, false);
- ren.transform.localScale = new Vector3(tileSize / ren.sprite.rect.width * 2, tileSize / ren.sprite.rect.height * 9, 1f);
- break;
- case GameMapTile.Type.Enemy:
- ren.transform.SetParent(EnemyContainer, false);
- ren.transform.localScale = new Vector3(tileSize / ren.sprite.rect.width, tileSize / ren.sprite.rect.height, 1f);
- break;
- }
- if (ren == null) continue;
- if (!t.hasOut)
- {
- t.hasOut = true;
- ts.Add(t);
- buttonTest.AddSwimLane(t);
- //foreach(SpawnTimeList spawnTimes in t.spawnTime)
- // {
- // if(spawnTimes.curLayer == map.layers[i - 1].name)
- // {
- // for (int j = 0; j < spawnTimes.spawnTimes.Length; j++)
- // {
- // buttonTest.AddEventToTimeline(spawnTimes.spawnTimes[j], t.index, t.color, startTime);
- // }
- // break;
- // }
- // }
- }
- ren.color = t.color;
- ren.transform.localPosition = new Vector3(x * sz + offsX, y * sz + offsY, 0f);
- Transform box = PoolManager.Instantiate(radiusBox).transform;
- box.SetParent(BoxContainer, false);
- box.GetComponent<SpriteRenderer>().color = t.color;
- box.localScale = new Vector3(t.radius.x * 0.4f, t.radius.y*0.4f, 1);
- box.position = ren.transform.position;
- }
- }
- startTime += map.layers[i - 1].duration;
- }
- for(int i = 0; i < ts.Count; i++)
- {
- ts[i].hasOut = false;
- ts[i].index = -1;
- }
- }
- #endregion
- // ----------------------------------------------------------------------------------------------------------------
- }
|