using System; using System.Collections; using System.Collections.Generic; using Spine; using Spine.Unity; using UnityEngine; public class MapManager : MonoBehaviour { public static MapManager ins; [SerializeField] private int bx; [SerializeField] private int by; [SerializeField] private float spacing=1; [SerializeField] private GameObject gridObj; [SerializeField] private Transform boxGridParent; [SerializeField] private List _boxGridList; [SerializeField] private int wx; [SerializeField] private int wy; [SerializeField] private Transform waitGridParent; [SerializeField] private List _waitGridList; [SerializeField] private SkeletonAnimation boxSpine; public Action boxani; public Grid[,] boxGrid; public Grid[,] WaitGrids; #if UNITY_EDITOR [InspectorButton("生成地图")] #endif private void InsMap() { for (var i = 0; i < bx; i++) { for (var j = 0; j < by; j++) { var obj = Instantiate(gridObj, boxGridParent); obj.transform.localPosition = new Vector3(i, j, 0); obj.GetComponent().SetData(i,j); obj.GetComponent().Type=GridType.box; _boxGridList.Add(obj.GetComponent()); } } for (var i = 0; i < wx; i++) { for (var j = 0; j < wy; j++) { var obj = Instantiate(gridObj, waitGridParent); obj.transform.localPosition = new Vector3(i, j, 0); obj.GetComponent().SetData(i,j); obj.GetComponent().Type=GridType.wait; _waitGridList.Add(obj.GetComponent()); } } } private void Awake() { ins = this; boxGrid = new Grid[bx, by]; foreach (var grid in _boxGridList) { boxGrid[grid.X, grid.Y] = grid; } WaitGrids = new Grid[wx, wy]; foreach (var grid in _waitGridList) { WaitGrids[grid.X, grid.Y] = grid; } boxSpine.state.Complete += delegate(TrackEntry track) { PlayBoxAniStop(track.Animation.Name); }; } /// /// 检测区域是否可以放置 /// /// /// /// /// public bool openSave(Grid grid,ItemObj itemObj) { switch (grid.Type) { case GridType.box: if (grid.X+itemObj.itemSize.x>bx) { return false; } if (grid.Y+itemObj.itemSize.y>by) { return false; } for (var i = grid.X; i < grid.X+itemObj.itemSize.x; i++) { for (var j = grid.Y; j < grid.Y+itemObj.itemSize.y; j++) { if (boxGrid[i,j].item!=itemObj&&boxGrid[i,j].item!=null) { return false; } } } return true; break; case GridType.wait: if (grid.X+itemObj.itemSize.x>wx) { return false; } if (grid.Y+itemObj.itemSize.y>wy) { return false; } for (int i = grid.X; i < grid.X+itemObj.itemSize.x; i++) { for (int j = grid.Y; j < grid.Y+itemObj.itemSize.y; j++) { if (WaitGrids[i,j].itemObj() !=null) { return false; } } } return true; break; default: throw new ArgumentOutOfRangeException(); } } #region 检测点击位置 /// /// 检测点击区域是否在范围内 /// /// /// /// public bool GetRegionGridType(Vector3 worldPosition ,out GridType type) { type = GridType.box; if (worldPosition.x > boxGridParent.position.x-(spacing/2) && worldPosition.xboxGridParent.position.y-(spacing/2) && worldPosition.y < boxGridParent.position.y +(by * spacing)-(spacing/2)) { return true; } if (worldPosition.x>waitGridParent.position.x-(spacing/2)&& worldPosition.xwaitGridParent.position.y-(spacing/2) && worldPosition.y < waitGridParent.position.y +(wy * spacing)-(spacing/2)) { type = GridType.wait; return true; } return false; } /// /// 获取坐标点区块 /// /// /// /// /// public void RegionGrid(Vector3 point,GridType type ,out Grid grid) { grid = null; point = type switch { GridType.box => point - boxGridParent.position - new Vector3(-0.5f, -0.5f, 0), GridType.wait => point - waitGridParent.position - new Vector3(-0.5f, -0.5f, 0), _ => throw new ArgumentOutOfRangeException(nameof(type), type, null) }; if (point.x<0 || point.y<0) { return; } var indexX = (int)(point.x / spacing); var indexY =(int) (point.y / spacing); switch( type) { case GridType.box: if (indexX>=0&&indexX=0&&indexY=0&&indexX=0&&indexY /// 重置动画状态 /// public void ResetAnimation() { foreach (var grid in boxGrid) { grid.PlayEnterAnimation(); } } /// /// 点击区域动画播放 /// /// /// /// /// public void RegionGridBack(Grid start,int x,int y) { switch (start.Type) { case GridType.box: foreach (var grid in boxGrid) { grid.PlayEnterAnimation(); } if (x>bx||y>by) { return; } for (int i = start.X; i < x; i++) { for (int j = start.Y; j < y; j++) { boxGrid[i, j].PlayExitAnimation(); } } break; case GridType.wait: break; default: throw new ArgumentOutOfRangeException(); } } public void PlayBoxOpen(Action action=null) { boxGridParent.gameObject.SetActive(false); boxani = action; boxSpine.state.SetAnimation(0, "play_1", false); } public void PlayBoxClose(Action action=null) { boxGridParent.gameObject.SetActive(false); boxani = action; boxSpine.state.SetAnimation(0, "play_2", false); } private void PlayBoxAniStop(string aniName) { if (aniName=="play_1") { boxGridParent.gameObject.SetActive(true); } if (boxani != null) { boxani.Invoke(); boxani = null; } } #endregion }