Example #1
0
        /// <summary>
        /// Adds an object to the WorldGrid.
        /// </summary>
        /// <param name="obj">Object to add.</param>
        public void Add(WorldComponent obj)
        {
            try
            {
                Tuple <int, int> cellIndex =
                    GetCellFromPoint(obj.Position.ToPoint());
                WorldCell cell = Cells[cellIndex.Item1][cellIndex.Item2];

                cell.Add(obj);
                obj.Cell = cell;
            }
            catch
            {
            }
        }
Example #2
0
        /// <summary>
        /// Creates a new instance of a WorldGrid.
        /// </summary>
        /// <param name="width">Width of world.</param>
        /// <param name="height">Height of world.</param>
        /// <param name="cellwidth">Width of a cell.</param>
        /// <param name="cellheight">Height of a cell.</param>
        public WorldGrid(int width, int height, int cellwidth, int cellheight)
        {
            Width      = width;
            Height     = height;
            CellWidth  = cellwidth;
            CellHeight = cellheight;

            // Initialise grid array
            Cells = new WorldCell[CellCountX][];
            for (int i = 0; i < CellCountX; i++)
            {
                Cells[i] = new WorldCell[CellCountY];
                for (int j = 0; j < CellCountY; j++)
                {
                    Cells[i][j] = new WorldCell(this);
                }
            }
        }
Example #3
0
        /// <summary>
        /// Updates a given object within the world grid. Moving it to a new
        /// cell if required.
        /// </summary>
        /// <param name="obj">Object to update.</param>
        public void Update(WorldComponent obj)
        {
            try
            {
                Tuple <int, int> cellIndex =
                    GetCellFromPoint(obj.Position.ToPoint());
                WorldCell cell = Cells[cellIndex.Item1][cellIndex.Item2];

                if (obj.Cell != cell)
                {
                    obj.Cell.Remove(obj);
                    cell.Add(obj);
                    obj.Cell = cell;
                }
            }
            catch
            {
            }
        }