Ejemplo n.º 1
0
 /// <summary>
 /// Serialize the effect
 /// </summary>
 /// <param name="writer">Writer instance</param>
 public override void Write(BinaryWriter writer)
 {
     base.Write(writer);
     writer.Write(mSourcePos);
     writer.Write(mTargetPos);
     writer.Write(mTargetEntity);
     writer.Write(mAlphaFade);
     writer.Write(mProjecticleColor);
     writer.Write(mProjecticleSprite);
     writer.Write(mProjecticleSprite45);
     writer.Write(mRotate);
     writer.Write(SoundSerializer.Serialize(mLaunchSound));
     writer.Write(SoundSerializer.Serialize(mLandSound));
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Save game
        /// </summary>
        /// <param name="filename">File name</param>
        /// <param name="player">Player</param>
        /// <param name="gameMap">Map</param>
        /// <param name="console">Console</param>
        /// <param name="gameState">Current game state</param>
        public static void SaveGame(string filename, EntityID player, GameMap gameMap, Console console, GameState gameState)
        {
            // Create data folder if it doesn't exist
            if (!Directory.Exists(Application.persistentDataPath + "/" + C.SAVE_FOLDER))
            {
                Directory.CreateDirectory(Application.persistentDataPath + "/" + C.SAVE_FOLDER);
            }

            if (!Directory.Exists(Application.persistentDataPath + "/" + C.SAVE_FOLDER))
            {
                console.Log(C.FSTR.Set("Can't create Save folder!"));
                return;
            }

            FileStream file         = null;
            string     fullFilename = Application.persistentDataPath + "/" + C.SAVE_FOLDER + "/" + filename;

            try
            {
                file = File.Open(fullFilename, FileMode.Create);
            }
            catch
            {
                // Do nothing
                return;
            }

            if (file == null)
            {
                console.Log(C.FSTR.Set("Failed to save to ").Append(fullFilename).Append(", can't open file."));
                return;
            }

#if !DEBUG_SAVES
            var writer = new BinaryWriter(file);
#else
            var writer = new GameWriter(file);
#endif

            EntityStore.Write(writer);

            writer.Write(player);

            // Write GameMap
            writer.Write(gameMap.size);

            // Write terrain tiles
            for (int x = 0; x < gameMap.size.width; x++)
            {
                for (int y = 0; y < gameMap.size.height; y++)
                {
                    writer.Write(gameMap.terrain[x, y]);
                }
            }

            // Write FOV tiles
            for (int x = 0; x < gameMap.size.width; x++)
            {
                for (int y = 0; y < gameMap.size.height; y++)
                {
                    writer.Write(gameMap.fov[x, y]);
                }
            }

            // Write background color
            writer.Write(gameMap.backgroundColor);

            // Write music
            writer.Write(SoundSerializer.Serialize(gameMap.music));

            // Write dungeon level
            writer.Write(gameMap.dungeonLevel);

            // Write Console contents
            writer.Write(Console.LOG_LINES);
            var consoleLogLines = console.GetLogLines();
            var consoleLine     = consoleLogLines.Last;
            while (consoleLine != null)
            {
                writer.Write(consoleLine.Value);
                consoleLine = consoleLine.Previous;
            }

            // Write the effects manager
            EffectManager.Instance.Write(writer);

            writer.Write((int)gameState);

            file.Flush();
            writer.Flush();
            writer.Close();
            file.Close();

            // On WebGL we should sync the file system by calling out to Javascript, if we don't do this
            // the game might not actually flush out to the filesystem (IndexDB) before the game quits or page is reloaded.
#if UNITY_WEBGL
            try
            {
                WebGLSyncFS();
            }
            catch
            {
                // Do nothing
            }
#endif
        }