#region usings using System; using System.Collections.Generic; using SM.Base.Utility; using SM.Base.Window; using SM.Base.Window.Contexts; #endregion namespace SM.Base.Scene { /// /// A generic scene, that imports functions for scene control. /// public abstract class GenericScene : IInitializable { private IBackgroundItem _background; private readonly Dictionary _extensions = new Dictionary(); private GenericItemCollection _hud; private GenericItemCollection _objectCollection; /// /// A collection for cameras to switch easier to different cameras. /// public Dictionary Cameras = new Dictionary(); /// /// This contains the background. /// protected IBackgroundItem _Background { get => _background; set { value.Parent = this; _background = value; } } /// /// Objects inside the scene. /// public GenericItemCollection Objects { get => _objectCollection; set { value.Parent = this; _objectCollection = value; } } /// /// This defines the HUD objects. /// public GenericItemCollection HUD { get => _hud; set { value.Parent = this; _hud = value; } } /// /// If true, shows a axis helper at (0,0,0) /// public bool ShowAxisHelper { get; set; } = false; /// /// The active camera, that is used if the context doesn't force the viewport camera. /// If none set, it automaticly uses the viewport camera. /// public GenericCamera Camera { get; set; } /// /// A camera to control the background. /// public GenericCamera BackgroundCamera { get; set; } /// /// A camera to control the HUD. /// public GenericCamera HUDCamera { get; set; } /// /// If true, the scene was already initialized. /// public bool IsInitialized { get; set; } /// public virtual void Activate() { } /// public virtual void Initialization() { } /// /// Updates this scene. /// /// public virtual void Update(UpdateContext context) { _objectCollection?.Update(context); _hud?.Update(context); } /// /// Executes a fixed update for this scene. /// public virtual void FixedUpdate(FixedUpdateContext context) { _objectCollection?.FixedUpdate(context); _hud?.FixedUpdate(context); } /// /// Draws this scene. /// public virtual void Draw(DrawContext context) { DrawBackground(context); DrawMainObjects(context); DrawHUD(context); DrawDebug(context); } /// /// Draws only the background. /// /// public virtual void DrawBackground(DrawContext context) { var backgroundDrawContext = context; backgroundDrawContext.SetCamera(BackgroundCamera); _Background?.Draw(backgroundDrawContext); } /// /// Draws only the main objects /// /// public virtual void DrawMainObjects(DrawContext context) { if (!context.Window.ForceViewportCamera && Camera != null) context.SetCamera(Camera); _objectCollection.Draw(context); } /// /// Draws only the HUD /// /// public virtual void DrawHUD(DrawContext context) { context.SetCamera(HUDCamera); _hud?.Draw(context); } /// /// Draw the debug informations. /// /// public virtual void DrawDebug(DrawContext context) { } /// /// Adds a extension to the scene. /// /// public virtual void SetExtension(object extension) { _extensions[extension.GetType()] = extension; } /// /// Gets a extension with the type. /// /// /// /// public virtual T GetExtension() where T : class { object ext = _extensions[typeof(T)]; if (ext == null) { Log.Write(LogType.Warning, $"Tried to get the extension '{typeof(T).Name}', that doesn't exist in the scene."); return null; } return (T) ext; } /// /// This is triggered when the scene gets deactivated. /// public virtual void Deactivate() { } } /// /// A generic scene that imports different functions. /// /// The type of cameras. /// The type of show items. /// The type for collections public abstract class GenericScene : GenericScene where TCamera : GenericCamera, new() where TCollection : GenericItemCollection, new() { public new TCollection Objects { get => (TCollection) base.Objects; set => base.Objects = value; } public new TCollection HUD { get { base.HUD ??= new TCollection(); return (TCollection) base.HUD; } set => base.HUD = value; } public new TCamera Camera { get => (TCamera) base.Camera; set => base.Camera = value; } public new TCamera HUDCamera { get => (TCamera) base.HUDCamera; set => base.HUDCamera = value; } public new TCamera BackgroundCamera { get => (TCamera) base.BackgroundCamera; set => base.BackgroundCamera = value; } } }