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.

100 lines
3.6 KiB

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
namespace ET
{
public static class BuildingViewHelper
{
public static GameObject CreateOcupyPlaceInicator(string name, Color color)
{
GameObject indicatorObj = new GameObject(name);
MeshRenderer meshRenderer = indicatorObj.AddComponent<MeshRenderer>();
meshRenderer.material = new Material(Shader.Find("Sprites/Default"));
meshRenderer.material.color = color;
meshRenderer.material.renderQueue = (int)RenderQueue.Transparent - 1;
var meshFilter = indicatorObj.AddComponent<MeshFilter>();
var mesh = new Mesh();
var verticies = new List<Vector3>();
var indicies = new List<int>();
//0--3
//|/ |
//1--2
verticies.Add(new Vector3());
verticies.Add(new Vector3());
verticies.Add(new Vector3());
verticies.Add(new Vector3());
indicies.Add(0);
indicies.Add(1);
indicies.Add(3);
indicies.Add(1);
indicies.Add(2);
indicies.Add(3);
mesh.vertices = verticies.ToArray();
mesh.SetIndices(indicies.ToArray(), MeshTopology.Triangles, 0);
meshFilter.mesh = mesh;
return indicatorObj;
}
public static void UpdateIndicatorLocal(GameObject indicatorObj, MapComponent mapComp, Vector3 worldPos, Vector2Int size)
{
indicatorObj.transform.position = worldPos;
var meshFilter = indicatorObj.GetComponent<MeshFilter>();
var mesh = meshFilter.mesh;
var verticies = mesh.vertices;
// 1
// / \
//0 2
// \ /
// 3
Vector3 left = mapComp.MapToLocalPos(size.x, 0);
Vector3 top = mapComp.MapToLocalPos(size.x, size.y);
Vector3 right = mapComp.MapToLocalPos(0, size.y);
Vector3 bottom = Vector3.zero;
verticies[0] = left;
verticies[1] = top;
verticies[2] = right;
verticies[3] = bottom;
mesh.vertices = verticies;
meshFilter.mesh = mesh;
mesh.RecalculateBounds();
}
public static void UpdateIndicator(GameObject indicatorObj, MapComponent mapComp, Vector3 worldPos, Vector2Int size)
{
RectIndices rectIndices = mapComp.GetUnitRectIndices(worldPos, size);
UpdateIndicator(indicatorObj, mapComp, rectIndices);
}
public static void UpdateIndicator(GameObject indicatorObj, MapComponent mapComp, RectIndices rectIndices)
{
var meshFilter = indicatorObj.GetComponent<MeshFilter>();
var mesh = meshFilter.mesh;
var verticies = mesh.vertices;
// 1
// / \
//0 2
// \ /
// 3
Vector3 left = mapComp.MapToWorldPos(rectIndices.endRow, rectIndices.startCol);
Vector3 top = mapComp.MapToWorldPos(rectIndices.endRow, rectIndices.endCol);
Vector3 right = mapComp.MapToWorldPos(rectIndices.startRow, rectIndices.endCol);
Vector3 bottom = mapComp.MapToWorldPos(rectIndices.startRow, rectIndices.startCol);
verticies[0] = left;
verticies[1] = top;
verticies[2] = right;
verticies[3] = bottom;
mesh.vertices = verticies;
meshFilter.mesh = mesh;
}
}
}