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
+29 -1
View File
@@ -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<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);
}
}
}
+14
View File
@@ -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<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;
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) {}
}
}