File size: 1,617 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
using Photon.Deterministic;
using System;

namespace Quantum
{
	[Serializable]
	public unsafe partial class BTCooldown : BTDecorator
	{
		// How many time should we wait
		public FP CooldownTime;

		// An indexer so we know when the time started counting
		public BTDataIndex StartTimeIndex;

		public override void Init(Frame frame, AIBlackboardComponent* blackboard, BTAgent* agent)
		{
			base.Init(frame, blackboard, agent);

			// We allocate space on the BTAgent so we can store the Start Time
			agent->AddFPData(frame, 0);
		}

		protected override BTStatus OnUpdate(BTParams btParams)
		{
			var result = base.OnUpdate(btParams);

			// We let the time check, which happens on the DryRun, happen
			// If it results in success, then we store on the BTAgent the time value of the moment that it happened
			if (result == BTStatus.Success)
			{
				var currentTime = btParams.Frame.DeltaTime * btParams.Frame.Number;

				var frame = btParams.Frame;
				var entity = btParams.Entity;
				btParams.Agent->SetFPData(frame, currentTime, StartTimeIndex.Index);
			}

			return result;
		}

		// We get the Start Time stored on the BTAgent, then we check if the time + cooldown is already over
		// If it is not over, then we return False, blocking the execution of the children nodes
		public override Boolean DryRun(BTParams btParams)
		{
			var frame = btParams.Frame;
			var entity = btParams.Entity;
			FP startTime = btParams.Agent->GetFPData(frame, StartTimeIndex.Index);

			var currentTime = btParams.Frame.DeltaTime * btParams.Frame.Number;

			return currentTime >= startTime + CooldownTime;
		}
	}
}