Scripting: FindEntityWithName and get script instance

+ Some C# UUID stuff
+ Added some Mono functions
+ ScriptEngine:
  + ID -> ScriptInstance map in ScriptEngine
  + GetManagedInstance

+ Fixed Scene not visible after loading a new scene
  + Renamed OnViewportResize to SetViewportSize for Scene and SceneRenderer
This commit is contained in:
Simon Lübeß
2023-07-17 20:27:18 +02:00
parent 66d66d3308
commit e8505f6bdb
16 changed files with 492 additions and 61 deletions
+16
View File
@@ -3,4 +3,20 @@ namespace GlitchyEngine.Core;
public struct UUID
{
private ulong _uuid;
public UUID(ulong uuid)
{
_uuid = uuid;
}
public static UUID Zero = new UUID(0);
public static bool operator ==(UUID left, UUID right) => left._uuid == right._uuid;
public static bool operator !=(UUID left, UUID right) => left._uuid != right._uuid;
public override string ToString()
{
return _uuid.ToString();
}
}
+44 -15
View File
@@ -1,5 +1,7 @@
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Xml.Linq;
using GlitchyEngine.Core;
using GlitchyEngine.Math;
@@ -11,22 +13,22 @@ internal struct EntityHandle
public uint Index;
}
public abstract class Entity : EngineObject
public class Entity : EngineObject
{
//private UUID _uuid;
//public UUID UUID => _uuid;
///// <summary>
///// Empty constructor not used. Do NOT USE!
///// </summary>
//protected Entity()
//{}
/// <summary>
/// Empty constructor not used. Do NOT USE!
/// </summary>
protected Entity()
{ }
//internal Entity(UUID uuid)
//{
// _uuid = uuid;
//}
private Entity(UUID uuid)
{
_uuid = uuid;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool HasComponent<T>() => HasComponent(typeof(T));
@@ -41,8 +43,6 @@ public abstract class Entity : EngineObject
/// <returns>The component or null, if the entity doesn't have a component of the specified type.</returns>
public T GetComponent<T>() where T : Component, new()
{
Log.Info("Trying to get component");
if (HasComponent<T>())
{
return new T
@@ -129,10 +129,39 @@ public abstract class Entity : EngineObject
public Transform Transform => GetComponent<Transform>();
public Vector3 Translation
//public Vector3 Translation
//{
// get => Transform.Translation;
// set => Transform.Translation = value;
//}
/// <summary>
/// Returns the first entity with the given name.
/// </summary>
/// <param name="name">The name of the entity.</param>
/// <returns>The first entity with the given name, or null.</returns>
public static Entity FindEntityWithName(string name)
{
get => Transform.Translation;
set => Transform.Translation = value;
ScriptGlue.Entity_FindEntityWithName(name, out UUID entityId);
Log.Error($"Found EntityID {entityId}");
if (entityId == UUID.Zero)
return null;
return new Entity(entityId);
}
/// <summary>
/// Returns the script of the given type, or null, if the entity has no script of the given type.
/// </summary>
/// <typeparam name="T">The type of the script.</typeparam>
/// <returns>The script instance or null.</returns>
public T As<T>() where T : Entity
{
object scriptInstance = ScriptGlue.Entity_GetScriptInstance(_uuid);
return scriptInstance as T;
}
// Will be executed once after the entity as be created.
+6 -1
View File
@@ -99,7 +99,12 @@ public struct Vector2
{
return !(a == b);
}
public float Length()
{
return (float)System.Math.Sqrt(X * X + Y * Y);
}
public override int GetHashCode()
{
unchecked
+7 -1
View File
@@ -20,7 +20,13 @@ internal static class ScriptGlue
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void Entity_RemoveComponent(UUID entityId, Type componentType);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void Entity_FindEntityWithName(string name, out UUID uuid);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern object Entity_GetScriptInstance(UUID entityId);
#endregion Entity
#region TransformComponent