summaryrefslogtreecommitdiff
path: root/Pathfinding Visualizer/Assets/Scripts/CellMesh.cs
blob: 5fc6bd8a01f2ec501ba466ddd1a411b6174ef78c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CellMesh : MonoBehaviour
{
    private LogicGrid grid;
    private Mesh mesh;

    private void Awake()
    {
        mesh = new Mesh();
        GetComponent<MeshFilter>().mesh = mesh;
    }

    public void SetGrid(LogicGrid grid)
    {
        this.grid = grid;
        UpdateCellVisual();

        // Subscribe to event
        grid.OnGridValueChanged += GridValueChanged;
    }

    private void GridValueChanged(object sender, LogicGrid.OnGridValueChangedEventArgs e)
    {
        Debug.Log("FIRE!!");
        UpdateCellVisual();
    }

    private void UpdateCellVisual()
    {
        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 = new Vector3(1, 1) * grid.GetCellSize();

                int gridValue = grid.GetValue(x, y);
                float gridValueNormalized = gridValue / 4f;
                Vector2 gridValueUV = new Vector2(gridValueNormalized, 0f);
                
                
                MeshUtils.AddToMeshArrays(vertices, uv, triangles, index, grid.GetWorldPosition(x, y) + (quadSize * .5f), 0f, quadSize, gridValueUV, gridValueUV);
            }

        mesh.vertices = vertices;
        mesh.uv = uv;
        mesh.triangles = triangles;
    }

    void Start()
    {
        
    }


    /*                MeshRenderer myRenderer = GetComponent<MeshRenderer>();
                Material m_material;
                if (myRenderer != null)
                {
                    m_material = myRenderer.material;

                    if(gridValue == -1)
                    {
                        m_material.color = Color.black; 
                    }
                    if (gridValue == 0)
                    {
                        m_material.color = Color.white;
                    }
                    if (gridValue == 1)
                    {
                        m_material.color = Color.green;
                    }
                    if (gridValue == 2)
                    {
                        m_material.color = Color.cyan;
                    }*//*
                }
*/
    private void Update()
    {
/*        if(Input.GetKeyDown("1"))
        {
            Debug.Log("1");
            m_Material.color = Color.white;
            //m_Material.SetColor("_Color", Color.white);
        }

        if (Input.GetKeyDown("2"))
        {
            Debug.Log("2");
            m_Material.color = Color.black;
            //m_Material.SetColor("_Color", Color.white);
        }

        if (Input.GetKeyDown("3"))
        {
            Debug.Log("3");
            m_Material.color = Color.green;
            //m_Material.SetColor("_Color", Color.white);
        }*/
    }
}