格子划分正常
This commit is contained in:
181
Assets/Scripts/Map/MapManager.Tb.cs
Normal file
181
Assets/Scripts/Map/MapManager.Tb.cs
Normal file
@@ -0,0 +1,181 @@
|
||||
using UnityEngine;
|
||||
using System.IO;
|
||||
using UnityEditor;
|
||||
using System;
|
||||
|
||||
//[ExecuteAlways]
|
||||
public partial class MapManager : MonoBehaviour
|
||||
{
|
||||
public Material gridMat;
|
||||
//public ComputeShader computeShader;
|
||||
public LineRenderer line;
|
||||
public Map map { get; private set; }
|
||||
|
||||
public event Action onMapCreated;
|
||||
|
||||
public CellType cellType;
|
||||
public Map GenerateMap(float width, float height,float sideWidth, float sideHeight)
|
||||
{
|
||||
if (map != null)
|
||||
{
|
||||
map.Release();
|
||||
map = null;
|
||||
}
|
||||
map = new Map(width, height, sideWidth, sideHeight);
|
||||
map.SetMapId(1000);
|
||||
//默认设置半透明0.3
|
||||
map.ChangeGridAlpha(0.4f);
|
||||
onMapCreated?.Invoke();
|
||||
return map;
|
||||
}
|
||||
public void ChangeGridAlpha(float alpha)
|
||||
{
|
||||
if (map == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
map.ChangeGridAlpha(alpha);
|
||||
}
|
||||
public void ChangeGridHeight(float height)
|
||||
{
|
||||
if (map == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
int spaceHight = (int)Math.Ceiling(height * 100); //4米为一档
|
||||
map.ChangeGridHight(spaceHight);
|
||||
}
|
||||
|
||||
public void ShowOrHideGrid()
|
||||
{
|
||||
if (map == null)
|
||||
return;
|
||||
map.mapGrid.gameObject.SetActive(!map.mapGrid.activeInHierarchy);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取地图位置的高度
|
||||
/// </summary>
|
||||
/// <param name="pos"></param>
|
||||
/// <returns></returns>
|
||||
public int GetMapY(Vector3 pos)
|
||||
{
|
||||
int posIndex = Mathf.FloorToInt(pos.x + Mathf.FloorToInt(pos.z) * map.horizontalNumber);
|
||||
return map.selector.GetGridData()[posIndex].barrier;
|
||||
}
|
||||
}
|
||||
public partial class Map
|
||||
{
|
||||
public GameObject mapGrid;
|
||||
public GridSelector selector;
|
||||
public int mapId { get; private set; }
|
||||
public float width { get; private set; }
|
||||
public float height { get; private set; }
|
||||
|
||||
public float sideLength { get; private set; }
|
||||
public float sideWidth { get; private set; }
|
||||
public float sideHeight { get; private set; }
|
||||
public int horizontalNumber { get { return (int)(width / sideWidth); } }
|
||||
public int verticalNumber { get { return (int)(height / sideHeight); } }
|
||||
|
||||
public Map(float width, float height, float sideWidth,float sideHeight)
|
||||
{
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.sideLength = sideLength;
|
||||
|
||||
this.sideWidth = sideWidth;
|
||||
this.sideHeight = sideHeight;
|
||||
mapGrid = CreateMapGrid();
|
||||
selector = mapGrid.AddComponent<GridSelector>();
|
||||
selector.OnMapCreated(this);
|
||||
mapGrid.transform.parent = MapManager.Instance.transform;
|
||||
mapGrid.transform.localPosition = Vector3.zero;
|
||||
mapGrid.transform.localEulerAngles = new Vector3(-90,0,0);
|
||||
//创建地图的时候需要赋值一下当前类型
|
||||
selector.CellType = MapManager.Instance.cellType;
|
||||
}
|
||||
|
||||
private GameObject CreateMapGrid()
|
||||
{
|
||||
Mesh mesh = new Mesh();
|
||||
Vector3[] vertices = new Vector3[4];
|
||||
vertices[0] = new Vector3(0f, 0f, 0f);
|
||||
vertices[1] = new Vector3(0f, 0f, height);
|
||||
vertices[2] = new Vector3(width, 0f, height);
|
||||
vertices[3] = new Vector3(width, 0f, 0f);
|
||||
mesh.vertices = vertices;
|
||||
|
||||
Vector2[] uv = new Vector2[4];
|
||||
uv[0] = new Vector2(0, 0);
|
||||
uv[1] = new Vector2(0, 1);
|
||||
uv[2] = new Vector2(1, 1);
|
||||
uv[3] = new Vector2(1, 0);
|
||||
mesh.uv = uv;
|
||||
|
||||
int[] triangles = new int[6] { 0, 1, 2, 0, 2, 3 };
|
||||
mesh.triangles = triangles;
|
||||
|
||||
Vector3[] normals = new Vector3[4] { Vector3.up, Vector3.up, Vector3.up, Vector3.up };
|
||||
mesh.normals = normals;
|
||||
|
||||
GameObject go = new GameObject("Grid");
|
||||
go.AddComponent<MeshFilter>().mesh = mesh;
|
||||
go.AddComponent<MeshRenderer>().sharedMaterial = MapManager.Instance.gridMat;
|
||||
go.AddComponent<MeshCollider>().sharedMesh = mesh;
|
||||
return go;
|
||||
}
|
||||
|
||||
public void SetMapId(int mapId)
|
||||
{
|
||||
this.mapId = mapId;
|
||||
}
|
||||
public void ChangeGridAlpha(float alpha)
|
||||
{
|
||||
mapGrid.GetComponent<MeshRenderer>().material.SetFloat("_Alpha", alpha);
|
||||
}
|
||||
public void ChangeGridHight(int height)
|
||||
{
|
||||
mapGrid.transform.position = new Vector3(0, height,0);
|
||||
}
|
||||
public void Release()
|
||||
{
|
||||
GameObject.Destroy(mapGrid);
|
||||
mapGrid = null;
|
||||
}
|
||||
|
||||
public static void Serialize(Map map, BinaryWriter bw)
|
||||
{
|
||||
int width = map.selector.horizontalNumber;
|
||||
int height = map.selector.verticalNumber;
|
||||
int sideLength = (int)(map.sideLength * 100);
|
||||
bw.Write(map.mapId);
|
||||
bw.Write(width);
|
||||
bw.Write(height);
|
||||
bw.Write(sideLength);
|
||||
GridSelector.RenderData[] data = map.selector.GetGridData();
|
||||
for (int i = 0, length = data.Length; i < length; i++)
|
||||
{
|
||||
bw.Write(data[i].barrier);
|
||||
}
|
||||
Debug.Log("地图保存成功");
|
||||
}
|
||||
public static Map Deserialize(BinaryReader br)
|
||||
{
|
||||
int mapId = br.ReadInt32();
|
||||
int horizontalNumber = br.ReadInt32();
|
||||
int verticalNumber = br.ReadInt32();
|
||||
//服务器那边是用的边长的100倍的int
|
||||
float sideWidth = br.ReadInt32() / 100f;
|
||||
float sideHeight = br.ReadInt32() / 100f;
|
||||
var map = MapManager.Instance.GenerateMap((int)(horizontalNumber * sideWidth), (int)(verticalNumber * sideHeight), sideWidth, sideHeight);
|
||||
map.SetMapId(mapId);
|
||||
GridSelector.RenderData[] data = map.selector.GetGridData();
|
||||
for (int i = 0; i < horizontalNumber * verticalNumber; i++) {
|
||||
data[i].barrier = br.ReadInt32();
|
||||
}
|
||||
map.selector.RefreshPlaneRender();
|
||||
return map;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user