Add initial Behavior simulation

This commit is contained in:
Wojtek Figat
2023-08-17 15:26:31 +02:00
parent d1e2d6699e
commit c18625e017
10 changed files with 444 additions and 10 deletions

View File

@@ -1,5 +1,7 @@
// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
#if FLAX_EDITOR
using System;
using FlaxEngine.Utilities;
@@ -39,4 +41,55 @@ namespace FlaxEngine
}
#endif
}
unsafe partial class BehaviorTreeNode
{
/// <summary>
/// Gets the size in bytes of the given typed node state structure.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetStateSize<T>()
{
// C# nodes state is stored via pinned GCHandle to support holding managed references (eg. string or Vector3[])
return sizeof(IntPtr);
}
/// <summary>
/// Sets the node state at the given memory address.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AllocState(IntPtr memory, object state)
{
var handle = GCHandle.Alloc(state);
var ptr = IntPtr.Add(memory, _memoryOffset).ToPointer();
Unsafe.Write<IntPtr>(ptr, (IntPtr)handle);
}
/// <summary>
/// Returns the typed node state at the given memory address.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ref T GetState<T>(IntPtr memory) where T : struct
{
var ptr = IntPtr.Add(memory, _memoryOffset).ToPointer();
var handle = GCHandle.FromIntPtr(Unsafe.Read<IntPtr>(ptr));
var state = handle.Target;
#if !BUILD_RELEASE
if (state == null)
throw new NullReferenceException();
#endif
return ref Unsafe.Unbox<T>(state);
}
/// <summary>
/// Frees the allocated node state at the given memory address.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FreeState(IntPtr memory)
{
var ptr = IntPtr.Add(memory, _memoryOffset).ToPointer();
var handle = GCHandle.FromIntPtr(Unsafe.Read<IntPtr>(ptr));
handle.Free();
}
}
}