mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 21:01:52 +00:00
Basic entity scripting
This commit is contained in:
@@ -4,6 +4,8 @@ using GlitchyEngine.Renderer;
|
||||
using GlitchyEngine.Core;
|
||||
using Box2D;
|
||||
using GlitchyEngine.Content;
|
||||
using GlitchyEngine.Scripting;
|
||||
using Mono;
|
||||
|
||||
namespace GlitchyEngine.World
|
||||
{
|
||||
@@ -442,4 +444,26 @@ namespace GlitchyEngine.World
|
||||
[Inline]
|
||||
internal ref b2Vec2 b2Offset mut => ref *(Box2D.b2Vec2*)(void*)&Offset;
|
||||
}
|
||||
|
||||
struct ScriptComponent : IDisposableComponent
|
||||
{
|
||||
private ScriptInstance _instance = null;
|
||||
|
||||
public ScriptInstance Instance
|
||||
{
|
||||
[Inline]
|
||||
get => _instance;
|
||||
[Inline]
|
||||
set mut => SetReference!(_instance, value);
|
||||
}
|
||||
|
||||
public bool InInstantiated => _instance?.IsInstatiated ?? false;
|
||||
|
||||
public void Dispose() mut
|
||||
{
|
||||
ReleaseRefAndNullify!(_instance);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ using System.Collections;
|
||||
using Box2D;
|
||||
using GlitchyEngine.Core;
|
||||
using GlitchyEngine.Content;
|
||||
using GlitchyEngine.Scripting;
|
||||
|
||||
namespace GlitchyEngine.World
|
||||
{
|
||||
@@ -75,7 +76,8 @@ namespace GlitchyEngine.World
|
||||
CopyComponents<Rigidbody2DComponent>(this, target);
|
||||
CopyComponents<BoxCollider2DComponent>(this, target);
|
||||
CopyComponents<CircleCollider2DComponent>(this, target);
|
||||
|
||||
CopyComponents<ScriptComponent>(this, target);
|
||||
|
||||
// Copy transforms
|
||||
for (let (sourceHandle, sourceTransform) in _ecsWorld.Enumerate<TransformComponent>())
|
||||
{
|
||||
@@ -127,10 +129,12 @@ namespace GlitchyEngine.World
|
||||
public void OnRuntimeStart()
|
||||
{
|
||||
OnSimulationStart();
|
||||
ScriptEngine.SetContext(this);
|
||||
}
|
||||
|
||||
public void OnRuntimeStop()
|
||||
{
|
||||
ScriptEngine.SetContext(null);
|
||||
OnSimulationStop();
|
||||
}
|
||||
|
||||
@@ -211,6 +215,8 @@ namespace GlitchyEngine.World
|
||||
Runtime = 0x04 | Physics,
|
||||
}
|
||||
|
||||
//private append List<UUID> _destroyQueue = .();
|
||||
|
||||
public void Update(GameTime gameTime, UpdateMode mode)
|
||||
{
|
||||
Debug.Profiler.ProfileRendererFunction!();
|
||||
@@ -231,6 +237,17 @@ namespace GlitchyEngine.World
|
||||
|
||||
script.Instance.[Friend]OnUpdate(gameTime);
|
||||
}
|
||||
|
||||
// Run scripts
|
||||
for (var (entity, script) in _ecsWorld.Enumerate<ScriptComponent>())
|
||||
{
|
||||
if (!script.InInstantiated)
|
||||
{
|
||||
ScriptEngine.InitializeInstance(Entity(entity, this), script);
|
||||
}
|
||||
|
||||
script.Instance.InvokeOnUpdate(gameTime.DeltaTime);
|
||||
}
|
||||
}
|
||||
|
||||
if (mode.HasFlag(.Physics))
|
||||
|
||||
Reference in New Issue
Block a user