public FactionData_v2(FactionData_v1 v1)
 {
     FactionFile.RelinkChildren(v1.factionDict);
     factionDict         = v1.factionDict;
     factionNameToIDDict = v1.factionNameToIDDict;
     Debug.Log("Migrated FactionData_v1 to FactionData_v2 and relinked children.");
 }
Beispiel #2
0
        public void RestoreFactionData(FactionData_v1 factionData)
        {
            PlayerEntity entity = playerEntityBehaviour.Entity as PlayerEntity;

            entity.FactionData.FactionDict         = factionData.factionDict;
            entity.FactionData.FactionNameToIDDict = factionData.factionNameToIDDict;
        }
Beispiel #3
0
        IEnumerator SaveGame(string saveName, string path)
        {
            // Build save data
            SaveData_v1 saveData = BuildSaveData();

            // Build save info
            SaveInfo_v1 saveInfo = new SaveInfo_v1();

            saveInfo.saveVersion   = LatestSaveVersion;
            saveInfo.saveName      = saveName;
            saveInfo.characterName = saveData.playerData.playerEntity.name;
            saveInfo.dateAndTime   = saveData.dateAndTime;

            // Build faction data
            FactionData_v1 factionData = GetPlayerFactionData();

            // Serialize save data to JSON strings
            string saveDataJson    = Serialize(saveData.GetType(), saveData);
            string saveInfoJson    = Serialize(saveInfo.GetType(), saveInfo);
            string factionDataJson = Serialize(factionData.GetType(), factionData);

            // Create screenshot for save
            // TODO: Hide UI for screenshot or use a different method
            yield return(new WaitForEndOfFrame());

            Texture2D screenshot = new Texture2D(Screen.width, Screen.height);

            screenshot.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
            screenshot.Apply();

            // Save data to files
            WriteSaveFile(Path.Combine(path, saveDataFilename), saveDataJson);
            WriteSaveFile(Path.Combine(path, saveInfoFilename), saveInfoJson);
            WriteSaveFile(Path.Combine(path, factionDataFilename), factionDataJson);

            // Save automap state
            try
            {
                Dictionary <string, DaggerfallAutomap.AutomapGeometryDungeonState> automapState = GameManager.Instance.Automap.GetState();
                string automapDataJson = Serialize(automapState.GetType(), automapState);
                WriteSaveFile(Path.Combine(path, automapDataFilename), automapDataJson);
            }
            catch (Exception ex)
            {
                string message = string.Format("Failed to save automap state. Message: {0}", ex.Message);
                Debug.Log(message);
            }

            // Save screenshot
            byte[] bytes = screenshot.EncodeToJPG();
            File.WriteAllBytes(Path.Combine(path, screenshotFilename), bytes);

            // Raise OnSaveEvent
            RaiseOnSaveEvent(saveData);

            // Notify
            DaggerfallUI.Instance.PopupMessage(HardStrings.gameSaved);
        }
Beispiel #4
0
        public object GetFactionSaveData()
        {
            FactionData_v1 factionData = new FactionData_v1();

            PlayerEntity entity = playerEntityBehaviour.Entity as PlayerEntity;

            factionData.factionDict         = entity.FactionData.FactionDict;
            factionData.factionNameToIDDict = entity.FactionData.FactionNameToIDDict;

            return(factionData);
        }
Beispiel #5
0
        void RestoreFactionData(FactionData_v1 factionData)
        {
            if (factionData == null)
            {
                return;
            }

            if (serializablePlayer)
            {
                serializablePlayer.RestoreFactionData(factionData);
            }
        }
