363 lines
10 KiB
C#
363 lines
10 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Spine;
|
|
using Spine.Unity;
|
|
using Unity.VisualScripting;
|
|
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<Grid> _boxGridList;
|
|
public int waitX => wx;
|
|
[SerializeField] private int wx;
|
|
public int waitY => wy;
|
|
[SerializeField] private int wy;
|
|
[SerializeField] private Transform waitGridParent;
|
|
[SerializeField] private List<Grid> _waitGridList;
|
|
[SerializeField] private SkeletonAnimation boxSpine;
|
|
public Transform lockParent=>unLockParent;
|
|
[SerializeField] private Transform unLockParent;
|
|
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<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;
|
|
}
|
|
boxSpine.state.Complete += delegate(TrackEntry track)
|
|
{
|
|
PlayBoxAniStop(track.Animation.Name);
|
|
};
|
|
ResetLockGrid();
|
|
}
|
|
|
|
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<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)
|
|
{
|
|
foreach (var data in _list)
|
|
{
|
|
if (boxGrid[grid.X + data.x, grid.Y + data.y].unLock)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 检测区域是否可以放置
|
|
/// </summary>
|
|
/// <param name="grid"></param>
|
|
/// <param name="itemObj"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="ArgumentOutOfRangeException"></exception>
|
|
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;
|
|
}
|
|
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;
|
|
}
|
|
|
|
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 检测点击位置
|
|
|
|
/// <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;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// <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;
|
|
}
|
|
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<UnlockData> unlockDatas)
|
|
{
|
|
|
|
}
|
|
/// <summary>
|
|
/// 行李箱动画
|
|
/// </summary>
|
|
/// <param name="action"></param>
|
|
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
|
|
|
|
} |