/// <summary> /// Creates a new PlacedTileObject from a TileObjectSO at a given position and direction. Uses NetworkServer.Spawn() if a server is running. /// </summary> /// <param name="worldPosition"></param> /// <param name="origin"></param> /// <param name="dir"></param> /// <param name="tileObjectSO"></param> /// <returns></returns> public static PlacedTileObject Create(Vector3 worldPosition, Vector2Int origin, Direction dir, TileObjectSO tileObjectSO) { GameObject placedGameObject = EditorAndRuntime.InstantiatePrefab(tileObjectSO.prefab); placedGameObject.transform.SetPositionAndRotation(worldPosition, Quaternion.Euler(0, TileHelper.GetRotationAngle(dir), 0)); // Alternative name is required for walls as they can occupy the same tile if (TileHelper.ContainsSubLayers(tileObjectSO.layer)) { placedGameObject.name += "_" + TileHelper.GetDirectionIndex(dir); } PlacedTileObject placedObject = placedGameObject.GetComponent <PlacedTileObject>(); if (placedObject == null) { placedObject = placedGameObject.AddComponent <PlacedTileObject>(); } placedObject.Setup(tileObjectSO, origin, dir); if (NetworkServer.active) { if (!NetworkClient.prefabs.ContainsValue(placedGameObject)) { Debug.LogWarning("Prefab was not found in the Spawnable list. Please add it."); } NetworkServer.Spawn(placedGameObject); } return(placedObject); }
/// <summary> /// Returns a list of TileObjects that will be destroyed if an object on the given layer is destroyed. /// </summary> /// <param name="map"></param> /// <param name="layer"></param> /// <param name="position"></param> /// <returns></returns> public static List <TileObject> GetToBeDestroyedObjects(TileMap map, TileLayer layer, Vector3 position) { List <TileObject> toBeDestroyedList = new List <TileObject>(); // Remove everything when the plenum is missing if (layer == TileLayer.Plenum) { foreach (TileLayer layerToCheck in TileHelper.GetTileLayers()) { if (layerToCheck == TileLayer.Plenum) { continue; } toBeDestroyedList.Add(map.GetTileObject(layerToCheck, position)); } } // Remove any wall fixtures when the turf is missing else if (layer == TileLayer.Turf) { toBeDestroyedList.Add(map.GetTileObject(TileLayer.HighWallMount, position)); toBeDestroyedList.Add(map.GetTileObject(TileLayer.LowWallMount, position)); } // Remove furniture top is furniture base is missing else if (layer == TileLayer.FurnitureBase) { toBeDestroyedList.Add(map.GetTileObject(TileLayer.FurnitureTop, position)); } return(toBeDestroyedList); }
/// <summary> /// Create empty grids for all layers. /// </summary> private void CreateAllGrids() { tileGridList = new List <TileGrid>(); foreach (TileLayer layer in TileHelper.GetTileLayers()) { tileGridList.Add(CreateGrid(layer)); } }
/// <summary> /// Main function for verifying if a tileObjectSO can be placed at a given location. /// </summary> /// <param name="map"></param> /// <param name="position"></param> /// <param name="subLayerIndex"></param> /// <param name="tileObjectSO"></param> /// <param name="dir"></param> /// <returns></returns> public static bool CanBuild(TileMap map, Vector3 position, int subLayerIndex, TileObjectSO tileObjectSO, Direction dir) { TileManager tileManager = TileManager.Instance; TileLayer placedLayer = tileObjectSO.layer; TileObject[] tileObjects = new TileObject[TileHelper.GetTileLayers().Length]; foreach (TileLayer layer in TileHelper.GetTileLayers()) { tileObjects[(int)layer] = map.GetTileObject(layer, position); } // Cannot build anything unless a plenum is placed if (placedLayer != TileLayer.Plenum && !CanBuildOnPlenum(map, position, tileObjectSO, dir)) { return(false); } // No wall mounts on non-walls if (tileObjects[(int)TileLayer.Turf].IsCompletelyEmpty() && (placedLayer == TileLayer.LowWallMount || placedLayer == TileLayer.HighWallMount)) { return(false); } if (placedLayer == TileLayer.LowWallMount || placedLayer == TileLayer.HighWallMount) { if (!CanBuildWallAttachment(map, position, tileObjectSO, dir)) { return(false); } } // No furniture inside walls if (placedLayer == TileLayer.FurnitureBase || placedLayer == TileLayer.FurnitureTop || placedLayer == TileLayer.Overlay) { TileObject wallObject = map.GetTileObject(TileLayer.Turf, position); if (!wallObject.IsCompletelyEmpty() && wallObject.GetPlacedObject(0).GetGenericType().Contains("wall")) { return(false); } } // No walls on furniture if (placedLayer == TileLayer.Turf && tileObjectSO.genericType.Contains("wall") && (!tileObjects[(int)TileLayer.FurnitureBase].IsCompletelyEmpty() || !tileObjects[(int)TileLayer.FurnitureTop].IsCompletelyEmpty() || !tileObjects[(int)TileLayer.Overlay].IsCompletelyEmpty())) { return(false); } return(true); }
/// <summary> /// Returns if all sub layers do not contain a PlacedObject. /// </summary> /// <returns></returns> public bool IsCompletelyEmpty() { bool occupied = false; for (int i = 0; i < TileHelper.GetSubLayerSize(layer); i++) { occupied |= !IsEmpty(i); } return(!occupied); }
/// <summary> /// Sets all gameobjects for a given layer to either enabled or disabled. /// </summary> /// <param name="layer"></param> /// <param name="enabled"></param> public void SetEnabled(TileLayer layer, bool enabled) { for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { for (int i = 0; i < TileHelper.GetSubLayerSize(layer); i++) { GetTileObject(layer, x, y).GetPlacedObject(i)?.gameObject.SetActive(enabled); } } } }
/// <summary> /// Determines if all layers in the chunk are completely empty. /// </summary> /// <returns></returns> public bool IsEmpty() { bool empty = true; foreach (TileLayer layer in TileHelper.GetTileLayers()) { for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { TileObject tileObject = GetTileObject(layer, x, y); if (!tileObject.IsCompletelyEmpty()) { empty = false; } } } } return(empty); }
/// <summary> /// Create a new empty grid for a given layer. /// </summary> /// <param name="layer"></param> /// <returns></returns> private TileGrid CreateGrid(TileLayer layer) { TileGrid grid = new TileGrid { layer = layer }; int gridSize = width * height; grid.tileObjectsGrid = new TileObject[gridSize]; int subLayerSize = TileHelper.GetSubLayerSize(layer); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { grid.tileObjectsGrid[y * width + x] = new TileObject(this, layer, x, y, subLayerSize); } } return(grid); }