A Smoll game engine
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 

85 řádky
2.6 KiB

using System.Numerics;
namespace Smoll {
public class Entity : IActionable {
private int zOrderImpl = 0;
public int zOrder{
get{ return zOrderImpl;}
set{
zOrderImpl = value;
if(parent != null) {
parent.actionables.Sort();
} else {
foreach (var item in layers)
{
item.actionables.Sort();
}
}
}
}
internal List<Component> components;
internal List<Entity> children;
internal List<IActionable> actionables;
internal Entity? parent;
internal List<Layer> layers;
public string name{get;}
public bool isEnabled = true;
public bool isVisible = true;
public Entity(Entity parent) {
this.parent = parent;
components = new List<Component>();
layers = new List<Layer>();
children = new List<Entity>();
actionables = new List<IActionable>();
name = "Unnamed Entity";
layers = parent.layers;
if(parent != null) {
parent.children.Add(this);
parent.actionables.Add(this);
parent.actionables.Sort();
}
}
public Entity(Layer parent) {
this.parent = null;
components = new List<Component>();
children = new List<Entity>();
actionables = new List<IActionable>();
name = "Unnamed Entity";
parent.actionables.Add(this);
layers = new List<Layer>();
layers.Add(parent);
}
public void Attach(Component component) {
component.owner = this;
components.Add(component);
actionables.Add(component);
actionables.Sort();
component.OnAttached();
}
public virtual void Update(float deltaTimeSeconds)
{
if(!isEnabled) return;
foreach (var elem in actionables)
{
elem.Update(deltaTimeSeconds);
}
}
public virtual void Draw(Layer.DrawMode drawMode)
{
if(!isVisible) return;
foreach (var elem in actionables)
{
elem.Draw(drawMode);
}
}
public T? GetComponent<T>() where T : Component {
var component = components.Find((Component a) => a is T);
if(component == null) return null;
return component as T;
}
}
}