root/src/BRAINSFramework/Map/Grid.cs

User picture

Author: conkerjo

Revision: 30 («Previous)


File Size: 5.32 KB

(November 01, 2009 14:48 UTC) Over 2 years ago

Added Zune HD support and simple sample

 
Show/hide line numbers
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;
        }
    }
}