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.
287 lines
8.7 KiB
287 lines
8.7 KiB
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using System;
|
|
namespace BuildSystem
|
|
{
|
|
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;
|
|
Grid[][] gridMap;
|
|
private PlaceObject_SO placeObject_SO;
|
|
[NonSerialized]
|
|
public PlaceObject placeObj;
|
|
|
|
public Material green;
|
|
public Material red;
|
|
|
|
[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();
|
|
|
|
CreatePlace(w[0], 0, 0);
|
|
CreatePlace(h2, 1, 0);
|
|
CreatePlace(h2, 2, 0);
|
|
CreatePlace(t2up, 3, 0);
|
|
CreatePlace(h2, 4, 0);
|
|
CreatePlace(h2, 5, 0);
|
|
CreatePlace(h2, 6, 0);
|
|
CreatePlace(h2, 7, 0);
|
|
CreatePlace(h2, 8, 0);
|
|
CreatePlace(h2, 9, 0);
|
|
CreatePlace(t2up, 10, 0);
|
|
CreatePlace(h2, 11, 0);
|
|
CreatePlace(h2, 12, 0);
|
|
CreatePlace(w[3], 13, 0);
|
|
|
|
CreatePlace(h2, 1, 7);
|
|
CreatePlace(h2, 2, 7);
|
|
CreatePlace(t2down, 3, 7);
|
|
CreatePlace(h2, 4, 7);
|
|
CreatePlace(h2, 5, 7);
|
|
CreatePlace(h2, 6, 7);
|
|
CreatePlace(h2, 7, 7);
|
|
CreatePlace(h2, 8, 7);
|
|
CreatePlace(h2, 9, 7);
|
|
CreatePlace(t2down, 10, 7);
|
|
CreatePlace(h2, 11, 7);
|
|
CreatePlace(h2, 12, 7);
|
|
|
|
|
|
|
|
CreatePlace(v2, 0, 1);
|
|
CreatePlace(t2right, 0, 2);
|
|
CreatePlace(v2, 0, 3);
|
|
CreatePlace(v2, 0, 4);
|
|
CreatePlace(t2left, 0, 5);
|
|
CreatePlace(v2, 0, 6);
|
|
CreatePlace(w[1], 0, 7);
|
|
|
|
CreatePlace(v2, 13, 1);
|
|
CreatePlace(t2left, 13, 2);
|
|
CreatePlace(v2, 13, 3);
|
|
CreatePlace(v2, 13, 4);
|
|
CreatePlace(t2right, 13, 5);
|
|
CreatePlace(v2, 13, 6);
|
|
CreatePlace(w[2], 13, 7);
|
|
|
|
gridMap[1][3].isPlace = true;
|
|
gridMap[7][5].isPlace = true;
|
|
gridMap[7][2].isPlace = true;
|
|
gridMap[8][2].isPlace = true;
|
|
gridMap[11][4].isPlace = true;
|
|
}
|
|
|
|
/// <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);
|
|
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);
|
|
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;
|
|
}
|
|
|
|
|
|
/// <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)
|
|
{
|
|
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;
|
|
}
|
|
|
|
public Grid GetGrid(Vector3Int pos) {
|
|
return gridMap[pos.x][pos.z];
|
|
}
|
|
|
|
public Vector3 GetGridPos(Vector3Int pos) {
|
|
if (pos == new Vector3Int(-1, -1, -1)) {
|
|
return Vector3Int.zero;
|
|
}
|
|
return gridMap[pos.x][pos.z].pos;
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
if (placeObj) {
|
|
Material[] materials = placeObj.GetComponent<MeshRenderer>().materials;
|
|
|
|
if (isCanCreateToTip())
|
|
{
|
|
placeObj.GetComponent<MeshRenderer>().materials = new Material[5] {
|
|
materials[0],
|
|
materials[1],
|
|
materials[2],
|
|
materials[3],
|
|
green
|
|
};
|
|
}
|
|
else {
|
|
placeObj.GetComponent<MeshRenderer>().materials = new Material[5] {
|
|
materials[0],
|
|
materials[1],
|
|
materials[2],
|
|
materials[3],
|
|
red
|
|
};
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
}
|
|
}
|