//list for game objects to capacity //List<GameObject> capGameObjects = new List<GameObject>(); public CustomGrid() { //initialize dictionary objectNames = new Dictionary <string, Constants.ObjectIDs>() { //utilities { "LevelStartPoint(Clone)", Constants.ObjectIDs.LevelStartPoint }, { "LevelEndPoint(Clone)", Constants.ObjectIDs.LevelEndPoint }, //environment - blocks { "Dirt_Block(Clone)", Constants.ObjectIDs.DirtBlock }, { "Dirt_Block_Grass(Clone)", Constants.ObjectIDs.DirtBlockGrass }, { "Dirt_Block_Sloped(Clone)", Constants.ObjectIDs.DirtBlockSloped }, { "Dirt_Block_Sloped_Grass(Clone)", Constants.ObjectIDs.DirtBlockSlopedGrass }, { "Stone_Block(Clone)", Constants.ObjectIDs.StoneBlock }, { "Stone_Block_Sloped(Clone)", Constants.ObjectIDs.StoneBlockSloped }, { "Stone_Block_Concrete_Top(Clone)", Constants.ObjectIDs.StoneBlockConcreteTop }, { "Stone_Block_Sloped_Concrete_Top(Clone)", Constants.ObjectIDs.StoneBlockSlopedConcreteTop }, //environment - buildings { "HangarClose(Clone)", Constants.ObjectIDs.HangarClose }, { "HangarMiddle(Clone)", Constants.ObjectIDs.HangarMiddle }, { "HangarFar(Clone)", Constants.ObjectIDs.HangarFar }, { "Tower(Clone)", Constants.ObjectIDs.Tower }, //environment - weather { "WeatherHazard1(Clone)", Constants.ObjectIDs.WeatherHazard1 }, { "WeatherHazard2(Clone)", Constants.ObjectIDs.WeatherHazard2 }, { "WeatherHazard3(Clone)", Constants.ObjectIDs.WeatherHazard3 }, //environment - other { "Bird(Clone)", Constants.ObjectIDs.Bird }, //enemies { "MotherShip(Clone)", Constants.ObjectIDs.MotherShip }, { "Zepplin(Clone)", Constants.ObjectIDs.Zepplin }, { "Tank(Clone)", Constants.ObjectIDs.Tank }, { "Soldier(Clone)", Constants.ObjectIDs.Soldier }, { "Jeep(Clone)", Constants.ObjectIDs.Jeep }, { "Bomber(Clone)", Constants.ObjectIDs.Bomber }, }; //declare the grid GridPoints = new CustomGridCell[Constants.LEVEL_EDITOR_GRID_SIZE_X, Constants.LEVEL_EDITOR_GRID_SIZE_Y]; //populate the point array for (int y = 0; y < Constants.LEVEL_EDITOR_GRID_SIZE_Y; y++) { for (int x = 0; x < Constants.LEVEL_EDITOR_GRID_SIZE_X; x++) { GridPoints[x, y] = new CustomGridCell(new Vector3(x, y, 0), new Vector2(x, y)); } } }
/// <summary> /// Attempts to remove an object at the given vector3 click point on the grid. /// </summary> /// <param name="clickPoint">The point on the grid of the mouse click</param> private void RemoveObjectAt(Vector3 clickPoint) { //set the z to 0f clickPoint.z = 0f; //get the target cell from world point location CustomGridCell targetCell = grid.GetGridCellInGrid(clickPoint); //if the target cell is inside the grid and it's occupied, remove object if (targetCell != null && targetCell.IsOccupied) { grid.RemoveGameObjectInGrid(targetCell); } }
/// <summary> /// Attempts to place the selected object at the given vector3 click point on the grid. /// </summary> /// <param name="clickPoint">The point on the grid of the mouse click</param> private void PlaceSelectedObjectAt(Vector3 clickPoint) { //set the z to 0f clickPoint.z = 0f; //get the target cell from world point location CustomGridCell targetCell = grid.GetGridCellInGrid(clickPoint); //if the target cell is inside the grid and it's empty, place object if (targetCell != null && !targetCell.IsOccupied) { grid.SetGameObjectInGrid(targetCell, SelectedObject, IsFlipped); } }
/// <summary> /// Removes a game object from the grid if the cell is occupied /// </summary> /// <param name="cell">the cell to remove a game object from</param> public void RemoveGameObjectInGrid(CustomGridCell cell) { //get coordinates int x = (int)cell.IndexLocation.x; int y = (int)cell.IndexLocation.y; //check if there is an object in the cell if (GridPoints[x, y].IsOccupied) { //remove game object from cell and remove from list //Debug.Log("RemoveGameObjectInGrid: Removing game object in cell at " + GridPoints[x, y].GridLocation); gameObjects.Remove(GridPoints[x, y].CellObject); GridPoints[x, y].CellObject = null; } }
/// <summary> /// Sets an object in the grid if the cell is not occupied /// </summary> /// <param name="cellObject">the object to place in the grid</param> public void SetGameObjectInGrid(CustomGridCell cell, GameObject cellObject, bool flipped) { //get coordinates int x = (int)cell.IndexLocation.x; int y = (int)cell.IndexLocation.y; //check if the cell is occupied if (!GridPoints[x, y].IsOccupied) { //set game object in grid and add to list if (flipped) { GridPoints[x, y].IsFlipped = true; } GridPoints[x, y].CellObject = cellObject; gameObjects.Add(GridPoints[x, y].CellObject); } }