Terrarium.Game.WorldState.FindOrganismsInCells C# (CSharp) Method

FindOrganismsInCells() public method

Finds all organisms within a range of cells.
public FindOrganismsInCells ( int minGridX, int maxGridX, int minGridY, int maxGridY ) : ArrayList
minGridX int Leftmost grid cell
maxGridX int Rightmost grid cell
minGridY int Topmost grid cell
maxGridY int Bottommost grid cell
return System.Collections.ArrayList
        public ArrayList FindOrganismsInCells(int minGridX, int maxGridX, int minGridY, int maxGridY)
        {
            Debug.Assert(IndexBuilt);

            Debug.Assert(minGridX <= maxGridX && minGridY <= maxGridY);
            Debug.Assert(minGridX >= 0 && maxGridX < _gridWidth && minGridY >= 0 && maxGridY < _gridHeight);

            OrganismState lastFound = null;

            // Since organisms are represented at multiple places in the grid, make
            // sure we only get one instance
            var foundHash = new Hashtable();
            var foundOrganisms = new ArrayList();
            for (var x = minGridX; x <= maxGridX; x++)
            {
                for (var y = minGridY; y <= maxGridY; y++)
                {
                    var current = _cellOrganisms[x, y];
                    if (current == null) continue;

                    // If it's the same as the last one, skip the hashable lookup
                    // since it's expensive and we'll often find the same organism over and over
                    // in a row
                    if (lastFound != null && lastFound == current) continue;
                    if (foundHash[current] == null)
                    {
                        foundHash[current] = current;
                        foundOrganisms.Add(current);
                    }

                    lastFound = current;
                }
            }

            return foundOrganisms;
        }