public ObjectGrid(int mWidth, int mHeight, int gWidth, int gHeight) { gridHeight = gHeight; gridWidth = gWidth; mapHeight = mHeight; mapWidth = mWidth; objects = new List<Entity>(); //created grids int verticalGrids = mHeight/gHeight; if (mHeight%gHeight != 0) verticalGrids++; int horizontalGrids = mWidth/gWidth; if (mWidth%gWidth != 0) horizontalGrids++; grids = new Grid[horizontalGrids][]; for (int i = 0; i < horizontalGrids; i++) { grids[i]=new Grid[verticalGrids]; for (int j = 0; j < verticalGrids; j++) { grids[i][j]=new Grid(); } } }
//parameters are expected to be in arrays of x, y format public List<Entity> GetActiveObjects(int[] upperLeftCorner, int[] upperRightCorner, int[] lowerLeftCorner, int[] lowerRightCorner) { List<Entity> activeObjects = new List<Entity>(); Grid[] foundGrid = new Grid[4]; foundGrid[0] = FindGrid(upperLeftCorner[0], upperLeftCorner[1]); foundGrid[1] = FindGrid(upperRightCorner[0], upperRightCorner[1]); foundGrid[2] = FindGrid(lowerLeftCorner[0], lowerLeftCorner[1]); foundGrid[3] = FindGrid(lowerRightCorner[0], lowerRightCorner[1]); //add grid objects to active objects list //add grid0 activeObjects.AddRange(foundGrid[0].getObjects()); //add grid1 if it hasn't been added yet if (foundGrid[1] != foundGrid[0]) { activeObjects.AddRange(foundGrid[1].getObjects()); } //add grid2 if it hasn't been if (foundGrid[2] != foundGrid[1] && foundGrid[2] != foundGrid[0]) { activeObjects.AddRange(foundGrid[2].getObjects()); } //add final grid if it hasn't been if (foundGrid[3] != foundGrid[2] && foundGrid[3] != foundGrid[1] && foundGrid[3] != foundGrid[0]) { activeObjects.AddRange(foundGrid[3].getObjects()); } return activeObjects; }