Example #1
0
 /// <summary>
 /// Set up a new PlacedTileObject.
 /// </summary>
 /// <param name="tileObjectSO"></param>
 /// <param name="origin"></param>
 /// <param name="dir"></param>
 public void Setup(TileObjectSO tileObjectSO, Vector2Int origin, Direction dir)
 {
     this.tileObjectSO  = tileObjectSO;
     this.origin        = origin;
     this.dir           = dir;
     adjacencyConnector = GetComponent <IAdjacencyConnector>();
 }
Example #2
0
        /// <summary>
        /// Determines if a TileObjectSO can be build at the given location.
        /// </summary>
        /// <param name="map"></param>
        /// <param name="subLayerIndex"></param>
        /// <param name="tileObjectSO"></param>
        /// <param name="position"></param>
        /// <param name="dir"></param>
        /// <returns></returns>
        public bool CanBuild(TileMap selectedMap, int subLayerIndex, TileObjectSO tileObjectSO, Vector3 position, Direction dir, bool overrideAllowed)
        {
            bool canBuild = true;

            foreach (TileMap map in mapList)
            {
                if (map == selectedMap)
                {
                    if (overrideAllowed)
                    {
                        // Do not check if the tile is occupied. Only apply tile restrictions.
                        canBuild &= map.CanBuild(subLayerIndex, tileObjectSO, position, dir, CheckRestrictions.OnlyRestrictions);
                    }
                    else
                    {
                        // Check for tile restrictions as well.
                        canBuild &= map.CanBuild(subLayerIndex, tileObjectSO, position, dir, CheckRestrictions.Everything);
                    }
                }
                else
                {
                    // Only check if the tile is occupied. Otherwise we cannot build furniture for example.
                    canBuild &= map.CanBuild(subLayerIndex, tileObjectSO, position, dir, CheckRestrictions.None);
                }
            }
            return(canBuild);
        }
Example #3
0
 /// <summary>
 /// Sets a new TileObjectSO at a map for a given position and direction. Wrapper function for TileMap.SetTileObject().
 /// </summary>
 /// <param name="map"></param>
 /// <param name="subLayerIndex"></param>
 /// <param name="tileObjectSO"></param>
 /// <param name="position"></param>
 /// <param name="dir"></param>
 public void SetTileObject(TileMap map, int subLayerIndex, TileObjectSO tileObjectSO, Vector3 position, Direction dir)
 {
     if (CanBuild(map, subLayerIndex, tileObjectSO, position, dir, false))
     {
         map.SetTileObject(subLayerIndex, tileObjectSO, position, dir);
     }
 }
Example #4
0
        /// <summary>
        /// Checks whether a wall attachment can be placed and if it collides with a nearby wall.
        /// </summary>
        /// <param name="map"></param>
        /// <param name="position"></param>
        /// <param name="wallAttachment"></param>
        /// <param name="dir"></param>
        /// <returns></returns>
        private static bool CanBuildWallAttachment(TileMap map, Vector3 position, TileObjectSO wallAttachment, Direction dir)
        {
            TileObject wallObject = map.GetTileObject(TileLayer.Turf, position);

            // Cannot build when there isn't a wall
            if (wallObject.IsCompletelyEmpty() || !wallObject.GetPlacedObject(0).GetGenericType().Contains("wall"))
            {
                return(false);
            }

            // No low wall mounts on windows
            if (wallObject.GetPlacedObject(0).GetName().Contains("window") && wallAttachment.layer == TileLayer.LowWallMount)
            {
                return(false);
            }

            // Cannot build wall mount if it collides with the next wall
            PlacedTileObject[] adjacentObjects = map.GetNeighbourObjects(TileLayer.Turf, 0, position);
            if (adjacentObjects[(int)dir] && adjacentObjects[(int)dir].GetGenericType().Contains("wall"))
            {
                return(false);
            }

            return(true);
        }
Example #5
0
        /// <summary>
        /// Checks whether an object can be build on top of a certain plenum
        /// </summary>
        /// <param name="map"></param>
        /// <param name="position"></param>
        /// <param name="plenumAttachment"></param>
        /// <param name="dir"></param>
        /// <returns></returns>
        private static bool CanBuildOnPlenum(TileMap map, Vector3 position, TileObjectSO plenumAttachment, Direction dir)
        {
            TileObject plenumObject = map.GetTileObject(TileLayer.Plenum, position);

            // No plenum means we cannot build anything on top
            if (plenumObject.IsCompletelyEmpty())
            {
                return(false);
            }

            // Only allow wires and machines on catwalks
            if (plenumObject.GetPlacedObject(0).name.Contains("Catwalk") && (plenumAttachment.layer != TileLayer.Wire &&
                                                                             plenumAttachment.layer != TileLayer.FurnitureBase))
            {
                return(false);
            }

            // Can only build on a Plenum and not Catwalks or Lattices
            if (!plenumObject.GetPlacedObject(0).name.Contains("Plenum") && !plenumObject.GetPlacedObject(0).name.Contains("Catwalk"))
            {
                return(false);
            }

            return(true);
        }
Example #6
0
        /// <summary>
        /// Simplified version of CanBuild(). Assumes the main map is used and no sub layers are needed.
        /// </summary>
        /// <param name="tileObjectSO"></param>
        /// <param name="position"></param>
        /// <param name="dir"></param>
        /// <returns></returns>
        public bool CanBuild(TileObjectSO tileObjectSO, Vector3 position, Direction dir)
        {
            if (tileObjectSO.layer == TileLayer.HighWallMount || tileObjectSO.layer == TileLayer.LowWallMount)
            {
                Debug.LogError("Simplified function CanBuild() is used. Do not use this function with layers where a sub index is required!");
            }

            return(CanBuild(GetMainMap(), 0, tileObjectSO, position, dir, false));
        }
Example #7
0
        /// <summary>
        /// Simplified version of SetTileObject. Will set a TileObjectSO on the main map without a sub layer.
        /// </summary>
        /// <param name="tileObjectSO"></param>
        /// <param name="position"></param>
        /// <param name="dir"></param>
        public void SetTileObject(TileObjectSO tileObjectSO, Vector3 position, Direction dir)
        {
            if (tileObjectSO.layer == TileLayer.HighWallMount || tileObjectSO.layer == TileLayer.LowWallMount)
            {
                Debug.LogError("Simplified function SetTileObject() is used. Do not use this function with layers where a sub index is required!");
            }

            GetMainMap().SetTileObject(0, tileObjectSO, position, dir);
        }
Example #8
0
        /// <summary>
        /// Returns a TileObjectSO for a given name. Used during loading to find a matching object.
        /// </summary>
        /// <param name="tileObjectSOName"></param>
        /// <returns></returns>
        public TileObjectSO GetTileObjectSO(string tileObjectSOName)
        {
            TileObjectSO tileObjectSO = tileObjectSOs.FirstOrDefault(tileObject => tileObject.nameString == tileObjectSOName);

            if (tileObjectSO == null)
            {
                Debug.LogError("TileObjectSO was not found: " + tileObjectSOName);
            }
            return(tileObjectSO);
        }
Example #9
0
        /// <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);
        }
Example #10
0
        /// <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);
        }