2
0
Fork 0
You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using DesperateDevs.Utils;
using UnityEngine;
namespace Game
{
public class BehaviorTreePoolManager : ObjectPoolBase<BehaviorTreePoolManager>
{
private readonly Dictionary<string, EventCallbackCreate> _dict = new Dictionary<string, EventCallbackCreate>();
public override void OnCreate()
{
var interfaceType = typeof(IAIObject);
foreach (var type in interfaceType.Assembly.GetTypes())
{
if (!interfaceType.IsAssignableFrom(type)) continue;
if (type.IsGenericType) continue;
if (type.IsInterface) continue;
if (type.BaseType == null) continue;
var method = type.BaseType.GetMethod("GetCreator", BindingFlags.Static | BindingFlags.Public);
if (method == null) continue;
var creator = (EventCallbackCreate)method.Invoke(null, new object[] { });
_dict.Add(type.Name, creator);
}
}
public BehaviorTreePoolItem Create(GameEntity entity, string name)
{
if (!_dict.ContainsKey(name))
{
Debug.Log("Unknown behavior tree:" + name);
return null;
}
var ret = Create<BehaviorTreePoolItem>(name, (item) =>
{
item.AIObject = _dict[name]();
item.AIObject.Create();
});
ret.AIObject.Reset(entity);
return ret;
}
}
public class BehaviorTreePoolItem : ObjectPoolItemBase
{
public IAIObject AIObject;
public void Tick()
{
AIObject.Tick();
}
}
}