Scripting: Basic copying of entities during runtime + Reference Components using UUID

This commit is contained in:
Simon Lübeß
2023-09-21 14:23:00 +02:00
parent ba54d48dfe
commit 446dc2ca2a
7 changed files with 175 additions and 43 deletions
+2 -2
View File
@@ -2,7 +2,7 @@ using GlitchyEngine.Core;
namespace GlitchyEngine;
public abstract class Component
public abstract class Component : EngineObject
{
public Entity Entity { get; internal set; }
public Entity Entity => new(_uuid);
}
+10 -10
View File
@@ -14,7 +14,7 @@ public class Rigidbody2D : Component
/// <param name="wakeUp">Wake up the body</param>
public void ApplyForce(float2 force, float2 point, bool wakeUp = true)
{
ScriptGlue.Rigidbody2D_ApplyForce(Entity._uuid, force, point, wakeUp);
ScriptGlue.Rigidbody2D_ApplyForce(_uuid, force, point, wakeUp);
}
/// <summary>
@@ -25,7 +25,7 @@ public class Rigidbody2D : Component
/// <param name="wakeUp">Wake up the body</param>
public void ApplyForceToCenter(float2 force, bool wakeUp = true)
{
ScriptGlue.Rigidbody2D_ApplyForceToCenter(Entity._uuid, force, wakeUp);
ScriptGlue.Rigidbody2D_ApplyForceToCenter(_uuid, force, wakeUp);
}
/// <summary>
@@ -35,11 +35,11 @@ public class Rigidbody2D : Component
{
get
{
ScriptGlue.Rigidbody2D_GetPosition(Entity._uuid, out float2 position);
ScriptGlue.Rigidbody2D_GetPosition(_uuid, out float2 position);
return position;
}
set => ScriptGlue.Rigidbody2D_SetPosition(Entity._uuid, value);
set => ScriptGlue.Rigidbody2D_SetPosition(_uuid, value);
}
/// <summary>
@@ -49,11 +49,11 @@ public class Rigidbody2D : Component
{
get
{
ScriptGlue.Rigidbody2D_GetRotation(Entity._uuid, out float rotation);
ScriptGlue.Rigidbody2D_GetRotation(_uuid, out float rotation);
return rotation;
}
set => ScriptGlue.Rigidbody2D_SetRotation(Entity._uuid, value);
set => ScriptGlue.Rigidbody2D_SetRotation(_uuid, value);
}
/// <summary>
@@ -63,11 +63,11 @@ public class Rigidbody2D : Component
{
get
{
ScriptGlue.Rigidbody2D_GetLinearVelocity(Entity._uuid, out float2 velocity);
ScriptGlue.Rigidbody2D_GetLinearVelocity(_uuid, out float2 velocity);
return velocity;
}
set => ScriptGlue.Rigidbody2D_SetLinearVelocity(Entity._uuid, value);
set => ScriptGlue.Rigidbody2D_SetLinearVelocity(_uuid, value);
}
/// <summary>
@@ -77,10 +77,10 @@ public class Rigidbody2D : Component
{
get
{
ScriptGlue.Rigidbody2D_GetAngularVelocity(Entity._uuid, out float velocity);
ScriptGlue.Rigidbody2D_GetAngularVelocity(_uuid, out float velocity);
return velocity;
}
set => ScriptGlue.Rigidbody2D_SetAngularVelocity(Entity._uuid, value);
set => ScriptGlue.Rigidbody2D_SetAngularVelocity(_uuid, value);
}
}
+8 -6
View File
@@ -72,7 +72,7 @@ public class Entity : EngineObject
{
return new T
{
Entity = this
_uuid = _uuid
};
}
@@ -111,7 +111,7 @@ public class Entity : EngineObject
return new T
{
Entity = this
_uuid = _uuid
};
}
@@ -132,7 +132,9 @@ public class Entity : EngineObject
Component component = Activator.CreateInstance(componentType) as Component;
if (component != null)
component.Entity = this;
{
component._uuid = _uuid;
}
return component;
}
@@ -160,7 +162,7 @@ public class Entity : EngineObject
foreach ((Type componentType, int index) in componentTypes.WithIndex())
{
components[index] = Activator.CreateInstance(componentType) as Component;
components[index].Entity = this;
components[index]._uuid = _uuid;
}
return components;
@@ -176,7 +178,7 @@ public class Entity : EngineObject
{
ScriptGlue.Entity_AddComponents(_uuid, new []{typeof(T1), typeof(T2)});
return (new T1 { Entity = this }, new T2 { Entity = this });
return (new T1 { _uuid = _uuid }, new T2 { _uuid = _uuid });
}
/// <summary>
@@ -190,7 +192,7 @@ public class Entity : EngineObject
{
ScriptGlue.Entity_AddComponents(_uuid, new []{typeof(T1), typeof(T2), typeof(T3)});
return (new T1 { Entity = this }, new T2 { Entity = this }, new T3 { Entity = this });
return (new T1 { _uuid = _uuid }, new T2 { _uuid = _uuid }, new T3 { _uuid = _uuid });
}
#endregion Add Components
+2 -2
View File
@@ -26,10 +26,10 @@ public struct Collision2D
/// Gets the rigidbody whose Collider takes part in the collision.
/// This Rigidbody is either a component of the entity whose script instance received the event or a parent of it.
/// </summary>
public Rigidbody2D Rigidbody => new() { Entity = Entity };
public Rigidbody2D Rigidbody => new() { _uuid = _entity };
/// <summary>
/// Gets the other rigidbody whose Collider takes part in the collision.
/// </summary>
public Rigidbody2D OtherRigidbody => new() { Entity = OtherEntity };
public Rigidbody2D OtherRigidbody => new() { _uuid = _otherEntity };
}