File size: 2,049 Bytes
7b5e094
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
using UnityEngine;

public class Grid : MonoBehaviour
{
    #region Singleton
    public static Grid instance;
    private void Awake()
    {
        if (instance == null)
            instance = this;

        goalPosition = (Random.Range(1, rows-1), Random.Range(1, columns-1));
        Instantiate(goalCube, new Vector3(goalPosition.Item1, 0.5f, goalPosition.Item2), Quaternion.identity);
    } 
    #endregion

    public GameObject floorCube;
    public GameObject goalCube;
    public int rows = 15;
    public int columns = 15;
    private GameObject[,] grid;
    public WeightDisplay weightDisplay;

    public (int, int) goalPosition;

    private void OnDrawGizmosSelected()
    {
        // Generate Grid
        for (int r = 0; r < rows; ++r)
        {
            for (int c = 0; c < columns; ++c)
            {
                Gizmos.color = new Color(0.5f, 0.5f, 0.5f, 0.5f); // Gray
                Vector3 position = new Vector3(c, transform.position.y, r);
                Gizmos.DrawCube(position, new Vector3(1, 1, 1));
            }
        }
     }

    private void Start()
    {
        grid = new GameObject[rows, columns];

        // Generate Grid
        for (int c = 0; c < columns; ++c)
        {
            for (int r = 0; r < rows; ++r)
            {
                Vector3 position = new Vector3(r, transform.position.y, c);
                grid[r, c] = Instantiate(floorCube, position, Quaternion.identity);
                grid[r, c].GetComponent<FloorCube>().position = (r, c);
                grid[r, c].GetComponent<FloorCube>().weightDisplay = weightDisplay;
            }
        }
    }

    public void ClearColors()
    {
        for (int r = 0; r < grid.GetLength(0); ++r)
        {
            for (int c = 0; c < grid.GetLength(1); ++c)
            {
                grid[r, c].GetComponent<FloorCube>().SetFadeScale(0.0f);
            }
        }
    }

    public void UpdateColor(int x, int y)
    {
        // Set fading 
        grid[x, y].GetComponent<FloorCube>().SetFadeScale(2.0f);
    }
}