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 |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Brains.Framework.PathFinding;
using Brains.Framework.Behaviors.PathFinding;
namespace Brains.Framework.Map
{
/// <summary>
/// Represents a rectangle grid of cells which will exist in a World Map
/// </summary>
public class Grid
{
public List<PreStoredPath> PredeterminedPaths = new List<PreStoredPath>();
private int _cols;
private int _rows;
private List<GridCell> _cells;
private int _width;
private int _height;
private int _cellSize;
private Vector2 _position;
private WorldMap _parent;
//HACK: Exposed to enable a demo feature
public Color[] ColorData;
/// <summary>
/// Gets the parent Map of the Grid
/// </summary>
public WorldMap Parent { get { return _parent; } }
/// <summary>
/// The top left position of the grid
/// </summary>
public Vector2 Position { get { return _position; } }
/// <summary>
/// Gets the position of the center of the Grid
/// </summary>
public Vector2 Center
{
get { return Position+(Size/2); }
}
/// <summary>
/// Gets the number of columns on the grid
/// </summary>
public int Cols { get { return _cols; } }
/// <summary>
/// Gets the number of rows on the grid
/// </summary>
public int Rows { get { return _rows; } }
/// <summary>
/// Gets the List of Cells
/// </summary>
public List<GridCell> Cells { get { return _cells; } }
/// <summary>
/// Gets the full width of the grid
/// </summary>
public int Width { get { return _width; } }
/// <summary>
/// Get the full height of the grid
/// </summary>
public int Height { get { return _height; } }
/// <summary>
/// Gets the size of the Grid in pixels as a Vector2
/// </summary>
public Vector2 Size
{
get { return new Vector2(_width, _height); }
}
/// <summary>
/// Gets the size of a cells width of height
/// </summary>
/// <remarks>Assumes the cells are square</remarks>
public int CellSize { get { return _cellSize; } }
/// <summary>
/// Initializes a new instance of Grid
/// </summary>
public Grid()
{
}
/// <summary>
/// Initializes a new instance of Grid
/// </summary>
public Grid(int width,int height,int cellSize):this(Vector2.Zero, width, height, cellSize)
{
}
/// <summary>
/// Initializes a new instance of Grid
/// </summary>
public Grid(Vector2 position,int width, int height, int cellSize)
{
this.Setup(position, width, height, cellSize);
}
public void Setup(Vector2 position, int width, int height, int cellSize)
{
_position = position;
_width = width;
_height = height;
_cellSize = cellSize;
Initialize();
}
public void Initialize()
{
_rows = (int)(Height / CellSize);
_cols = (int)(Width / CellSize);
_cells = new List<GridCell>(_rows * _cols);
float _halfCell = CellSize / 2;
for (int y = 0; y < _rows; y++)
{
for (int x = 0; x < _cols; x++)
{
GridCell _cell = new GridCell(
x,
y,
Position +
new Vector2(CellSize * x, CellSize * y) +
new Vector2(_halfCell));
_cell.Type = 1; //Empty
_cell.Parent = this;
_cells.Add(_cell);
}
}
}
public GridCell GetCellAtPosition(Vector2 position)
{
Vector2 vDistanceFromMapCenter = position;
float _hstep = (Width/ ((float)Cols));
float _vstep = (Height/ ((float)Rows));
float xComponent = (vDistanceFromMapCenter.X / _hstep);
float yComponent = (vDistanceFromMapCenter.Y / _vstep);
xComponent = (float)Math.Floor(xComponent);
yComponent = (float)Math.Floor(yComponent);
int indexX = (int)xComponent;
int indexY = (int)yComponent;
GridCell desiredNode = GetCell(indexX,indexY);
return desiredNode;
}
public GridCell GetCell(int x, int y)
{
for (int i = 0; i < Cells.Count; i++)
{
if (Cells[i].X == x &&
Cells[i].Y == y)
return Cells[i];
}
return null;
}
internal void SetParent(WorldMap map)
{
_parent = map;
}
}
} |