using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Serialization; using Random = UnityEngine.Random; namespace Script.Map { //地图总控制脚本 public class MapManager : MonoBehaviour { public static MapManager ins; [SerializeField] private GameObject carObj; [SerializeField] private Transform carParent; [SerializeField] public Transform startCar; [SerializeField] public Transform waitCar; [SerializeField] public Transform endCar; public CarObject seleCar => seleCarObject; /// /// 当前选中车辆 /// [SerializeField] public CarObject seleCarObject; /// /// 总车辆节点 /// [SerializeField] public List carObjects; /// /// 标准格子预制体 /// [SerializeField] private GameObject templateGrid; /// /// 等待节点 /// public WaitSlot WaitSlot => _waitSlot; [SerializeField] private WaitSlot _waitSlot; /// /// 地图格子 /// public Grid[,] MapGrid ; /// /// 缓存格子 /// [SerializeField] private List MapGrids; /// /// 地图格子生成节点 /// [SerializeField] private Transform gridParent; public int x => mX; [SerializeField] private int mX; public int y => mY; [SerializeField] private int mY; public bool InitReady { get; private set; } = false; private void Awake() { ins = this; } /// /// 初始化游戏数据 /// public void InitPlayData() { MapGrid = new Grid[x, y]; foreach (var grid in MapGrids) { MapGrid[grid.X, grid.Y] = grid; } for (int i = 0; i < 10; i++) { var car = Instantiate(carObj, carParent); car.transform.position = startCar.position; var colorIndex =Random.Range(0, 3); carObjects.Add(car.GetComponent()); car.GetComponent().movePostion = startCar; car.GetComponent().carColor = (ColorEnum)colorIndex; } carObjects[0].movePostion = waitCar; seleCarObject = carObjects[0]; seleCarObject.CarAni(); } [InspectorButton("初始化地图")] public void InitMap() { if (ins==null) { ins = this; } Init(10, 10); } /// /// 地图生成调用 /// /// /// public void Init(int _x,int _y) { InitReady = false; mX = _x; mY = _y; MapGrid = new Grid[x, y]; InitGrid(); } private void InitGrid(Action action=null) { for (var i = 0; i < x; i++) { for (var j = 0; j < y; j++) { MapGrid[i, j] = Instantiate(templateGrid,gridParent).GetComponent(); MapGrid[i,j].SetData(i,j); MapGrid[i,j].SetBool(true); MapGrid[i, j].SetRestPostion(); MapGrids.Add(MapGrid[i,j]); } } foreach (var grid in MapGrid) { grid.SetAdjacentGrid(); } action?.Invoke(); InitReady = true; } public void RestGrid(int _gx,int _gy,bool bo) { MapGrid[_gx,_gy].SetBool(bo); } } }