/// <summary> /// Scan all rooms and create new world data /// </summary> private void Scan() { foreach (var room in MVMain.Room.rooms) { string debugString = room.Key + " : "; foreach (var receiver in room.Value.receivers) { debugString += receiver.name + " "; } Debug.Log(debugString); } roomQueue = new Queue <Tuple <string, Vector2Int> >(); roomQueueList = new HashSet <string>(); string startMap = MVMain.Room.activeRoomName; roomQueue.Enqueue(new Tuple <string, Vector2Int>(startMap, startScan)); roomQueueList.Add(startMap); while (roomQueue.Count > 0) { var now = roomQueue.Dequeue(); roomPos.Add(now.Item1, now.Item2); if (MVMain.Room.rooms.ContainsKey(now.Item1)) { RoomData roomNow = MVMain.Room.rooms[now.Item1]; for (int i = 0; i < roomNow.chunkSize.x; i++) { for (int j = 0; j < roomNow.chunkSize.y; j++) { ChunkData nowChunk = map[now.Item2.x + i, now.Item2.y + j]; nowChunk.roomName = now.Item1; nowChunk.localChunkPos = new Vector2Int(i, j); //nowChunk.roomColor = roomNow.mapColor; nowChunk.roomColor = Resources.Load <StageData>(MVMain.settings.stageDataPath + roomNow.stage).color; bool up = false; bool down = false; bool left = false; bool right = false; //have up sibling if (i > 0) { nowChunk.SetBig(Direction.Up); up = true; } //have down sibling if (i < roomNow.chunkSize.x - 1) { nowChunk.SetBig(Direction.Down); down = true; } //have left sibling if (j > 0) { nowChunk.SetBig(Direction.Left); left = true; } //have right sibling if (j < roomNow.chunkSize.y - 1) { nowChunk.SetBig(Direction.Right); right = true; } //topleft if (up && left) { nowChunk.diagonal[3] = true; } //topright if (up && right) { nowChunk.diagonal[2] = true; } //bottomright if (down && right) { nowChunk.diagonal[1] = true; } //topleft if (down && left) { nowChunk.diagonal[0] = true; } } } for (int i = 0; i < roomNow.teleporters.Count; i++) { TeleporterData connector = roomNow.teleporters[i]; ChunkData nowChunk = map[now.Item2.x + connector.chunkPos.x, now.Item2.y + connector.chunkPos.y]; nowChunk.SetSmall(connector.teleporterDirection); if (!roomQueueList.Contains(connector.targetRoom)) { Vector2Int nextPos = now.Item2 + connector.chunkPos + MVUtility.DirectionToVector2Int(connector.teleporterDirection); if (MVMain.Room.rooms.ContainsKey(connector.targetRoom)) { RoomData nextRoom = MVMain.Room.rooms[connector.targetRoom]; ReceiverData nextReceiver = nextRoom.receivers.FirstOrDefault(x => x.name == connector.targetTeleporter); nextPos -= nextReceiver.chunkPos; roomQueue.Enqueue(new Tuple <string, Vector2Int>(nextRoom.name, nextPos)); roomQueueList.Add(nextRoom.name); } } } } } }
/// <summary> /// Recalculate RoomData /// </summary> public void RecalculateData() { receivers = new List <ReceiverData>(); Grid grid = Room.GetComponentInChildren <Grid>(); foreach (Tilemap map in grid.GetComponentsInChildren <Tilemap>()) { if (map.name == "Teleporter") { foreach (Vector3Int pos in map.cellBounds.allPositionsWithin) { Vector3Int localplace = new Vector3Int(pos.x, pos.y, pos.z); if (!map.HasTile(localplace)) { continue; } string tileName = map.GetTile(pos).name; Vector2Int tileChunkPos = MVUtility.TilePosToLocalChunkPos(pos.x, pos.y, chunkTopLeft, new Vector2Int(24, 16)); if (tileName[0] == 'O' && tileName.Length == 2 && char.IsDigit(tileName[1])) { //Receiver ReceiverData receiverData = new ReceiverData { name = tileName, chunkPos = tileChunkPos }; receivers.Add(receiverData); } else if (tileName[0] == 'I' && tileName.Length == 2 && char.IsDigit(tileName[1])) { //Teleporter TeleporterData teleporter = teleporters.FirstOrDefault(x => x.teleporter == tileName); if (teleporter == null) { continue; } //atas if (tileChunkPos.x < 0) { teleporter.teleporterType = TeleporterData.TeleporterType.Vertical; teleporter.teleporterDirection = Direction.Up; teleporter.chunkPos = tileChunkPos + Vector2Int.right; } //bawah else if (tileChunkPos.x >= chunkSize.x) { teleporter.teleporterType = TeleporterData.TeleporterType.Vertical; teleporter.teleporterDirection = Direction.Down; teleporter.chunkPos = tileChunkPos + Vector2Int.left; } //kiri else if (tileChunkPos.y < 0) { teleporter.teleporterType = TeleporterData.TeleporterType.Horizontal; teleporter.teleporterDirection = Direction.Left; teleporter.chunkPos = tileChunkPos + Vector2Int.up; } //kanan else if (tileChunkPos.y >= chunkSize.y) { teleporter.teleporterType = TeleporterData.TeleporterType.Horizontal; teleporter.teleporterDirection = Direction.Right; teleporter.chunkPos = tileChunkPos + Vector2Int.down; } //bug :v else { teleporter.teleporterType = TeleporterData.TeleporterType.Direct; teleporter.teleporterDirection = Direction.Up; teleporter.chunkPos = tileChunkPos; } } } } } }