Native scripting

This commit is contained in:
Simon Lübeß
2022-03-03 01:39:04 +01:00
parent cbee9f59d8
commit e82f70ba02
5 changed files with 113 additions and 2 deletions
+39
View File
@@ -31,6 +31,41 @@ namespace GlitchyEditor
Entity _cameraEntity; Entity _cameraEntity;
Entity _otherCameraEntity; Entity _otherCameraEntity;
class CameraController : ScriptableEntity
{
protected override void OnCreate()
{
Log.EngineLogger.Trace("Cam controller created!");
}
protected override void OnUpdate(GameTime gameTime)
{
var transformCmp = GetComponent<SimpleTransformComponent>();
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") public this() : base("Example")
{ {
Application.Get().Window.IsVSync = false; Application.Get().Window.IsVSync = false;
@@ -47,6 +82,8 @@ namespace GlitchyEditor
camera.FixedAspectRatio = false; camera.FixedAspectRatio = false;
let transform = _cameraEntity.GetComponent<SimpleTransformComponent>(); let transform = _cameraEntity.GetComponent<SimpleTransformComponent>();
transform.Transform = Matrix.Translation(0, 0, -5); transform.Transform = Matrix.Translation(0, 0, -5);
_cameraEntity.AddComponent<NativeScriptComponent>().Bind<CameraController>();
} }
{ {
@@ -57,6 +94,8 @@ namespace GlitchyEditor
camera.FixedAspectRatio = false; camera.FixedAspectRatio = false;
let transform = _otherCameraEntity.GetComponent<SimpleTransformComponent>(); let transform = _otherCameraEntity.GetComponent<SimpleTransformComponent>();
transform.Transform = Matrix.Translation(0, 0, -5); transform.Transform = Matrix.Translation(0, 0, -5);
_otherCameraEntity.AddComponent<NativeScriptComponent>().Bind<CameraController>();
} }
} }
+28
View File
@@ -123,4 +123,32 @@ namespace GlitchyEngine.World
Camera = .(); 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<T>() mut where T : ScriptableEntity
{
InstantiateFunction = () =>
{
return new T();
};
DestroyInstanceFunction = (self) =>
{
delete self.Instance;
};
}
public void Dispose() mut
{
DestroyInstanceFunction(&this);
}
}
} }
-1
View File
@@ -51,6 +51,5 @@ namespace GlitchyEngine.World
_scene._ecsWorld.RemoveComponent<T>(_entity); _scene._ecsWorld.RemoveComponent<T>(_entity);
} }
} }
} }
+14
View File
@@ -4,6 +4,8 @@ using System;
namespace GlitchyEngine.World namespace GlitchyEngine.World
{ {
using internal ScriptableEntity;
class Scene class Scene
{ {
internal EcsWorld _ecsWorld = new .() ~ delete _; internal EcsWorld _ecsWorld = new .() ~ delete _;
@@ -22,6 +24,18 @@ namespace GlitchyEngine.World
{ {
//TransformSystem.Update(_ecsWorld); //TransformSystem.Update(_ecsWorld);
for (var (entity, script) in _ecsWorld.Enumerate<NativeScriptComponent>())
{
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; Camera* primaryCamera = null;
Matrix* primaryCameraTransform = null; Matrix* primaryCameraTransform = null;
@@ -0,0 +1,31 @@
namespace GlitchyEngine.World
{
class ScriptableEntity
{
internal Entity _entity;
public T* AddComponent<T>(T value = T()) where T: struct, new
{
return _entity.AddComponent<T>(value);
}
public T* GetComponent<T>() where T: struct, new
{
return _entity.GetComponent<T>();
}
public bool HasComponent<T>() where T: struct, new
{
return _entity.HasComponent<T>();
}
public void RemoveComponent<T>() where T: struct, new
{
_entity.RemoveComponent<T>();
}
protected virtual void OnCreate() {}
protected virtual void OnDestroy() {}
protected virtual void OnUpdate(GameTime gt) {}
}
}