diff --git a/GlitchyEditor/src/EditorLayer.bf b/GlitchyEditor/src/EditorLayer.bf index f3da79a..2063c94 100644 --- a/GlitchyEditor/src/EditorLayer.bf +++ b/GlitchyEditor/src/EditorLayer.bf @@ -31,6 +31,41 @@ namespace GlitchyEditor Entity _cameraEntity; Entity _otherCameraEntity; + class CameraController : ScriptableEntity + { + protected override void OnCreate() + { + Log.EngineLogger.Trace("Cam controller created!"); + } + + protected override void OnUpdate(GameTime gameTime) + { + var transformCmp = GetComponent(); + + if (Input.IsKeyPressed(Key.A)) + { + transformCmp.Transform.Translation.X -= gameTime.DeltaTime; + } + if (Input.IsKeyPressed(Key.D)) + { + transformCmp.Transform.Translation.X += gameTime.DeltaTime; + } + if (Input.IsKeyPressed(Key.W)) + { + transformCmp.Transform.Translation.Y += gameTime.DeltaTime; + } + if (Input.IsKeyPressed(Key.S)) + { + transformCmp.Transform.Translation.Y -= gameTime.DeltaTime; + } + } + + protected override void OnDestroy() + { + Log.EngineLogger.Trace("Cam controller destroyed!"); + } + } + public this() : base("Example") { Application.Get().Window.IsVSync = false; @@ -47,6 +82,8 @@ namespace GlitchyEditor camera.FixedAspectRatio = false; let transform = _cameraEntity.GetComponent(); transform.Transform = Matrix.Translation(0, 0, -5); + + _cameraEntity.AddComponent().Bind(); } { @@ -57,6 +94,8 @@ namespace GlitchyEditor camera.FixedAspectRatio = false; let transform = _otherCameraEntity.GetComponent(); transform.Transform = Matrix.Translation(0, 0, -5); + + _otherCameraEntity.AddComponent().Bind(); } } diff --git a/GlitchyEngine/src/World/Components.bf b/GlitchyEngine/src/World/Components.bf index 9cdf424..04f6751 100644 --- a/GlitchyEngine/src/World/Components.bf +++ b/GlitchyEngine/src/World/Components.bf @@ -115,7 +115,7 @@ namespace GlitchyEngine.World struct CameraComponent { public SceneCamera Camera; - public bool Primary = true; // Todo: probably move into scene + public bool Primary = true; // Todo: probably move into scene public bool FixedAspectRatio = false; public this() @@ -123,4 +123,32 @@ namespace GlitchyEngine.World Camera = .(); } } + + struct NativeScriptComponent : IDisposableComponent + { + public ScriptableEntity Instance = null; + + public function void (mut NativeScriptComponent this) Func; + + public function ScriptableEntity () InstantiateFunction; + public function void (NativeScriptComponent* self) DestroyInstanceFunction; + + public void Bind() mut where T : ScriptableEntity + { + InstantiateFunction = () => + { + return new T(); + }; + + DestroyInstanceFunction = (self) => + { + delete self.Instance; + }; + } + + public void Dispose() mut + { + DestroyInstanceFunction(&this); + } + } } \ No newline at end of file diff --git a/GlitchyEngine/src/World/Entity.bf b/GlitchyEngine/src/World/Entity.bf index 227a40c..962838c 100644 --- a/GlitchyEngine/src/World/Entity.bf +++ b/GlitchyEngine/src/World/Entity.bf @@ -51,6 +51,5 @@ namespace GlitchyEngine.World _scene._ecsWorld.RemoveComponent(_entity); } - } } diff --git a/GlitchyEngine/src/World/Scene.bf b/GlitchyEngine/src/World/Scene.bf index 2a4d641..5beb249 100644 --- a/GlitchyEngine/src/World/Scene.bf +++ b/GlitchyEngine/src/World/Scene.bf @@ -4,6 +4,8 @@ using System; namespace GlitchyEngine.World { + using internal ScriptableEntity; + class Scene { internal EcsWorld _ecsWorld = new .() ~ delete _; @@ -22,6 +24,18 @@ namespace GlitchyEngine.World { //TransformSystem.Update(_ecsWorld); + for (var (entity, script) in _ecsWorld.Enumerate()) + { + if (script.Instance == null) + { + script.Instance = script.InstantiateFunction(); + script.Instance._entity = Entity(entity, this); + script.Instance.[Friend]OnCreate(); + } + + script.Instance.[Friend]OnUpdate(gameTime); + } + Camera* primaryCamera = null; Matrix* primaryCameraTransform = null; diff --git a/GlitchyEngine/src/World/ScriptableEntity.bf b/GlitchyEngine/src/World/ScriptableEntity.bf new file mode 100644 index 0000000..bb57ab9 --- /dev/null +++ b/GlitchyEngine/src/World/ScriptableEntity.bf @@ -0,0 +1,31 @@ +namespace GlitchyEngine.World +{ + class ScriptableEntity + { + internal Entity _entity; + + public T* AddComponent(T value = T()) where T: struct, new + { + return _entity.AddComponent(value); + } + + public T* GetComponent() where T: struct, new + { + return _entity.GetComponent(); + } + + public bool HasComponent() where T: struct, new + { + return _entity.HasComponent(); + } + + public void RemoveComponent() where T: struct, new + { + _entity.RemoveComponent(); + } + + protected virtual void OnCreate() {} + protected virtual void OnDestroy() {} + protected virtual void OnUpdate(GameTime gt) {} + } +} \ No newline at end of file