WXGame/box1/Assets/Script/Map/MapManager.cs
2024-10-25 18:09:59 +08:00

121 lines
3.5 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MapManager : MonoBehaviour
{
public static MapManager ins;
[SerializeField] private int bx;
[SerializeField] private int by;
[SerializeField] private GameObject gridObj;
[SerializeField] private Transform boxGridParent;
[SerializeField] private List<Grid> _boxGridList;
[SerializeField] private int wx;
[SerializeField] private int wy;
[SerializeField] private Transform waitGridParent;
[SerializeField] private List<Grid> _waitGridList;
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;
}
}
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!=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].item!=null)
{
return false;
}
}
}
return true;
break;
default:
throw new ArgumentOutOfRangeException();
}
return false;
}
}