Beispiel #1
0
    // Funktio, jota käytetään maailman tilanteen tallentamiseen.
    public void SaveWorld(string fileName)
    {
        // Etitään PlayerController maailmasta
        PlayerController player = world.GetComponentInChildren <PlayerController>();
        // Luodaan WorldSaveData, jonka avulla tallennetaan kaikki maailmasta löytyvä tieto.
        WorldSaveData worldSaveData = new WorldSaveData()
        {
            dateAndTime = DateTime.Now.ToString()
        };

        // Kopioidaan pelaajan tiedot WorldSaveDataan
        worldSaveData.psd = player.psd;
        CopyToSaveFromUnity(worldSaveData.psd, player.transform);

        // Muutetaan WorldSaveData JSON -muotoon.
        string jsonData  = JsonUtility.ToJson(worldSaveData, true);
        string finalData = Encode(jsonData);

        // Tarkistetaan, että hakemisto on olemassa
        CreateFolderIfNotExists(filePath);

        // Tallennetaan tiedot tiedostoon
        File.WriteAllText(filePath + fileName, finalData);
        Debug.Log(filePath);
    }
Beispiel #2
0
    public void LoadWorld(string fileName)
    {
        CreateFolderIfNotExists(filePath);
        // Tarkistetaan, että tiedosto löytyy
        if (!File.Exists(filePath + fileName))
        {
            Debug.Log($"Tiedostoa {fileName} ei löytynyt.");
            return;
        }
        // Tuhotaan vanha maailma ja luodaan uusi tyhjä maailma tilalle. Uutta maailmaa ruvetaa
        // täyttämään objekteilla jotka löytyvät tiedostosta
        DestroyWorldAndCreateNewWorld();
        // Luetaan tiedoston data
        string fileData = File.ReadAllText(filePath + fileName);
        // Muutetaan se JSON -muotoon
        string jsonData = Decode(fileData);
        // Muutetan vielä lopuksi se C# -muotoon WorldSaveData -luokan objektiksi
        WorldSaveData worldSaveData = JsonUtility.FromJson <WorldSaveData>(jsonData);
        // Spawnataan uusi pelaaja maailmaan
        GameObject newPlayer = Instantiate(FindGameObjectByComponentName("PlayerController"), world.transform);

        // Asetetaan pelaajan PlayerSaveData tiedostosta löytyvän PlayerSaveDatan mukaan.
        newPlayer.GetComponent <PlayerController>().psd = worldSaveData.psd;
        // Pitää myös päivittää transformin position ja rotation erikseen.
        CopyToUnityFromSave(newPlayer.transform, worldSaveData.psd);
    }
    public object SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
    {
        WorldSaveData data = (WorldSaveData)obj;

        data.buildings = new List <BuildingSaveData>(info.GetListValue <BuildingSaveData>("buildings"));
        obj            = data;
        return(obj);
    }
Beispiel #4
0
        public override void DeserializeClientReadyExtra(LiteNetLibIdentity playerIdentity, long connectionId, NetDataReader reader)
        {
            if (LogDev)
            {
                Debug.Log("[LanRpgNetworkManager] Deserializing client ready extra");
            }
            var playerCharacterData = new PlayerCharacterData().DeserializeCharacterData(reader);
            BasePlayerCharacterEntity entityPrefab = playerCharacterData.GetEntityPrefab() as BasePlayerCharacterEntity;

            // If it is not allow this character data, disconnect user
            if (entityPrefab == null)
            {
                Debug.LogError("[LanRpgNetworkManager] Cannot find player character with entity Id: " + playerCharacterData.EntityId);
                return;
            }
            if (!SceneManager.GetActiveScene().name.Equals(playerCharacterData.CurrentMapName))
            {
                playerCharacterData.CurrentPosition = teleportPosition.HasValue ? teleportPosition.Value : CurrentMapInfo.startPosition;
            }
            var identity = Assets.NetworkSpawn(entityPrefab.Identity.HashAssetId, playerCharacterData.CurrentPosition, Quaternion.identity, 0, connectionId);
            var playerCharacterEntity = identity.GetComponent <BasePlayerCharacterEntity>();

            playerCharacterData.CloneTo(playerCharacterEntity);
            // Summon saved summons
            for (var i = 0; i < playerCharacterEntity.Summons.Count; ++i)
            {
                var summon = playerCharacterEntity.Summons[i];
                summon.Summon(playerCharacterEntity, summon.Level, summon.summonRemainsDuration, summon.Exp, summon.CurrentHp, summon.CurrentMp);
                playerCharacterEntity.Summons[i] = summon;
            }
            // Notify clients that this character is spawn or dead
            if (!playerCharacterEntity.IsDead())
            {
                playerCharacterEntity.RequestOnRespawn();
            }
            else
            {
                playerCharacterEntity.RequestOnDead();
            }
            // Load world for first character (host)
            if (playerCharacters.Count == 0)
            {
                var worldSaveData = new WorldSaveData();
                worldSaveData.LoadPersistentData(playerCharacterEntity.Id, playerCharacterEntity.CurrentMapName);
                StartCoroutine(SpawnBuildingsAndHarvestables(worldSaveData));
            }
            // Register player, will use registered player to send chat / player messages
            RegisterPlayerCharacter(connectionId, playerCharacterEntity);
        }
