Exemplo n.º 1
0
        /// <summary>
        /// Places or removes an object from the grid.
        /// </summary>
        /// <param name="placeable"></param>
        /// <param name="isPlaced"></param>
        public void TogglePlaceObject(IPlaceable placeable, bool isPlaced)
        {
            Vector2Int gridId = placeable.StartGridId;

            for (int i = 0; i < placeable.GridCost().x; i++)
            {
                for (int j = 0; j < placeable.GridCost().y; j++)
                {
                    grids[gridId.x + i, gridId.y + j].isOccupied = isPlaced;
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Checks if an object of type IPlaceable can be placed on the grid.
        /// Returns true, if the object is inside the grid and on unoccupied tiles.
        /// </summary>
        /// <param name="placeable"></param>
        /// <param name="inputPosition"></param>
        /// <param name="gridPosition"></param>
        /// <returns></returns>
        public bool CanPlaceObject(IPlaceable placeable, Vector2 inputPosition, out Vector2 gridPosition)
        {
            Vector2Int gridId = placeable.StartGridId = GetNearestGridId(inputPosition);

            gridPosition = gridPositions[gridId.x, gridId.y];

            for (int i = 0; i < placeable.GridCost().x; i++)
            {
                for (int j = 0; j < placeable.GridCost().y; j++)
                {
                    if (gridId.x + i >= gridSize || gridId.y + j >= gridSize)
                    {
                        return(false);
                    }

                    if (grids[gridId.x + i, gridId.y + j].isOccupied)
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }