root/src/BRAINSFramework/Map/WorldMap.cs

User picture

Author: conkerjo

Revision: 30 («Previous)


File Size: 2.71 KB

(July 01, 2009 23:47 UTC) Almost 3 years ago

Documentation comments

 
Show/hide line numbers

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);
        }
    }
}