root/src/BRAINSFramework/Map/Cluster.cs

User picture

Author: conkerjo

Revision: 30 («Previous)


File Size: 2.69 KB

(July 08, 2009 19:40 UTC) Almost 3 years ago


  

 
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.PathFinding;

namespace Brains.Framework.Map
{
    public class Cluster
    {
        
        private int _rows;
        private int _cols;
        private List<Grid> _grids;
        public int Rows { get { return _rows; } }
        public int Cols { get { return _cols; } }
        public List<Grid> Grids { get { return _grids; } }

        internal Cluster()
        {
            _grids = new List<Grid>();
        }

        internal void AddGrid(Grid grid)
        {
            Grids.Add(grid);
        }

        internal void Setup(int rows, int cols)
        {
            _rows = rows;
            _cols = cols;
        }

        public int TotalCols
        {
            //HACK: should support different sized grids
            get { return Grids[0].Cols * Cols; }
        }

        public int TotalRows
        {
            //HACK: should support different sized grids
            get { return Grids[0].Rows* Rows; }
        }

        public Grid GetGridAtPosition(Vector2 position)
        {
            foreach (var item in Grids)
            {
                if (
                    (position.X > item.Position.X && position.X < (item.Position.X + item.Width)) &&
                    (position.Y > item.Position.Y && position.Y < (item.Position.Y + item.Height))
                    )
                {
                    return item;
                }
            }
            return null;
        }

        public int GetGridIndex(int x, int y)
        {
            return y * Cols + x;
        }
        public Grid GetGrid(int x, int y)
        {
            return Grids[GetGridIndex(x, y)];
        }

        internal void AddEntrance(GridCell cellA, GridCell cellB)
        {
            if (!EntranceExists(cellA, cellB))
                Entrances.Add(new Entrance(cellA, cellB));
        }

        private bool EntranceExists(GridCell cellA, GridCell cellB)
        {
            for (int i = 0; i < Entrances.Count; i++)
            {
                Entrance ent = Entrances[i];
                if (ent.CellA == cellA && ent.CellB == cellB)
                    return true;
            }
            return false;
        }
        public List<Entrance> Entrances = new List<Entrance>();
    }
    public class Entrance
    {
        public GridCell CellA { get; set; }
        public GridCell CellB { get; set; }
        public Entrance(GridCell a, GridCell b)
        {
            CellA = a;
            CellB = b;
        }
      
    }
}