Beispiel #1
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);
        }
        public override void DeserializeClientReadyExtra(LiteNetLibIdentity playerIdentity, long connectionId, NetDataReader reader)
        {
            if (LogDev)
            {
                Debug.Log("[LanRpgNetworkManager] Deserializing client ready extra");
            }
            PlayerCharacterData       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 (!CurrentMapInfo.Id.Equals(playerCharacterData.CurrentMapName))
            {
                playerCharacterData.CurrentPosition = teleportPosition.HasValue ? teleportPosition.Value : CurrentMapInfo.startPosition;
            }
            GameObject spawnObj = Instantiate(entityPrefab.gameObject, playerCharacterData.CurrentPosition, Quaternion.identity);
            BasePlayerCharacterEntity playerCharacterEntity = spawnObj.GetComponent <BasePlayerCharacterEntity>();

            playerCharacterData.CloneTo(playerCharacterEntity);
            Assets.NetworkSpawn(spawnObj, 0, connectionId);

            // Set user Id
            playerCharacterEntity.UserId = playerCharacterEntity.Id;

            // Enable GM commands in Singleplayer / LAN mode
            // TODO: Don't use fixed user level
            if (enableGmCommands == EnableGmCommandType.Everyone)
            {
                playerCharacterEntity.UserLevel = 1;
            }

            // Load world / storage for first character (host)
            if (playerCharacters.Count == 0)
            {
                if (enableGmCommands == EnableGmCommandType.HostOnly)
                {
                    playerCharacterEntity.UserLevel = 1;
                }

                // Load and Spawn buildings
                worldSaveData.LoadPersistentData(playerCharacterEntity.Id, playerCharacterEntity.CurrentMapName);
                foreach (BuildingSaveData building in worldSaveData.buildings)
                {
                    CreateBuildingEntity(building, true);
                }
                // Load storage data
                storageSaveData.LoadPersistentData(playerCharacterEntity.Id);
                foreach (StorageCharacterItem storageItem in storageSaveData.storageItems)
                {
                    StorageId storageId = new StorageId(storageItem.storageType, storageItem.storageOwnerId);
                    if (!storageItems.ContainsKey(storageId))
                    {
                        storageItems[storageId] = new List <CharacterItem>();
                    }
                    storageItems[storageId].Add(storageItem.characterItem);
                }
                // Spawn harvestables
                HarvestableSpawnArea[] harvestableSpawnAreas = FindObjectsOfType <HarvestableSpawnArea>();
                foreach (HarvestableSpawnArea harvestableSpawnArea in harvestableSpawnAreas)
                {
                    harvestableSpawnArea.SpawnAll();
                }
            }

            // Summon saved summons
            for (int i = 0; i < playerCharacterEntity.Summons.Count; ++i)
            {
                CharacterSummon 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();
            }

            // Register player, will use registered player to send chat / player messages
            RegisterPlayerCharacter(connectionId, playerCharacterEntity);
        }