Add initial Behavior simulation
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user