File size: 1,694 Bytes
24b81cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
72
73
74
75
76
77
78
class PluginObjectsInteractionManager extends PluginBase
{
	private ref 		array<Object> m_LockedObjects;
	private ref 		array<float> m_LockedObjectsDecay;
	private const float		TIME_TO_FORCED_UNLOCK = 60;
	private const float 	TICK_RATE = 10;
	private ref Timer	m_DecayTimer;
		
	void PluginObjectsInteractionManager()
	{
		m_LockedObjects = new array<Object>;
		m_LockedObjectsDecay = new array<float>;
		//TIMERDEPRECATED - timer for decaying objects 
		m_DecayTimer = new Timer();
		m_DecayTimer.Run(TICK_RATE, this, "Decay", NULL,true);
	}

	bool IsFree(Object target)
	{
		if ( target && m_LockedObjects.Count() > 0 )
		{
			for ( int i = 0; i < m_LockedObjects.Count(); i++ )
			{	
				if ( m_LockedObjects.Get(i) == target )
				{
					return false;
				}
			}
		}
		return true;
	}

	void Lock(Object target)
	{
		if ( target && !IsFree(target) )
		{
			m_LockedObjects.Insert(target);
			m_LockedObjectsDecay.Insert(0);
		}
	}

	void Release(Object target)
	{
		if ( target && m_LockedObjects.Count() > 0 )
		{
			for ( int i = 0; i < m_LockedObjects.Count(); i++ )
			{	
				if ( m_LockedObjects.Get(i) == target )
				{
					m_LockedObjects.Remove(i);
					m_LockedObjectsDecay.Remove(i);
					break;
				}
			}
		}
	}
	
	//FAILSAFE - checks periodically locked objects and releases them automaticaly if given time has passed
	void Decay()
	{
		if ( m_LockedObjectsDecay.Count() > 0 )
		{
			for ( int i = 0; i < m_LockedObjectsDecay.Count(); i++ )
			{	
				if ( m_LockedObjectsDecay.Get(i) >= TIME_TO_FORCED_UNLOCK )
				{
					m_LockedObjects.Remove(i);
					m_LockedObjectsDecay.Remove(i);
				}
				else
				{
					m_LockedObjectsDecay.Get(i) += TICK_RATE;
				}
			}
		}
	}
};