/// <summary> /// Loads a room data and image file /// </summary> /// <param name="dataFile">Room data file</param> /// <param name="imageFile">Room image file</param> /// <returns></returns> public static Room Load(string dataFile,string imageFile) { var dataFileWithPath = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName+@"\Images\"+dataFile; Room newRoom; if (!File.Exists(dataFileWithPath)) return null; using (var stream = File.Open(dataFileWithPath, FileMode.Open)) using (var reader = new BinaryReader(stream, Encoding.UTF8)) { var hd = Encoding.UTF8.GetBytes(Header); var buffer = new byte[hd.Length]; reader.Read(buffer, 0, buffer.Length); var readHeader = Encoding.UTF8.GetString(buffer); if (readHeader != Header) return null; var posX = reader.ReadDouble(); var posY = reader.ReadDouble(); var tilesLength = reader.ReadInt32(); newRoom = new Room(new Vector2((float)posX,(float)posY),imageFile) { Tiles = new Tile[tilesLength] }; for (var i = 0; i < tilesLength; i++) { var tt = (TileTypes)reader.ReadInt32(); newRoom.Tiles[i] = new Tile() { TileType = tt }; } } return newRoom; }
public override void afterChanged() { List<PacketType_SC> packetList = new List<PacketType_SC>(); packetList.Add( PacketType_SC.Room_PlayerArrive ); packetList.Add( PacketType_SC.Room_PlayerLeave ); packetList.Add( PacketType_SC.GameStart ); networkManager.register(this, packetList); room = (Room)data["RoomInfo"]; user = (User)data["UserInfo"]; map = room.getMap(); }
/// <summary> /// Saves a room to data file /// </summary> /// <param name="room">Room to save</param> /// <param name="filename">Absolute path to file</param> /// <returns></returns> public static bool Save(Room room, string filename) { using (var stream = File.Open(filename, FileMode.Create)) using (var writer = new BinaryWriter(stream, Encoding.UTF8)) { var hd = Encoding.UTF8.GetBytes(Header); writer.Write(hd, 0, hd.Length); writer.Write(room.Position.X); writer.Write(room.Position.Y); writer.Write(room.Tiles.Length); foreach (var tile in room.Tiles) { var tt = (int)tile.TileType; writer.Write(tt); } } return true; }
public override void read(PacketData _data) { PacketType_SC type = (PacketType_SC)_data.getShort(); if( type == PacketType_SC.LobbyPlayersInfo ) { } else if( type == PacketType_SC.LobbyRoomsInfo ) { // 기존에 있던 방정보 다 삭제 // for(int i=0; i<rooms.Length; ++i) rooms[i] = null; // 총 방의 갯수 totalNumberOfRoom = _data.getInt(); // 현재 페이지의 방 갯수 // int nRoomsInPage = _data.getInt(); //Debug.Log("nRooms In Page : " + nRoomsInPage ); // 매 방마다 초기화 // for(int i=0; i<nRoomsInPage; ++i) { // 방 ID // int roomId = _data.getInt(); // 방 이름 // byte[] nameOfRoom = new byte[16]; _data.getStr( nameOfRoom, 16 ); string name = new string(Encoding.ASCII.GetChars( nameOfRoom )); Debug.Log(i + "th Room Name : " + name); // 맵 ID // int mapId = _data.getInt(); // 방에 있는 유져 // int nUsersInRoom = _data.getInt(); // 방이 게임 중인지 아닌지 // bool onPlaying = _data.getBoolean(); //int _id, string newRoomName, Map _map // rooms[i] = new Room( roomId, name, Map.getMap( mapId ), nUsersInRoom ); } } else if( type == PacketType_SC.CreateRoom_Success ) { int roomId = _data.getInt(); Room newRoom = new Room( roomId, creatingRoomName, currentCreatingMap, 1 ); newRoom.setCreator( user ); ArrayList users = new ArrayList(); users.Add( user ); newRoom.setUsers(users); data["RoomInfo"] = newRoom ; data["UserInfo"] = user ; gameStateMachine.changeState( enum_States.ROOM, data ); } else if( type == PacketType_SC.JoinRoom_Success ) { //Debug.Log("Join Success"); Room joiningRoom = null; ArrayList usersInRoom = new ArrayList(); // find room int roomId = _data.getInt(); for(int i=0; i<6; ++i){ if( roomId == rooms[i].getId() ){ joiningRoom = rooms[i]; break; } } if( joiningRoom == null ) return; // init users // int nUsers = _data.getInt(); int ownerId = _data.getInt(); for(int i=0; i<nUsers; ++i) { int uid = _data.getInt(); byte[] userIDInBytes = new byte[16+1]; _data.getStr( userIDInBytes, 16 ); string userID = new string(Encoding.ASCII.GetChars( userIDInBytes )); User newUser = new User( uid, userID, ""); usersInRoom.Add( newUser ); //set owner if( uid == ownerId ) joiningRoom.setCreator(newUser); } usersInRoom.Add( user ); // replace rooms users joiningRoom.setUsers( usersInRoom ); data["RoomInfo"] = joiningRoom ; data["UserInfo"] = user ; gameStateMachine.changeState(enum_States.ROOM,data); } else if( type == PacketType_SC.JoinRoom_Full ) { Debug.Log("Join Full"); } else if( type == PacketType_SC.JoinRoom_Fail ) { Debug.Log("Join Fail"); } }
/// <summary> /// Adds a room to the campaign. /// </summary> /// <param name="id">The room ID.</param> /// <param name="room">The room object.</param> protected void AddRoom(string id, Room room) { this.rooms.Add(id, room); }