/// <summary> /// Loads a Scene by deserializing a file. /// </summary> /// <param name="filePath">The path to the Scene file.</param> /// <param name="fullPath">Whether to use the full path of the file.</param> /// <returns></returns> public static Scene_cl LoadScene(string filePath, bool fullPath = true) { Scene_cl scene = new Scene_cl(); string currentDirectory = filePath; int pathEnd = filePath.LastIndexOf("\\"); mCurrentSceneDirectory = currentDirectory.Remove(pathEnd + 1); if (File.Exists(filePath)) { // Clear current scene scene.Clear(); FileStream sceneFile = new FileStream(filePath, FileMode.Open, FileAccess.Read); BinaryFormatter formatter = new BinaryFormatter(); BinaryReader reader = new BinaryReader(sceneFile); try { scene.SetAmbientLight((Entity_cl)formatter.Deserialize(sceneFile)); int count; int num = reader.ReadInt32(); for (count = 0; count < num; count++) { Entity_cl entity = (Entity_cl)formatter.Deserialize(sceneFile); foreach (List<Component_cl> componentList in entity.Components.Values) { foreach (Component_cl component in componentList) { if (typeof(FNA.Interfaces.IInitializeAble).IsAssignableFrom(component.GetType())) { ((FNA.Interfaces.IInitializeAble)component).Initialize(); } } } scene.AddEntity(entity); } // Entities num = reader.ReadInt32(); for (count = 0; count < num; count++) { scene.AddEntity((Entity_cl)formatter.Deserialize(sceneFile)); } // Layers num = reader.ReadInt32(); for (count = 0; count < num; count++) { scene.AddLayer((Entity_cl)formatter.Deserialize(sceneFile)); } } catch (SerializationException e) { Console.WriteLine("Failed to deserialize Scene: " + e.Message); throw; } finally { sceneFile.Close(); reader.Close(); } } return scene; }
/// <summary> /// Initialized for testing. /// </summary> private void InitializeScene() { mScene = new Scene_cl(); ComponentManager_cl.Instance.PreUpdate(); FNA.Components.InputComponent_cl cameraInput = new FNA.Components.InputComponent_cl(mCameraEntity); mCameraEntity.RemoveComponent(mCameraEntity.GetComponentOfType(typeof(SunburnCameraComponent_cl))); PositionComponent_cl position = (PositionComponent_cl)mCameraEntity.GetComponentOfType(typeof(PositionComponent_cl)); position.SetPosition3D(new Vector3(0, 0, 0)); TheFinaleCameraComponent_cl cameraComponent = new TheFinaleCameraComponent_cl(mCameraEntity); cameraComponent.ViewWidth = 20.0f; cameraComponent.FarPlane = 100.0f; CameraManager_cl.Instance.ActiveCamera = (CameraComponent_cl)mCameraEntity.GetComponentOfType(typeof(CameraComponent_cl)); /************************************************************************ * HACK: * Use the AmbientLightComponent to set up the scene's ambient light. * * Jay Sternfield - 2011/12/07 ************************************************************************/ mScene.SetAmbientLight(Color.White.ToVector3(), 0.15f); SynapseGaming.LightingSystem.Lights.AmbientLight ambient = new SynapseGaming.LightingSystem.Lights.AmbientLight(); ambient.Depth = 0.0f; ambient.DiffuseColor = new Vector3(1f); ambient.Intensity = 1f; mSceneInterface.LightManager.Submit(ambient); RenderableManager_cl.Instance.Initialize(mWindowWidth, mWindowHeight); //// texture to vertices - terrain test //Entity_cl floor = new Entity_cl(); //RenderableComponent floorRenderable = new RenderableComponent(floor); //floorRenderable.LoadSpriteContent("terrain"); //floorRenderable.Sprite.Rectangle = new FloatRectangle(0, 0, 1, 1); //PositionComponent floorPosition = new PositionComponent(floor); //PhysicsComponent floorPhysics = new PhysicsComponent(floor); //floorPhysics.IsStatic = true; //floorPhysics.InitializePhysics("Terrain"); //floorPhysics.SetCollisionGroup((short)1); //textureScale.X = floorPhysics._polygonTexture.Width / 64; // this is converting display to sim units //textureScale.Y = floorPhysics._polygonTexture.Height / 64; //floorRenderable.Sprite.Size = textureScale; //// Scarfellina //Entity_cl scarfellina = PrefabManager_cl.Instance.LoadPrefab("C:/dev2/Cohort7-Public/Venture/FSS/Finale/WIP/Content/Prefabs/scarfellina.prefab"); // easier to test hardcoded //PositionComponent scarfellinaPosition = (PositionComponent)scarfellina.GetComponentOfType(typeof(PositionComponent)); //PhysicsComponent scarfellinaPhysics = (PhysicsComponent)scarfellina.GetComponentOfType(typeof(PhysicsComponent)); //RenderableComponent scarfellinaRenderable = (RenderableComponent)scarfellina.GetComponentOfType(typeof(RenderableComponent)); //scarfellinaRenderable.Sprite.Rectangle = new FloatRectangle(0, 0, 1, 1); // does this matter for animated entities? //scarfellinaRenderable.Sprite.Size = new Vector2(2, 2); //scarfellinaPhysics.Position = new Vector2(0, 200); //scarfellinaPhysics.SetCollisionGroup((short)1); //mScene.AddEntity(scarfellina); instance = music.CreateInstance(); instance.IsLooped = true; instance.Play(); }
/// <summary> /// /// </summary> /// <param name="filePath"></param> /// <param name="scene"></param> public static void SaveScene(string filePath, Scene_cl scene) { string currentDirectory = filePath; int pathEnd = filePath.LastIndexOf("\\"); mCurrentSceneDirectory = currentDirectory.Remove(pathEnd + 1); Stream stream = File.Create(filePath); BinaryFormatter formatter = new BinaryFormatter(); BinaryWriter writer = new BinaryWriter(stream); try { formatter.Serialize(stream, scene.AmbientLight); writer.Write(scene.Lights.Count); foreach (Entity_cl light in scene.Lights) { formatter.Serialize(stream, light); } writer.Write(scene.Entities.Count); foreach (Entity_cl entity in scene.Entities) { formatter.Serialize(stream, entity); } writer.Write(scene.Layers.Count); foreach (Entity_cl layer in scene.Layers) { formatter.Serialize(stream, layer); } } catch (System.Exception e) { MessageBox.Show("Failed to save Scene: " + e.Message); throw; } finally { stream.Close(); } }