public static void LoadPlayerData(EntityManager entityManager, Entity entity, string playerName) { FileStream file = null; PlayerSaveData psd = new PlayerSaveData { }; try { BinaryFormatter bf = new BinaryFormatter(); file = File.Open(Application.persistentDataPath + "/Players/" + playerName + ".dat", FileMode.Open); psd = (PlayerSaveData)bf.Deserialize(file); } catch (Exception e) { if (e != null) { //handle exception Debug.LogError("Failed to load the player data"); } } finally { if (file != null) { file.Close(); } if (psd.isValid) { Debug.Log("Loading player data, name = '" + psd.playerData.Name + "' pokemon = '" + psd.playerData.PokemonName + "'"); string pokemonName = psd.playerData.PokemonName.ToString(); if (entityManager.HasComponent <PlayerData>(entity)) { entityManager.SetComponentData(entity, psd.isValid ? psd.playerData : new PlayerData { Name = new ByteString30(playerName), PokemonName = new ByteString30("Bulbasaur") }); } else { entityManager.AddComponentData(entity, psd.isValid ? psd.playerData : new PlayerData { Name = new ByteString30(playerName), PokemonName = new ByteString30("Electrode") }); } LoadPokemonEntity(entity, entityManager, psd); } else { Debug.LogWarning("Attempting to save new player data and reload"); string pokemonName = psd.playerData.PokemonName.ToString(); if (pokemonName == "") { pokemonName = "Electrode"; } PlayerData pd = new PlayerData { Name = new ByteString30(playerName), PokemonName = new ByteString30(pokemonName) }; PokemonEntityData ped = PokemonDataClass.GenerateBasePokemonEntityData(PokemonDataClass.StringToPokedexEntry(pokemonName)); if (SavePlayerData(pd, ped)) { LoadPlayerData(entityManager, entity, playerName); } } } }
public static void LoadPokemonEntity(Entity entity, EntityManager entityManager, PlayerSaveData psd) { Debug.Log("name = " + psd.playerData.PokemonName + ",,," + psd.playerData.Name); string pokemonName = psd.playerData.PokemonName.ToString(); string playerName = psd.playerData.Name.ToString(); entityManager.SetName(entity, playerName + ":" + pokemonName); Debug.Log(Application.persistentDataPath); //get gameobject prefab GameObject go = PokemonDataClass.GetGameObjectPrefab(psd.playerData.PokemonName, 0); //get and add renderMesh PokemonDataClass.SetRenderMesh(entityManager, entity, psd.playerData.PokemonName, 0); if (entityManager.HasComponent <PokemonEntityData>(entity)) { entityManager.SetComponentData(entity, psd.pokemonEntityData); } else { entityManager.AddComponentData(entity, psd.pokemonEntityData); } //add this component (filter to seperate living entities from nonliving) if (!entityManager.HasComponent <LivingEntity>(entity)) { entityManager.AddComponentData <LivingEntity>(entity, new LivingEntity { }); } //add the CoreData if (entityManager.HasComponent <CoreData>(entity)) { entityManager.SetComponentData(entity, new CoreData(psd.playerData.Name, psd.playerData.PokemonName, go)); } else { entityManager.AddComponentData(entity, new CoreData(psd.playerData.Name, psd.playerData.PokemonName, go)); } // Debug.LogWarning("Player name = "+playerName+", pokemon name = "+pokemonName); //add the UI Components(s) entityManager.AddComponentData <UIComponentRequest>(entity, new UIComponentRequest { addToWorld = false, followPlayer = false, visible = true }); //add third and first person camera offets PokemonDataClass.SetPokemonCameraData(new ByteString30(pokemonName), entity, entityManager); // we can't do this in ECS yet // if (!entityManager.HasComponent<AudioListenerData>(entity)) // entityManager.AddComponentData(entity, new RequestAudioListenerData{ }); //add the state data if (!entityManager.HasComponent <StateData>(entity)) { entityManager.AddComponentData(entity, new StateData { }); } //add the PhysicsCollider data PhysicsCollider ps = PokemonDataClass.getPokemonPhysicsCollider(pokemonName, psd.pokemonEntityData, new CollisionFilter { BelongsTo = TriggerEventClass.Collidable | TriggerEventClass.Pokemon | TriggerEventClass.Player, CollidesWith = TriggerEventClass.Collidable | TriggerEventClass.Pokemon, GroupIndex = 1 }); Debug.Log("Creating player eith group index of " + ps.Value.Value.Filter.GroupIndex.ToString()); if (!entityManager.HasComponent <PhysicsCollider>(entity)) { entityManager.AddComponentData(entity, ps); } else { entityManager.SetComponentData(entity, ps); } //add mass if (!entityManager.HasComponent <PhysicsMass>(entity)) { entityManager.AddComponentData(entity, PhysicsMass.CreateDynamic(MassProperties.UnitSphere, psd.pokemonEntityData.Mass)); } else { entityManager.SetComponentData(entity, PhysicsMass.CreateDynamic(MassProperties.UnitSphere, 1f)); } //add physics velocity if (!entityManager.HasComponent <PhysicsVelocity>(entity)) { entityManager.AddComponentData(entity, new PhysicsVelocity { }); } else { entityManager.SetComponentData <PhysicsVelocity>(entity, new PhysicsVelocity { }); } PokemonDataClass.SetPhysicsDamping(entityManager, entity, psd.playerData.PokemonName, psd.pokemonEntityData); //add position and rotation if (!entityManager.HasComponent <Translation>(entity)) { // Debug.Log("Adding translation!"); entityManager.AddComponentData(entity, new Translation { Value = float3.zero }); } // else entityManager.SetComponentData(entity, new Translation { Value = float3.zero }); if (!entityManager.HasComponent <Rotation>(entity)) { // Debug.Log("Adding Rotation!"); entityManager.AddComponentData(entity, new Rotation { Value = new quaternion() }); } // else entityManager.SetComponentData(entity, new Rotation { Value = new quaternion() }); if (!entityManager.HasComponent <Scale>(entity)) { // Debug.Log("Adding Scale"); entityManager.AddComponentData(entity, new Scale { Value = 1f }); } // else entityManager.SetComponentData(entity, new Scale { Value = 1f }); //add the group index entityManager.AddComponentData(entity, new GroupIndexInfo { CurrentGroupIndex = 1, OldGroupIndex = 1, OriginalGroupIndex = 1, Update = true }); }