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

1 year ago
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
namespace BuildSystem
{
[DefaultExecutionOrder(-10)]
1 year ago
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;
1 year ago
[Header("拆除")]
public bool isRemove = false;
1 year ago
public Grid[][] gridMap;
[Header("转换后的数据")]
public GridInfo[][] gridInfos;
public List<GridInfo> grid = new List<GridInfo>();
1 year ago
private PlaceObject_SO placeObject_SO;
public bool isUI = false;
1 year ago
[NonSerialized]
public PlaceObject placeObj;
1 year ago
public GameObject flag;
1 year ago
public bool isGameEnd = false;
1 year ago
[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();
1 year ago
DontDestroyOnLoad(gameObject);
1 year ago
1 year ago
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);
//特殊建筑
1 year ago
gridMap[1][3].isPlace = true;
gridMap[7][5].isPlace = true;
1 year ago
gridMap[5][2].isPlace = true;
1 year ago
gridMap[8][2].isPlace = true;
gridMap[11][4].isPlace = true;
1 year ago
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;
1 year ago
}
/// <summary>
/// 创建地形
/// </summary>
void CreateTerrain()
{
1 year ago
//创建地图
gridMap = new Grid[mapSize.x][];
for (int i = 0; i < mapSize.x; i++)
{
1 year ago
gridMap[i] = new Grid[mapSize.y];
for (int j = 0; j < mapSize.y; j++)
{
Grid grid = new Grid(i, 0, j);
1 year ago
gridMap[i][j] = grid;
if (i > 0)
{
1 year ago
grid.BindLeftAndThis(gridMap[i - 1][j]);
}
if (j > 0)
{
1 year ago
grid.BindDownAndThis(gridMap[i][j - 1]);
}
1 year ago
//CreateFlag();
1 year ago
}
}
}
/// <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);
1 year ago
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) {
1 year ago
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)));
1 year ago
gridMap[x][y].isPlace = true;
gridMap[x][y].type = place.type;
gridMap[x][y].isHaves = place.ishave;
1 year ago
gridMap[x][y].roadType = place.roadType;
gridMap[x][y].placeObject = obj;
1 year ago
if (gridMap[x][y].roadType != RoadType.T && gridMap[x][y].roadType != RoadType.Ten) return;
var gridinfo = trans.gameObject.AddComponent<GridInfo>();
grid.Add(gridinfo);
1 year ago
if (gridMap[x][y].isPlace)
{
if (gridMap[x][y].roadType == RoadType.Ten)
{
gridinfo.roadType = TraficRoadType.;
1 year ago
}
else if (gridMap[x][y].roadType == RoadType.T)
{
if (gridMap[x][y].isHaves[0] != IsHave.Yes)
1 year ago
{
gridinfo.roadType = TraficRoadType.downT;
1 year ago
}
else if (gridMap[x][y].isHaves[2] != IsHave.Yes)
1 year ago
{
gridinfo.roadType = TraficRoadType.upT;
1 year ago
}
else if (gridMap[x][y].isHaves[3] != IsHave.Yes)
1 year ago
{
gridinfo.roadType = TraficRoadType.rightT;
1 year ago
}
else if (gridMap[x][y].isHaves[1] != IsHave.Yes)
1 year ago
{
gridinfo.roadType = TraficRoadType.leftT;
1 year ago
}
}
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;
1 year ago
}
}
public void RemoveGrid(GridInfo info)
{
if (grid.Contains(info))
{
grid.Remove(info);
}
1 year ago
}
/// <summary>
/// 创建一个gridtype为初始化类型的数组
1 year ago
/// </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;
1 year ago
gridMap[x][y].isHaves = place.ishave;
gridMap[x][y].roadType = place.roadType;
1 year ago
gridMap[x][y].placeObject = obj;
1 year ago
}
1 year ago
/// <summary>
/// 移除某个物体
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
1 year ago
void RemovePlace(int x, int y) {
1 year ago
RemoveGrid(gridMap[x][y].placeObject.gameObject.GetComponent<GridInfo>());
1 year ago
Destroy(gridMap[x][y].placeObject.gameObject);
gridMap[x][y].RemoveInit();
1 year ago
}
1 year ago
/// <summary>
/// 创建grid标识
/// </summary>
void CreateFlag() {
}
/// <summary>
1 year ago
/// 改变当前的选择的指示器
1 year ago
/// </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("当前位置不可创建");
}
1 year ago
return false;
}
//判断类型是否可以放置
if (isCreateSuccess)
{
if (!gridMap[start.x][start.z].IsCanPlace(placeObj))
{
buildTip.ShowTip("当前位置存在放置存在风险");
1 year ago
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;
}
1 year ago
/// <summary>
/// 获取grid的世界坐标
/// </summary>
/// <param name="pos"></param>
/// <returns></returns>
1 year ago
public Vector3 GetGridPos(Vector3Int pos) {
if (pos == new Vector3Int(-1, -1, -1)) {
return Vector3Int.zero;
}
return gridMap[pos.x][pos.z].pos;
}
1 year ago
/// <summary>
/// 判断是否通关
/// </summary>
/// <returns></returns>
public bool IsGameEnd() {
1 year ago
//判断是否闭环
for (int i = 0; i < mapSize.x; i++)
{
for (int j = 0; j < mapSize.y; j++)
{
1 year ago
if (!gridMap[i][j].isClosedLoop()) {
return false;
}
}
}
1 year ago
//判断是否有两个十字
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;
}
1 year ago
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);
1 year ago
} 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) {
1 year ago
RemovePlace(mouseClick.MouseInGrid().x, mouseClick.MouseInGrid().z);
buildTip.ShowTip("拆除提示");
}
}
1 year ago
}
//变色提示
1 year ago
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));
}
}
1 year ago
1 year ago
//判断通关
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();
}
}
1 year ago
}
1 year ago
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;
1 year ago
}
}
}
return gridInfos;
}
//public
1 year ago
}
}