//Checks if cystom shape is in boundaries of the grid public bool IsInMapRange(GCoord origin, List <GCoord> pattern) { bool gate = true; foreach (GCoord localc in pattern) { gate = (origin.x + localc.x <= GridSize.x && origin.y + localc.y <= GridSize.y); } return(gate); }
public Grid(int xsize, int ysize) { //initializing Grid itself this.GridSize = new GCoord(xsize, ysize); this.GridArray = new int[xsize, ysize]; //list of gObjects this.GridObjects = new Dictionary <int, GridObject <T> >(); //Initializing "EMPTY CELL gObject" this.GridObjects.Add(0, new GridObject <T>(0)); FreeIDs = new List <int>(); }
//Detects any Grid collision provided with origin coordinate in global Grid and list of local coordinates relative to origin public bool FitsPattern(GCoord origin, List <GCoord> pattern) { bool gate = IsInMapRange(origin, pattern); if (!gate) { return(gate); } foreach (GCoord localc in pattern) { gate = (GridArray[origin.x + localc.x, origin.y + localc.y] == 0); } return(gate); }
//Checks if rectangle (origin : origin + size) is all empty public bool FitsPattern(GCoord origin, GCoord size) { bool gate = IsInMapRange(origin, size); if (!gate) { return(gate); } for (int x = origin.x; x < origin.x + size.x; x++) { for (int y = origin.y; y < origin.y + size.y; y++) { gate = (GridArray[x, y] == 0); } } return(gate); }
//Place method that works with rectangle shapes public int Place(T gObject, GCoord coordinate, GCoord size) { if (FitsPattern(coordinate, size)) { //finds Id for the place method int assignedID; if (FreeIDs.Count > 0) { assignedID = FreeIDs[0]; FreeIDs.Remove(assignedID); } else { assignedID = GridObjects.Count + 1; } //sets up Grid cells List <GCoord> assignedCells = new List <GCoord>(); for (int y = coordinate.y; y < coordinate.y + size.y; y++) { for (int x = coordinate.x; x < coordinate.x + size.x; x++) { GridArray[x, y] = assignedID; assignedCells.Add(new GCoord(x, y)); } } //Creates gObject and adds it to gridObjects dictionary with corresponding id GridObjects.Add(assignedID, new GridObject <T>(assignedCells, assignedID, gObject)); return(assignedID); } else { return(0); } }
//Place method that works with custom shapes public int Place(T gObject, GCoord origin, List <GCoord> pattern) { if (FitsPattern(origin, pattern)) { //finds Id for the place method int assignedID; if (FreeIDs.Count > 0) { assignedID = FreeIDs[0]; FreeIDs.Remove(assignedID); } else { assignedID = GridObjects.Count + 1; } //sets up Grid cells List <GCoord> assignedCells = new List <GCoord>(); foreach (GCoord localc in pattern) { int cx = origin.x + localc.x, cy = origin.y + localc.y; GridArray[cx, cy] = assignedID; assignedCells.Add(new GCoord(cx, cy)); } //Creates gObject and adds it to gridObjects dictionary with corresponding id GridObjects.Add(assignedID, new GridObject <T>(assignedCells, assignedID, gObject)); return(assignedID); } else { return(0); } }
//Returns GridObject at coordinates public GridObject <T> GridObjectAt(GCoord coord) { return(GridObjects[GridArray[coord.x, coord.y]]); }
//Returns object at coordinates public T ObjectAt(GCoord coord) { return(GridObjects[GridArray[coord.x, coord.y]].gObject); }
//Checks if rectangle shape is in boundaries of the grid public bool IsInMapRange(GCoord origin, GCoord size) { return(origin.x + size.x <= GridSize.x && origin.y + size.y <= GridSize.y); }