mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 21:01:52 +00:00
Fixed physics only simulation
+ Don't run scripts in physics only mode + Don't initialize scritps in physics only mode + Don't setup physics callbacks in physics only mode
This commit is contained in:
@@ -734,7 +734,7 @@ namespace GlitchyEditor
|
|||||||
|
|
||||||
using (Scene runtimeScene = new Scene())
|
using (Scene runtimeScene = new Scene())
|
||||||
{
|
{
|
||||||
_editorScene.CopyTo(runtimeScene);
|
_editorScene.CopyTo(runtimeScene, true);
|
||||||
|
|
||||||
runtimeScene.OnRuntimeStart();
|
runtimeScene.OnRuntimeStart();
|
||||||
|
|
||||||
@@ -755,7 +755,7 @@ namespace GlitchyEditor
|
|||||||
|
|
||||||
using (Scene simulationScene = new Scene())
|
using (Scene simulationScene = new Scene())
|
||||||
{
|
{
|
||||||
_editorScene.CopyTo(simulationScene);
|
_editorScene.CopyTo(simulationScene, false);
|
||||||
|
|
||||||
simulationScene.OnSimulationStart();
|
simulationScene.OnSimulationStart();
|
||||||
|
|
||||||
|
|||||||
@@ -65,7 +65,10 @@ namespace GlitchyEngine.World
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public void CopyTo(Scene target)
|
/// Copies all entities with their components to the given target-scene.
|
||||||
|
/// @param target The scene that the entities are copied to.
|
||||||
|
/// @param initializeScripts If true script instances will be initialized.
|
||||||
|
public void CopyTo(Scene target, bool initializeScripts)
|
||||||
{
|
{
|
||||||
// Copy entities
|
// Copy entities
|
||||||
for (let sourceHandle in _ecsWorld.Enumerate())
|
for (let sourceHandle in _ecsWorld.Enumerate())
|
||||||
@@ -83,37 +86,43 @@ namespace GlitchyEngine.World
|
|||||||
CopyComponents<SpriteRendererComponent>(this, target);
|
CopyComponents<SpriteRendererComponent>(this, target);
|
||||||
CopyComponents<CircleRendererComponent>(this, target);
|
CopyComponents<CircleRendererComponent>(this, target);
|
||||||
CopyComponents<CameraComponent>(this, target);
|
CopyComponents<CameraComponent>(this, target);
|
||||||
CopyComponents<NativeScriptComponent>(this, target);
|
|
||||||
CopyComponents<LightComponent>(this, target);
|
CopyComponents<LightComponent>(this, target);
|
||||||
|
|
||||||
|
|
||||||
CopyComponents<Rigidbody2DComponent>(this, target);
|
CopyComponents<Rigidbody2DComponent>(this, target);
|
||||||
CopyComponents<BoxCollider2DComponent>(this, target);
|
CopyComponents<BoxCollider2DComponent>(this, target);
|
||||||
CopyComponents<CircleCollider2DComponent>(this, target);
|
CopyComponents<CircleCollider2DComponent>(this, target);
|
||||||
CopyComponents<PolygonCollider2DComponent>(this, target);
|
CopyComponents<PolygonCollider2DComponent>(this, target);
|
||||||
|
|
||||||
// Copy ScriptComponents... needs extra handling for the script instances
|
if (initializeScripts)
|
||||||
for (let (sourceHandle, sourceComponent) in _ecsWorld.Enumerate<ScriptComponent>())
|
|
||||||
{
|
{
|
||||||
Entity sourceEntity = .(sourceHandle, this);
|
CopyComponents<NativeScriptComponent>(this, target);
|
||||||
|
|
||||||
Entity targetEntity = target.GetEntityByID(sourceEntity.UUID);
|
// Copy ScriptComponents... needs extra handling for the script instances
|
||||||
ScriptComponent* targetComponent = targetEntity.AddComponent<ScriptComponent>();
|
for (let (sourceHandle, sourceComponent) in _ecsWorld.Enumerate<ScriptComponent>())
|
||||||
|
{
|
||||||
|
Entity sourceEntity = .(sourceHandle, this);
|
||||||
|
|
||||||
targetComponent.ScriptClassName = sourceComponent.ScriptClassName;
|
Entity targetEntity = target.GetEntityByID(sourceEntity.UUID);
|
||||||
|
ScriptComponent* targetComponent = targetEntity.AddComponent<ScriptComponent>();
|
||||||
|
|
||||||
// Initializes the created instance
|
targetComponent.ScriptClassName = sourceComponent.ScriptClassName;
|
||||||
// TODO: this returns false, if no script with ScriptClassName exists, we have to handle this case correctly I think.
|
|
||||||
ScriptEngine.InitializeInstance(targetEntity, targetComponent);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Copy values to entities.
|
// Initializes the created instance
|
||||||
// We do this in a separate loop because we might reference other entities.
|
// TODO: this returns false, if no script with ScriptClassName exists, we have to handle this case correctly I think.
|
||||||
// If we did it in a single loop these entities might not exist yet.
|
ScriptEngine.InitializeInstance(targetEntity, targetComponent);
|
||||||
for (let (handle, script) in target._ecsWorld.Enumerate<ScriptComponent>())
|
}
|
||||||
{
|
|
||||||
Entity entity = .(handle, this);
|
|
||||||
|
|
||||||
if (script.Instance != null)
|
// Copy values to entities.
|
||||||
ScriptEngine.CopyEditorFieldsToInstance(entity, script);
|
// We do this in a separate loop because we might reference other entities.
|
||||||
|
// If we did it in a single loop these entities might not exist yet.
|
||||||
|
for (let (handle, script) in target._ecsWorld.Enumerate<ScriptComponent>())
|
||||||
|
{
|
||||||
|
Entity entity = .(handle, this);
|
||||||
|
|
||||||
|
if (script.Instance != null)
|
||||||
|
ScriptEngine.CopyEditorFieldsToInstance(entity, script);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy transforms... needs special handling for the Parent<->Child relations
|
// Copy transforms... needs special handling for the Parent<->Child relations
|
||||||
@@ -167,6 +176,7 @@ namespace GlitchyEngine.World
|
|||||||
public void OnRuntimeStart()
|
public void OnRuntimeStart()
|
||||||
{
|
{
|
||||||
OnSimulationStart();
|
OnSimulationStart();
|
||||||
|
SetupPhysicsCallbacks();
|
||||||
ScriptEngine.SetContext(this);
|
ScriptEngine.SetContext(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -180,6 +190,61 @@ namespace GlitchyEngine.World
|
|||||||
{
|
{
|
||||||
_physicsWorld2D = World.Create(_gravity2D);
|
_physicsWorld2D = World.Create(_gravity2D);
|
||||||
|
|
||||||
|
draw.drawPolygonCallback = (vertices, vertexCount, color, userData) =>
|
||||||
|
{
|
||||||
|
for (int i = 1; i < vertexCount; i++)
|
||||||
|
{
|
||||||
|
let start = vertices[i - 1];
|
||||||
|
let end = vertices[i];
|
||||||
|
|
||||||
|
Renderer2D.DrawLine(float3(start.x, start.y, 0.0f), float3(end.x, end.y, 0.0f), *(ColorRGBA*)&color);
|
||||||
|
}
|
||||||
|
let start = vertices[vertexCount - 1];
|
||||||
|
let end = vertices[0];
|
||||||
|
|
||||||
|
Renderer2D.DrawLine(float3(start.x, start.y, 0.0f), float3(end.x, end.y, 0.0f), *(ColorRGBA*)&color);
|
||||||
|
};
|
||||||
|
draw.drawSolidPolygonCallback = (vertices, vertexCount, color, userData) =>
|
||||||
|
{
|
||||||
|
for (int i = 1; i < vertexCount; i++)
|
||||||
|
{
|
||||||
|
let start = vertices[i - 1];
|
||||||
|
let end = vertices[i];
|
||||||
|
|
||||||
|
Renderer2D.DrawLine(float3(start.x, start.y, 0.0f), float3(end.x, end.y, 0.0f), *(ColorRGBA*)&color);
|
||||||
|
}
|
||||||
|
let start = vertices[vertexCount - 1];
|
||||||
|
let end = vertices[0];
|
||||||
|
|
||||||
|
Renderer2D.DrawLine(float3(start.x, start.y, 0.0f), float3(end.x, end.y, 0.0f), *(ColorRGBA*)&color);
|
||||||
|
};
|
||||||
|
draw.drawCircleCallback = (center, radius, color, userData) =>
|
||||||
|
{
|
||||||
|
Renderer2D.DrawCircle(center, radius.XX, *(ColorRGBA*)&color, 0.1f);
|
||||||
|
};
|
||||||
|
draw.drawSolidCircleCallback = (center, radius, axis, color, userData) =>
|
||||||
|
{
|
||||||
|
Renderer2D.DrawCircle(center, radius.XX, *(ColorRGBA*)&color);
|
||||||
|
};
|
||||||
|
draw.drawSegmentCallback = (p1, p2, color, userData) =>
|
||||||
|
{
|
||||||
|
};
|
||||||
|
draw.drawPointCallback = (p, size, color, userData) =>
|
||||||
|
{
|
||||||
|
};
|
||||||
|
draw.drawTransformCallback = (xf, userData) =>
|
||||||
|
{
|
||||||
|
};
|
||||||
|
draw.userData = Internal.UnsafeCastToPtr(_physicsWorld2D);
|
||||||
|
|
||||||
|
World.SetDebugDraw(_physicsWorld2D, &draw);
|
||||||
|
World.SetDebugDrawFlags(_physicsWorld2D, .e_shapeBit);
|
||||||
|
|
||||||
|
InitPhysics2D();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void SetupPhysicsCallbacks()
|
||||||
|
{
|
||||||
_contactListener = b2ContactListener();
|
_contactListener = b2ContactListener();
|
||||||
_contactListener.beginContactCallback = (contact, userData) =>
|
_contactListener.beginContactCallback = (contact, userData) =>
|
||||||
{
|
{
|
||||||
@@ -289,58 +354,6 @@ namespace GlitchyEngine.World
|
|||||||
};
|
};
|
||||||
|
|
||||||
World.SetContactListener(_physicsWorld2D, &_contactListener);
|
World.SetContactListener(_physicsWorld2D, &_contactListener);
|
||||||
|
|
||||||
draw.drawPolygonCallback = (vertices, vertexCount, color, userData) =>
|
|
||||||
{
|
|
||||||
for (int i = 1; i < vertexCount; i++)
|
|
||||||
{
|
|
||||||
let start = vertices[i - 1];
|
|
||||||
let end = vertices[i];
|
|
||||||
|
|
||||||
Renderer2D.DrawLine(float3(start.x, start.y, 0.0f), float3(end.x, end.y, 0.0f), *(ColorRGBA*)&color);
|
|
||||||
}
|
|
||||||
let start = vertices[vertexCount - 1];
|
|
||||||
let end = vertices[0];
|
|
||||||
|
|
||||||
Renderer2D.DrawLine(float3(start.x, start.y, 0.0f), float3(end.x, end.y, 0.0f), *(ColorRGBA*)&color);
|
|
||||||
};
|
|
||||||
draw.drawSolidPolygonCallback = (vertices, vertexCount, color, userData) =>
|
|
||||||
{
|
|
||||||
for (int i = 1; i < vertexCount; i++)
|
|
||||||
{
|
|
||||||
let start = vertices[i - 1];
|
|
||||||
let end = vertices[i];
|
|
||||||
|
|
||||||
Renderer2D.DrawLine(float3(start.x, start.y, 0.0f), float3(end.x, end.y, 0.0f), *(ColorRGBA*)&color);
|
|
||||||
}
|
|
||||||
let start = vertices[vertexCount - 1];
|
|
||||||
let end = vertices[0];
|
|
||||||
|
|
||||||
Renderer2D.DrawLine(float3(start.x, start.y, 0.0f), float3(end.x, end.y, 0.0f), *(ColorRGBA*)&color);
|
|
||||||
};
|
|
||||||
draw.drawCircleCallback = (center, radius, color, userData) =>
|
|
||||||
{
|
|
||||||
Renderer2D.DrawCircle(center, radius.XX, *(ColorRGBA*)&color, 0.1f);
|
|
||||||
};
|
|
||||||
draw.drawSolidCircleCallback = (center, radius, axis, color, userData) =>
|
|
||||||
{
|
|
||||||
Renderer2D.DrawCircle(center, radius.XX, *(ColorRGBA*)&color);
|
|
||||||
};
|
|
||||||
draw.drawSegmentCallback = (p1, p2, color, userData) =>
|
|
||||||
{
|
|
||||||
};
|
|
||||||
draw.drawPointCallback = (p, size, color, userData) =>
|
|
||||||
{
|
|
||||||
};
|
|
||||||
draw.drawTransformCallback = (xf, userData) =>
|
|
||||||
{
|
|
||||||
};
|
|
||||||
draw.userData = Internal.UnsafeCastToPtr(_physicsWorld2D);
|
|
||||||
|
|
||||||
World.SetDebugDraw(_physicsWorld2D, &draw);
|
|
||||||
World.SetDebugDrawFlags(_physicsWorld2D, .e_shapeBit);
|
|
||||||
|
|
||||||
InitPhysics2D();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void InitPhysics2D()
|
private void InitPhysics2D()
|
||||||
@@ -543,8 +556,10 @@ namespace GlitchyEngine.World
|
|||||||
Editor = 0x01,
|
Editor = 0x01,
|
||||||
/// Update the physics related stuff
|
/// Update the physics related stuff
|
||||||
Physics = 0x02,
|
Physics = 0x02,
|
||||||
|
/// Execute scripts.
|
||||||
|
Scripts = 0x04,
|
||||||
/// Update the runtume related stuff (e.g. execute scripts). Also run physics!
|
/// Update the runtume related stuff (e.g. execute scripts). Also run physics!
|
||||||
Runtime = 0x04 | Physics,
|
Runtime = Scripts | Physics,
|
||||||
}
|
}
|
||||||
|
|
||||||
//private append List<UUID> _destroyQueue = .();
|
//private append List<UUID> _destroyQueue = .();
|
||||||
@@ -555,7 +570,7 @@ namespace GlitchyEngine.World
|
|||||||
|
|
||||||
TransformSystem.Update(_ecsWorld);
|
TransformSystem.Update(_ecsWorld);
|
||||||
|
|
||||||
if (mode.HasFlag(.Runtime))
|
if (mode.HasFlag(.Scripts))
|
||||||
{
|
{
|
||||||
// Run scripts
|
// Run scripts
|
||||||
for (var (entity, script) in _ecsWorld.Enumerate<NativeScriptComponent>())
|
for (var (entity, script) in _ecsWorld.Enumerate<NativeScriptComponent>())
|
||||||
|
|||||||
Reference in New Issue
Block a user