using System; using System.Collections; using System.Collections.Generic; using Spine; using Spine.Unity; // using Unity.VisualScripting; using UnityEngine; using Random = UnityEngine.Random; 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; public int waitX => wx; [SerializeField] private int wx; public int waitY => wy; [SerializeField] private int wy; [SerializeField] private Transform waitGridParent; public List WaitList => _waitGridList; [SerializeField] private List _waitGridList; [SerializeField] private SkeletonAnimation boxSpine; public Transform lockParent=>unLockParent; [SerializeField] private Transform unLockParent; [SerializeField] private Transform unlockPool; [SerializeField] private UnLockGrid randomLockGrid; [SerializeField] private List _unLockGrids; public int _unLockIndex=0; public Action boxani; public Grid[,] boxGrid; public Grid[,] WaitGrids; #if UNITY_EDITOR [InspectorButton("生成地图")] #endif public void InitLockData() { _unLockIndex = 0; } public void LockAddData() { _unLockIndex++; } 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); }; ResetLockGrid(); } public void RandomUnlock() { foreach (var variaGrid in _unLockGrids) { variaGrid.transform.localPosition = Vector3.zero; } if (_unLockIndex==0) { for (int i = 0; i < _unLockGrids.Count; i++) { if (_unLockGrids[i].openData.Count==1) { randomLockGrid = _unLockGrids[i]; break; } } } else { List lockGrids = new List(); for (int i = 0; i < _unLockGrids.Count; i++) { if (_unLockGrids[i].openData.Count<=2) { lockGrids.Add(_unLockGrids[i]); } } int index = Random.Range(0, lockGrids.Count); randomLockGrid = lockGrids[index]; } randomLockGrid.transform.position = unLockParent.position; } public void ResetLockGrid() { foreach (var grid in _boxGridList) { boxGrid[grid.X, grid.Y].SetLock(false); } for (int i = 3; i < 6; i++) { for (int j = 1; j < 4; j++) { boxGrid[i,j].SetLock(true); } } } public void RayToutchGrid(Grid grid,List _list) { foreach (var box in _boxGridList) { box.ResetBack(0); } foreach (var data in _list) { if (grid.X + data.x _list) { if (grid==null) { return false; } foreach (var data in _list) { if (grid.X + data.x>=bx) { return false; } if (grid.Y + data.y>=by) { return false; } if (boxGrid[grid.X + data.x, grid.Y + data.y]==null) { return false; } if (boxGrid[grid.X + data.x, grid.Y + data.y].unLock) { return false; } } var lockNum=0; foreach (var data in _list) { var x = grid.X + data.x; var y = grid.Y + data.y; if (x+1=0) { if (boxGrid[x-1,y].unLock) { lockNum++; } } if (y+1=0) { if (boxGrid[x,y-1].unLock) { lockNum++; } } } return lockNum != 0; } public void LockRayRest(Grid grid ,List _list,out bool bo) { bo = false; if (UnLockOpen(grid,_list)) { foreach (var data in _list) { boxGrid[grid.X + data.x, grid.Y + data.y].SetLock(true); } bo = true; } } /// /// 检测区域是否可以放置 /// /// /// /// /// public bool openSave(Grid grid,ItemObj itemObj) { if (grid==null) { return false; } 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; } if (!boxGrid[i,j].unLock) { 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; } boxGrid[start.X,start.Y].PlayExitAnimation(); 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 UnLockBackRest(Grid start,List unlockDatas) { } /// /// 行李箱动画 /// /// 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 }