-
Notifications
You must be signed in to change notification settings - Fork 0
/
SupportUnit.cs
112 lines (81 loc) · 2.64 KB
/
SupportUnit.cs
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
[RequireComponent(typeof(Movement))]
public class SupportUnit : Unit
{
#region Variables
private Unit m_target;
private Agent m_targetAgressor = null;
[SerializeField] private float defendRange = 2f;
#endregion
#region Properties
public Unit Target
{
get { return m_target; }
set
{
// Unregister the assigned support from the old target
if (m_target != null) m_target.assignedSupport = null;
// If the new target does not have an assigned support
if (value == null || value.assignedSupport == null)
{
m_target = value;
// If the new target is valid register 'this' as the assigned support
if (m_target)
{
m_target.assignedSupport = this;
m_targetAgressor = m_target.agent.Agressor;
}
}
}
}
#endregion
#region MonoBehaviour
protected new void OnDisable()
{
base.OnDisable();
Target = null;
}
private void OnDestroy()
{
Target = null;
}
#endregion
#region Functions
public float TargetCoverNeedFactor()
{
if (m_target != null && m_target.gameObject.activeInHierarchy)
{
bool targetIsBeingHurted = m_target.agent.Agressor != null;
if(targetIsBeingHurted)
{
m_targetAgressor = m_target.agent.Agressor;
}
bool isCurrentlyProtectingTarget = false;
if(agent.Agressor != null)
{
Vector3 agressorToAgent = Vector3.Normalize(agent.Agressor.transform.position - transform.position);
Vector3 agressorToTarget = Vector3.Normalize(agent.Agressor.transform.position - m_target.transform.position);
isCurrentlyProtectingTarget = Vector3.Dot(agressorToTarget, agressorToAgent) >= 0.75f;
}
return Convert.ToSingle(targetIsBeingHurted || isCurrentlyProtectingTarget);
}
return 0f;
}
public void Cover()
{
movement.MoveTo(GetCoverPosition(m_target.transform.position, m_targetAgressor.transform.position));
}
public Vector3 GetCoverPosition(Vector3 targetPosition, Vector3 agressorPosition)
{
Vector3 dir = agressorPosition - targetPosition;
return targetPosition + dir.normalized * defendRange;
}
public override bool HasDuty()
{
return TargetCoverNeedFactor() > 0f;
}
#endregion
}