private void UpdateWallCaps() { if (wallCapPrefab == null) { return; } // Go through each direction and ensure the wallcap is present. for (Direction direction = Direction.North; direction < Direction.NorthWest; direction += 2) { int i = (int)direction / 2; // Get the direction this applies to for the external world Direction outsideDirection = DirectionHelper.Apply(OrientationHelper.ToPrincipalDirection(TileState.orientation), direction); bool isPresent = adjacents.Adjacent(outsideDirection) == 1; if (isPresent && wallCaps[i] == null) { wallCaps[i] = SpawnWallCap(direction); wallCaps[i].name = $"WallCap{i}"; } else if (!isPresent && wallCaps[i] != null) { EditorAndRuntime.Destroy(wallCaps[i]); wallCaps[i] = null; } } }
/// <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); }
private void UpdateWallCaps() { if (wallCapPrefab == null) { return; } // Go through each direction and ensure the wallcap is present. for (Direction direction = Direction.North; direction < Direction.NorthWest; direction += 2) { int i = (int)direction / 2; // Get the direction this applies to for the external world Direction outsideDirection = DirectionHelper.Apply(OrientationHelper.ToPrincipalDirection(TileState.orientation), direction); bool isPresent = adjacents.Adjacent(outsideDirection) == 1; if (isPresent && wallCaps[i] == null) { wallCaps[i] = EditorAndRuntime.InstantiatePrefab(wallCapPrefab, transform); wallCaps[i].name = $"WallCap{i}"; var cardinal = DirectionHelper.ToCardinalVector(DirectionHelper.Apply(Direction.East, direction)); var rotation = DirectionHelper.AngleBetween(Direction.South, direction); wallCaps[i].transform.localRotation = Quaternion.Euler(0, rotation, 0); wallCaps[i].transform.localPosition = new Vector3(cardinal.Item1, 0, cardinal.Item2); } else if (!isPresent && wallCaps[i] != null) { EditorAndRuntime.Destroy(wallCaps[i]); wallCaps[i] = null; } } }
/// <summary> /// Removes a map. Should only be called by the editor. /// </summary> /// <param name="map"></param> public void RemoveMap(TileMap map) { map.Clear(); EditorAndRuntime.Destroy(map.gameObject); mapList.Remove(map); }
/// <summary> /// Destroys itself. /// </summary> public void DestroySelf() { if (adjacencyConnector != null) { adjacencyConnector.CleanAdjacencies(); } EditorAndRuntime.Destroy(gameObject); }
private GameObject CreateGhost() { GameObject ghostObject = EditorAndRuntime.InstantiatePrefab(tileObjectSO.prefab); ghostObject.transform.SetPositionAndRotation(Vector3.zero, Quaternion.identity); ghostObject.name = "Ghost object"; ghostObject.tag = "EditorOnly"; ghostObject.transform.SetParent(tileManager.transform); return(ghostObject); }
/** * <summary>Spawns a wall cap facing a direction, with appropriate position & settings</summary> * <param name="direction">Direction from the centre of the door</param> */ private GameObject SpawnWallCap(Direction direction) { var wallCap = EditorAndRuntime.InstantiatePrefab(wallCapPrefab, transform); var cardinal = DirectionHelper.ToCardinalVector(DirectionHelper.Apply(Direction.East, direction)); var rotation = DirectionHelper.AngleBetween(Direction.East, direction); wallCap.transform.localRotation = Quaternion.Euler(0, rotation, 0); wallCap.transform.localPosition = new Vector3(cardinal.Item1 * WALL_CAP_DISTANCE_FROM_CENTRE, 0, cardinal.Item2 * WALL_CAP_DISTANCE_FROM_CENTRE); return(wallCap); }
private void Awake() { if (_instance != null && _instance != this) { Debug.LogWarning("Duplicate TileManager found. Deleting the last instance"); EditorAndRuntime.Destroy(gameObject); } else { _instance = this; } }
/// <summary> /// Destroys all existing maps. /// </summary> private void DestroyMaps() { foreach (TileMap map in mapList) { map.Clear(); } for (int i = transform.childCount - 1; i >= 0; --i) { EditorAndRuntime.Destroy(transform.GetChild(i).gameObject); } mapList.Clear(); }
/** * <summary>Spawns a wall cap facing a direction, with appropriate position & settings</summary> * <param name="direction">Direction from the centre of the door</param> */ private GameObject SpawnWallCap(Direction direction) { var wallCap = EditorAndRuntime.InstantiatePrefab(wallCapPrefab, transform); Direction cardinalDirectionInput = TileHelper.GetRelativeDirection(direction, doorDirection); var cardinal = TileHelper.ToCardinalVector(cardinalDirectionInput); var rotation = TileHelper.AngleBetween(direction, doorDirection); wallCap.transform.localRotation = Quaternion.Euler(0, rotation, 0); wallCap.transform.localPosition = new Vector3(cardinal.Item1 * WALL_CAP_DISTANCE_FROM_CENTRE, 0, cardinal.Item2 * WALL_CAP_DISTANCE_FROM_CENTRE); return(wallCap); }
/** * Load all children into being tiles, but doesn't do anything with them. * Destroys any child which isn't a tile. */ private void LoadAllChildren() { List <GameObject> queuedDestroy = new List <GameObject>(); for (int i = 0; i < transform.childCount; i++) { var child = transform.GetChild(i); var childTile = child.GetComponent <TileObject>(); if (childTile == null) { Debug.LogWarning("TileMap has child which is not a tile: " + child.name); continue; } #if UNITY_EDITOR // It may be a ghost tile, which should be ignored. if (child.tag == "EditorOnly") { continue; } #endif // If the tile doesn't actually have anything, destroy it if (childTile.Tile.turf == null && childTile.Tile.fixture == null) { queuedDestroy.Add(child.gameObject); continue; } var index = GetIndexAt(childTile.transform.position); ulong key = GetKey(index.x, index.y); if (tiles.ContainsKey(key) && tiles[key] != childTile) { Debug.LogWarning("Tile already exists at [" + index.x.ToString() + ", " + index.y.ToString() + "]. Deleting other."); EditorAndRuntime.Destroy(child.gameObject); } else { tiles[key] = childTile; } } foreach (var gameObject in queuedDestroy) { EditorAndRuntime.Destroy(gameObject); } }
private void CreateWallCaps(bool isPresent, Direction direction) { int capIndex = TileHelper.GetDirectionIndex(direction); if (isPresent && wallCaps[capIndex] == null) { wallCaps[capIndex] = SpawnWallCap(direction); wallCaps[capIndex].name = $"WallCap{capIndex}"; } else if (!isPresent && wallCaps[capIndex] != null) { EditorAndRuntime.Destroy(wallCaps[capIndex]); wallCaps[capIndex] = null; } }
private void Start() { for (int i = transform.childCount - 1; i > 0; --i) { var child = transform.GetChild(i); if (child.name.StartsWith("WallCap")) { int num = 0; bool success = int.TryParse(child.name.Substring(7), out num); if (!success || num > wallCaps.Length) { Debug.LogWarning($"Unusual child found whilst searching for wall caps: {child.name}, deleting"); EditorAndRuntime.Destroy(child.gameObject); continue; } wallCaps[num] = child.gameObject; } } }
private void ValidateChildren() { // Note: This only needs to run on the server, which loads from the scene. // Anywhere else (including clients, mostly), doesn't really need this. for (int i = transform.childCount - 1; i > 0; --i) { var child = transform.GetChild(i); if (child.name.StartsWith("WallCap")) { bool success = int.TryParse(child.name.Substring(7), out int num); // Remove if no int, int out of bounds, or duplicate if (!success || num > wallCaps.Length || num < 0 || (wallCaps[num] != null && !ReferenceEquals(wallCaps[num], child.gameObject))) { Debug.LogWarning($"Unusual child found whilst searching for wall caps: {child.name}, deleting"); EditorAndRuntime.Destroy(child.gameObject); continue; } wallCaps[num] = child.gameObject; } } }