Exemple #1
0
        private static void ReceivePackets(int channel = -1)
        {
            int i   = 0;
            int max = 10;

            if (channel != -1)
            {
                i   = channel;
                max = channel + 1;
            }
            for (i = 0; i < max; i++)
            {
                while (SteamNetworking.IsP2PPacketAvailable(out uint messageSize, i))
                {
                    Console.WriteLine("Packet available of size: " + messageSize);
                    byte[] pubDest = new byte[messageSize];
                    if (SteamNetworking.ReadP2PPacket(pubDest, messageSize, out uint bytesRead, out CSteamID steamIdRemote, i))
                    {
                        switch (i)
                        {
                        case BB_LevelData:
                            IsActive = true;
                            WorldConfigFile config = (WorldConfigFile)CalcHelper.ConvertToObject(pubDest);
                            config.LoadIntoEditor();
                            break;

                        case BB_Ready:
                            _playersReadyCount++;
                            break;

                        case BB_StartTime:
                            _startTime = new DateTime((long)CalcHelper.ConvertToObject(pubDest));
                            break;

                        case BB_TileIdChange:
                            Packet.TileIdChange packet = (Packet.TileIdChange)CalcHelper.ConvertToObject(pubDest);
                            LevelEditor.UpdateTileFromP2P(packet);
                            break;

                        case BB_AllEntities:
                            UpdateEntities(pubDest);
                            break;

                        case BB_EntityUpdate:
                            UpdateEntities(pubDest);
                            break;
                        }

                        // Relay message to others.
                        if (IsHost)
                        {
                            var allButSender = (from client in OtherPlayers
                                                where client != steamIdRemote
                                                select client).ToList();
                            Send(pubDest, EP2PSend.k_EP2PSendReliable, i, allButSender);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Attempts to create a new world.
        /// </summary>
        /// <param name="levelName">The name of the level.</param>
        /// <param name="width">The width of the level.</param>
        /// <param name="height">The height of the level.</param>
        public static string CreateNewLevel(string levelName, short width, short height)
        {
            string filePath;

            try
            {
                filePath = GetFilePath(levelName);
            }
            catch
            {
                throw;
            }

            WorldConfigFile config = new WorldConfigFile(levelName, width, height);

            // Creates the file for the world.
            XmlSerializer xs = new XmlSerializer(typeof(WorldConfigFile));

            using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate))
            {
                xs.Serialize(fs, config);
            }

            return(filePath);
        }
        /// <summary>
        /// Loads the level but does not change game state.
        /// </summary>
        /// <param name="filePath"></param>
        public static void LoadLevelForBackground(string filePath)
        {
            // Do not check if level is valid or the game will not launch if there are errors in the file.
            WorldConfigFile config = GetWorldConfigFile(filePath);

            CurrentLevelFilePath = filePath;
            config.LoadIntoView();
        }
        private static void SaveLevel(WorldConfigFile config)
        {
            XmlSerializer xs = new XmlSerializer(typeof(WorldConfigFile));

            using (FileStream fs = new FileStream(CurrentLevelFilePath, FileMode.OpenOrCreate))
            {
                xs.Serialize(fs, config);
            }
        }
Exemple #5
0
        public static void SendLevel()
        {
            DataFolder.DeleteLevel(DataFolder.LevelDirectory + "/temp.lvl");
            string filePath = DataFolder.CreateNewLevel("temp", 256, 256);

            WorldConfigFile config = DataFolder.GetWorldConfigFile(filePath);

            byte[] levelData = CalcHelper.ToByteArray(config);

            Send(levelData, EP2PSend.k_EP2PSendReliable, BB_LevelData, null);

            config.LoadIntoEditor();
        }
        /// <summary>
        /// Used to load a level into gameworld and play it.
        /// </summary>
        /// <param name="filePath"></param>
        public static void PlayLevel(string filePath)
        {
            WorldConfigFile config = GetWorldConfigFile(filePath);

            CurrentLevelFilePath = filePath;

            if (!config.IsValidLevel())
            {
                throw new Exception("This level cannot be played because there is no player spawn point. Edit the level first.");
            }

            config.LoadIntoPlay();
        }
        /// <summary>
        /// Saves the current game world to the current level file.
        /// </summary>
        public static void SaveLevel()
        {
            XmlSerializer xs = new XmlSerializer(typeof(WorldConfigFile));

            DeleteLevel(CurrentLevelFilePath);

            try
            {
                using (FileStream fs = new FileStream(CurrentLevelFilePath, FileMode.OpenOrCreate))
                {
                    WorldConfigFile config = new WorldConfigFile();
                    config.GetDataFromGameWorld();
                    xs.Serialize(fs, config);
                }
            }
            catch
            {
            }
        }
        /// <summary>
        /// Used to load a level into gameworld and edit it.
        /// </summary>
        /// <param name="filePath"></param>
        public static void EditLevel(string filePath)
        {
            WorldConfigFile config = GetWorldConfigFile(filePath);

            if (config.TileIDs.Length == 0)
            {
                TMBAW_Game.MessageBox.Show("There is something wrong with this level and it cannot be loaded.");
                return;
            }

            if (!config.CanBeEdited)
            {
                TMBAW_Game.MessageBox.Show("This level cannot be edited.");
                return;
            }

            CurrentLevelFilePath = filePath;
            config.LoadIntoEditor();
        }
        /// <summary>
        /// Renames the specified file.
        /// </summary>
        /// <param name="filePath">The file path of the file.</param>
        /// <param name="oldName">The current name of the file.</param>
        /// <param name="newName">The new name for the file.</param>
        public static void RenameFile(string filePath, string oldName, string newName)
        {
            // Try to see if level can be edited. If there is a problem with the lvl file, abort.
            WorldConfigFile config;

            try
            {
                config = GetWorldConfigFile(filePath);
                if (!config.CanBeEdited)
                {
                    TMBAW_Game.MessageBox.Show("This level cannot be renamed.");
                    return;
                }
            }
            catch
            {
                return;
            }

            // Exceptions are thrown when level name is invalid and are handled by the caller.
            string newFilePath;

            try
            {
                newFilePath = GetFilePath(newName);
            }
            catch
            {
                throw;
            }

            // Proceed if eveything worked.
            File.Move(filePath, newFilePath);

            // Rename the level inside the config file.
            WorldConfigFile config2 = GetWorldConfigFile(newFilePath);

            config.LevelName     = newName;
            CurrentLevelFilePath = newFilePath;
            SaveLevel(config2);
        }
        /// <summary>
        /// Deletes the specified file.
        /// </summary>
        /// <param name="filePath"></param>
        public static void DeleteLevel(string filePath)
        {
            try
            {
                WorldConfigFile config = GetWorldConfigFile(filePath);
                if (!config.CanBeEdited)
                {
                    TMBAW_Game.MessageBox.Show("This level cannot be deleted.");
                    return;
                }
            }
            catch
            {
                // Do nothing. If it can't read the file, let the user delete it.
            }

            try
            {
                File.Delete(filePath);
            }
            catch
            {
            }
        }
Exemple #11
0
 public LevelPacket(WorldConfigFile config)
 {
     this._config = config;
 }
Exemple #12
0
 public void CreateNewWorld(string levelName)
 {
     WorldConfigFile config = new WorldConfigFile(levelName,256, 256);
     config.LoadIntoEditor();
 }
Exemple #13
0
 public LevelPacket(WorldConfigFile config)
 {
     this._config = config;
 }
Exemple #14
0
        /// <summary>
        /// Attempts to create a new world.
        /// </summary>
        /// <param name="levelName">The name of the level.</param>
        /// <param name="width">The width of the level.</param>
        /// <param name="height">The height of the level.</param>
        public static string CreateNewLevel(string levelName, short width, short height)
        {
            // Checks to see if levelName is valid.
            if (levelName == null)
            {
                throw new Exception("The name of your level cannot be nothing.");
            }
            if (levelName.Length > 20 || levelName.Length < 3)
            {
                throw new Exception("The name of your level must be between 3 and 20 characters.");
            }
            if (levelName.IndexOfAny(Path.GetInvalidFileNameChars()) > 0)
            {
                throw new Exception("The name of your level contains invalid characters");
            }

            // Creates the new world and sets the file path for it.
            string filePath = Path.Combine(LevelDirectory, levelName);
            filePath += LevelFileExt;

            // Checks to see if name already exists.
            if (File.Exists(filePath))
            {
                throw new Exception("A level with this name: " + filePath + " already exists.");
            }

            WorldConfigFile config = new WorldConfigFile(levelName, width, height);

            // Creates the file for the world.
            XmlSerializer xs = new XmlSerializer(typeof(WorldConfigFile));
            using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate))
            {
                xs.Serialize(fs, config);
            }

            return filePath;
        }
Exemple #15
0
 private static void SaveLevel(WorldConfigFile config)
 {
     XmlSerializer xs = new XmlSerializer(typeof(WorldConfigFile));
     using (FileStream fs = new FileStream(CurrentLevelFilePath, FileMode.OpenOrCreate))
     {
         xs.Serialize(fs, config);
     }
 }
Exemple #16
0
 /// <summary>
 /// Send a level packet of the specified world config file.
 /// </summary>
 /// <param name="config"></param>
 public void SendLevelPacket(WorldConfigFile config)
 {
     byte[] data = CalcHelper.ToByteArray(new LevelPacket(config));
     foreach (Client c in Clients)
     {
         c.SendLevelOverTcp(data);
     }
 }