Beispiel #5
0
        IEnumerator SpawnBuildingsAndHarvestables(WorldSaveData worldSaveData)
        {
            yield return(new WaitForSecondsRealtime(0.1f));

            // Spawn buildings
            foreach (var building in worldSaveData.buildings)
            {
                CreateBuildingEntity(building, true);
            }
            // Spawn harvestables
            var harvestableSpawnAreas = FindObjectsOfType <HarvestableSpawnArea>();

            foreach (var harvestableSpawnArea in harvestableSpawnAreas)
            {
                harvestableSpawnArea.SpawnAll();
            }
        }
    public void LoadPersistentData(string id, string map)
    {
        string path = Application.persistentDataPath + "/" + id + "_world_" + map + ".sav";

        buildings.Clear();
        if (File.Exists(path))
        {
            BinaryFormatter   binaryFormatter   = new BinaryFormatter();
            SurrogateSelector surrogateSelector = new SurrogateSelector();
            surrogateSelector.AddAllUnitySurrogate();
            BuildingSaveDataSerializationSurrogate buildingSaveDataSS = new BuildingSaveDataSerializationSurrogate();
            WorldSaveDataSerializationSurrogate    worldSaveDataSS    = new WorldSaveDataSerializationSurrogate();
            surrogateSelector.AddSurrogate(typeof(BuildingSaveData), new StreamingContext(StreamingContextStates.All), buildingSaveDataSS);
            surrogateSelector.AddSurrogate(typeof(WorldSaveData), new StreamingContext(StreamingContextStates.All), worldSaveDataSS);
            binaryFormatter.SurrogateSelector = surrogateSelector;
            FileStream    file   = File.Open(path, FileMode.Open);
            WorldSaveData result = (WorldSaveData)binaryFormatter.Deserialize(file);
            buildings = result.buildings;
            file.Close();
        }
    }
Beispiel #7
0
        private void SaveWorld()
        {
            // Save building entities / Tree / Rocks
            var playerCharacterEntity = BasePlayerCharacterController.OwningCharacter;
            var worldSaveData         = new WorldSaveData();

            foreach (var buildingEntity in buildingEntities.Values)
            {
                worldSaveData.buildings.Add(new BuildingSaveData()
                {
                    Id          = buildingEntity.Id,
                    ParentId    = buildingEntity.ParentId,
                    DataId      = buildingEntity.DataId,
                    Position    = buildingEntity.Position,
                    Rotation    = buildingEntity.Rotation,
                    CurrentHp   = buildingEntity.CurrentHp,
                    CreatorId   = buildingEntity.CreatorId,
                    CreatorName = buildingEntity.CreatorName,
                });
            }
            worldSaveData.SavePersistentWorldData(playerCharacterEntity.Id, playerCharacterEntity.CurrentMapName);
        }
    public void GetObjectData(object obj, SerializationInfo info, StreamingContext context)
    {
        WorldSaveData data = (WorldSaveData)obj;

        info.AddListValue("buildings", data.buildings);
    }
Beispiel #9
0
 private void OnSave()
 {
     saveData = new WorldSaveData(areas);
 }