using System.Numerics; namespace Fjord.Scenes; public class Entity { internal SceneKeyboard Keyboard; internal SceneMouse Mouse; internal SceneInput Input; internal Scene Parent; internal string name; internal bool excludeFromInspector = false; internal List Components = new(); internal Entity() { name = this.GetType().Name; Keyboard = new("")!; Mouse = new("")!; Input = new("")!; Parent = default(Scene)!; } public Entity(Scene parent) { this.name = this.GetType().Name; this.Parent = parent; this.Keyboard = parent.Keyboard; this.Mouse = parent.Mouse; this.Input = parent.Input; this.Components.Add(new Transform()); } public Entity Name(string name) { this.name = name; return this; } public Entity ExcludeFromInspector(bool exclude) { this.excludeFromInspector = exclude; return this; } public Entity Add(Component component) { component.Keyboard = Keyboard; component.Mouse = Mouse; component.Input = Input; component.ParentEntity = this; component.ParentScene = this.Parent; this.Components.Add(component); try { this.Components[this.Components.Count - 1].AwakeCall(); } catch(Exception e) { Debug.Log(LogLevel.Error, $"Component \"{this.Components[this.Components.Count - 1].GetType().Name}\" awake crashed!"); Debug.Log(LogLevel.Message, e.ToString()); } return this; } public void Remove() { Component? comp = this.Components.Find((comp) => comp.GetType() == typeof(T)); if(comp != null) { try { comp.SleepCall(); } catch(Exception e) { Debug.Log(LogLevel.Error, $"Component \"{comp.GetType().Name}\" sleep crashed!"); Debug.Log(LogLevel.Message, e.ToString()); } this.Components.Remove(comp); } } public T Get() { var scene = Components.Find((comp) => comp.GetType() == typeof(T)); if (scene != null) return (T)(dynamic)scene; else throw new Exception($"Component '{typeof(T)}' doesn't exist in entity"); } public bool TryGet(out T component) { var scene = Components.Find((comp) => comp.GetType() == typeof(T)); if (scene != null) { component = (T)(dynamic)scene; return true; } else { component = default(T)!; return false; } } public float DistanceTo(Entity e) { return Helpers.PointDistance(Get().Position, e.Get().Position); } public float DistanceTo(Transform e) { return Helpers.PointDistance(Get().Position, e.Position); } public float DistanceTo(Vector2 e) { return Helpers.PointDistance(Get().Position, e); } public float DirectionTo(Entity e) { return Helpers.PointDirection(Get().Position, e.Get().Position); } public float DirectionTo(Transform e) { return Helpers.PointDirection(Get().Position, e.Position); } public float DirectionTo(Vector2 e) { return Helpers.PointDirection(Get().Position, e); } internal void AwakeCall() { foreach(Component comp in Components) { comp.AwakeCall(); } } internal void SleepCall() { foreach(Component comp in Components) { comp.SleepCall(); } } internal void UpdateCall() { foreach(Component comp in Components) { comp.UpdateCall(); } } }