137 lines
4.0 KiB
C#
137 lines
4.0 KiB
C#
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;
|
|
/// <summary>
|
|
/// 当前选中车辆
|
|
/// </summary>
|
|
[SerializeField] public CarObject seleCarObject;
|
|
/// <summary>
|
|
/// 总车辆节点
|
|
/// </summary>
|
|
[SerializeField] public List<CarObject> carObjects;
|
|
/// <summary>
|
|
/// 标准格子预制体
|
|
/// </summary>
|
|
[SerializeField] private GameObject templateGrid;
|
|
/// <summary>
|
|
/// 等待节点
|
|
/// </summary>
|
|
public WaitSlot WaitSlot => _waitSlot;
|
|
[SerializeField] private WaitSlot _waitSlot;
|
|
/// <summary>
|
|
/// 地图格子
|
|
/// </summary>
|
|
public Grid[,] MapGrid ;
|
|
/// <summary>
|
|
/// 缓存格子
|
|
/// </summary>
|
|
[SerializeField] private List<Grid> MapGrids;
|
|
/// <summary>
|
|
/// 地图格子生成节点
|
|
/// </summary>
|
|
[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;
|
|
}
|
|
/// <summary>
|
|
/// 初始化游戏数据
|
|
/// </summary>
|
|
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<CarObject>());
|
|
car.GetComponent<CarObject>().movePostion = startCar;
|
|
car.GetComponent<CarObject>().carColor = (ColorEnum)colorIndex;
|
|
}
|
|
|
|
carObjects[0].movePostion = waitCar;
|
|
seleCarObject = carObjects[0];
|
|
seleCarObject.CarAni();
|
|
}
|
|
|
|
|
|
[InspectorButton("初始化地图")]
|
|
public void InitMap()
|
|
{
|
|
if (ins==null)
|
|
{
|
|
ins = this;
|
|
}
|
|
Init(10, 10);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 地图生成调用
|
|
/// </summary>
|
|
/// <param name="最大纵向列数x"></param>
|
|
/// <param name="最大横向行数y"></param>
|
|
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<Grid>();
|
|
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);
|
|
}
|
|
}
|
|
} |