WXGame/box1/Assets/Script/Map/MapManager.cs

511 lines
14 KiB
C#
Raw Permalink Normal View History

2024-10-23 17:55:55 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
2024-10-29 15:22:32 +08:00
using Spine;
using Spine.Unity;
2024-11-08 18:21:41 +08:00
// using Unity.VisualScripting;
2024-10-23 17:55:55 +08:00
using UnityEngine;
2024-11-26 15:44:50 +08:00
using Random = UnityEngine.Random;
2024-10-23 17:55:55 +08:00
public class MapManager : MonoBehaviour
{
public static MapManager ins;
[SerializeField] private int bx;
[SerializeField] private int by;
2024-10-29 15:22:32 +08:00
[SerializeField] private float spacing=1;
2024-10-23 17:55:55 +08:00
[SerializeField] private GameObject gridObj;
[SerializeField] private Transform boxGridParent;
[SerializeField] private List<Grid> _boxGridList;
2024-11-04 17:29:11 +08:00
public int waitX => wx;
2024-10-23 17:55:55 +08:00
[SerializeField] private int wx;
2024-11-04 17:29:11 +08:00
public int waitY => wy;
2024-10-23 17:55:55 +08:00
[SerializeField] private int wy;
[SerializeField] private Transform waitGridParent;
2024-11-05 18:15:49 +08:00
public List<Grid> WaitList => _waitGridList;
2024-10-23 17:55:55 +08:00
[SerializeField] private List<Grid> _waitGridList;
2024-10-29 15:22:32 +08:00
[SerializeField] private SkeletonAnimation boxSpine;
public Transform lockParent=>unLockParent;
[SerializeField] private Transform unLockParent;
2024-11-26 15:44:50 +08:00
[SerializeField] private Transform unlockPool;
[SerializeField] private UnLockGrid randomLockGrid;
[SerializeField] private List<UnLockGrid> _unLockGrids;
2024-11-29 21:37:01 +08:00
public int _unLockIndex=0;
2024-10-29 15:22:32 +08:00
public Action boxani;
2024-10-23 17:55:55 +08:00
public Grid[,] boxGrid;
public Grid[,] WaitGrids;
2024-11-29 21:37:01 +08:00
public void InitLockData()
{
_unLockIndex = 0;
foreach (var variaGrid in _unLockGrids)
{
variaGrid.gameObject.SetActive(true);
}
2024-11-29 21:37:01 +08:00
}
public void LockAddData()
{
_unLockIndex++;
}
2024-12-13 20:38:15 +08:00
#if UNITY_EDITOR
[InspectorButton("生成地图")]
#endif
2024-10-23 17:55:55 +08:00
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<Grid>().SetData(i,j);
obj.GetComponent<Grid>().Type=GridType.box;
_boxGridList.Add(obj.GetComponent<Grid>());
}
}
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<Grid>().SetData(i,j);
obj.GetComponent<Grid>().Type=GridType.wait;
_waitGridList.Add(obj.GetComponent<Grid>());
}
}
}
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;
}
2024-10-29 15:22:32 +08:00
boxSpine.state.Complete += delegate(TrackEntry track)
{
PlayBoxAniStop(track.Animation.Name);
};
ResetLockGrid();
}
2024-11-26 15:44:50 +08:00
public void ResetUnlock()
{
foreach (var variaGrid in _unLockGrids)
{
variaGrid.transform.localPosition = Vector3.zero;
}
randomLockGrid.transform.position = unLockParent.position;
}
2024-12-18 11:12:52 +08:00
public void CloseUnlock()
{
foreach (var variaGrid in _unLockGrids)
{
variaGrid.gameObject.SetActive(false);
}
MainPanel.ins.ResetPackLockButtonRest();
}
public bool Lock()
{
return (_unLockIndex == 0|| GameManager.ins.VideoBo)&&_unLockIndex<10;
}
2024-11-26 15:44:50 +08:00
public void RandomUnlock()
{
if (_unLockIndex>=10)
{
foreach (var variaGrid in _unLockGrids)
{
variaGrid.gameObject.SetActive(false);
}
2024-12-18 11:12:52 +08:00
MainPanel.ins.ResetPackLockButtonRest();
return;
}
2024-11-29 21:37:01 +08:00
foreach (var variaGrid in _unLockGrids)
2024-12-18 11:12:52 +08:00
{
variaGrid.gameObject.SetActive(true);
}
foreach (var variaGrid in _unLockGrids)
2024-11-29 21:37:01 +08:00
{
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
2024-11-26 15:44:50 +08:00
{
2024-11-29 21:37:01 +08:00
List<UnLockGrid> lockGrids = new List<UnLockGrid>();
for (int i = 0; i < _unLockGrids.Count; i++)
{
if (_unLockGrids[i].openData.Count ==2)
2024-11-29 21:37:01 +08:00
{
lockGrids.Add(_unLockGrids[i]);
}
}
int index = Random.Range(0, lockGrids.Count);
randomLockGrid = lockGrids[index];
2024-11-26 15:44:50 +08:00
}
randomLockGrid.transform.position = unLockParent.position;
2024-12-18 11:12:52 +08:00
MainPanel.ins.ResetPackLockButtonRest();
2024-11-26 15:44:50 +08:00
}
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);
}
}
}
2024-11-04 17:29:11 +08:00
public void RayToutchGrid(Grid grid,List<UnlockData> _list)
{
foreach (var box in _boxGridList)
{
box.ResetBack(0);
}
foreach (var data in _list)
{
if (grid.X + data.x<bx&&grid.Y + data.y<by)
{
boxGrid[grid.X + data.x, grid.Y + data.y].ResetBack(1);
}
}
}
public void ResetRayLockGrid()
{
foreach (var box in _boxGridList)
{
box.ResetBack(2);
}
}
public bool UnLockOpen(Grid grid,List<UnlockData> _list)
{
2024-11-26 15:44:50 +08:00
if (grid==null)
{
return false;
}
foreach (var data in _list)
{
2024-11-26 15:44:50 +08:00
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;
}
}
2024-11-26 15:44:50 +08:00
var lockNum=0;
foreach (var data in _list)
{
var x = grid.X + data.x;
var y = grid.Y + data.y;
if (x+1<bx)
{
if (boxGrid[x+1,y].unLock)
{
lockNum++;
}
}
if (x-1>=0)
{
if (boxGrid[x-1,y].unLock)
{
lockNum++;
}
}
if (y+1<by)
{
if (boxGrid[x,y+1].unLock)
{
lockNum++;
}
}
if (y-1>=0)
{
if (boxGrid[x,y-1].unLock)
{
lockNum++;
}
}
}
return lockNum != 0;
}
public void LockRayRest(Grid grid ,List<UnlockData> _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;
}
2024-10-23 17:55:55 +08:00
}
2024-10-29 15:22:32 +08:00
/// <summary>
/// 检测区域是否可以放置
/// </summary>
/// <param name="grid"></param>
/// <param name="itemObj"></param>
/// <returns></returns>
/// <exception cref="ArgumentOutOfRangeException"></exception>
2024-10-23 17:55:55 +08:00
public bool openSave(Grid grid,ItemObj itemObj)
{
2024-11-26 15:44:50 +08:00
if (grid==null)
{
return false;
}
2024-10-23 17:55:55 +08:00
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++)
{
2024-10-29 15:22:32 +08:00
if (boxGrid[i,j].item!=itemObj&&boxGrid[i,j].item!=null)
2024-10-23 17:55:55 +08:00
{
return false;
}
if (!boxGrid[i,j].unLock)
{
return false;
}
2024-10-23 17:55:55 +08:00
}
}
return true;
break;
case GridType.wait:
if (grid.X+itemObj.itemSize.x>wx)
{
return false;
}
if (grid.Y+itemObj.itemSize.y>wy)
{
return false;
}
2024-10-23 17:55:55 +08:00
for (int i = grid.X; i < grid.X+itemObj.itemSize.x; i++)
{
for (int j = grid.Y; j < grid.Y+itemObj.itemSize.y; j++)
{
2024-10-29 15:22:32 +08:00
if (WaitGrids[i,j].itemObj() !=null)
2024-10-23 17:55:55 +08:00
{
return false;
}
}
}
return true;
break;
default:
throw new ArgumentOutOfRangeException();
}
2024-10-29 15:22:32 +08:00
}
#region
2024-10-23 17:55:55 +08:00
2024-10-29 15:22:32 +08:00
/// <summary>
/// 检测点击区域是否在范围内
/// </summary>
/// <param name="worldPosition"></param>
/// <param name="type"></param>
/// <returns></returns>
public bool GetRegionGridType(Vector3 worldPosition ,out GridType type)
{
type = GridType.box;
if (worldPosition.x > boxGridParent.position.x-(spacing/2) && worldPosition.x<boxGridParent.position.x+(bx*spacing)-(spacing/2) && worldPosition.y>boxGridParent.position.y-(spacing/2) && worldPosition.y < boxGridParent.position.y +(by * spacing)-(spacing/2))
{
return true;
}
if (worldPosition.x>waitGridParent.position.x-(spacing/2)&& worldPosition.x<waitGridParent.position.x+(wx*spacing)-(spacing/2) && worldPosition.y>waitGridParent.position.y-(spacing/2) && worldPosition.y < waitGridParent.position.y +(wy * spacing)-(spacing/2))
{
type = GridType.wait;
return true;
}
2024-10-23 17:55:55 +08:00
return false;
}
2024-10-29 15:22:32 +08:00
/// <summary>
/// 获取坐标点区块
/// </summary>
/// <param name="point"></param>
/// <param name="type"></param>
/// <param name="grid"></param>
/// <exception cref="ArgumentOutOfRangeException"></exception>
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<bx&& indexY>=0&&indexY<by)
{
grid = boxGrid[indexX, indexY];
}
break;
case GridType.wait:
if (indexX>=0&&indexX<wx&& indexY>=0&&indexY<wy)
{
grid = WaitGrids[indexX, indexY];
}
break;
default:
throw new ArgumentOutOfRangeException(nameof(type), type, null);
}
}
#endregion
#region
/// <summary>
/// 重置动画状态
/// </summary>
public void ResetAnimation()
{
foreach (var grid in boxGrid)
{
grid.PlayEnterAnimation();
}
}
/// <summary>
/// 点击区域动画播放
/// </summary>
/// <param name="start"></param>
/// <param name="x"></param>
/// <param name="y"></param>
/// <exception cref="ArgumentOutOfRangeException"></exception>
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;
}
2024-11-29 21:37:01 +08:00
boxGrid[start.X,start.Y].PlayExitAnimation();
2024-10-29 15:22:32 +08:00
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();
}
}
2024-11-04 17:29:11 +08:00
public void UnLockBackRest(Grid start,List<UnlockData> unlockDatas)
{
}
/// <summary>
/// 行李箱动画
/// </summary>
/// <param name="action"></param>
2024-10-29 15:22:32 +08:00
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
}