You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
481 lines
17 KiB
481 lines
17 KiB
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using System;
|
|
namespace BuildSystem
|
|
{
|
|
[DefaultExecutionOrder(-10)]
|
|
public class BuildSystemManager : MonoSingleton<BuildSystemManager>
|
|
{
|
|
[NonSerialized]
|
|
public MouseClick mouseClick;
|
|
public BuildTip buildTip;
|
|
[Header("地图大小")]
|
|
public Vector2Int mapSize;
|
|
[Header("grid大小")]
|
|
public int gridSize;
|
|
[Header("起始位置")]
|
|
public Vector3 startPos;
|
|
[Header("是否可以放置")]
|
|
public bool isCanPlace;
|
|
[Header("拆除")]
|
|
public bool isRemove = false;
|
|
public Grid[][] gridMap;
|
|
[Header("转换后的数据")]
|
|
public GridInfo[][] gridInfos;
|
|
public List<GridInfo> grid = new List<GridInfo>();
|
|
private PlaceObject_SO placeObject_SO;
|
|
public bool isUI = false;
|
|
[NonSerialized]
|
|
public PlaceObject placeObj;
|
|
|
|
public GameObject flag;
|
|
|
|
public bool isGameEnd = false;
|
|
|
|
[Header("用于初始化地图")]
|
|
public PlaceObject_SO t2up;
|
|
public PlaceObject_SO t2down;
|
|
public PlaceObject_SO h2;
|
|
|
|
public PlaceObject_SO t2left;
|
|
public PlaceObject_SO t2right;
|
|
public PlaceObject_SO v2;
|
|
|
|
public PlaceObject_SO[] w;
|
|
|
|
protected override void Awake()
|
|
{
|
|
base.Awake();
|
|
mouseClick = GetComponent<MouseClick>();
|
|
CreateTerrain();
|
|
DontDestroyOnLoad(gameObject);
|
|
|
|
CreatePlaceSpecial(w[0], 0, 0);
|
|
CreatePlaceSpecial(h2, 1, 0);
|
|
CreatePlaceSpecial(h2, 2, 0);
|
|
CreatePlaceSpecial(t2up, 3, 0);
|
|
CreatePlaceSpecial(h2, 4, 0);
|
|
CreatePlaceSpecial(h2, 5, 0);
|
|
CreatePlaceSpecial(h2, 6, 0);
|
|
CreatePlaceSpecial(h2, 7, 0);
|
|
CreatePlaceSpecial(h2, 8, 0);
|
|
CreatePlaceSpecial(h2, 9, 0);
|
|
CreatePlaceSpecial(t2up, 10, 0);
|
|
CreatePlaceSpecial(h2, 11, 0);
|
|
CreatePlaceSpecial(h2, 12, 0);
|
|
CreatePlaceSpecial(w[3], 13, 0);
|
|
|
|
CreatePlaceSpecial(w[1], 0, 7);
|
|
CreatePlaceSpecial(h2, 1, 7);
|
|
CreatePlaceSpecial(h2, 2, 7);
|
|
CreatePlaceSpecial(t2down, 3, 7);
|
|
CreatePlaceSpecial(h2, 4, 7);
|
|
CreatePlaceSpecial(h2, 5, 7);
|
|
CreatePlaceSpecial(h2, 6, 7);
|
|
CreatePlaceSpecial(h2, 7, 7);
|
|
CreatePlaceSpecial(h2, 8, 7);
|
|
CreatePlaceSpecial(h2, 9, 7);
|
|
CreatePlaceSpecial(t2down, 10, 7);
|
|
CreatePlaceSpecial(h2, 11, 7);
|
|
CreatePlaceSpecial(h2, 12, 7);
|
|
CreatePlaceSpecial(w[2], 13, 7);
|
|
|
|
|
|
CreatePlaceSpecial(v2, 0, 1);
|
|
CreatePlaceSpecial(t2right, 0, 2);
|
|
CreatePlaceSpecial(v2, 0, 3);
|
|
CreatePlaceSpecial(v2, 0, 4);
|
|
CreatePlaceSpecial(v2, 0, 5);
|
|
CreatePlaceSpecial(v2, 0, 6);
|
|
|
|
|
|
CreatePlaceSpecial(v2, 13, 1);
|
|
CreatePlaceSpecial(v2, 13, 2);
|
|
CreatePlaceSpecial(v2, 13, 3);
|
|
CreatePlaceSpecial(v2, 13, 4);
|
|
CreatePlaceSpecial(t2left, 13, 5);
|
|
CreatePlaceSpecial(v2, 13, 6);
|
|
|
|
//特殊建筑
|
|
|
|
gridMap[1][3].isPlace = true;
|
|
gridMap[7][5].isPlace = true;
|
|
gridMap[5][2].isPlace = true;
|
|
gridMap[8][2].isPlace = true;
|
|
gridMap[11][4].isPlace = true;
|
|
|
|
gridMap[1][3].gridType = GridType.Special;
|
|
gridMap[7][5].gridType = GridType.Special;
|
|
gridMap[5][2].gridType = GridType.Special;
|
|
gridMap[8][2].gridType = GridType.Special;
|
|
gridMap[11][4].gridType = GridType.Special;
|
|
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 创建地形
|
|
/// </summary>
|
|
void CreateTerrain()
|
|
{
|
|
//创建地图
|
|
gridMap = new Grid[mapSize.x][];
|
|
for (int i = 0; i < mapSize.x; i++)
|
|
{
|
|
gridMap[i] = new Grid[mapSize.y];
|
|
for (int j = 0; j < mapSize.y; j++)
|
|
{
|
|
Grid grid = new Grid(i, 0, j);
|
|
gridMap[i][j] = grid;
|
|
if (i > 0)
|
|
{
|
|
grid.BindLeftAndThis(gridMap[i - 1][j]);
|
|
}
|
|
if (j > 0)
|
|
{
|
|
grid.BindDownAndThis(gridMap[i][j - 1]);
|
|
}
|
|
//CreateFlag();
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 创建一个当前选择的物体,用于指示器的更换
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
PlaceObject CreatePlaceObject() {
|
|
Transform trans = Instantiate(placeObject_SO.prefab, transform);
|
|
PlaceObject obj = trans.gameObject.AddComponent<PlaceObject>();
|
|
obj.Init(placeObject_SO.nameString,
|
|
placeObject_SO.width, placeObject_SO.height, placeObject_SO.type,placeObject_SO.ishave,placeObject_SO.roadType);
|
|
obj.SetPosition(GetGridPos(mouseClick.MouseInGrid()));
|
|
return obj;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 创建一个物体
|
|
/// </summary>
|
|
/// <param name="place"></param>
|
|
/// <param name="x"></param>
|
|
/// <param name="y"></param>
|
|
void CreatePlace(PlaceObject_SO place, int x, int y) {
|
|
Transform trans = Instantiate(place.prefab, transform);
|
|
PlaceObject obj = trans.gameObject.AddComponent<PlaceObject>();
|
|
obj.Init(place.nameString, place.width, place.height, place.type, place.ishave, place.roadType);
|
|
obj.SetPosition(GetGridPos(new Vector3Int(x, 0, y)));
|
|
|
|
gridMap[x][y].isPlace = true;
|
|
gridMap[x][y].type = place.type;
|
|
gridMap[x][y].isHaves = place.ishave;
|
|
gridMap[x][y].roadType = place.roadType;
|
|
gridMap[x][y].placeObject = obj;
|
|
|
|
if (gridMap[x][y].roadType != RoadType.T && gridMap[x][y].roadType != RoadType.Ten) return;
|
|
|
|
var gridinfo = trans.gameObject.AddComponent<GridInfo>();
|
|
grid.Add(gridinfo);
|
|
|
|
if (gridMap[x][y].isPlace)
|
|
{
|
|
if (gridMap[x][y].roadType == RoadType.Ten)
|
|
{
|
|
gridinfo.roadType = TraficRoadType.十;
|
|
}
|
|
else if (gridMap[x][y].roadType == RoadType.T)
|
|
{
|
|
if (gridMap[x][y].isHaves[0] != IsHave.Yes)
|
|
{
|
|
gridinfo.roadType = TraficRoadType.downT;
|
|
}
|
|
else if (gridMap[x][y].isHaves[2] != IsHave.Yes)
|
|
{
|
|
gridinfo.roadType = TraficRoadType.upT;
|
|
}
|
|
else if (gridMap[x][y].isHaves[3] != IsHave.Yes)
|
|
{
|
|
gridinfo.roadType = TraficRoadType.rightT;
|
|
}
|
|
else if (gridMap[x][y].isHaves[1] != IsHave.Yes)
|
|
{
|
|
gridinfo.roadType = TraficRoadType.leftT;
|
|
}
|
|
}
|
|
gridinfo.up = gridMap[x][y].placeObject.transform.GetChild(0).position;
|
|
gridinfo.down = gridMap[x][y].placeObject.transform.GetChild(1).position;
|
|
gridinfo.left = gridMap[x][y].placeObject.transform.GetChild(2).position;
|
|
gridinfo.right = gridMap[x][y].placeObject.transform.GetChild(3).position;
|
|
}
|
|
|
|
}
|
|
|
|
public void RemoveGrid(GridInfo info)
|
|
{
|
|
if (grid.Contains(info))
|
|
{
|
|
grid.Remove(info);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 创建一个gridtype为初始化类型的数组
|
|
/// </summary>
|
|
/// <param name="place"></param>
|
|
/// <param name="x"></param>
|
|
/// <param name="y"></param>
|
|
void CreatePlaceSpecial(PlaceObject_SO place, int x, int y)
|
|
{
|
|
Transform trans = Instantiate(place.prefab, transform);
|
|
PlaceObject obj = trans.gameObject.AddComponent<PlaceObject>();
|
|
obj.Init(place.nameString, place.width, place.height, place.type, place.ishave, place.roadType);
|
|
obj.SetPosition(GetGridPos(new Vector3Int(x, 0, y)));
|
|
|
|
gridMap[x][y].isPlace = true;
|
|
gridMap[x][y].gridType = GridType.Initial;
|
|
gridMap[x][y].type = place.type;
|
|
gridMap[x][y].isHaves = place.ishave;
|
|
gridMap[x][y].roadType = place.roadType;
|
|
gridMap[x][y].placeObject = obj;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 移除某个物体
|
|
/// </summary>
|
|
/// <param name="x"></param>
|
|
/// <param name="y"></param>
|
|
void RemovePlace(int x, int y) {
|
|
RemoveGrid(gridMap[x][y].placeObject.gameObject.GetComponent<GridInfo>());
|
|
Destroy(gridMap[x][y].placeObject.gameObject);
|
|
gridMap[x][y].RemoveInit();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 创建grid标识
|
|
/// </summary>
|
|
void CreateFlag() {
|
|
|
|
}
|
|
/// <summary>
|
|
/// 改变当前的选择的指示器
|
|
/// </summary>
|
|
public void ChangeChoose(PlaceObject_SO placeObject_SO) {
|
|
this.placeObject_SO = placeObject_SO;
|
|
if (placeObj)
|
|
{
|
|
Destroy(placeObj.gameObject);
|
|
}
|
|
placeObj = CreatePlaceObject();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 是否可以创建
|
|
/// </summary>
|
|
public bool isCanCreate()
|
|
{
|
|
//将自身大小记录
|
|
bool isCreateSuccess = true;
|
|
Vector3Int start = mouseClick.MouseInGrid();
|
|
|
|
if (start == new Vector3Int(-1, -1, -1) || gridMap[start.x][start.z].isPlace)
|
|
{
|
|
isCreateSuccess = false;
|
|
}
|
|
if (!isCreateSuccess)
|
|
{
|
|
if (!isUI) {
|
|
buildTip.ShowTip("当前位置不可创建");
|
|
}
|
|
return false;
|
|
}
|
|
|
|
//判断类型是否可以放置
|
|
if (isCreateSuccess)
|
|
{
|
|
if (!gridMap[start.x][start.z].IsCanPlace(placeObj))
|
|
{
|
|
buildTip.ShowTip("当前位置存在放置存在风险");
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 判断是否可以创建,用于提示
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public bool isCanCreateToTip()
|
|
{
|
|
//将自身大小记录
|
|
bool isCreateSuccess = true;
|
|
Vector3Int start = mouseClick.MouseInGrid();
|
|
|
|
if (start == new Vector3Int(-1, -1, -1) || gridMap[start.x][start.z].isPlace)
|
|
{
|
|
isCreateSuccess = false;
|
|
}
|
|
if (!isCreateSuccess)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
//判断类型是否可以放置
|
|
if (isCreateSuccess)
|
|
{
|
|
if (!gridMap[start.x][start.z].IsCanPlace(placeObj))
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取grid的世界坐标
|
|
/// </summary>
|
|
/// <param name="pos"></param>
|
|
/// <returns></returns>
|
|
public Vector3 GetGridPos(Vector3Int pos) {
|
|
if (pos == new Vector3Int(-1, -1, -1)) {
|
|
return Vector3Int.zero;
|
|
}
|
|
return gridMap[pos.x][pos.z].pos;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 判断是否通关
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public bool IsGameEnd() {
|
|
//判断是否闭环
|
|
for (int i = 0; i < mapSize.x; i++)
|
|
{
|
|
for (int j = 0; j < mapSize.y; j++)
|
|
{
|
|
if (!gridMap[i][j].isClosedLoop()) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
//判断是否有两个十字
|
|
int number = 0;
|
|
for (int i = 0; i < mapSize.x; i++)
|
|
{
|
|
for (int j = 0; j < mapSize.y; j++)
|
|
{
|
|
if (gridMap[i][j].isPlace && gridMap[i][j].roadType == RoadType.Ten) {
|
|
number++;
|
|
}
|
|
}
|
|
}
|
|
if (number < 2) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
//物体跟随鼠标
|
|
if (placeObj && isCanPlace) {
|
|
if (mouseClick.MouseInGrid() != new Vector3Int(-1,-1,-1))
|
|
{
|
|
placeObj.SetPosition(GetGridPos(mouseClick.MouseInGrid()));
|
|
}
|
|
}
|
|
|
|
////创建物体
|
|
if (Input.GetMouseButtonDown(0) && placeObj && isCanPlace && isCanCreate())
|
|
{
|
|
CreatePlace(placeObject_SO, mouseClick.MouseInGrid().x, mouseClick.MouseInGrid().z);
|
|
|
|
} else if (Input.GetMouseButtonDown(1) && mouseClick.MouseInGrid()!=new Vector3Int(-1,-1,-1)) {
|
|
if (gridMap[mouseClick.MouseInGrid().x][mouseClick.MouseInGrid().z].placeObject) {
|
|
if (gridMap[mouseClick.MouseInGrid().x][mouseClick.MouseInGrid().z].gridType == GridType.General) {
|
|
RemovePlace(mouseClick.MouseInGrid().x, mouseClick.MouseInGrid().z);
|
|
buildTip.ShowTip("拆除提示");
|
|
}
|
|
}
|
|
}
|
|
|
|
//变色提示
|
|
if (placeObj)
|
|
{
|
|
flag.transform.position = placeObj.transform.position - new Vector3(0,0.5f,0);
|
|
if (isCanCreateToTip())
|
|
{
|
|
flag.GetComponent<MeshRenderer>().material.SetColor("_Color", new Color32(48,255,30,128));
|
|
}
|
|
else
|
|
{
|
|
flag.GetComponent<MeshRenderer>().material.SetColor("_Color", new Color32(255, 41, 0, 128));
|
|
}
|
|
}
|
|
|
|
//判断通关
|
|
if (IsGameEnd() && !isGameEnd)
|
|
{
|
|
isGameEnd = true;
|
|
EduCoderTool.WebConnecter.Singleton.SendResultToWeb(true);
|
|
}
|
|
|
|
for (int i = 0; i < mapSize.x; i++)
|
|
{
|
|
for (int j = 0; j < mapSize.y; j++)
|
|
{
|
|
gridMap[i][j].InitGround();
|
|
}
|
|
}
|
|
}
|
|
|
|
public GridInfo[][] DataTrans() {
|
|
isCanPlace = false;
|
|
gridInfos = new GridInfo[mapSize.x][] ;
|
|
for (int i = 0; i < mapSize.x; i++)
|
|
{
|
|
gridInfos[i] = new GridInfo[mapSize.y];
|
|
for (int j = 0; j < mapSize.y; j++)
|
|
{
|
|
GridInfo grid = new GridInfo();
|
|
gridInfos[i][j] = grid;
|
|
}
|
|
}
|
|
|
|
for (int i = 0; i < mapSize.x; i++)
|
|
{
|
|
for (int j = 0; j < mapSize.y; j++)
|
|
{
|
|
if (gridMap[i][j].isPlace)
|
|
{
|
|
if (gridMap[i][j].roadType == RoadType.Ten) {
|
|
gridInfos[i][j].roadType = TraficRoadType.十;
|
|
} else if (gridMap[i][j].roadType == RoadType.T) {
|
|
if (gridMap[i][j].up == null) {
|
|
gridInfos[i][j].roadType = TraficRoadType.downT;
|
|
} else if (gridMap[i][j].down == null) {
|
|
gridInfos[i][j].roadType = TraficRoadType.upT;
|
|
}
|
|
else if (gridMap[i][j].left == null)
|
|
{
|
|
gridInfos[i][j].roadType = TraficRoadType.rightT;
|
|
}
|
|
else if (gridMap[i][j].right == null)
|
|
{
|
|
gridInfos[i][j].roadType = TraficRoadType.leftT;
|
|
}
|
|
}
|
|
gridInfos[i][j].up = gridMap[i][j].placeObject.transform.GetChild(0).position;
|
|
gridInfos[i][j].down = gridMap[i][j].placeObject.transform.GetChild(1).position;
|
|
gridInfos[i][j].left = gridMap[i][j].placeObject.transform.GetChild(2).position;
|
|
gridInfos[i][j].right = gridMap[i][j].placeObject.transform.GetChild(3).position;
|
|
}
|
|
}
|
|
}
|
|
return gridInfos;
|
|
}
|
|
|
|
|
|
//public
|
|
}
|
|
}
|