mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 21:01:52 +00:00
Native scripting
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -51,6 +51,5 @@ namespace GlitchyEngine.World
|
||||
|
||||
_scene._ecsWorld.RemoveComponent<T>(_entity);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) {}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user