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.
		
		
		
		
		
			
		
			
				
	
	
		
			72 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			C#
		
	
			
		
		
	
	
			72 lines
		
	
	
		
			2.2 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?.BaseType == null) continue;
 | |
|                 var method = type.BaseType.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 CreateAIEntity(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();
 | |
|             });
 | |
|             ((IAIEntity)ret.AIObject).Reset(entity);
 | |
|             return ret;
 | |
|         }
 | |
| 
 | |
|         public BehaviorTreePoolItem CreateAILevel(string name)
 | |
|         {
 | |
|             if (!_dict.ContainsKey(name))
 | |
|             {
 | |
|                 return CreateAILevel("AIDirectorDefault");
 | |
|             }
 | |
| 
 | |
|             var ret = Create<BehaviorTreePoolItem>(name, (item) =>
 | |
|             {
 | |
|                 item.AIObject = _dict[name]();
 | |
|                 item.AIObject.Create();
 | |
|             });
 | |
|             return ret;
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     public class BehaviorTreePoolItem : ObjectPoolItemBase
 | |
|     {
 | |
|         public IAIObject AIObject;
 | |
| 
 | |
|         public void Tick()
 | |
|         {
 | |
|             AIObject.Tick();
 | |
|         }
 | |
|     }
 | |
| } |