File size: 1,842 Bytes
00437a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using Photon.Deterministic;
using System;

namespace Quantum
{
	public unsafe abstract partial class BTService
	{
		public FP IntervalInSec;

		[BotSDKHidden] public Int32 Id;

		public virtual void Init(Frame frame, BTAgent* agent, AIBlackboardComponent* blackboard)
		{
			var endTimesList = frame.ResolveList<FP>(agent->ServicesEndTimes);
			endTimesList.Add(0);
		}

		public void SetEndTime(Frame frame, BTAgent* agent)
		{
			var endTimesList = frame.ResolveList<FP>(agent->ServicesEndTimes);
			endTimesList[Id] = frame.BotSDKGameTime + IntervalInSec;
		}

		public FP GetEndTime(Frame frame, BTAgent* agent)
		{
			var endTime = frame.ResolveList(agent->ServicesEndTimes);
			return endTime[Id];
		}

		public virtual void RunUpdate(BTParams btParams)
		{
			var endTime = GetEndTime(btParams.Frame, btParams.Agent);
			if (btParams.Frame.BotSDKGameTime >= endTime)
			{
				OnUpdate(btParams);
				SetEndTime(btParams.Frame, btParams.Agent);
			}
		}

		public virtual void OnEnter(BTParams btParams)
		{
			SetEndTime(btParams.Frame, btParams.Agent);
		}

		/// <summary>
		/// Called whenever the Service is part of the current subtree
		/// and its waiting time is already over
		/// </summary>
		protected abstract void OnUpdate(BTParams btParams);

		public static void TickServices(BTParams btParams)
		{
			var activeServicesList = btParams.Frame.ResolveList<AssetRefBTService>(btParams.Agent->ActiveServices);

			for (int i = 0; i < activeServicesList.Count; i++)
			{
				var service = btParams.Frame.FindAsset<BTService>(activeServicesList[i].Id);
				try
				{
					service.RunUpdate(btParams);
				}
				catch (Exception e)
				{
					Log.Error("Exception in Behaviour Tree service '{0}' ({1}) - setting node status to Failure", service.GetType().ToString(), service.Guid);
					Log.Exception(e);
				}
			}
		}
	}
}