07.10.2020

+ Parent, Name and Flags to objects.

~ Improved Matrix calculations
This commit is contained in:
Michel Fedde 2020-10-08 12:25:20 +02:00
parent f865496414
commit 2c00dbd31a
21 changed files with 383 additions and 42 deletions

View file

@ -14,7 +14,37 @@ namespace SM.Base.Scene
public List<TItem> Objects => this;
/// <inheritdoc />
public void Update(UpdateContext context)
public object Parent { get; set; }
/// <inheritdoc />
public string Name { get; set; } = "Unnamed Item Collection";
/// <inheritdoc />
public ICollection<string> Flags { get; set; } = new[] {"collection"};
/// <summary>
/// Adds a item.
/// </summary>
public new void Add(TItem item)
{
base.Add(item);
item.Parent = this;
item.OnAdded(this);
}
/// <summary>
/// Removes a item.
/// </summary>
/// <param name="item"></param>
public new void Remove(TItem item)
{
base.Remove(item);
item.Parent = null;
item.OnRemoved(this);
}
/// <inheritdoc />
public virtual void Update(UpdateContext context)
{
for(int i = 0; i < Objects.Count; i++)
this[i].Update(context);
@ -26,6 +56,64 @@ namespace SM.Base.Scene
for (int i = 0; i < Objects.Count; i++)
this[i].Draw(context);
}
/// <inheritdoc />
public virtual void OnAdded(object sender)
{
}
/// <inheritdoc />
public virtual void OnRemoved(object sender)
{ }
/// <summary>
/// Returns a object with this name or the default, if not available.
/// <para>Not reclusive.</para>
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public TItem GetItemByName(string name)
{
TItem obj = default;
for (var i = 0; i < this.Count; i++)
{
if (this[i].Name == name)
{
obj = this[i];
break;
}
}
return obj;
}
/// <summary>
/// Returns a object with this name or the default if not available.
/// <para>Not reclusive.</para>
/// </summary>
/// <typeparam name="TGetItem">Type of return</typeparam>
public TGetItem GetItemByName<TGetItem>(string name)
where TGetItem : TItem
{
return (TGetItem)GetItemByName(name);
}
/// <summary>
/// Returns all object that have this flag.
/// <para>Only in this list.</para>
/// </summary>
public ICollection<TItem> GetItemsWithFlag(string flag)
{
List<TItem> list = new List<TItem>();
for (var i = 0; i < this.Count; i++)
{
TItem obj = this[i];
if (obj.Flags.Contains(flag)) list.Add(obj);
}
return list;
}
}
/// <summary>
@ -45,7 +133,7 @@ namespace SM.Base.Scene
/// <inheritdoc />
public override void Draw(DrawContext context)
{
context.View = Transform.GetMatrix() * context.View;
context.ModelMaster = Transform.GetMatrix() * context.ModelMaster;
base.Draw(context);
}

View file

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using OpenTK;
using SM.Base.Contexts;
@ -9,10 +10,29 @@ namespace SM.Base.Scene
/// </summary>
public abstract class GenericScene
{
private IBackgroundItem _background;
/// <summary>
/// This contains the background.
/// </summary>
protected IBackgroundItem _background;
protected IBackgroundItem _Background
{
get => _background;
set
{
value.Parent = this;
_background = value;
}
}
/// <summary>
/// Updates this scene.
/// </summary>
/// <param name="context"></param>
public virtual void Update(UpdateContext context)
{
}
/// <summary>
/// Draws this scene.
@ -48,6 +68,9 @@ namespace SM.Base.Scene
where TCollection : GenericItemCollection<TItem>, new()
where TItem : IShowItem
{
private TCollection _objectCollection = new TCollection();
private TCollection _hud = new TCollection();
/// <summary>
/// The active camera, that is used if the context doesn't force the viewport camera.
/// <para>If none set, it automaticly uses the viewport camera.</para>
@ -65,16 +88,42 @@ namespace SM.Base.Scene
/// <summary>
/// Objects inside the scene.
/// </summary>
public TCollection Objects { get; set; } = new TCollection();
public TCollection Objects
{
get => _objectCollection;
set
{
value.Parent = this;
_objectCollection = value;
}
}
/// <summary>
/// This defines the HUD objects.
/// </summary>
public TCollection HUD { get; } = new TCollection();
public TCollection HUD
{
get => _hud;
set
{
value.Parent = this;
_hud = value;
}
}
/// <summary>
/// A collection for cameras to switch easier to different cameras.
/// </summary>
public Dictionary<string, TCamera> Cameras = new Dictionary<string, TCamera>();
/// <inheritdoc />
public override void Update(UpdateContext context)
{
_Background?.Update(context);
_objectCollection.Update(context);
_hud.Update(context);
}
/// <inheritdoc />
public override void Draw(DrawContext context)
{
@ -82,12 +131,12 @@ namespace SM.Base.Scene
DrawContext backgroundDrawContext = context;
backgroundDrawContext.View = BackgroundCamera.CalculateViewMatrix();
_background?.Draw(backgroundDrawContext);
_Background?.Draw(backgroundDrawContext);
Objects.Draw(context);
_objectCollection.Draw(context);
context.View = HUDCamera.CalculateViewMatrix();
HUD.Draw(context);
_hud.Draw(context);
}
}
}

View file

@ -1,4 +1,5 @@
using SM.Base.Contexts;
using System.Collections.Generic;
using SM.Base.Contexts;
namespace SM.Base.Scene
{
@ -7,6 +8,21 @@ namespace SM.Base.Scene
/// </summary>
public interface IShowItem
{
/// <summary>
/// Parent of the object.
/// </summary>
object Parent { get; set; }
/// <summary>
/// Contains the name for the object.
/// </summary>
string Name { get; set; }
/// <summary>
/// Contains specific flags for the object.
/// </summary>
ICollection<string> Flags { get; set; }
/// <summary>
/// Tells the object to update own systems.
/// </summary>
@ -17,5 +33,14 @@ namespace SM.Base.Scene
/// </summary>
/// <param name="context"></param>
void Draw(DrawContext context);
/// <summary>
/// Action, that is called, when the object was added to a GenericItemCollection.
/// </summary>
void OnAdded(object sender);
/// <summary>
/// Action, that is called, when the object was removed from a GenericItemCollection.
/// </summary>
void OnRemoved(object sender);
}
}