123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using KairoEngine.Utility;
- namespace KairoEngine.Grids
- {
- public class HeatMapVisual : MonoBehaviour
- {
- private Grid<HeatMapGridObject> grid;
- private Mesh mesh;
- private bool updateMesh = false;
-
- private void Awake()
- {
- mesh = new Mesh();
- GetComponent<MeshFilter>().mesh = mesh;
- }
- public void SetGrid(Grid<HeatMapGridObject> grid)
- {
- this.grid = grid;
- UpdateHeatMapVisual();
- grid.OnGridObjectChanged += Grid_OnGridObjectChanged;
- }
- private void UpdateHeatMapVisual()
- {
- MeshUtils.CreateEmptyMeshArrays(grid.GetWidth() * grid.GetHeight(), out Vector3[] vertices, out Vector2[] uv, out int[] triangles);
- for (int x = 0; x < grid.GetWidth(); x++)
- {
- for (int y = 0; y < grid.GetHeight(); y++)
- {
- int index = x * grid.GetHeight() + y;
- Vector3 quadSize = grid.GetQuadSize();
- HeatMapGridObject gridObject = grid.GetObject(x, y);
- Vector2 gridValueUV = new Vector2(gridObject.GetValueNormalized(), 0f);
- Vector3 pos = grid.GetWorldPosition(x, y) + (quadSize * 0.5f);
- float rot = 0f;
- MeshUtils.AddToMeshArrays(vertices, uv, triangles, index, pos, rot, quadSize, gridValueUV, gridValueUV);
- }
- }
- mesh.vertices = vertices;
- mesh.uv = uv;
- mesh.triangles = triangles;
- }
- private void Grid_OnGridObjectChanged(object sender, Grid<HeatMapGridObject>.OnGridObjectChangedEventArgs e) => updateMesh = true;
- private void LateUpdate()
- {
- if(updateMesh)
- {
- updateMesh = false;
- Debug.Log("Grid event fired");
- UpdateHeatMapVisual();
- }
- }
- public void AddValue(int x, int y, int value)
- {
- HeatMapGridObject obj = grid.GetObject(x, y);
- if(obj != null)
- {
- obj.AddValue(value);
- //grid.SetObject(x, y, obj);
- }
- }
- public void AddValue(Vector3 worldPosition, int value, int fullValueRange, int totalRange)
- {
- int lowerValueAmmount = Mathf.RoundToInt((float)value / (totalRange - fullValueRange));
- grid.GetGridPosition(worldPosition, out int originX, out int originY);
- for (int x = 0; x < totalRange; x++)
- {
- for (int y = 0; y < totalRange - x; y++)
- {
- int radius = x + y;
- int addValueAmmount = value;
- if(radius > fullValueRange) addValueAmmount -= lowerValueAmmount * (radius - fullValueRange);
- AddValue(originX + x, originY + y, addValueAmmount);
- if(x != 0) AddValue(originX - x, originY + y, addValueAmmount);
- if(y != 0) AddValue(originX + x, originY - y, addValueAmmount);
- if(y != 0 && x != 0) AddValue(originX - x, originY - y, addValueAmmount);
- }
- }
- }
- }
- public class HeatMapGridObject
- {
- private const int MIN = 0;
- private const int MAX = 100;
- private Grid<HeatMapGridObject> grid;
- private int x;
- private int y;
- public int value;
- public HeatMapGridObject(Grid<HeatMapGridObject> grid, int x, int y)
- {
- this.grid = grid;
- this.x = x;
- this.y = y;
- }
- public void AddValue(int addValue)
- {
- value += addValue;
- value = Mathf.Clamp(value, MIN, MAX);
- grid.TriggerGridObjectChanged(x, y);
- }
- public float GetValueNormalized() => (float)value / MAX;
- public override string ToString() => value.ToString();
- }
- }
|