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 |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Brains.Framework;
using Microsoft.Xna.Framework;
using Brains.Framework.QuadTree;
namespace Brains.Framework.Map
{
/// <summary>
/// Holds data about a whole game map.
/// </summary>
public class WorldMap
{
/// <summary>
/// Gets the Quadtree of all cells in the map
/// </summary>
public QuadTree<GridCell> GridCellTree;
/// <summary>
/// Gets the cluster of grids
/// </summary>
public Cluster ClusterGrid { get { return _clusterGrid; } }
private Cluster _clusterGrid;
private int _width;
private int _height;
private List<GridCell> _allCells=new List<GridCell>();
/// <summary>
/// Gets all of the cells in the map
/// </summary>
public List<GridCell> AllCells
{
get { return _allCells; }
set { _allCells = value; }
}
/// <summary>
/// Gets the top left position of the world
/// </summary>
public Vector2 TopLeft
{
get { return _clusterGrid.Grids[0].Position; }
}
/// <summary>
/// Gets the bottom right position of the world
/// </summary>
public Vector2 BottomRight
{
get { return _clusterGrid.Grids[_clusterGrid.Grids.Count - 1].Position + _clusterGrid.Grids[_clusterGrid.Grids.Count - 1].Size; }
}
internal WorldMap(int width, int height)
{
_clusterGrid = new Cluster();
_width = width;
_height = height;
GridCellTree = new QuadTree<GridCell>(
new RectangleF(Vector2.Zero, new Vector2(width, height)),64);
}
/// <summary>
/// Adds the specified Grid to the Grids Collection
/// </summary>
/// <param name="grid">The grid to add</param>
internal void AddGrid(Grid grid)
{
grid.SetParent(this);
_clusterGrid.AddGrid(grid);
AddToQuadTree(grid);
}
private void AddToQuadTree(Grid grid)
{
foreach (var item in grid.Cells)
{
AllCells.Add(item);
GridCellTree.Insert(new QuadTreePositionItem<GridCell>(
item, item.Position, new Vector2(grid.CellSize)));
}
}
internal void AddGrid(int width, int height, int cellSize)
{
Grid _grid = new Grid(width, height, cellSize);
AddGrid(_grid);
}
}
} |