A Smoll game engine
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

93 рядки
2.8 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.mustReorder = true;
}
}
}
}
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 bool mustDelete = false;
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)
{
actionables.RemoveAll(x => x.ScheduledForRemoval());
if(!isEnabled) return;
for (int i = 0; i < actionables.Count; i++)
{
actionables[i].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;
}
public virtual bool ScheduledForRemoval() {
return mustDelete;
}
}
}