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

137 lines
4.0 KiB
C#
Raw Permalink Normal View History

2024-10-22 09:17:28 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Serialization;
2024-10-23 09:12:15 +08:00
using Random = UnityEngine.Random;
2024-10-22 09:17:28 +08:00
namespace Script.Map
{
2024-10-22 11:31:23 +08:00
//地图总控制脚本
2024-10-22 09:17:28 +08:00
public class MapManager : MonoBehaviour
{
public static MapManager ins;
2024-10-23 09:12:15 +08:00
[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>
2024-10-22 09:17:28 +08:00
[SerializeField] private GameObject templateGrid;
2024-10-23 09:12:15 +08:00
/// <summary>
/// 等待节点
/// </summary>
2024-10-22 11:31:23 +08:00
public WaitSlot WaitSlot => _waitSlot;
[SerializeField] private WaitSlot _waitSlot;
2024-10-23 09:12:15 +08:00
/// <summary>
/// 地图格子
/// </summary>
2024-10-22 09:17:28 +08:00
public Grid[,] MapGrid ;
2024-10-23 09:12:15 +08:00
/// <summary>
/// 缓存格子
/// </summary>
2024-10-22 11:31:23 +08:00
[SerializeField] private List<Grid> MapGrids;
2024-10-23 09:12:15 +08:00
/// <summary>
/// 地图格子生成节点
/// </summary>
2024-10-22 09:17:28 +08:00
[SerializeField] private Transform gridParent;
2024-10-22 11:31:23 +08:00
public int x => mX;
[SerializeField] private int mX;
public int y => mY;
[SerializeField] private int mY;
2024-10-22 09:17:28 +08:00
public bool InitReady { get; private set; } = false;
private void Awake()
{
ins = this;
2024-10-22 11:31:23 +08:00
}
/// <summary>
/// 初始化游戏数据
/// </summary>
public void InitPlayData()
{
MapGrid = new Grid[x, y];
foreach (var grid in MapGrids)
{
MapGrid[grid.X, grid.Y] = grid;
}
2024-10-23 09:12:15 +08:00
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();
2024-10-22 11:31:23 +08:00
}
[InspectorButton("初始化地图")]
public void InitMap()
{
if (ins==null)
{
ins = this;
}
2024-10-22 09:17:28 +08:00
Init(10, 10);
}
2024-10-22 11:31:23 +08:00
/// <summary>
/// 地图生成调用
/// </summary>
/// <param name="最大纵向列数x"></param>
/// <param name="最大横向行数y"></param>
2024-10-22 09:17:28 +08:00
public void Init(int _x,int _y)
{
InitReady = false;
2024-10-22 11:31:23 +08:00
mX = _x;
mY = _y;
2024-10-22 09:17:28 +08:00
MapGrid = new Grid[x, y];
2024-10-22 11:31:23 +08:00
InitGrid();
2024-10-22 09:17:28 +08:00
}
2024-10-22 11:31:23 +08:00
private void InitGrid(Action action=null)
2024-10-22 09:17:28 +08:00
{
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();
2024-10-22 11:31:23 +08:00
MapGrids.Add(MapGrid[i,j]);
2024-10-22 09:17:28 +08:00
}
}
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);
}
}
2024-10-23 09:12:15 +08:00
}