Loads and loads of small improvements I added while developing on my game

This commit is contained in:
Michel Fedde 2021-03-02 19:54:19 +01:00
parent 41421b1df9
commit a7c71e7ea1
107 changed files with 2278 additions and 1023 deletions

View file

@ -2,8 +2,10 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using SM.Base.Contexts;
using OpenTK;
using SM.Base;
using SM.Base.Drawing;
using SM.Base.Windows;
#endregion
@ -12,9 +14,7 @@ namespace SM.Base.Scene
/// <summary>
/// Contains a list of show items.
/// </summary>
/// <typeparam name="TItem">The type of show items.</typeparam>
public abstract class GenericItemCollection<TItem> : List<TItem>, IShowItem, IShowCollection<TItem>, IScriptable
where TItem : IShowItem
public abstract class GenericItemCollection : List<IShowItem>, IShowItem, IShowCollection, IScriptable
{
private List<IScriptable> _scriptableObjects = new List<IScriptable>();
@ -23,7 +23,7 @@ namespace SM.Base.Scene
/// </summary>
public ReadOnlyCollection<IScriptable> ScriptableObjects => new ReadOnlyCollection<IScriptable>(_scriptableObjects);
/// <inheritdoc />
public List<TItem> Objects => this;
public List<IShowItem> Objects => this;
/// <inheritdoc />
public object Parent { get; set; }
@ -34,20 +34,32 @@ namespace SM.Base.Scene
/// <inheritdoc />
public ICollection<string> Flags { get; set; } = new List<string>() {"collection"};
public bool Active { get; set; } = true;
public bool UpdateActive { get; set; } = true;
public bool RenderActive { get; set; } = true;
/// <inheritdoc />
public virtual void Update(UpdateContext context)
{
if (!Active || !UpdateActive) return;
for (var i = 0; i < _scriptableObjects.Count; i++)
{
if (!_scriptableObjects[i].Active) continue;
_scriptableObjects[i].Update(context);
}
}
/// <inheritdoc cref="IShowCollection{TItem}.Draw" />
/// <inheritdoc cref="IShowCollection.Draw" />
public virtual void Draw(DrawContext context)
{
context.LastPassthough = this;
if (!Active || !RenderActive) return;
for (var i = 0; i < Objects.Count; i++)
{
if (!this[i].Active) continue;
this[i].Draw(context);
}
}
/// <inheritdoc />
@ -63,20 +75,22 @@ namespace SM.Base.Scene
/// <summary>
/// Adds a item to the draw and the script collection, when applicable.
/// </summary>
public new void Add(TItem item)
public new void Add(params IShowItem[] items)
{
AddObject(item);
if (item is IScriptable scriptable)
AddScript(scriptable);
foreach (var item in items)
{
AddObject(item);
if (item is IScriptable scriptable)
AddScript(scriptable);
}
}
/// <summary>
/// Adds the object to the collection.
/// </summary>
/// <param name="item"></param>
public void AddObject(TItem item)
public void AddObject(IShowItem item)
{
base.Add(item);
item.Parent = this;
@ -91,23 +105,22 @@ namespace SM.Base.Scene
_scriptableObjects.Add(item);
}
/// <summary>
/// Removes a item from the draw and script collection, when applicable.
/// </summary>
/// <param name="item"></param>
public new void Remove(TItem item)
public new void Remove(params IShowItem[] items)
{
RemoveObject(item);
foreach (var item in items)
{
RemoveObject(item);
if (item.GetType().IsAssignableFrom(typeof(IScriptable)))
RemoveScript((IScriptable)item);
if (item is IScriptable scriptable)
RemoveScript(scriptable);
}
}
/// <summary>
/// Remove the object from the draw collection.
/// </summary>
/// <param name="item"></param>
public void RemoveObject(TItem item)
public void RemoveObject(IShowItem item)
{
base.Remove(item);
item.Parent = null;
@ -123,15 +136,27 @@ namespace SM.Base.Scene
_scriptableObjects.Remove(item);
}
public ICollection<IShowItem> GetAllItems(bool includeCollections = false)
{
List<IShowItem> items = new List<IShowItem>();
for (var i = 0; i < this.Count; i++)
{
if (!includeCollections && this[i] is IShowCollection) continue;
items.Add(this[i]);
}
return items;
}
/// <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)
public IShowItem GetItemByName(string name)
{
TItem obj = default;
IShowItem obj = default;
for (var i = 0; i < Count; i++)
if (this[i].Name == name)
{
@ -148,7 +173,7 @@ namespace SM.Base.Scene
/// </summary>
/// <typeparam name="TGetItem">Type of return</typeparam>
public TGetItem GetItemByName<TGetItem>(string name)
where TGetItem : TItem
where TGetItem : IShowItem
{
return (TGetItem) GetItemByName(name);
}
@ -157,9 +182,9 @@ namespace SM.Base.Scene
/// Returns all object that have this flag.
/// <para>Only in this list.</para>
/// </summary>
public ICollection<TItem> GetItemsWithFlag(string flag)
public ICollection<IShowItem> GetItemsWithFlag(string flag)
{
var list = new List<TItem>();
var list = new List<IShowItem>();
for (var i = 0; i < Count; i++)
{
var obj = this[i];
@ -176,19 +201,19 @@ namespace SM.Base.Scene
/// </summary>
/// <typeparam name="TItem">The type of show items.</typeparam>
/// <typeparam name="TTransformation">The type of transformation.</typeparam>
public abstract class GenericItemCollection<TItem, TTransformation> : GenericItemCollection<TItem>
where TItem : IShowItem
public abstract class GenericItemCollection<TTransformation> : GenericItemCollection, IShowTransformItem<TTransformation>
where TTransformation : GenericTransformation, new()
{
/// <summary>
/// Transformation of the collection
/// </summary>
public TTransformation Transform = new TTransformation();
public TTransformation Transform { get; set; } = new TTransformation();
/// <inheritdoc />
public override void Draw(DrawContext context)
{
context.ModelMaster = Transform.GetMatrix() * context.ModelMaster;
Transform.LastMaster = context.ModelMatrix;
context.ModelMatrix = Transform.MergeMatrix(context.ModelMatrix);
base.Draw(context);
}