summaryrefslogtreecommitdiff
path: root/Pathfinding Visualizer/Assets/Scripts/LogicGrid.cs
blob: c87514e785957e0c97d69e1968750484aaca13dc (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;


// INSPIRED BY "CODE MONKEY" ON YOUTUBE
public class LogicGrid
{
    private int width;
    private int height;
    private float cellSize;

    private Vector3 originPosition;

    // Actual Grid Array
    private int[,] gridArray;
    // Debug Grid Array used for updating the text-objects 
    private TextMesh[,] debugTextArray;


    // Coordinates for pathfinding
    public int[] agent;
    public List<int[][]> obstacles;
    public List<int[][]> samples;

    public event EventHandler<OnGridValueChangedEventArgs> OnGridValueChanged;
    public class OnGridValueChangedEventArgs : EventArgs
    {
        public int x;
        public int y;
    }



    public LogicGrid(int width, int height, float cellSize, Vector3 originPosition)
    {
        this.width = width;
        this.height = height;
        this.cellSize = cellSize;

        this.originPosition = originPosition;

        gridArray = new int[width, height];
        debugTextArray = new TextMesh[width, height];

        // Used to place cell values in middle of cell
        Vector3 textOffset = new Vector3(cellSize/2, cellSize/2); 

        // Visualize the grid
        for(int x = 0; x < gridArray.GetLength(0); x++)
            for(int y = 0; y < gridArray.GetLength(1); y++)
            {
                // Create text that prints the value of a given cell inside of that cell.
                debugTextArray[x, y] = CodeMonkey.Utils.UtilsClass.CreateWorldText(gridArray[x, y].ToString(), null, GetWorldPosition(x, y) + textOffset, 20, Color.white, TextAnchor.MiddleCenter);

                // Give the cells a visual representation (must enable gizmos to see)
                Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x + 1, y), Color.white, 100f);
                Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x, y + 1), Color.white, 100f);
            }

        // Draw top-line for whole grid, draw right-line for whole grid
        Debug.DrawLine(GetWorldPosition(0, height), GetWorldPosition(width, height), Color.white, 100f);
        Debug.DrawLine(GetWorldPosition(width, 0), GetWorldPosition(width, height), Color.white, 100f);
    }


    // Enables us to convert the array's (x,y) pair into the user's world-space (also used to scale grid in constructor)
    public Vector3 GetWorldPosition(int x, int y)
    {
        return new Vector3(x, y) * this.cellSize + this.originPosition;
    }

    private void getXY(Vector3 worldPosition, out int x, out int y)
    {
        x = Mathf.FloorToInt((worldPosition - originPosition).x / cellSize);
        y = Mathf.FloorToInt((worldPosition - originPosition).y / cellSize);
    }


    // Set cell value based on world-location (enables user interaction)
    public void SetValue(Vector3 worldPosition, int value)
    {
        int x, y;
        getXY(worldPosition, out x, out y);
        SetValue(x, y, value);
    }

    // Set cell value based on array index
    public void SetValue(int x, int y, int value)
    {
        if((x >= 0 && x < width) && (y >= 0 && y < height))
        {
            // Set the value in the real grid
            gridArray[x, y] = value;

            // Set the value in our debug mesh so that our Text on-screen gets updated
            debugTextArray[x, y].text = gridArray[x, y].ToString();

            if (OnGridValueChanged != null)
                OnGridValueChanged(this, new OnGridValueChangedEventArgs
                {
                    x = x,
                    y = y
                });

            // If deleting a cell's value, remove coord from set
            if (value == 0)
            {

                // make behavior to check for -1 values in coords
                if (agent[0].Equals(x) && agent[1].Equals(y))
                {
                    agent[0] = -1;
                    agent[1] = -2;
                }

            }

            // Add coordinate to obstacle array
            if (value == 1)
            {
                int[][] coord = new int[2][];

                coord[0][0] = x;
                coord[1][0] = y;

                obstacles.Add(coord);
            }
            // Add coordinate to sample array
            if (value == 2)
            {
                int[][] coord = new int[2][];

                coord[0][0] = x;
                coord[1][0] = y;

                samples.Add(coord);
            }
            // Add coordinate to obstacle array
            if (value == 3)
            {
                agent[0] = x;
                agent[1] = y;
            }
        }
    }

    // Get cell's value based on array index
    public int GetValue(int x, int y)
    {
        if ((x >= 0 && x < width) && (y >= 0 && y < height))
        {
            return gridArray[x, y];
        }
        return 0;
    }

    // Takes a world position, converts to array index, returns value held
    public int GetValue(Vector3 worldPosition)
    {
        int x, y;
        getXY(worldPosition, out x, out y);
        return GetValue(x, y);
    }

    public int GetHeight()
    {
        return this.height;
    }

    public int GetWidth()
    {
        return this.width;
    }

    public float GetCellSize()
    {
        return this.cellSize;
    }

    public void reset()
    {
        gridArray = new int[width, height];

        agent = new int[2];
        obstacles = new List<int[][]>();
        samples = new List<int[][]>();

        if (OnGridValueChanged != null)
            OnGridValueChanged(this, new OnGridValueChangedEventArgs
            {
                x = -1,
                y = -1
            });
    }
}