Beispiel #6
0
        IEnumerator LoadGame(string path)
        {
            GameManager.Instance.PlayerDeath.ClearDeathAnimation();
            GameManager.Instance.PlayerMotor.CancelMovement = true;
            InputManager.Instance.ClearAllActions();

            // Read save data from files
            string saveDataJson    = ReadSaveFile(Path.Combine(path, saveDataFilename));
            string factionDataJson = ReadSaveFile(Path.Combine(path, factionDataFilename));

            // Deserialize JSON strings
            SaveData_v1 saveData = Deserialize(typeof(SaveData_v1), saveDataJson) as SaveData_v1;

            // Must have a serializable player
            if (!serializablePlayer)
            {
                yield break;
            }

            // Call start load event
            RaiseOnStartLoadEvent(saveData);

            // Immediately set date so world is loaded with correct season
            RestoreDateTimeData(saveData.dateAndTime);

            // Must have PlayerEnterExit to respawn player at saved location
            PlayerEnterExit playerEnterExit = serializablePlayer.GetComponent <PlayerEnterExit>();

            if (!playerEnterExit)
            {
                yield break;
            }

            // Check exterior doors are included in save, we need these to exit building
            bool hasExteriorDoors;

            if (saveData.playerData.playerPosition.exteriorDoors == null || saveData.playerData.playerPosition.exteriorDoors.Length == 0)
            {
                hasExteriorDoors = false;
            }
            else
            {
                hasExteriorDoors = true;
            }

            // Restore faction data to player entity
            // This is done early as later objects may require faction information on restore
            if (!string.IsNullOrEmpty(factionDataJson))
            {
                FactionData_v1 factionData = Deserialize(typeof(FactionData_v1), factionDataJson) as FactionData_v1;
                RestoreFactionData(factionData);
                Debug.Log("LoadGame() restored faction state from save.");
            }
            else
            {
                Debug.Log("LoadGame() did not find saved faction data. Player will resume with default faction state.");
            }

            // Raise reposition flag if terrain sampler changed
            // This is required as changing terrain samplers will invalidate serialized player coordinates
            bool repositionPlayer = false;

            if (saveData.playerData.playerPosition.terrainSamplerName != DaggerfallUnity.Instance.TerrainSampler.ToString() ||
                saveData.playerData.playerPosition.terrainSamplerVersion != DaggerfallUnity.Instance.TerrainSampler.Version)
            {
                repositionPlayer = true;
                if (DaggerfallUI.Instance.DaggerfallHUD != null)
                {
                    DaggerfallUI.Instance.DaggerfallHUD.PopupText.AddText("Terrain sampler changed. Repositioning player.");
                }
            }

            // Raise reposition flag if player is supposed to start indoors but building has no doors
            if (saveData.playerData.playerPosition.insideBuilding && !hasExteriorDoors)
            {
                repositionPlayer = true;
                if (DaggerfallUI.Instance.DaggerfallHUD != null)
                {
                    DaggerfallUI.Instance.DaggerfallHUD.PopupText.AddText("Building has no exterior doors. Repositioning player.");
                }
            }

            // Start the respawn process based on saved player location
            if (saveData.playerData.playerPosition.insideDungeon && !repositionPlayer)
            {
                // Start in dungeon
                playerEnterExit.RespawnPlayer(
                    saveData.playerData.playerPosition.worldPosX,
                    saveData.playerData.playerPosition.worldPosZ,
                    true);
            }
            else if (saveData.playerData.playerPosition.insideBuilding && hasExteriorDoors && !repositionPlayer)
            {
                // Start in building
                playerEnterExit.RespawnPlayer(
                    saveData.playerData.playerPosition.worldPosX,
                    saveData.playerData.playerPosition.worldPosZ,
                    saveData.playerData.playerPosition.insideDungeon,
                    saveData.playerData.playerPosition.insideBuilding,
                    saveData.playerData.playerPosition.exteriorDoors);
            }
            else
            {
                // Start outside
                playerEnterExit.RespawnPlayer(
                    saveData.playerData.playerPosition.worldPosX,
                    saveData.playerData.playerPosition.worldPosZ,
                    false,
                    false,
                    null,
                    repositionPlayer);
            }

            // Smash to black while respawning
            DaggerfallUI.Instance.SmashHUDToBlack();

            // Keep yielding frames until world is ready again
            while (playerEnterExit.IsRespawning)
            {
                yield return(new WaitForEndOfFrame());
            }

            // Wait another frame so everthing has a chance to register
            yield return(new WaitForEndOfFrame());

            // Restore save data to objects in newly spawned world
            RestoreSaveData(saveData);

            // Load automap state
            try
            {
                string automapDataJson = ReadSaveFile(Path.Combine(path, automapDataFilename));
                Dictionary <string, DaggerfallAutomap.AutomapGeometryDungeonState> automapState = null;

                if (!string.IsNullOrEmpty(automapDataJson))
                {
                    automapState = Deserialize(typeof(Dictionary <string, DaggerfallAutomap.AutomapGeometryDungeonState>), automapDataJson) as Dictionary <string, DaggerfallAutomap.AutomapGeometryDungeonState>;
                }

                if (automapState != null)
                {
                    GameManager.Instance.Automap.SetState(automapState);
                }
            }
            catch (Exception ex)
            {
                string message = string.Format("Failed to load automap state. Message: {0}", ex.Message);
                Debug.Log(message);
            }

            // Lower load in progress flag
            loadInProgress = false;

            // Fade out from black
            DaggerfallUI.Instance.FadeHUDFromBlack(1.0f);

            // Raise OnLoad event
            RaiseOnLoadEvent(saveData);
        }