/// <summary>
        /// Attach flats component to an entity.
        /// </summary>
        /// <param name="entity">Entity to attach billboards.</param>
        /// <param name="block">Block to get flats from.</param>
        private void AddBlockFlats(BaseEntity entity, DaggerfallBlockComponent block)
        {
            // Exit if no flats
            if (block.BlockFlats.Count == 0)
            {
                return;
            }

            // Add flats to component
            foreach (var flat in block.BlockFlats)
            {
                // Get position
                Vector3 position = new Vector3(flat.Position.X, flat.Position.Y, flat.Position.Z);

                // Add billboard component
                DaggerfallBillboardComponent billboard = new DaggerfallBillboardComponent(core, flat);
                billboard.Matrix = block.Matrix * Matrix.CreateTranslation(position);
                entity.Components.Add(billboard);

                // Add a light commponent for each billboard light source
                if (flat.Archive == 210)
                {
                    position.Y += billboard.Size.Y;
                    LightComponent lightComponent = new LightComponent(core, block.Matrix.Translation + position, 750f, Color.White, 1.1f);
                    entity.Components.Add(lightComponent);
                }
            }
        }
        /// <summary>
        /// Attach light components to an entity.
        /// </summary>
        /// <param name="entity">Entity to attach lights.</param>
        /// <param name="block">Block to get lights from</param>
        private void AddBlockLights(BaseEntity entity, DaggerfallBlockComponent block)
        {
            // Exit if no lights
            if (block.BlockLights.Count == 0)
            {
                return;
            }

            // Add lights to entity
            foreach (var light in block.BlockLights)
            {
                // Get position
                Vector3 position = new Vector3(light.Position.X, light.Position.Y, light.Position.Z);

                // Add test sphere
                //GeometricPrimitiveComponent sphereGeometry = new GeometricPrimitiveComponent(core);
                //sphereGeometry.MakeSphere(light.Radius / 6, 16);
                //sphereGeometry.Color = new Vector4(Color.White.ToVector3(), 1);
                //sphereGeometry.Matrix = block.Matrix * Matrix.CreateTranslation(position);
                //entity.Components.Add(sphereGeometry);

                // Add light
                LightComponent lightComponent = new LightComponent(
                    core,
                    position,
                    light.Radius,
                    Color.White,
                    1.0f);
                entity.Components.Add(lightComponent);
            }
        }
        private void LoadBlockScene()
        {
            // Set day/night mode for window textures
            core.MaterialManager.Daytime = false;

            // Set camera position
            core.ActiveScene.Camera.Position = new Vector3(22, 27, -20);

            // Create level entity
            DynamicEntity level = new DynamicEntity(core.ActiveScene);

            // Create block component
            DaggerfallBlockComponent block = new DaggerfallBlockComponent(core);

            //block.LoadBlock("MAGEAA13.RMB", MapsFile.DefaultClimateSettings);
            //block.LoadBlock("BOOKAL02.RMB", MapsFile.DefaultClimateSettings);
            //block.LoadBlock("S0000040.RDB", MapsFile.DefaultClimateSettings);
            //block.LoadBlock("S0000999.RDB", MapsFile.DefaultClimateSettings);
            block.LoadBlock("S0000181.RDB", MapsFile.DefaultClimateSettings, core.ActiveScene, true);
            //block.LoadBlock("N0000002.RDB", MapsFile.DefaultClimateSettings);
            //block.LoadBlock("N0000000.RDB", MapsFile.DefaultClimateSettings);
            level.Components.Add(block);

            // Attach block flats
            AddBlockFlats(level, block);

            // Attach block lights
            AddBlockLights(level, block);

            // Create directional light
            //float lightIntensity = 1f;
            //WorldEntity directionalLight = new WorldEntity(core.ActiveScene);
            //directionalLight.Components.Add(new LightComponent(core, Vector3.Normalize(Vector3.Down + Vector3.Right), Color.White, lightIntensity));
            //directionalLight.Components.Add(new LightComponent(core, Vector3.Normalize(Vector3.Forward + Vector3.Left), Color.White, lightIntensity));
        }
        /// <summary>
        /// Attach flats component to an entity.
        /// </summary>
        /// <param name="entity">Entity to attach billboards.</param>
        /// <param name="block">Block to get flats from.</param>
        private void AddBlockFlats(BaseEntity entity, DaggerfallBlockComponent block)
        {
            // Exit if no flats
            if (block.BlockFlats.Count == 0)
            {
                return;
            }

            // Add flats to entity
            foreach (var flat in block.BlockFlats)
            {
                // Filter editor flats
                if (flat.Type == DaggerfallBlockComponent.FlatTypes.Editor)
                {
                    continue;
                }

                // Get position
                Vector3 position = new Vector3(flat.Position.X, flat.Position.Y, flat.Position.Z);

                // Add billboard component
                DaggerfallBillboardComponent billboard = new DaggerfallBillboardComponent(core, flat);
                billboard.Matrix = block.Matrix * Matrix.CreateTranslation(position);
                entity.Components.Add(billboard);
            }
        }
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="document">Scene document.</param>
        /// <param name="entity">Entity owning this proxy.</param>
        /// <param name="block">Block to proxy.</param>
        public DaggerfallBlockProxy(SceneDocument document, EntityProxy entity, DaggerfallBlockComponent block)
            : base(document, entity)
        {
            base.name  = defaultName;
            this.block = block;

            // Add to parent entity
            base.Entity.Components.Add(block);
        }
        /// <summary>
        /// Loads a map scene.
        /// </summary>
        private void LoadExteriorMapScene()
        {
            // Set day/night mode for window textures
            core.MaterialManager.Daytime = false;

            // Set camera position
            core.ActiveScene.Camera.Position = new Vector3(0, 0, 0);

            // Create level entity
            DynamicEntity level = new DynamicEntity(core.ActiveScene);

            // Get location
            DFLocation location = core.MapManager.GetLocation("Wayrest", "Wayrest");
            int        width    = location.Exterior.ExteriorData.Width;
            int        height   = location.Exterior.ExteriorData.Height;

            // Add block components
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    // Get block name
                    string name = core.BlockManager.CheckName(
                        core.MapManager.GetRmbBlockName(ref location, x, y));

                    // Get block translation
                    Vector3 blockPosition = new Vector3(x * BlocksFile.RMBDimension, 0f, -(y * BlocksFile.RMBDimension)) * ModelManager.GlobalScale;

                    // Attach block component
                    DaggerfallBlockComponent block = new DaggerfallBlockComponent(core);
                    block.LoadBlock(name, location.Climate, core.ActiveScene, true);
                    block.Matrix = Matrix.CreateTranslation(blockPosition);
                    level.Components.Add(block);

                    // Attach block flats
                    //AddBlockFlats(level, block);
                }
            }

            // Clear model cache to release some memory
            core.ModelManager.ClearModelData();

            // Create directional light
            float         lightIntensity   = 0.25f;
            DynamicEntity directionalLight = new DynamicEntity(core.ActiveScene);

            directionalLight.Components.Add(new LightComponent(core, Vector3.Normalize(Vector3.Down + Vector3.Right), Color.White, lightIntensity));
            directionalLight.Components.Add(new LightComponent(core, Vector3.Normalize(Vector3.Forward + Vector3.Left), Color.White, lightIntensity));
        }
        /// <summary>
        /// Creates a new block component proxy.
        /// </summary>
        private DaggerfallBlockProxy AddBlockProxy(EntityProxy parent, string name)
        {
            // Create new block
            DaggerfallBlockComponent block = new DaggerfallBlockComponent(worldControl.Core);

            block.LoadBlock(defaultBlockName, MapsFile.DefaultClimateSettings, worldControl.Core.ActiveScene, false);

            // Create proxy for component
            DaggerfallBlockProxy blockProxy = new DaggerfallBlockProxy(sceneDocument, parent, block);

            // Add new proxy to tree view
            TreeNode node = AddTreeNode(parent.TreeNode, blockProxy);

            return(blockProxy);
        }
        /// <summary>
        /// Attach flats component to an entity.
        /// </summary>
        /// <param name="entity">Entity to attach billboards.</param>
        /// <param name="block">Block to get flats from.</param>
        private void AddBlockFlats(BaseEntity entity, DaggerfallBlockComponent block)
        {
            // Exit if no flats
            if (block.BlockFlats.Count == 0)
            {
                return;
            }

            // Add flats to entity
            foreach (var flat in block.BlockFlats)
            {
                // Filter editor flats
                if (flat.Type == DaggerfallBlockComponent.FlatTypes.Editor)
                {
                    continue;
                }

                // Get position
                Vector3 position = new Vector3(flat.Position.X, flat.Position.Y, flat.Position.Z);

                // Add billboard component
                DaggerfallBillboardComponent billboard = new DaggerfallBillboardComponent(core, flat);
                billboard.Matrix = block.Matrix * Matrix.CreateTranslation(position);
                entity.Components.Add(billboard);

                // Add a light component for each billboard light source outside of dungeons
                if (flat.Archive == 210 && flat.Dungeon == false)
                {
                    position.Y += billboard.Size.Y;
                    LightComponent lightComponent = new LightComponent(core, block.Matrix.Translation + position, 46f, Color.White, 1.1f);
                    entity.Components.Add(lightComponent);
                }

                // Add a light component for each billboard light source inside dungeons
                // Daggerfall often misses light sources in dungeons, this ensures at least
                // one small light be positioned over every light source. This is very cheap
                // with a deferred renderer.
                if (flat.Archive == 210 && flat.Dungeon == true)
                {
                    //LightComponent lightComponent = new LightComponent(core, block.Matrix.Translation + position, 6f, Color.White, 1.0f);
                    //entity.Components.Add(lightComponent);
                }
            }
        }
        /// <summary>
        /// Attach light components to an entity.
        /// </summary>
        /// <param name="entity">Entity to attach lights.</param>
        /// <param name="block">Block to get lights from</param>
        private void AddBlockLights(BaseEntity entity, DaggerfallBlockComponent block)
        {
            // Exit if no lights
            if (block.BlockLights.Count == 0)
            {
                return;
            }

            // Add lights to entity
            foreach (var light in block.BlockLights)
            {
                // Get position and radius
                Vector3 position = new Vector3(light.Position.X, light.Position.Y, light.Position.Z);
                float   radius   = light.Radius * 2.5f;

                // Add light
                LightComponent pointLightComponent = new LightComponent(
                    core,
                    position,
                    radius,
                    Color.White,
                    1.0f);
                entity.Components.Add(pointLightComponent);

                // Add test sphere to see light overlap
                //GeometricPrimitiveComponent sphereGeometry = new GeometricPrimitiveComponent(core);
                //sphereGeometry.MakeSphere(radius, 16);
                //sphereGeometry.Color = new Vector4(Color.White.ToVector3(), 1);
                //sphereGeometry.Matrix = block.Matrix * Matrix.CreateTranslation(position);
                //entity.Components.Add(sphereGeometry);
            }

            // Setup ambient light
            LightComponent ambientLightComponent = new LightComponent(core, Color.White, 0.25f);

            entity.Components.Add(ambientLightComponent);

            // Setup directional light
            //LightComponent directionalLightComponent = new LightComponent(core, Vector3.Normalize(Vector3.Down + Vector3.Forward + Vector3.Right), Color.White, 0.25f);
            //entity.Components.Add(directionalLightComponent);
        }
        protected override void LoadContent()
        {
            // Create scene
            scene = new Scene(core);
            scene.Camera.Position = new Vector3(33.0625f, 4.4375f, 89.8125f);
            scene.Camera.Transform(327, 0, Vector3.Zero);
            scene.Camera.Update();
            core.ActiveScene = scene;

            // Disable core input handling
            core.Input.ActiveDevices = Input.DeviceFlags.None;

            core.Renderer.ClearColor = clearColor;

            // Set day/night mode for window textures
            core.MaterialManager.Daytime = false;

            // Set climate
            DFLocation.ClimateSettings climateSettings = MapsFile.DefaultClimateSettings;
            climateSettings.ClimateType    = DFLocation.ClimateBaseType.Swamp;
            climateSettings.SceneryArchive = 510;

            // Create level entity
            DynamicEntity level = new DynamicEntity(core.ActiveScene);

            // Create block component
            DaggerfallBlockComponent block = new DaggerfallBlockComponent(core);

            block.LoadBlock("CASTAA26.RMB", climateSettings, core.ActiveScene, true);
            level.Components.Add(block);

            // Attach block flats
            AddBlockFlats(level, block);

            // Create directional lights
            Color         lightColor       = new Color(100, 100, 200);
            DynamicEntity directionalLight = new DynamicEntity(core.ActiveScene);

            directionalLight.Components.Add(new LightComponent(core, Vector3.Normalize(Vector3.Down + Vector3.Right), lightColor, 0.60f));
            directionalLight.Components.Add(new LightComponent(core, Vector3.Normalize(Vector3.Forward + Vector3.Left), lightColor, 0.90f));

            // Create fireflies in courtyard
            for (int i = 0; i < 10; i++)
            {
                Entities.Firefly firefly = new Entities.Firefly(scene);
                firefly.Matrix = Matrix.CreateTranslation(62.5f, 3.125f, 43.75f);
            }

            // Create fireflies in left of courtyard
            for (int i = 0; i < 10; i++)
            {
                Entities.Firefly firefly = new Entities.Firefly(scene);
                firefly.Matrix = Matrix.CreateTranslation(31.25f, 3.125f, 43.75f);
            }

            // Create fireflies near camera
            for (int i = 0; i < 10; i++)
            {
                Entities.Firefly firefly = new Entities.Firefly(scene);
                firefly.Matrix = Matrix.CreateTranslation(37.5f, 3.125f, 75f);
            }

            // Load songs
            song = Game.Content.Load <Song>("Songs/DanGoodale_DF-11");
            MediaPlayer.IsRepeating = true;
            MediaPlayer.Play(song);

            // Load fonts
            titleFont   = Game.Content.Load <SpriteFont>("Fonts/TitleFont");
            consoleFont = Game.Content.Load <SpriteFont>("Fonts/ConsoleFont");
            menuFont    = Game.Content.Load <SpriteFont>("Fonts/MenuFont");
            menuFont2   = Game.Content.Load <SpriteFont>("Fonts/MenuFont2");

            // Create gui manager
            gui = new InterfaceManager(core);
            gui.SetMargins(Margins.All, 20);

            // Create a stack panel
            StackPanelScreenComponent stackPanel = new StackPanelScreenComponent(core, Vector2.Zero, new Vector2(500, 300), 0);

            stackPanel.HorizontalAlignment = HorizontalAlignment.Right;
            gui.Components.Add(stackPanel);

            // Create title text
            TextItemScreenComponent title = new TextItemScreenComponent(core, titleFont, titleText, HorizontalAlignment.Right, VerticalAlignment.None);

            title.OutlineColor  = Color.Gray;
            title.EnableOutline = true;
            title.ShadowColor   = Color.MidnightBlue;
            title.ShadowVector  = new Vector2(3, 3);

            // Create version text
            TextItemScreenComponent version = new TextItemScreenComponent(core, consoleFont, versionText, HorizontalAlignment.Right, VerticalAlignment.None);

            version.TextColor = Color.Gold;

            // Create start menu item
            startMenuItem              = new TextItemScreenComponent(core, menuFont2, startMenuText, HorizontalAlignment.Right, VerticalAlignment.None);
            startMenuItem.TextColor    = Color.White;
            startMenuItem.OutlineColor = Color.Goldenrod;
            startMenuItem.ShadowColor  = Color.MidnightBlue;
            startMenuItem.ShadowVector = new Vector2(2, 2);

            // Exit menu item
            exitMenuItem              = new TextItemScreenComponent(core, menuFont2, exitMenuText, HorizontalAlignment.Right, VerticalAlignment.None);
            exitMenuItem.TextColor    = Color.White;
            exitMenuItem.OutlineColor = Color.Gold;
            exitMenuItem.ShadowColor  = Color.MidnightBlue;
            exitMenuItem.ShadowVector = new Vector2(2, 2);

            // Add items to stack panel
            stackPanel.Components.Add(title);
            stackPanel.Components.Add(version);
            stackPanel.Components.Add(new StackSpacerScreenComponent(core, 50));
            stackPanel.Components.Add(startMenuItem);
            stackPanel.Components.Add(new StackSpacerScreenComponent(core, 20));
            stackPanel.Components.Add(exitMenuItem);

            // Wire up menu events
            startMenuItem.OnMouseEnter += new EventHandler(StartMenuItem_OnMouseEnter);
            startMenuItem.OnMouseLeave += new EventHandler(StartMenuItem_OnMouseLeave);
            exitMenuItem.OnMouseEnter  += new EventHandler(ExitMenuItem_OnMouseEnter);
            exitMenuItem.OnMouseLeave  += new EventHandler(ExitMenuItem_OnMouseLeave);
            startMenuItem.OnMouseClick += new EventHandler(StartMenuItem_OnMouseClick);
            exitMenuItem.OnMouseClick  += new EventHandler(ExitMenuItem_OnMouseClick);
        }
        protected override void LoadContent()
        {
            // Load fonts
            consoleFont = Game.Content.Load <SpriteFont>("Fonts/MenuFont");

            core.Renderer.ShowDebugBuffers = true;

            // Load fonts
            SpriteFont menuFont2 = Game.Content.Load <SpriteFont>("Fonts/MenuFont2");

            // Create gui manager
            gui = new InterfaceManager(core);
            gui.SetMargins(Margins.All, 20);

            // Create status text
            physicsTextItem = new TextItemScreenComponent(core, menuFont2, string.Empty);
            physicsTextItem.EnableOutline       = true;
            physicsTextItem.OutlineColor        = Color.Gray;
            physicsTextItem.ShadowColor         = Color.Black;
            physicsTextItem.ShadowVector        = new Vector2(2, 2);
            physicsTextItem.HorizontalAlignment = HorizontalAlignment.Left;
            physicsTextItem.VerticalAlignment   = VerticalAlignment.Top;

            // Add to gui
            gui.Components.Add(physicsTextItem);

            // Create scene
            scene = new Scene(core);
            scene.Camera.Position = new Vector3(22, 27, -20);
            scene.Camera.Update();
            core.ActiveScene = scene;

            // Set day/night mode for window textures
            core.MaterialManager.Daytime = false;

            // Create level entity
            DynamicEntity level = new DynamicEntity(core.ActiveScene);

            // Create block component
            DaggerfallBlockComponent block = new DaggerfallBlockComponent(core);

            block.LoadBlock("S0000181.RDB", MapsFile.DefaultClimateSettings, core.ActiveScene, true);

            // Increase bounding sphere radius as block component does not current calculate properly
            block.BoundingSphere = new BoundingSphere(block.BoundingSphere.Center, block.BoundingSphere.Radius * 2);

            // Add block to level
            level.Components.Add(block);

            // Attach block flats
            AddBlockFlats(level, block);

            // Attach block lights
            AddBlockLights(level, block);

            // Add player point light
            playerLight = new LightComponent(core, Vector3.Zero, 7f, Color.White, 1f);
            level.Components.Add(playerLight);

            // Disable core input handling
            core.Input.ActiveDevices = Input.DeviceFlags.None;

            // Create player controller
            playerInput = new CharacterControllerInput(core.ActiveScene.Space, core.ActiveScene.Camera);
            playerInput.UseCameraSmoothing            = true;
            playerInput.CharacterController.JumpSpeed = 9;
            playerInput.CharacterController.HorizontalMotionConstraint.Speed = 8;
            playerInput.StandingCameraOffset = 1.4f;
            playerInput.Activate();

            // Initialise mouse state
            Game.IsMouseVisible = false;
            Mouse.SetPosition(GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height / 2);
            startMouseState = mouseState = Mouse.GetState();

            // Load songs
            MediaPlayer.Stop();
            song = Game.Content.Load <Song>("Songs/DanGoodale_DF-D2");
            MediaPlayer.Play(song);

            UpdatePhysicsObjectText();
        }